Exemplo n.º 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)
Exemplo n.º 2
0
    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)