Ejemplo n.º 1
0
    def update(self, cmd_params: CommandParameters) -> Optional[str]:
        resource_id = cmd_params.resource_id
        actor = cmd_params.actor
        key = cmd_params.key
        value = cmd_params.value
        dashboard = DashboardDAO.get_by_id_or_slug(str(resource_id))
        if dashboard and value:
            entry: Entry = cache_manager.filter_state_cache.get(
                cache_key(resource_id, key))
            if entry:
                user_id = actor.get_user_id()
                if entry["owner"] != user_id:
                    raise KeyValueAccessDeniedError()

                # Generate a new key if tab_id changes or equals 0
                contextual_key = cache_key(session.get("_id"),
                                           cmd_params.tab_id, resource_id)
                key = cache_manager.filter_state_cache.get(contextual_key)
                if not key or not cmd_params.tab_id:
                    key = random_key()
                    cache_manager.filter_state_cache.set(contextual_key, key)

                new_entry: Entry = {
                    "owner": actor.get_user_id(),
                    "value": value
                }
                cache_manager.filter_state_cache.set(
                    cache_key(resource_id, key), new_entry)
        return key
Ejemplo n.º 2
0
 def delete(self, actor: User, resource_id: int,
            key: str) -> Optional[bool]:
     dashboard = DashboardDAO.get_by_id_or_slug(str(resource_id))
     if dashboard:
         entry: Entry = cache_manager.filter_state_cache.get(
             cache_key(resource_id, key))
         if entry:
             if entry["owner"] != actor.get_user_id():
                 raise KeyValueAccessDeniedError()
             return cache_manager.filter_state_cache.delete(
                 cache_key(resource_id, key))
     return False
Ejemplo n.º 3
0
 def create(self, cmd_params: CommandParameters) -> str:
     resource_id = cmd_params.resource_id
     actor = cmd_params.actor
     tab_id = cmd_params.tab_id
     contextual_key = cache_key(session.get("_id"), tab_id, resource_id)
     key = cache_manager.filter_state_cache.get(contextual_key)
     if not key or not tab_id:
         key = random_key()
     value = cmd_params.value
     dashboard = DashboardDAO.get_by_id_or_slug(str(resource_id))
     if dashboard and value:
         entry: Entry = {"owner": actor.get_user_id(), "value": value}
         cache_manager.filter_state_cache.set(cache_key(resource_id, key), entry)
         cache_manager.filter_state_cache.set(contextual_key, key)
     return key
Ejemplo n.º 4
0
 def run(self) -> str:
     try:
         dataset_id = self._cmd_params.dataset_id
         chart_id = self._cmd_params.chart_id
         tab_id = self._cmd_params.tab_id
         actor = self._cmd_params.actor
         form_data = self._cmd_params.form_data
         check_access(dataset_id, chart_id, actor)
         contextual_key = cache_key(session.get("_id"), tab_id, dataset_id,
                                    chart_id)
         key = cache_manager.explore_form_data_cache.get(contextual_key)
         if not key or not tab_id:
             key = random_key()
         if form_data:
             state: TemporaryExploreState = {
                 "owner": actor.get_user_id(),
                 "dataset_id": dataset_id,
                 "chart_id": chart_id,
                 "form_data": form_data,
             }
             cache_manager.explore_form_data_cache.set(key, state)
             cache_manager.explore_form_data_cache.set(contextual_key, key)
         return key
     except SQLAlchemyError as ex:
         logger.exception("Error running create command")
         raise KeyValueCreateFailedError() from ex
Ejemplo n.º 5
0
 def delete(self, cmd_params: CommandParameters) -> bool:
     resource_id = cmd_params.resource_id
     actor = cmd_params.actor
     key = cache_key(resource_id, cmd_params.key)
     dashboard = DashboardDAO.get_by_id_or_slug(str(resource_id))
     if dashboard:
         entry: Entry = cache_manager.filter_state_cache.get(key)
         if entry:
             if entry["owner"] != actor.get_user_id():
                 raise KeyValueAccessDeniedError()
             tab_id = cmd_params.tab_id
             contextual_key = cache_key(session.get("_id"), tab_id,
                                        resource_id)
             cache_manager.filter_state_cache.delete(contextual_key)
             return cache_manager.filter_state_cache.delete(key)
     return False
Ejemplo n.º 6
0
 def create(self, actor: User, resource_id: int, key: str,
            value: str) -> Optional[bool]:
     dashboard = DashboardDAO.get_by_id_or_slug(str(resource_id))
     if dashboard:
         entry: Entry = {"owner": actor.get_user_id(), "value": value}
         return cache_manager.filter_state_cache.set(
             cache_key(resource_id, key), entry)
     return False
Ejemplo n.º 7
0
 def update(self, actor: User, resource_id: int, key: str,
            value: str) -> Optional[bool]:
     dashboard = DashboardDAO.get_by_id_or_slug(str(resource_id))
     if dashboard:
         entry: Entry = cache_manager.filter_state_cache.get(
             cache_key(resource_id, key))
         if entry:
             user_id = actor.get_user_id()
             if entry["owner"] != user_id:
                 raise KeyValueAccessDeniedError()
             new_entry: Entry = {
                 "owner": actor.get_user_id(),
                 "value": value
             }
             return cache_manager.filter_state_cache.set(
                 cache_key(resource_id, key), new_entry)
     return False
