Beispiel #1
0
 def run(self) -> str:
     self.validate()
     try:
         d_id, d_type = self.datasource.split("__")
         datasource_id = int(d_id)
         datasource_type = DatasourceType(d_type)
         check_chart_access(datasource_id, self.chart_id, datasource_type)
         value = {
             "chartId": self.chart_id,
             "datasourceId": datasource_id,
             "datasourceType": datasource_type,
             "datasource": self.datasource,
             "state": self.state,
         }
         command = CreateKeyValueCommand(
             resource=self.resource,
             value=value,
         )
         key = command.run()
         if key.id is None:
             raise ExplorePermalinkCreateFailedError(
                 "Unexpected missing key id")
         return encode_permalink_key(key=key.id, salt=self.salt)
     except SQLAlchemyError as ex:
         logger.exception("Error running create command")
         raise ExplorePermalinkCreateFailedError() from ex
Beispiel #2
0
 def upsert(self) -> Optional[Key]:
     filter_ = get_filter(self.resource, self.key)
     entry: KeyValueEntry = (
         db.session.query(KeyValueEntry)
         .filter_by(**filter_)
         .autoflush(False)
         .first()
     )
     if entry:
         entry.value = pickle.dumps(self.value)
         entry.expires_on = self.expires_on
         entry.changed_on = datetime.now()
         entry.changed_by_fk = (
             None if self.actor is None or self.actor.is_anonymous else self.actor.id
         )
         db.session.merge(entry)
         db.session.commit()
         return Key(entry.id, entry.uuid)
     return CreateKeyValueCommand(
         resource=self.resource,
         value=self.value,
         actor=self.actor,
         key=self.key,
         expires_on=self.expires_on,
     ).run()
Beispiel #3
0
def test_create_uuid_entry(app_context: AppContext, admin: User) -> None:
    from superset.key_value.commands.create import CreateKeyValueCommand
    from superset.key_value.models import KeyValueEntry

    key = CreateKeyValueCommand(actor=admin, resource=RESOURCE,
                                value=VALUE).run()
    entry = (db.session.query(KeyValueEntry).filter_by(
        uuid=key.uuid).autoflush(False).one())
    assert pickle.loads(entry.value) == VALUE
    assert entry.created_by_fk == admin.id
    db.session.delete(entry)
    db.session.commit()
 def run(self) -> str:
     self.validate()
     try:
         dataset_id = int(self.datasource.split("__")[0])
         check_access(dataset_id, self.chart_id, self.actor)
         value = {
             "chartId": self.chart_id,
             "datasetId": dataset_id,
             "datasource": self.datasource,
             "state": self.state,
         }
         command = CreateKeyValueCommand(
             actor=self.actor,
             resource=self.resource,
             value=value,
             key_type=self.key_type,
         )
         return command.run()
     except SQLAlchemyError as ex:
         logger.exception("Error running create command")
         raise ExplorePermalinkCreateFailedError() from ex
Beispiel #5
0
    def add(self, key: str, value: Any, timeout: Optional[int] = None) -> bool:
        # pylint: disable=import-outside-toplevel
        from superset.key_value.commands.create import CreateKeyValueCommand

        try:
            CreateKeyValueCommand(
                resource=RESOURCE,
                value=value,
                key=self.get_key(key),
                expires_on=self._get_expiry(timeout),
            ).run()
            self._prune()
            return True
        except KeyValueCreateFailedError:
            return False
Beispiel #6
0
 def run(self) -> str:
     self.validate()
     try:
         DashboardDAO.get_by_id_or_slug(self.dashboard_id)
         value = {
             "dashboardId": self.dashboard_id,
             "state": self.state,
         }
         return CreateKeyValueCommand(
             actor=self.actor,
             resource=self.resource,
             value=value,
             key_type=self.key_type,
         ).run()
     except SQLAlchemyError as ex:
         logger.exception("Error running create command")
         raise DashboardPermalinkCreateFailedError() from ex
Beispiel #7
0
 def run(self) -> str:
     self.validate()
     try:
         DashboardDAO.get_by_id_or_slug(self.dashboard_id)
         value = {
             "dashboardId": self.dashboard_id,
             "state": self.state,
         }
         key = CreateKeyValueCommand(
             actor=self.actor,
             resource=self.resource,
             value=value,
         ).run()
         if key.id is None:
             raise DashboardPermalinkCreateFailedError("Unexpected missing key id")
         return encode_permalink_key(key=key.id, salt=self.salt)
     except SQLAlchemyError as ex:
         logger.exception("Error running create command")
         raise DashboardPermalinkCreateFailedError() from ex
Beispiel #8
0
 def upsert(self) -> Key:
     filter_ = get_filter(self.resource, self.key)
     entry: KeyValueEntry = (
         db.session.query(KeyValueEntry)
         .filter_by(**filter_)
         .autoflush(False)
         .first()
     )
     if entry:
         entry.value = pickle.dumps(self.value)
         entry.expires_on = self.expires_on
         entry.changed_on = datetime.now()
         entry.changed_by_fk = get_user_id()
         db.session.merge(entry)
         db.session.commit()
         return Key(entry.id, entry.uuid)
     return CreateKeyValueCommand(
         resource=self.resource,
         value=self.value,
         key=self.key,
         expires_on=self.expires_on,
     ).run()
Beispiel #9
0
def set_shared_value(key: SharedKey, value: Any) -> None:
    # pylint: disable=import-outside-toplevel
    from superset.key_value.commands.create import CreateKeyValueCommand

    uuid_key = uuid3(NAMESPACE, key)
    CreateKeyValueCommand(resource=RESOURCE, value=value, key=uuid_key).run()