ProjectDjango/DjangoLoaddataDataDialog.py

Sat, 23 Dec 2023 15:48:51 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 23 Dec 2023 15:48:51 +0100
branch
eric7
changeset 191
7b03dfe583e3
parent 190
f394b11f9f31
child 192
a4b66a1acea6
permissions
-rw-r--r--

Updated copyright for 2024.

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

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

"""
Module implementing a dialog to enter the data for the 'loaddata' command.
"""

import contextlib

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

try:
    from eric7.EricGui import EricPixmapCache
except ImportError:
    from UI import PixmapCache as EricPixmapCache
from eric7.EricWidgets import EricFileDialog

try:
    from eric7.SystemUtilities.FileSystemUtilities import toNativeSeparators
except ImportError:
    # imports for eric < 23.1
    from eric7.Utilities import toNativeSeparators

from .Ui_DjangoLoaddataDataDialog import Ui_DjangoLoaddataDataDialog


class DjangoLoaddataDataDialog(QDialog, Ui_DjangoLoaddataDataDialog):
    """
    Class implementing a dialog to enter the data for the 'loaddata' command.
    """

    def __init__(self, project, parent=None):
        """
        Constructor

        @param project reference to the Django project object
        @type Project
        @param parent reference to the parent widget
        @type QWidget
        """
        super().__init__(parent)
        self.setupUi(self)

        self.fixtureFileButton.setIcon(EricPixmapCache.getIcon("open"))

        self.__project = project

        self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(False)

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

    @pyqtSlot(str)
    def on_fixturesEdit_textChanged(self, txt):
        """
        Private slot to handle a change of the fixtures text.

        @param txt text of the line edit (string)
        """
        self.buttonBox.button(QDialogButtonBox.StandardButton.Ok).setEnabled(bool(txt))

    @pyqtSlot()
    def on_fixtureFileButton_clicked(self):
        """
        Private slot to select a fixture file via a file selection dialog.
        """
        fileFilters = self.tr("JSON Files (*.json);;XML Files (*.xml);;")
        with contextlib.suppress(ImportError):
            import yaml  # __IGNORE_WARNING__

            fileFilters += self.tr("YAML Files (*.yaml);;")
        fileFilters += self.tr("All Files (*)")

        fixtureFiles = EricFileDialog.getOpenFileNames(
            self,
            self.tr("Select fixture file"),
            self.__project.getProjectPath(),
            fileFilters,
        )

        if fixtureFiles:
            self.fixturesEdit.setText(
                " ".join([toNativeSeparators(f) for f in fixtureFiles])
            )

    def getData(self):
        """
        Public method to get the data entered into the dialog.

        @return tuple containing the list of fixtures, list of apps to exclude,
            application to search in and a flag indicating to ignore
            non-existing fields and models
        @rtype tuple of (list of str, list of str, str, bool)
        """
        fixturesStr = self.fixturesEdit.text()
        fixtures = fixturesStr.split()

        excludeStr = self.excludeEdit.text()
        excludes = excludeStr.split()

        return (
            fixtures,
            excludes,
            self.appEdit.text(),
            self.ignoreCheckBox.isChecked(),
        )

eric ide

mercurial