Ejemplo n.º 8
0
 def get(self, resource_id: int, key: str,
         refresh_timeout: bool) -> Optional[str]:
     DashboardDAO.get_by_id_or_slug(str(resource_id))
     entry = cache_manager.filter_state_cache.get(
         cache_key(resource_id, key)) or {}
     if entry and refresh_timeout:
         cache_manager.filter_state_cache.set(key, entry)
     return entry.get("value")
Ejemplo n.º 9
0
 def get(self, cmd_params: CommandParameters) -> Optional[str]:
     resource_id = cmd_params.resource_id
     key = cache_key(resource_id, cmd_params.key)
     DashboardDAO.get_by_id_or_slug(str(resource_id))
     entry = cache_manager.filter_state_cache.get(key) or {}
     if entry and self._refresh_timeout:
         cache_manager.filter_state_cache.set(key, entry)
     return entry.get("value")
Ejemplo n.º 10
0
 def create(self, cmd_params: CommandParameters) -> bool:
     resource_id = cmd_params.resource_id
     actor = cmd_params.actor
     key = cache_key(resource_id, cmd_params.key)
     value = cmd_params.value
     dashboard = DashboardDAO.get_by_id_or_slug(str(resource_id))
     if dashboard and value:
         entry: Entry = {"owner": actor.get_user_id(), "value": value}
         return cache_manager.filter_state_cache.set(key, entry)
     return False
Ejemplo n.º 11
0
 def get(self, resource_id: int, key: str,
         refresh_timeout: bool) -> Optional[str]:
     dashboard = DashboardDAO.get_by_id_or_slug(str(resource_id))
     if dashboard:
         entry: Entry = cache_manager.filter_state_cache.get(
             cache_key(resource_id, key))
         if refresh_timeout:
             cache_manager.filter_state_cache.set(key, entry)
         return entry["value"]
     return None
Ejemplo n.º 12
0
 def update(self, cmd_params: CommandParameters) -> bool:
     resource_id = cmd_params.resource_id
     actor = cmd_params.actor
     key = cache_key(resource_id, cmd_params.key)
     value = cmd_params.value
     dashboard = DashboardDAO.get_by_id_or_slug(str(resource_id))
     if dashboard and value:
         entry: Entry = cache_manager.filter_state_cache.get(key)
         if entry:
             user_id = actor.get_user_id()
             if entry["owner"] != user_id:
                 raise KeyValueAccessDeniedError()
             new_entry: Entry = {
                 "owner": actor.get_user_id(),
                 "value": value
             }
             return cache_manager.filter_state_cache.set(key, new_entry)
     return False
Ejemplo n.º 13
0
 def run(self) -> bool:
     try:
         actor = self._cmd_params.actor
         key = self._cmd_params.key
         state: TemporaryExploreState = cache_manager.explore_form_data_cache.get(
             key
         )
         if state:
             dataset_id = state["dataset_id"]
             chart_id = state["chart_id"]
             check_access(dataset_id, chart_id, actor)
             if state["owner"] != actor.get_user_id():
                 raise KeyValueAccessDeniedError()
             tab_id = self._cmd_params.tab_id
             contextual_key = cache_key(
                 session.get("_id"), tab_id, dataset_id, chart_id
             )
             cache_manager.explore_form_data_cache.delete(contextual_key)
             return cache_manager.explore_form_data_cache.delete(key)
         return False
     except SQLAlchemyError as ex:
         logger.exception("Error running delete command")
         raise KeyValueDeleteFailedError() from ex
Ejemplo n.º 14
0
    def run(self) -> Optional[str]:
        try:
            dataset_id = self._cmd_params.dataset_id
            chart_id = self._cmd_params.chart_id
            actor = self._cmd_params.actor
            key = self._cmd_params.key
            form_data = self._cmd_params.form_data
            check_access(dataset_id, chart_id, actor)
            state: TemporaryExploreState = cache_manager.explore_form_data_cache.get(
                key
            )
            if state and form_data:
                user_id = actor.get_user_id()
                if state["owner"] != user_id:
                    raise KeyValueAccessDeniedError()

                # Generate a new key if tab_id changes or equals 0
                tab_id = self._cmd_params.tab_id
                contextual_key = cache_key(
                    session.get("_id"), tab_id, dataset_id, chart_id
                )
                key = cache_manager.explore_form_data_cache.get(contextual_key)
                if not key or not tab_id:
                    key = random_key()
                    cache_manager.explore_form_data_cache.set(contextual_key, key)

                new_state: TemporaryExploreState = {
                    "owner": actor.get_user_id(),
                    "dataset_id": dataset_id,
                    "chart_id": chart_id,
                    "form_data": form_data,
                }
                cache_manager.explore_form_data_cache.set(key, new_state)
            return key
        except SQLAlchemyError as ex:
            logger.exception("Error running update command")
            raise KeyValueUpdateFailedError() from ex
Ejemplo n.º 15
0
def cache(dashboard_id, admin_id):
    entry: Entry = {"owner": admin_id, "value": value}
    cache_manager.filter_state_cache.set(cache_key(dashboard_id, key), entry)
Ejemplo n.º 16
0
def cache(dashboard_id, admin_id):
    app.config["FILTER_STATE_CACHE_CONFIG"] = {"CACHE_TYPE": "SimpleCache"}
    cache_manager.init_app(app)
    entry: Entry = {"owner": admin_id, "value": value}
    cache_manager.filter_state_cache.set(cache_key(dashboard_id, key), entry)