Example #1
0
    def decorated_function(*args, **kwargs):
        inst = args[0] if type(
            args[0]).__name__ == 'AsistenteLADMCOLPlugin' else args[0].ladmcol
        context = args[1]

        for db_source in context.get_db_sources():
            db = inst.conn_manager.get_db_connector_from_source(
                db_source=db_source)
            db.test_connection()
            if not db.cadastral_cartography_model_exists():
                widget = inst.iface.messageBar().createMessage(
                    "Asistente LADM-COL",
                    QCoreApplication.translate(
                        "AsistenteLADMCOLPlugin",
                        "Check your {} database connection. The '{} {}' model is required for this functionality, but could not be found in your current database. Click the button to go to Settings."
                    ).format(
                        Tr.tr_db_source(db_source),
                        LADMColModelRegistry().model(
                            LADMNames.CADASTRAL_CARTOGRAPHY_MODEL_KEY).alias(),
                        LADMColModelRegistry().model(
                            LADMNames.CADASTRAL_CARTOGRAPHY_MODEL_KEY).alias())
                )
                button = QPushButton(widget)
                button.setText(
                    QCoreApplication.translate("AsistenteLADMCOLPlugin",
                                               "Settings"))

                settings_context = SettingsContext(db_source)
                settings_context.required_models = [
                    LADMNames.CADASTRAL_CARTOGRAPHY_MODEL_KEY
                ]
                settings_context.tab_pages_list = [
                    SETTINGS_CONNECTION_TAB_INDEX
                ]
                settings_context.title = QCoreApplication.translate(
                    "SettingsDialog", "{} Connection Settings").format(
                        Tr.tr_db_source(db_source))
                settings_context.tip = QCoreApplication.translate(
                    "SettingsDialog",
                    "Set a DB connection with the '{}' model.").format(
                        LADMColModelRegistry().model(
                            LADMNames.CADASTRAL_CARTOGRAPHY_MODEL_KEY).alias())
                button.pressed.connect(
                    partial(inst.show_settings_clear_message_bar,
                            settings_context))

                widget.layout().addWidget(button)
                inst.iface.messageBar().pushWidget(widget, Qgis.Warning, 15)
                inst.logger.warning(
                    __name__,
                    QCoreApplication.translate(
                        "AsistenteLADMCOLPlugin",
                        "A dialog/tool couldn't be opened/executed, connection to DB was not valid."
                    ))
                return

        func_to_decorate(*args, **kwargs)
    def __init__(self, conn_manager=None, context=None, parent=None):
        QDialog.__init__(self, parent)
        self.setupUi(self)
        self.parent = parent
        self.logger = Logger()
        self.conn_manager = conn_manager
        self.app = AppInterface()

        self.sbx_tolerance.setMaximum(TOLERANCE_MAX_VALUE)
        self._valid_document_repository = False  # Needs to be True if users want to enable doc repo (using test button)

        context = context if context else SettingsContext()

        self.db_source = context.db_source  # default db source is COLLECTED_DB_SOURCE
        self._required_models = context.required_models
        self._tab_pages_list = context.tab_pages_list
        self._blocking_mode = context.blocking_mode  # Whether the dialog can only be accepted on valid DB connections or not
        self._action_type = context.action_type  # By default "config"
        self.setWindowTitle(context.title)

        self._db = None
        self.init_db_engine = None
        self.dbs_supported = ConfigDBsSupported()
        self._open_dlg_import_schema = False  # After accepting, if non-valid DB is configured, we can go to import schema

        self.online_models_radio_button.setEnabled(
            False)  # This option is disabled until we have online models back!
        self.online_models_radio_button.setChecked(True)
        self.online_models_radio_button.toggled.connect(
            self.model_provider_toggle)
        self.custom_model_directories_line_edit.setText("")
        self.custom_models_dir_button.clicked.connect(
            self.show_custom_model_dir)
        self.custom_model_directories_line_edit.setVisible(False)
        self.custom_models_dir_button.setVisible(False)

        # Set connections
        self.buttonBox.accepted.disconnect()
        self.buttonBox.accepted.connect(self.accepted)
        self.buttonBox.helpRequested.connect(self.show_help)
        self.finished.connect(self.finished_slot)
        self.btn_test_connection.clicked.connect(self.test_connection)
        self.btn_test_ladm_col_structure.clicked.connect(
            self.test_ladm_col_structure)

        self.btn_test_service.clicked.connect(self.test_service)
        self.btn_test_service_transitional_system.clicked.connect(
            self.test_service_transitional_system)
        self.txt_service_endpoint.textEdited.connect(
            self.source_service_endpoint_changed)  # For manual changes only

        self.btn_default_value_sources.clicked.connect(
            self.set_default_value_source_service)
        self.btn_default_value_transitional_system.clicked.connect(
            self.set_default_value_transitional_system_service)

        self.chk_use_roads.toggled.connect(self.update_images_state)

        self.bar = QgsMessageBar()
        self.bar.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed)
        self.layout().addWidget(self.bar, 0, 0, Qt.AlignTop)

        self.cbo_db_engine.clear()

        self._lst_db = self.dbs_supported.get_db_factories()
        self._lst_panel = dict()

        for key, value in self._lst_db.items():
            self.cbo_db_engine.addItem(value.get_name(), key)
            self._lst_panel[key] = value.get_config_panel(self)
            self._lst_panel[key].notify_message_requested.connect(
                self.show_message)
            self.db_layout.addWidget(self._lst_panel[key])

        self.db_engine_changed()

        # Trigger some default behaviours
        self.restore_db_source_settings(
        )  # restore settings with default db source
        self.restore_settings()

        self.roles = RoleRegistry()
        self.load_roles()

        self.cbo_db_engine.currentIndexChanged.connect(self.db_engine_changed)
        self.rejected.connect(self.close_dialog)

        self._update_tabs()

        if context.tip:
            self.show_tip(context.tip)
    def decorated_function(*args, **kwargs):
        """
        For all db_source in the context check:
        1. that db connection is valid, otherwise show message with button that opens db connection setting dialog.
        2. that db connection parameters are equal to db source connection parameters (in QSettings), otherwise show
           message with two buttons "Use DB from QSettings" and "Use the current connection".
        """
        # Check if current connection is valid and disable access if not
        inst = args[0]
        context = args[1]
        db_connections_in_conflict = list()
        invalid_db_connections = list()

        for db_source in context.get_db_sources():
            db = inst.conn_manager.get_db_connector_from_source(db_source)
            qsettings_db = inst.conn_manager.get_db_connection_from_qsettings(
                db_source)

            if db.equals(qsettings_db):
                res, code, msg = db.test_connection()
                if not res:
                    invalid_db_connections.append(db_source)
                    widget = inst.iface.messageBar().createMessage(
                        "Asistente LADM-COL",
                        QCoreApplication.translate(
                            "AsistenteLADMCOLPlugin",
                            "The {} DB connection is not valid. Details: {}").
                        format(Tr.tr_db_source(db_source), msg))
                    button = QPushButton(widget)
                    button.setText(
                        QCoreApplication.translate("AsistenteLADMCOLPlugin",
                                                   "Settings"))

                    settings_context = SettingsContext(db_source)
                    settings_context.title = QCoreApplication.translate(
                        "AsistenteLADMCOLPlugin",
                        "{} Connection Settings").format(
                            Tr.tr_db_source(db_source))
                    settings_context.tip = QCoreApplication.translate(
                        "AsistenteLADMCOLPlugin",
                        "Configure a valid DB connection for {} source."
                    ).format(Tr.tr_db_source(db_source))
                    settings_context.tab_pages_list = [
                        SETTINGS_CONNECTION_TAB_INDEX
                    ]
                    button.pressed.connect(
                        partial(inst.show_settings_clear_message_bar,
                                settings_context))

                    widget.layout().addWidget(button)
                    inst.iface.messageBar().pushWidget(widget, Qgis.Warning,
                                                       15)
                    inst.logger.warning(
                        __name__,
                        QCoreApplication.translate(
                            "AsistenteLADMCOLPlugin",
                            "A dialog/tool couldn't be opened/executed, connection to DB was not valid."
                        ))
                else:
                    # Update cache if there is none and source is Collected
                    if db_source == COLLECTED_DB_SOURCE:
                        if not inst.app.core.get_cached_layers(
                        ) and not inst.app.core.get_cached_relations():
                            inst.app.core.cache_layers_and_relations(
                                db, ladm_col_db=True, db_source=None)
            else:
                db_connections_in_conflict.append(db_source)
                msg = QCoreApplication.translate(
                    "AsistenteLADMCOLPlugin",
                    "Your current {} DB does not match with the one registered in QSettings. Which connection would you like to use?"
                ).format(Tr.tr_db_source(db_source))

                widget = inst.iface.messageBar().createMessage(
                    "Asistente LADM-COL", msg)
                btn_current_connection = QPushButton(widget)
                btn_current_connection.setText(
                    QCoreApplication.translate("AsistenteLADMCOLPlugin",
                                               "Use the current connection"))
                btn_current_connection.pressed.connect(
                    partial(inst.use_current_db_connection, db_source))

                btn_update_connection = QPushButton(widget)
                btn_update_connection.setText(
                    QCoreApplication.translate("AsistenteLADMCOLPlugin",
                                               "Use DB from QSettings"))
                btn_update_connection.pressed.connect(
                    partial(inst.update_db_connection_from_qsettings,
                            db_source))

                widget.layout().addWidget(btn_current_connection)
                widget.layout().addWidget(btn_update_connection)

                inst.iface.messageBar().pushWidget(widget, Qgis.Warning)
                inst.logger.warning(__name__, msg)

        if db_connections_in_conflict or invalid_db_connections:
            return  # If any db connection changed or it's invalid, we don't return the decorated function
        else:
            func_to_decorate(*args, **kwargs)