From bd798e78b6ca98324954f89862680bad11a1991a Mon Sep 17 00:00:00 2001 From: Andrew Shumway Date: Thu, 1 Jun 2023 10:44:13 -0600 Subject: [PATCH] Added global remaining daily messages across services to ui --- app/__init__.py | 24 ++++++++++++++----- app/config.py | 2 ++ app/notify_client/service_api_client.py | 12 +++++++++- app/templates/main_nav.html | 10 +++++--- app/templates/service_navigation.html | 1 - app/templates/views/check/ok.html | 6 ++--- app/templates/views/send.html | 6 ++--- .../views/accounts/test_choose_accounts.py | 2 +- 8 files changed, 43 insertions(+), 20 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 65a08337b..c108d6a8c 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -312,9 +312,24 @@ def init_app(application): return navigation @application.context_processor - def _attach_current_remaining_messages(): - request - return {'side_nav_remaining_messages': current_service.message_limit - service_api_client.get_notification_count(service_id=current_service.id)} + def _attach_current_daily_remaining_messages_per_service(): + remaining_messages = 0 + + if hasattr(current_service, 'message_limit'): + remaining_messages = current_service.message_limit - service_api_client.get_notification_count( + service_id=current_service.id) + + return {'daily_remaining_messages': remaining_messages} + + @application.context_processor + def _attach_current_global_daily_messages(): + remaining_global_messages = 0 + + if current_app: + global_limit = current_app.config['GLOBAL_SERVICE_MESSAGE_LIMIT'] + global_messages_count = service_api_client.get_global_notification_count() + remaining_global_messages = global_limit - global_messages_count + return {'daily_global_messages_remaining': remaining_global_messages} @application.before_request def record_start_time(): @@ -400,9 +415,6 @@ def load_organisation_before_request(): raise -def load_current_daily_messages_per_service(): - service = request.args.get('service') - def save_service_or_org_after_request(response): # Only save the current session if the request is 200 service_id = request.view_args.get('service_id', None) if request.view_args else None diff --git a/app/config.py b/app/config.py index 34da3b1a8..18e2f6f4a 100644 --- a/app/config.py +++ b/app/config.py @@ -45,6 +45,8 @@ class Config(object): DEFAULT_SERVICE_LIMIT = 50 + GLOBAL_SERVICE_MESSAGE_LIMIT = 5000 + EMAIL_EXPIRY_SECONDS = 3600 # 1 hour INVITATION_EXPIRY_SECONDS = 3600 * 24 * 2 # 2 days - also set on api EMAIL_2FA_EXPIRY_SECONDS = 1800 # 30 Minutes diff --git a/app/notify_client/service_api_client.py b/app/notify_client/service_api_client.py index 449a5e4b0..4e7488246 100644 --- a/app/notify_client/service_api_client.py +++ b/app/notify_client/service_api_client.py @@ -1,6 +1,9 @@ from datetime import datetime -from notifications_utils.clients.redis import daily_limit_cache_key +from notifications_utils.clients.redis import ( + daily_limit_cache_key, + daily_total_cache_key, +) from app.extensions import redis_client from app.notify_client import NotifyAdminAPIClient, _attach_current_user, cache @@ -532,5 +535,12 @@ class ServiceAPIClient(NotifyAdminAPIClient): return int(count) + def get_global_notification_count(self): + # if cache is not set, or not enabled, return 0 + + count = redis_client.get(daily_total_cache_key()) or 0 + + return int(count) + service_api_client = ServiceAPIClient() diff --git a/app/templates/main_nav.html b/app/templates/main_nav.html index 2ddd198d9..133f93861 100644 --- a/app/templates/main_nav.html +++ b/app/templates/main_nav.html @@ -26,11 +26,15 @@
  • Team members
  • {% endif %} - +
    -

    Messages Left / Daily Limit

    +

    Messages Left / Daily Limit


    +

    Messages Left Across Services

    +
    {% endif %} diff --git a/app/templates/service_navigation.html b/app/templates/service_navigation.html index d5f0ce807..3271603f6 100644 --- a/app/templates/service_navigation.html +++ b/app/templates/service_navigation.html @@ -1,7 +1,6 @@ {% macro navigation_service_name(service) %}