From b2b4fac254be26a1a79c14b97f75162e56b45e12 Mon Sep 17 00:00:00 2001 From: "theodore.therone@gmail.com" Date: Wed, 11 Nov 2015 11:04:12 +0000 Subject: [PATCH 1/5] fix attempt to fix my mess --- notifications/static/notifications/notify.js | 2 +- .../templates/notifications/test_tags.html | 2 +- .../templatetags/notifications_tags.py | 4 ++-- notifications/views.py | 19 +++++++++++++++---- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/notifications/static/notifications/notify.js b/notifications/static/notifications/notify.js index 6ec2796..546261a 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.actor+" "+item.verb+" "+item.target+"
  • "; } } } diff --git a/notifications/templates/notifications/test_tags.html b/notifications/templates/notifications/test_tags.html index 6ace50b..eab7bda 100644 --- a/notifications/templates/notifications/test_tags.html +++ b/notifications/templates/notifications/test_tags.html @@ -1,6 +1,6 @@ {% load notifications_tags %} -{% register_notify_callbacks callbacks='fill_aristotle_notification_menu,fill_notification_badge' %} +{% register_notify_callbacks callbacks='fill_notification_menu,fill_notification_badge' %} {% notifications_unread as unread %} {{ unread }} diff --git a/notifications/templatetags/notifications_tags.py b/notifications/templatetags/notifications_tags.py index 4cbc6f5..3263a19 100644 --- a/notifications/templatetags/notifications_tags.py +++ b/notifications/templatetags/notifications_tags.py @@ -32,7 +32,7 @@ def register_notify_callbacks(badge_id='live_notify_badge',menu_id='live_notify_ 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')) - + script = " {% register_notify_callbacks callbacks='fill_notification_list,fill_notification_badge' %} ``register_notify_callbacks`` takes the following arguments: - 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). - #. ``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``). + 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). + #. ``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``). - 3. To insert a live-updating unread count, use the following template:: + 3. To insert a live-updating unread count, use the following template:: {% live_notify_badge %} ``live_notify_badge`` takes the following arguments: - 1. ``badge_id`` (default ``live_notify_badge``) - The ``id`` attribute for the ```` element that will be created to show the unread count. - #. ``classes`` (default ````) - A string used to populate the ``class`` attribute of the above element. + 1. ``badge_id`` (default ``live_notify_badge``) - The ``id`` attribute for the ```` element that will be created to show the unread count. + #. ``classes`` (default ````) - A string used to populate the ``class`` attribute of the above element. - 4. To insert a live-updating unread count, use the following template:: + 4. To insert a live-updating unread count, use the following template:: {% live_notify_list %} - + ``live_notify_list`` takes the following arguments: - 1. ``list_id`` (default ``live_notify_list``) - The ``id`` attribute for the ``
      `` element that will be created to insert the list of notifications into. - #. ``classes`` (default ````) - A string used to populate the ``class`` attribute of the above element. + 1. ``list_id`` (default ``live_notify_list``) - The ``id`` attribute for the ``
        `` element that will be created to insert the list of notifications into. + #. ``classes`` (default ````) - A string used to populate the ``class`` attribute of the above element. Using the live-updater with bootstrap ------------------------------------- @@ -319,7 +319,7 @@ While the live notifier for unread counts should suit most use cases, users may unread notifications are shown. The ``callbacks`` argument of the ``register_notify_callbacks`` dictates which javascript functions are called when -the unread api call is made. +the unread api call is made. To add a custom javascript callback, simply add this to the list, like so:: diff --git a/notifications/__init__.py b/notifications/__init__.py index 03628d4..96bd755 100644 --- a/notifications/__init__.py +++ b/notifications/__init__.py @@ -11,7 +11,7 @@ except ImportError: __version_info__ = { 'major': 0, - 'minor': 7, + 'minor': 8, 'micro': 0, 'releaselevel': 'final', 'serial': 0 From 20548d195ff1c8a19f2b97262c5ebf5ba72fd984 Mon Sep 17 00:00:00 2001 From: Samuel Spencer Date: Sat, 12 Dec 2015 07:09:20 +0000 Subject: [PATCH 5/5] 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: + + +
          + {% for notice in notifications %} + {% include 'notifications/notice.html' %} + {% endfor %} +
        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) +