ProjectDjango/DjangoSquashMigrationSelectionDialog.py

Sun, 30 Mar 2025 15:25:10 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sun, 30 Mar 2025 15:25:10 +0200
branch
eric7
changeset 206
4dfd8f6f2503
parent 204
ef2a6680ad2a
permissions
-rw-r--r--

Created global tag <release-10.4.0>.

# -*- coding: utf-8 -*-

# Copyright (c) 2016 - 2025 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing a dialog to enter data needed to squash migrations.
"""

import os

from PyQt6.QtCore import pyqtSlot
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QDialog, QDialogButtonBox

from .Ui_DjangoSquashMigrationSelectionDialog import (
    Ui_DjangoSquashMigrationSelectionDialog,
)


class DjangoSquashMigrationSelectionDialog(
    QDialog, Ui_DjangoSquashMigrationSelectionDialog
):
    """
    Class implementing a dialog to enter data needed to squash migrations.
    """

    def __init__(self, migrations, django, suffix, parent=None):
        """
        Constructor

        @param migrations dictionary containing the available migrations
        @type dict
        @param django reference to the Django project object
        @type Project
        @param suffix icon suffix
        @type str
        @param parent reference to the parent widget
        @type QWidget
        """
        super().__init__(parent)
        self.setupUi(self)

        self.__appliedIcon = QIcon(
            os.path.join(
                os.path.dirname(__file__), "icons", "applied-{0}".format(suffix)
            )
        )

        self.__migrations = migrations
        self.applicationComboBox.addItems([""] + sorted(self.__migrations.keys()))
        self.on_applicationComboBox_currentTextChanged("")

        msh = self.minimumSizeHint()
        self.resize(max(self.width(), msh.width()), msh.height())

    def __updateOkButton(self):
        """
        Private slot to set the enabled state of the OK button.
        """
        enabled = bool(self.applicationComboBox.currentText()) and bool(
            self.endMigrationComboBox.currentText()
        )
        self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(enabled)

    @pyqtSlot(str)
    def on_applicationComboBox_currentTextChanged(self, app):
        """
        Private slot to update the migrations combo boxes.

        @param app name of the selected application
        @type str
        """
        self.startMigrationComboBox.clear()
        self.endMigrationComboBox.clear()
        if app:
            for combo in [self.startMigrationComboBox, self.endMigrationComboBox]:
                combo.addItem("")
                for applied, migration in self.__migrations[app]:
                    if applied:
                        combo.addItem(self.__appliedIcon, migration)
                    else:
                        combo.addItem(migration)
        self.__updateOkButton()

    @pyqtSlot(str)
    def on_endMigrationComboBox_currentTextChanged(self, migration):  # noqa: U-100
        """
        Private slot handling the selection of a migration.

        @param migration name of the selected migration
        @type str
        """
        self.__updateOkButton()

    def getData(self):
        """
        Public method to retrieve the data entered by the user.

        @return tuple containing the selected app, the start migration,
            the end migration and a flag indicating no optimization is wanted
        @rtype tuple of three str and a bool
        """
        startMigration = (
            self.startMigrationComboBox.currentText()
            if self.startMigrationComboBox.isEnabled()
            else ""
        )
        name = self.nameEdit.text() if self.nameEdit.isEnabled() else ""

        return (
            self.applicationComboBox.currentText(),
            startMigration,
            self.endMigrationComboBox.currentText(),
            self.noOptimizeCheckBox.isChecked(),
            name,
        )

eric ide

mercurial