From 9e1bb93d2fabb670b5a5be6223d5eb34916de466 Mon Sep 17 00:00:00 2001 From: Mohamed Feddad Date: Thu, 14 Mar 2024 05:10:53 +0400 Subject: [PATCH] fix: start as an operator bug Resolves #293 Resolves #290 --- app/events.py | 8 ++++++-- app/helpers.py | 4 ++++ app/main.py | 6 +++--- app/utils.py | 5 ----- templates/sb_manage.html | 2 +- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/events.py b/app/events.py index 39e01b7..2b740a1 100644 --- a/app/events.py +++ b/app/events.py @@ -8,10 +8,11 @@ def setup_events(db): get_number_of_active_tickets_cached, get_number_of_active_tickets_office_cached, get_number_of_active_tickets_task_cached, + is_user_office_operator, ) from app.database import ( Serial, Display_store, Settings, Aliases, Touch_store, Task, - Slides_c, Slides, Vid, Office, Settings, + Slides_c, Slides, Vid, Office, Settings, User, ) serial_funcs = [ @@ -23,7 +24,7 @@ def setup_events(db): model_action_func_map = { (Office, 'insert'): [repeat_announcement, get_all_offices_cached], - (Office, 'delete'): [repeat_announcement, get_all_offices_cached], + (Office, 'delete'): [repeat_announcement, get_all_offices_cached, is_user_office_operator], (Serial, 'update'): serial_funcs, (Serial, 'insert'): serial_funcs, (Serial, 'delete'): serial_funcs, @@ -40,6 +41,9 @@ def setup_events(db): (Slides, 'insert'): [display], (Slides, 'delete'): [display], (Settings, 'update'): [get_settings_cached], + (User, 'update'): [is_user_office_operator], + (User, 'insert'): [is_user_office_operator], + (User, 'delete'): [is_user_office_operator], } def clear_cache(session): diff --git a/app/helpers.py b/app/helpers.py index 063a8da..d7c1a6a 100644 --- a/app/helpers.py +++ b/app/helpers.py @@ -445,3 +445,7 @@ def get_number_of_active_tickets_task_cached(*args, **kwargs): .filter_by(p=False) .count() ) + +@cache_call(None) +def is_user_office_operator(user, office): + return user and user.id in {o.id for o in office.operators} diff --git a/app/main.py b/app/main.py index 0aa28b7..2ca01af 100644 --- a/app/main.py +++ b/app/main.py @@ -23,8 +23,8 @@ from app.views.core import core from app.views.customize import cust_app from app.views.manage import manage_app -from app.utils import (absolute_path, log_error, create_default_records, get_bp_endpoints, - in_records) +from app.utils import (absolute_path, log_error, create_default_records, get_bp_endpoints) +from app.helpers import is_user_office_operator from app.database import Serial from app.tasks import start_tasks from app.api.setup import setup_api @@ -208,7 +208,7 @@ def inject_vars(): get_active_tickets_task=get_number_of_active_tickets_task_cached, next=next, it=iter, - checkId=in_records, + is_office_operator=is_user_office_operator, offices=get_all_offices_cached(), moment_wrapper=moment_wrapper, current_path=quote(path, safe=''), diff --git a/app/utils.py b/app/utils.py index d05c9e1..5c1d6ce 100644 --- a/app/utils.py +++ b/app/utils.py @@ -374,11 +374,6 @@ def check_iterator_empty(iterator): return i1, value == unique -@functools.lru_cache -def in_records(id, records): - return id in {r.id for r in records} - - def create_default_records(): ''' create database necessary records, if not existing. ''' tables = [data.Display_store, data.Touch_store, data.Slides_c, diff --git a/templates/sb_manage.html b/templates/sb_manage.html index 5af990d..21961a9 100644 --- a/templates/sb_manage.html +++ b/templates/sb_manage.html @@ -68,7 +68,7 @@ {% if not settings.single_row %} {% for office in offices %} - {% if getattr(current_user, 'role_id', None) != 3 or checkId(current_user.id, office.operators) %} + {% if getattr(current_user, 'role_id', None) != 3 or is_office_operator(current_user, office) %}