Пример #1
0
    def _openModel(self, file, reuse_instance=False):
        """
        Opens model and sets modelInfo of current_session
        """
        res = ModelInfo()
        calcEngine = CalcEngine.tryLoadFromAppPool(self.client_session, file)

        res.name = calcEngine.getModelName()
        res.uri = file
        res.daysToExpire = 30
        res.modelId = calcEngine.getModelId()
        res.engineUID = calcEngine.getEngineUID()
        res.engineURI = calcEngine.getEngineURI()
        res.engineParams = calcEngine.getEngineParams()
        res.readonly = True

        current_session = self.getSession()
        current_session.modelInfo = res
        self.saveSession()

        return res
Пример #2
0
    def openModel(self, file, forceSingleInstance=False):
        """Open model"""
        res = None

        found = False
        if not forceSingleInstance:

            my_sessions = _getAllSessions(self, onlyMySessions=True)
            for item_session in my_sessions:
                if item_session.currentModelFile == file:
                    found = True
                    res = ModelInfo()
                    res.name = item_session.currentModelName
                    res.uri = item_session.currentModelFile
                    res.modelId = item_session.currentModel
                    res.new_session_key = item_session.session_key
                    break

        if not found:
            current_session = self.getSession()
            calcEngine = CalcEngine.tryLoadFromAppPool(current_session, file)

            res = current_session.modelInfo
            res.name = calcEngine.getModelName()
            res.uri = file
            # TODO: ver cuando tengamos licenciamiento
            res.daysToExpire = 365
            _modelPreferences = calcEngine.getModelPreferences()
            res.modelId = _modelPreferences[
                "identifier"] if "identifier" in _modelPreferences else ""
            res.onOpenModel = _modelPreferences[
                "onOpenModel"] if "onOpenModel" in _modelPreferences else ""
            res.onOpenDashId = _modelPreferences[
                "onOpenDashId"] if "onOpenDashId" in _modelPreferences else ""
            if not calcEngine.from_app_pool:
                res.engineUID = calcEngine.getEngineUID()
                res.engineURI = calcEngine.getEngineURI()
                res.engineParams = calcEngine.getEngineParams()

            self.saveSession()
            calcEngine = None

        if not res is None:
            is_public = False
            can_edit = False
            if "/public/" in file.lower():
                is_public = True
            if self.current_user.has_perm(
                    "pyplan.change_model"
            ) and not is_public or self.current_user.has_perm(
                    "pyplan.change_public_model") and is_public:
                can_edit = True

            res.canEdit = can_edit
            res.readonly = not can_edit
            # check for other session for mark as readonly
            try:
                for db_session in self.session_store.get_model_class(
                ).objects.all():
                    _decoded = db_session.get_decoded()
                    if "data" in _decoded and "modelInfo" in _decoded[
                            "data"] and _decoded["data"]["modelInfo"][
                                "uri"] == file and int(
                                    _decoded["data"]
                                    ["userCompanyId"]) != self.getSession(
                                    ).userCompanyId and not _decoded["data"][
                                        "modelInfo"]["readonly"]:
                        res.readonly = True
                        res.readOnlyReason = f"The model is being used by '{_decoded['data']['userFullName']}'. The model will be opened in read-only mode."
                        break
            except Exception as ex:
                print(
                    f"Error checking other session for mark as readonly: {str(ex)}"
                )

            self.saveSession()

        return res