From d76d4704a08501abb607d5e43e46c9b801c0f159 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Tue, 26 Aug 2014 15:19:39 -0400 Subject: [PATCH] Add pagination to the notifications API and make the UI only show a maximum of 5 notifications (beyond that, it shows "5+"). --- data/model/legacy.py | 12 +++++++--- endpoints/api/user.py | 24 +++++++++++++++----- endpoints/notificationevent.py | 2 ++ static/css/quay.css | 2 +- static/directives/header-bar.html | 16 ++----------- static/directives/notification-bar.html | 5 ++++- static/directives/notifications-bubble.html | 7 ++++++ static/js/app.js | 25 +++++++++++++++++++-- 8 files changed, 67 insertions(+), 26 deletions(-) create mode 100644 static/directives/notifications-bubble.html diff --git a/data/model/legacy.py b/data/model/legacy.py index 866587f7e..cc32b8979 100644 --- a/data/model/legacy.py +++ b/data/model/legacy.py @@ -1717,19 +1717,20 @@ def create_notification(kind_name, target, metadata={}): def create_unique_notification(kind_name, target, metadata={}): with config.app_config['DB_TRANSACTION_FACTORY'](db): - if list_notifications(target, kind_name).count() == 0: + if list_notifications(target, kind_name, limit=1).count() == 0: create_notification(kind_name, target, metadata) def lookup_notification(user, uuid): - results = list(list_notifications(user, id_filter=uuid, include_dismissed=True)) + results = list(list_notifications(user, id_filter=uuid, include_dismissed=True, limit=1)) if not results: return None return results[0] -def list_notifications(user, kind_name=None, id_filter=None, include_dismissed=False): +def list_notifications(user, kind_name=None, id_filter=None, include_dismissed=False, + page=None, limit=None): Org = User.alias() AdminTeam = Team.alias() AdminTeamMember = TeamMember.alias() @@ -1767,6 +1768,11 @@ def list_notifications(user, kind_name=None, id_filter=None, include_dismissed=F .switch(Notification) .where(Notification.uuid == id_filter)) + if page: + query = query.paginate(page, limit) + elif limit: + query = query.limit(limit) + return query diff --git a/endpoints/api/user.py b/endpoints/api/user.py index 3d79a806d..e2e6a0ff4 100644 --- a/endpoints/api/user.py +++ b/endpoints/api/user.py @@ -7,8 +7,9 @@ from flask.ext.principal import identity_changed, AnonymousIdentity from app import app, billing as stripe, authentication from endpoints.api import (ApiResource, nickname, resource, validate_json_request, request_error, - log_action, internal_only, NotFound, require_user_admin, - InvalidToken, require_scope, format_date, hide_if, show_if, license_error) + log_action, internal_only, NotFound, require_user_admin, parse_args, + query_param, InvalidToken, require_scope, format_date, hide_if, show_if, + license_error) from endpoints.api.subscribe import subscribe from endpoints.common import common_login from data import model @@ -403,11 +404,24 @@ class Recovery(ApiResource): @internal_only class UserNotificationList(ApiResource): @require_user_admin + @parse_args + @query_param('page', 'Offset page number. (int)', type=int, default=0) + @query_param('limit', 'Limit on the number of results (int)', type=int, default=5) @nickname('listUserNotifications') - def get(self): - notifications = model.list_notifications(get_authenticated_user()) + def get(self, args): + page = args['page'] + limit = args['limit'] + + notifications = list(model.list_notifications(get_authenticated_user(), page=page, limit=limit + 1)) + has_more = False + + if len(notifications) > limit: + has_more = True + notifications = notifications[0:limit] + return { - 'notifications': [notification_view(notification) for notification in notifications] + 'notifications': [notification_view(notification) for notification in notifications], + 'additional': has_more } diff --git a/endpoints/notificationevent.py b/endpoints/notificationevent.py index f1cbec42c..e393dc134 100644 --- a/endpoints/notificationevent.py +++ b/endpoints/notificationevent.py @@ -184,6 +184,8 @@ class BuildFailureEvent(NotificationEvent): return 'build_failure' def get_sample_data(self, repository): + build_uuid = 'fake-build-id' + return build_event_data(repository, { 'build_id': build_uuid, 'build_name': 'some-fake-build', diff --git a/static/css/quay.css b/static/css/quay.css index 721253ab9..224029444 100644 --- a/static/css/quay.css +++ b/static/css/quay.css @@ -745,7 +745,7 @@ i.toggle-icon:hover { } .user-notification.notification-animated { - width: 21px; + min-width: 21px; transform: scale(0); -moz-transform: scale(0); diff --git a/static/directives/header-bar.html b/static/directives/header-bar.html index d440e3a86..3f395b34d 100644 --- a/static/directives/header-bar.html +++ b/static/directives/header-bar.html @@ -37,15 +37,7 @@ {{ user.username }} - - {{ notificationService.notifications.length }} - +