From 20548d195ff1c8a19f2b97262c5ebf5ba72fd984 Mon Sep 17 00:00:00 2001 From: Samuel Spencer Date: Sat, 12 Dec 2015 07:09:20 +0000 Subject: [PATCH] added live tester page. Confirmed all works. --- README.rst | 10 +++++++ manage.py | 2 +- notifications/static/notifications/notify.js | 2 +- .../templatetags/notifications_tags.py | 12 +++++++-- notifications/tests/settings.py | 13 +++++++-- .../tests/static/notifications/live-test.js | 7 +++++ notifications/tests/templates/test_live.html | 14 ++++++++++ notifications/tests/urls.py | 2 ++ notifications/tests/views.py | 27 +++++++++++++++++++ 9 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 notifications/tests/static/notifications/live-test.js create mode 100644 notifications/tests/templates/test_live.html create mode 100644 notifications/tests/views.py diff --git a/README.rst b/README.rst index 756b67e..d376519 100644 --- a/README.rst +++ b/README.rst @@ -278,6 +278,7 @@ How to use: 1. ``badge_id`` (default ``live_notify_badge``) - The `id` attribute of the element to show the unread count, that will be periodically updated. #. ``menu_id`` (default ``live_notify_list``) - The `id` attribute of the element to insert a list of unread items, that will be periodically updated. #. ``refresh_period`` (default ``15``) - How often to fetch unread items from the server (integer in seconds). + #. ``to_fetch`` (default ``5``) - How many notifications to fetch each time. #. ``callbacks`` (default ````) - A comma-separated list of javascript functions to call each period. #. ``api_url_name`` (default ``list``) - The name of the API to call (this can be either ``list`` or ``count``). @@ -337,6 +338,15 @@ For example, the below function would get the recent list of unread messages and } } +Testing the live-updater +------------------------ + +1. Clone the repo +2. Set the 'NOTIFICATION_TEST' environemnt variable. E.g. `export NOTIFICATION_TEST=1` +3. Run `./manage.py runserver` +4. Browse to `yourserverip/test/` +5. Click 'Make a notification' and a new notification should appear in the list in 5-10 seconds. + ``django-notifications`` Team ============================== diff --git a/manage.py b/manage.py index 367aa92..af81138 100755 --- a/manage.py +++ b/manage.py @@ -3,7 +3,7 @@ import os import sys if __name__ == "__main__": - if 'test' in sys.argv: + if 'test' in sys.argv or 'NOTIFICATION_TEST' in os.environ.keys(): os.environ.setdefault("DJANGO_SETTINGS_MODULE", "notifications.tests.settings") from django.core.management import execute_from_command_line diff --git a/notifications/static/notifications/notify.js b/notifications/static/notifications/notify.js index 6ec2796..e785549 100644 --- a/notifications/static/notifications/notify.js +++ b/notifications/static/notifications/notify.js @@ -20,7 +20,7 @@ function fill_notification_list(data) { menu.innerHTML = ""; for (var i=0; i < data.unread_list.length; i++) { var item = data.unread_list[i]; - menu.innerHTML = menu.innerHTML + "
  • "+item.object+" "+item.verb+" "+item.subject+"
  • "; + menu.innerHTML = menu.innerHTML + "
  • "+item.object+" "+item.verb+" "+item.subject+" at " +item.timestamp + "
  • "; } } } diff --git a/notifications/templatetags/notifications_tags.py b/notifications/templatetags/notifications_tags.py index 4cbc6f5..e001623 100644 --- a/notifications/templatetags/notifications_tags.py +++ b/notifications/templatetags/notifications_tags.py @@ -15,7 +15,7 @@ def notifications_unread(context): # Requires vanilla-js framework - http://vanilla-js.com/ @register.simple_tag -def register_notify_callbacks(badge_id='live_notify_badge',menu_id='live_notify_list',refresh_period=15,callbacks="",api_name='list'): +def register_notify_callbacks(badge_id='live_notify_badge',menu_id='live_notify_list',refresh_period=15,callbacks="",api_name='list',to_fetch=5): refresh_period=int(refresh_period)*1000 if api_name=='list': @@ -31,7 +31,15 @@ def register_notify_callbacks(badge_id='live_notify_badge',menu_id='live_notify_ notify_api_url='{api_url}'; notify_unread_url='{unread_url}'; notify_mark_all_unread_url='{mark_all_unread_url}'; - notify_refresh_period={refresh};""".format(badge_id=badge_id,menu_id=menu_id,refresh=refresh_period,api_url=api_url,unread_url=reverse('notifications:unread'),mark_all_unread_url=reverse('notifications:mark_all_as_read')) + notify_to_fetch='{to_fetch}'; + notify_refresh_period={refresh};""".format( badge_id=badge_id, + menu_id=menu_id, + refresh=refresh_period, + api_url=api_url, + unread_url=reverse('notifications:unread'), + mark_all_unread_url=reverse('notifications:mark_all_as_read'), + to_fetch=to_fetch, + ) script = " + +{% register_notify_callbacks callbacks='fill_notification_list,fill_notification_badge' to_fetch=20 refresh_period=5 %} + +There are this many notifications pending: + + + diff --git a/notifications/tests/urls.py b/notifications/tests/urls.py index 48aa629..5f2f2b5 100644 --- a/notifications/tests/urls.py +++ b/notifications/tests/urls.py @@ -8,5 +8,7 @@ from notifications import urls urlpatterns = patterns('', url(r'^login/$', 'django.contrib.auth.views.login', name='login'), # needed for Django 1.6 tests url(r'^admin/', include(admin.site.urls)), + url(r'^test_make/', 'notifications.tests.views.make_notification'), + url(r'^test/', 'notifications.tests.views.live_tester'), url(r'^', include(urls, 'notifications')), ) diff --git a/notifications/tests/views.py b/notifications/tests/views.py new file mode 100644 index 0000000..1f1d1b7 --- /dev/null +++ b/notifications/tests/views.py @@ -0,0 +1,27 @@ +from django.conf import settings +from django.contrib.auth.decorators import login_required +from django.shortcuts import get_object_or_404, render + +from notifications import notify +import random + +def live_tester(request): + notify.send(request.user, recipient=request.user, verb='you loaded the page') + + data = { + 'unread_count': request.user.notifications.unread().count(), + 'notifications': request.user.notifications.all() + } + return render(request,'test_live.html',data) + +def make_notification(request): + + the_notification = random.choice([ + 'reticulating splines', + 'cleaning the car', + 'jumping the shark', + 'testing the app', + ]) + + notify.send(request.user, recipient=request.user, verb='you asked for a notification - you are '+the_notification) +