Exemplo n.º 1
0
    def __init__(self, data_store, str_model, str_edit_node=None):
        """
        Initializes the saving of STRDBHandler.
        :param data_store: The data store containing STR record.
        :type data_store: Object
        :param str_model: The model of STR
        :type str_model: SQLAlchemy Model
        :param str_edit_node: The STR Edit node data containing STR model and
        supporting document models when in edit mode.
        :type str_edit_node: Tuple or STRNode
        """
        self.str_model = str_model
        self.data_store = data_store
        self.str_edit_obj = None
        self.progress = STDMProgressDialog(iface.mainWindow())
        self.social_tenure = current_profile().social_tenure

        self.str_edit_node = str_edit_node
        if str_edit_node is not None:
            if isinstance(str_edit_node, tuple):
                self.str_edit_obj = str_edit_node[0]
                self.str_doc_edit_obj = str_edit_node[1]
            else:
                self.str_edit_obj = str_edit_node.model()
                self.str_doc_edit_obj = str_edit_node.documents()
Exemplo n.º 2
0
    def commit_str(self):
        """
        Slot raised when the user clicks on Finish
        button in order to create a new STR entry.
        """
        isValid = True
        # Create a progress dialog
        try:
            progress = STDMProgressDialog(iface.mainWindow())
            progress.show()

            if not self.str_edit_node:
                QApplication.processEvents()
                progress.setRange(0, len(self.data_store))
                progress.overall_progress('Creating a STR...', )

                for i, str_store in enumerate(self.data_store.values()):
                    progress.progress_message('Saving STR {}'.format(i + 1),
                                              '')
                    progress.setValue(i + 1)

                    self.on_add_str(str_store)  # ==>

                progress.deleteLater()
                progress = None

                strMsg = QApplication.translate(
                    "STRDBHandler", "The social tenure relationship has "
                    "been successfully created!")
                QMessageBox.information(
                    iface.mainWindow(),
                    QApplication.translate("STRDBHandler",
                                           "Social Tenure Relationship"),
                    strMsg)
            else:
                QApplication.processEvents()
                progress.setRange(0, 1)
                progress.setValue(0)
                progress.overall_progress('Editing a STR...', )

                progress.progress_message('Updating STR', '')

                updated_str_obj = self.on_edit_str(self.data_store[1])  # ===>

                progress.deleteLater()
                progress = None

                strMsg = QApplication.translate(
                    "STRDBHandler", "The social tenure relationship has "
                    "been successfully updated!")

                QMessageBox.information(
                    iface.mainWindow(),
                    QApplication.translate("STRDBHandler",
                                           "Social Tenure Relationship"),
                    strMsg)

                return updated_str_obj

        except exc.OperationalError as oe:
            errMsg = str(oe)
            QMessageBox.critical(
                iface.mainWindow(),
                QApplication.translate("STRDBHandler", "Unexpected Error"),
                errMsg)
            progress.deleteLater()
            progress = None
            isValid = False
            STDMDb.instance().session.rollback()
            LOGGER.debug(str(oe))

        except exc.IntegrityError as ie:
            errMsg = str(ie)
            QMessageBox.critical(
                iface.mainWindow(),
                QApplication.translate("STRDBHandler",
                                       "Duplicate Relationship Error"), errMsg)
            progress.deleteLater()
            progress = None

            isValid = False
            STDMDb.instance().session.rollback()
            LOGGER.debug(str(ie))
        except exc.InternalError as ie:

            QMessageBox.critical(
                iface.mainWindow(),
                QApplication.translate('STRDBHandler', 'InternalError Error'),
                QApplication.translate(
                    'STRDBHandler', 'Sorry, there is an internal error. \n'
                    'Restart QGIS to fix the issue.'))
            LOGGER.debug(str(ie))
            progress.deleteLater()
            progress = None

            isValid = False
            STDMDb.instance().session.rollback()
        except DummyException as e:
            errMsg = str(e)
            QMessageBox.critical(
                iface.mainWindow(),
                QApplication.translate('STRDBHandler', 'Unexpected Error'),
                errMsg)
            LOGGER.debug(str(e))
            isValid = False
            STDMDb.instance().session.rollback()
            progress.deleteLater()
            progress = None

        finally:

            STDMDb.instance().session.rollback()
            if progress is not None:
                progress.deleteLater()
                progress = None

        return isValid