mirror of
https://github.com/Hopiu/django-notifications.git
synced 2026-03-17 05:40:25 +00:00
added live tester page. Confirmed all works.
This commit is contained in:
parent
6d368fb717
commit
20548d195f
9 changed files with 83 additions and 6 deletions
10
README.rst
10
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 ``<empty string>``) - 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
|
||||
==============================
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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 + "<li>"+item.object+" "+item.verb+" "+item.subject+"</li>";
|
||||
menu.innerHTML = menu.innerHTML + "<li>"+item.object+" "+item.verb+" "+item.subject+" at " +item.timestamp + "</li>";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 = "<script>"+definitions
|
||||
for callback in callbacks.split(','):
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
import os
|
||||
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
|
||||
SECRET_KEY = 'secret_key'
|
||||
SOUTH_TESTS_MIGRATE = True
|
||||
|
||||
DEBUG = True
|
||||
TESTING = True
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': ':memory:',
|
||||
'NAME': 'test.sqlite3',
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -20,11 +22,18 @@ INSTALLED_APPS = (
|
|||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.staticfiles',
|
||||
'django.contrib.sessions',
|
||||
'notifications.tests',
|
||||
'notifications',
|
||||
)
|
||||
|
||||
ROOT_URLCONF = 'notifications.tests.urls'
|
||||
STATIC_URL = '/static/'
|
||||
STATICFILES_DIRS = [
|
||||
os.path.join(BASE_DIR, "static"),
|
||||
]
|
||||
STATIC_ROOT = os.path.join(BASE_DIR, "static-files")
|
||||
|
||||
|
||||
# Need to skip migrations for now as migrations created with python2 break with python3
|
||||
|
|
|
|||
7
notifications/tests/static/notifications/live-test.js
Normal file
7
notifications/tests/static/notifications/live-test.js
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
console.log('running tester')
|
||||
|
||||
function make_notification() {
|
||||
var r = new XMLHttpRequest();
|
||||
r.open("GET", '/test_make/', true);
|
||||
r.send();
|
||||
}
|
||||
14
notifications/tests/templates/test_live.html
Normal file
14
notifications/tests/templates/test_live.html
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
{% load static notifications_tags %}
|
||||
|
||||
<script src="{% static 'notifications/notify.js' %}" type="text/javascript"></script>
|
||||
<script src="{% static 'notifications/live-test.js' %}" type="text/javascript"></script>
|
||||
{% register_notify_callbacks callbacks='fill_notification_list,fill_notification_badge' to_fetch=20 refresh_period=5 %}
|
||||
|
||||
There are this many notifications pending: <span id='live_notify_badge'></span>
|
||||
|
||||
<button onclick='make_notification()'>Make a notification</button>
|
||||
<ul class="notifications" id="live_notify_list">
|
||||
{% for notice in notifications %}
|
||||
{% include 'notifications/notice.html' %}
|
||||
{% endfor %}
|
||||
</ul>
|
||||
|
|
@ -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')),
|
||||
)
|
||||
|
|
|
|||
27
notifications/tests/views.py
Normal file
27
notifications/tests/views.py
Normal file
|
|
@ -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)
|
||||
|
||||
Loading…
Reference in a new issue