mirror of
https://github.com/Hopiu/django-notifications.git
synced 2026-04-19 20:51:00 +00:00
Fix Pep8 & use setuptools
This commit is contained in:
parent
7109663f11
commit
7634dff290
11 changed files with 109 additions and 115 deletions
|
|
@ -7,4 +7,4 @@ if __name__ == "__main__":
|
|||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "notifications.tests.settings")
|
||||
from django.core.management import execute_from_command_line
|
||||
|
||||
execute_from_command_line(sys.argv)
|
||||
execute_from_command_line(sys.argv)
|
||||
|
|
|
|||
|
|
@ -1,2 +1 @@
|
|||
default_app_config = 'notifications.apps.Config'
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
from django.contrib import admin
|
||||
from .models import Notification
|
||||
|
||||
|
||||
class NotificationAdmin(admin.ModelAdmin):
|
||||
list_display = ('recipient', 'actor',
|
||||
'level', 'target', 'unread', 'public')
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class Config(AppConfig):
|
||||
name = "notifications"
|
||||
|
||||
def ready(self):
|
||||
super(Config, self).ready()
|
||||
# this is for backwards compability
|
||||
import notifications.signals
|
||||
notifications.notify = notifications.signals.notify
|
||||
import notifications.signals
|
||||
notifications.notify = notifications.signals.notify
|
||||
|
|
|
|||
|
|
@ -212,7 +212,7 @@ class Notification(models.Model):
|
|||
return u'%(actor)s %(verb)s %(action_object)s %(timesince)s ago' % ctx
|
||||
return u'%(actor)s %(verb)s %(timesince)s ago' % ctx
|
||||
|
||||
def __str__(self):#Adds support for Python 3
|
||||
def __str__(self): # Adds support for Python 3
|
||||
return self.__unicode__()
|
||||
|
||||
def timesince(self, now=None):
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.template import Library
|
||||
from django.template.base import TemplateSyntaxError
|
||||
from django.template import Node
|
||||
|
||||
register = Library()
|
||||
|
||||
|
||||
@register.assignment_tag(takes_context=True)
|
||||
def notifications_unread(context):
|
||||
user = user_context(context)
|
||||
|
|
@ -13,26 +12,26 @@ def notifications_unread(context):
|
|||
return ''
|
||||
return user.notifications.unread().count()
|
||||
|
||||
|
||||
# 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'):
|
||||
refresh_period=int(refresh_period)*1000
|
||||
def register_notify_callbacks(badge_id='live_notify_badge',menu_id='live_notify_list', refresh_period=15,callbacks="",api_name='list'):
|
||||
refresh_period = int(refresh_period)*1000
|
||||
|
||||
if api_name=='list':
|
||||
if api_name == 'list':
|
||||
api_url = reverse('notifications:live_unread_notification_list')
|
||||
elif api_name=='count':
|
||||
elif api_name == 'count':
|
||||
api_url = reverse('notifications:live_unread_notification_count')
|
||||
else:
|
||||
return ""
|
||||
|
||||
definitions="""
|
||||
definitions = """
|
||||
notify_badge_id='{badge_id}';
|
||||
notify_menu_id='{menu_id}';
|
||||
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_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 = "<script>"+definitions
|
||||
for callback in callbacks.split(','):
|
||||
script += "register_notifier("+callback+");"
|
||||
|
|
@ -41,20 +40,22 @@ def register_notify_callbacks(badge_id='live_notify_badge',menu_id='live_notify_
|
|||
|
||||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def live_notify_badge(context,badge_id='live_notify_badge',classes=""):
|
||||
def live_notify_badge(context, badge_id='live_notify_badge', classes=""):
|
||||
user = user_context(context)
|
||||
if not user:
|
||||
return ''
|
||||
|
||||
html="<span id='{badge_id}' class='{classes}'>{unread}</span>".format(badge_id=badge_id,classes=classes,unread=user.notifications.unread().count())
|
||||
html = "<span id='{badge_id}' class='{classes}'>{unread}</span>".format(badge_id=badge_id, classes=classes, unread=user.notifications.unread().count())
|
||||
return html
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def live_notify_list(list_id='live_notify_list',classes=""):
|
||||
html="<ul id='{list_id}' class='{classes}'></ul>".format(list_id=list_id,classes=classes)
|
||||
def live_notify_list(list_id='live_notify_list', classes=""):
|
||||
html = "<ul id='{list_id}' class='{classes}'></ul>".format(list_id=list_id, classes=classes)
|
||||
return html
|
||||
|
||||
def user_context(context):
|
||||
|
||||
def user_context(context):
|
||||
if 'user' not in context:
|
||||
return None
|
||||
|
||||
|
|
@ -62,4 +63,4 @@ def user_context(context):
|
|||
user = request.user
|
||||
if user.is_anonymous():
|
||||
return None
|
||||
return user
|
||||
return user
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ DATABASES = {
|
|||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'NAME': ':memory:',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -47,8 +47,8 @@ TEMPLATES = [
|
|||
class DisableMigrations(object):
|
||||
def __contains__(self, item):
|
||||
return True
|
||||
|
||||
def __getitem__(self, item):
|
||||
return "notmigrations"
|
||||
|
||||
MIGRATION_MODULES = DisableMigrations()
|
||||
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ class NotificationTest(TestCase):
|
|||
to_user = User.objects.create(username="to", password="pwd", email="example@example.com")
|
||||
notify.send(from_user, recipient=to_user, verb='commented', action_object=from_user)
|
||||
notification = Notification.objects.get(recipient=to_user)
|
||||
delta = timezone.now().replace(tzinfo=utc) - localtime(notification.timestamp,pytz.timezone(settings.TIME_ZONE))
|
||||
delta = timezone.now().replace(tzinfo=utc) - localtime(notification.timestamp, pytz.timezone(settings.TIME_ZONE))
|
||||
self.assertTrue(delta.seconds < 60)
|
||||
# The delta between the two events will still be less than a second despite the different timezones
|
||||
# The call to now and the immediate call afterwards will be within a short period of time, not 8 hours as the test above was originally.
|
||||
|
|
@ -71,19 +71,19 @@ class NotificationManagersTest(TestCase):
|
|||
self.assertEqual(Notification.objects.unread().count(), self.message_count)
|
||||
n = Notification.objects.filter(recipient=self.to_user).first()
|
||||
n.mark_as_read()
|
||||
self.assertEqual(Notification.objects.read().count(),1)
|
||||
self.assertEqual(Notification.objects.read().count(), 1)
|
||||
for n in Notification.objects.read():
|
||||
self.assertFalse(n.unread)
|
||||
|
||||
def test_mark_all_as_read_manager(self):
|
||||
self.assertEqual(Notification.objects.unread().count(), self.message_count)
|
||||
Notification.objects.filter(recipient=self.to_user).mark_all_as_read()
|
||||
self.assertEqual(Notification.objects.unread().count(),0)
|
||||
self.assertEqual(Notification.objects.unread().count(), 0)
|
||||
|
||||
def test_mark_all_as_unread_manager(self):
|
||||
self.assertEqual(Notification.objects.unread().count(), self.message_count)
|
||||
Notification.objects.filter(recipient=self.to_user).mark_all_as_read()
|
||||
self.assertEqual(Notification.objects.unread().count(),0)
|
||||
self.assertEqual(Notification.objects.unread().count(), 0)
|
||||
Notification.objects.filter(recipient=self.to_user).mark_all_as_unread()
|
||||
self.assertEqual(Notification.objects.unread().count(), self.message_count)
|
||||
|
||||
|
|
@ -129,54 +129,54 @@ class NotificationTestPages(TestCase):
|
|||
def logout(self):
|
||||
self.client.post(reverse('admin:logout')+'?next=/', {})
|
||||
|
||||
def login(self,username,password):
|
||||
def login(self, username, password):
|
||||
self.logout()
|
||||
response = self.client.post(reverse('login'), {'username': username, 'password': password})
|
||||
self.assertEqual(response.status_code,302)
|
||||
self.assertEqual(response.status_code, 302)
|
||||
return response
|
||||
|
||||
def test_all_messages_page(self):
|
||||
self.login('to','pwd')
|
||||
self.login('to', 'pwd')
|
||||
response = self.client.get(reverse('notifications:all'))
|
||||
|
||||
self.assertEqual(response.status_code,200)
|
||||
self.assertEqual(len(response.context['notifications']),len(self.to_user.notifications.all()))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.context['notifications']), len(self.to_user.notifications.all()))
|
||||
|
||||
def test_unread_messages_pages(self):
|
||||
self.login('to','pwd')
|
||||
self.login('to', 'pwd')
|
||||
response = self.client.get(reverse('notifications:unread'))
|
||||
self.assertEqual(response.status_code,200)
|
||||
self.assertEqual(len(response.context['notifications']),len(self.to_user.notifications.unread()))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.context['notifications']), len(self.to_user.notifications.unread()))
|
||||
self.assertEqual(len(response.context['notifications']), self.message_count)
|
||||
|
||||
for i,n in enumerate(self.to_user.notifications.all()):
|
||||
if i%3 == 0:
|
||||
response = self.client.get(reverse('notifications:mark_as_read',args=[id2slug(n.id)]))
|
||||
self.assertEqual(response.status_code,302)
|
||||
for i, n in enumerate(self.to_user.notifications.all()):
|
||||
if i % 3 == 0:
|
||||
response = self.client.get(reverse('notifications:mark_as_read', args=[id2slug(n.id)]))
|
||||
self.assertEqual(response.status_code, 302)
|
||||
|
||||
response = self.client.get(reverse('notifications:unread'))
|
||||
self.assertEqual(response.status_code,200)
|
||||
self.assertEqual(len(response.context['notifications']),len(self.to_user.notifications.unread()))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertEqual(len(response.context['notifications']), len(self.to_user.notifications.unread()))
|
||||
self.assertTrue(len(response.context['notifications']) < self.message_count)
|
||||
|
||||
response = self.client.get(reverse('notifications:mark_all_as_read'))
|
||||
self.assertRedirects(response,reverse('notifications:all'))
|
||||
self.assertRedirects(response, reverse('notifications:all'))
|
||||
response = self.client.get(reverse('notifications:unread'))
|
||||
self.assertEqual(len(response.context['notifications']),len(self.to_user.notifications.unread()))
|
||||
self.assertEqual(len(response.context['notifications']),0)
|
||||
self.assertEqual(len(response.context['notifications']), len(self.to_user.notifications.unread()))
|
||||
self.assertEqual(len(response.context['notifications']), 0)
|
||||
|
||||
def test_next_pages(self):
|
||||
self.login('to','pwd')
|
||||
self.login('to', 'pwd')
|
||||
response = self.client.get(reverse('notifications:mark_all_as_read')+"?next="+reverse('notifications:unread'))
|
||||
self.assertRedirects(response,reverse('notifications:unread'))
|
||||
self.assertRedirects(response, reverse('notifications:unread'))
|
||||
|
||||
slug = id2slug(self.to_user.notifications.first().id)
|
||||
response = self.client.get(reverse('notifications:mark_as_read',args=[slug])+"?next="+reverse('notifications:unread'))
|
||||
self.assertRedirects(response,reverse('notifications:unread'))
|
||||
response = self.client.get(reverse('notifications:mark_as_read', args=[slug])+"?next="+reverse('notifications:unread'))
|
||||
self.assertRedirects(response, reverse('notifications:unread'))
|
||||
|
||||
slug = id2slug(self.to_user.notifications.first().id)
|
||||
response = self.client.get(reverse('notifications:mark_as_unread',args=[slug])+"?next="+reverse('notifications:unread'))
|
||||
self.assertRedirects(response,reverse('notifications:unread'))
|
||||
response = self.client.get(reverse('notifications:mark_as_unread', args=[slug])+"?next="+reverse('notifications:unread'))
|
||||
self.assertRedirects(response, reverse('notifications:unread'))
|
||||
|
||||
def test_delete_messages_pages(self):
|
||||
self.login('to', 'pwd')
|
||||
|
|
@ -213,64 +213,62 @@ class NotificationTestPages(TestCase):
|
|||
self.assertEqual(len(response.context['notifications']), len(self.to_user.notifications.unread()))
|
||||
self.assertEqual(len(response.context['notifications']), self.message_count-1)
|
||||
|
||||
|
||||
def test_unread_count_api(self):
|
||||
self.login('to', 'pwd')
|
||||
|
||||
response = self.client.get(reverse('notifications:live_unread_notification_count'))
|
||||
data = json.loads(response.content.decode('utf-8'))
|
||||
self.assertEqual(list(data.keys()),['unread_count'])
|
||||
self.assertEqual(data['unread_count'],10)
|
||||
|
||||
self.assertEqual(list(data.keys()), ['unread_count'])
|
||||
self.assertEqual(data['unread_count'], 10)
|
||||
|
||||
Notification.objects.filter(recipient=self.to_user).mark_all_as_read()
|
||||
response = self.client.get(reverse('notifications:live_unread_notification_count'))
|
||||
data = json.loads(response.content.decode('utf-8'))
|
||||
self.assertEqual(list(data.keys()),['unread_count'])
|
||||
self.assertEqual(data['unread_count'],0)
|
||||
self.assertEqual(list(data.keys()), ['unread_count'])
|
||||
self.assertEqual(data['unread_count'], 0)
|
||||
|
||||
notify.send(self.from_user, recipient=self.to_user, verb='commented', action_object=self.from_user)
|
||||
response = self.client.get(reverse('notifications:live_unread_notification_count'))
|
||||
data = json.loads(response.content.decode('utf-8'))
|
||||
self.assertEqual(list(data.keys()),['unread_count'])
|
||||
self.assertEqual(data['unread_count'],1)
|
||||
self.assertEqual(list(data.keys()), ['unread_count'])
|
||||
self.assertEqual(data['unread_count'], 1)
|
||||
|
||||
def test_unread_list_api(self):
|
||||
self.login('to', 'pwd')
|
||||
|
||||
response = self.client.get(reverse('notifications:live_unread_notification_list'))
|
||||
data = json.loads(response.content.decode('utf-8'))
|
||||
self.assertEqual(sorted(list(data.keys())),['unread_count','unread_list'])
|
||||
self.assertEqual(data['unread_count'],10)
|
||||
self.assertEqual(len(data['unread_list']),5)
|
||||
self.assertEqual(sorted(list(data.keys())), ['unread_count', 'unread_list'])
|
||||
self.assertEqual(data['unread_count'], 10)
|
||||
self.assertEqual(len(data['unread_list']), 5)
|
||||
|
||||
response = self.client.get(reverse('notifications:live_unread_notification_list')+"?max=12")
|
||||
data = json.loads(response.content.decode('utf-8'))
|
||||
self.assertEqual(sorted(list(data.keys())),['unread_count','unread_list'])
|
||||
self.assertEqual(data['unread_count'],10)
|
||||
self.assertEqual(len(data['unread_list']),10)
|
||||
self.assertEqual(sorted(list(data.keys())), ['unread_count', 'unread_list'])
|
||||
self.assertEqual(data['unread_count'], 10)
|
||||
self.assertEqual(len(data['unread_list']), 10)
|
||||
|
||||
# Test with a bad 'max' value
|
||||
response = self.client.get(reverse('notifications:live_unread_notification_list')+"?max=this_is_wrong")
|
||||
data = json.loads(response.content.decode('utf-8'))
|
||||
self.assertEqual(sorted(list(data.keys())),['unread_count','unread_list'])
|
||||
self.assertEqual(data['unread_count'],10)
|
||||
self.assertEqual(len(data['unread_list']),5)
|
||||
self.assertEqual(sorted(list(data.keys())), ['unread_count', 'unread_list'])
|
||||
self.assertEqual(data['unread_count'], 10)
|
||||
self.assertEqual(len(data['unread_list']), 5)
|
||||
|
||||
Notification.objects.filter(recipient=self.to_user).mark_all_as_read()
|
||||
response = self.client.get(reverse('notifications:live_unread_notification_list'))
|
||||
data = json.loads(response.content.decode('utf-8'))
|
||||
self.assertEqual(sorted(list(data.keys())),['unread_count','unread_list'])
|
||||
self.assertEqual(data['unread_count'],0)
|
||||
self.assertEqual(len(data['unread_list']),0)
|
||||
self.assertEqual(sorted(list(data.keys())), ['unread_count', 'unread_list'])
|
||||
self.assertEqual(data['unread_count'], 0)
|
||||
self.assertEqual(len(data['unread_list']), 0)
|
||||
|
||||
notify.send(self.from_user, recipient=self.to_user, verb='commented', action_object=self.from_user)
|
||||
response = self.client.get(reverse('notifications:live_unread_notification_list'))
|
||||
data = json.loads(response.content.decode('utf-8'))
|
||||
self.assertEqual(sorted(list(data.keys())),['unread_count','unread_list'])
|
||||
self.assertEqual(data['unread_count'],1)
|
||||
self.assertEqual(len(data['unread_list']),1)
|
||||
self.assertEqual(data['unread_list'][0]['verb'],'commented')
|
||||
self.assertEqual(sorted(list(data.keys())), ['unread_count', 'unread_list'])
|
||||
self.assertEqual(data['unread_count'], 1)
|
||||
self.assertEqual(len(data['unread_list']), 1)
|
||||
self.assertEqual(data['unread_list'][0]['verb'], 'commented')
|
||||
|
||||
def test_live_update_tags(self):
|
||||
from django.shortcuts import render
|
||||
|
|
@ -281,6 +279,6 @@ class NotificationTestPages(TestCase):
|
|||
request = self.factory.get('/notification/live_updater')
|
||||
request.user = self.to_user
|
||||
|
||||
page = render(request, 'notifications/test_tags.html', {'request':request})
|
||||
|
||||
#TODO: Add more tests to check what is being output.
|
||||
page = render(request, 'notifications/test_tags.html', {'request': request})
|
||||
|
||||
#TODO: Add more tests to check what is being output.
|
||||
|
|
|
|||
|
|
@ -4,8 +4,10 @@ import sys
|
|||
if sys.version > '3':
|
||||
long = int
|
||||
|
||||
|
||||
def slug2id(slug):
|
||||
return long(slug) - 110909
|
||||
|
||||
|
||||
def id2slug(id):
|
||||
return id + 110909
|
||||
|
|
|
|||
|
|
@ -141,4 +141,3 @@ def live_unread_notification_list(request):
|
|||
'unread_list': [model_to_dict(n) for n in request.user.notifications.unread()[0:num_to_fetch]]
|
||||
}
|
||||
return JsonResponse(data)
|
||||
|
||||
|
|
|
|||
67
setup.py
67
setup.py
|
|
@ -1,29 +1,24 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from distutils.core import setup
|
||||
from setuptools import setup
|
||||
from version import __version__
|
||||
|
||||
setup(name='django-notifications-hq',
|
||||
|
||||
setup(
|
||||
name='django-notifications-hq',
|
||||
version=__version__,
|
||||
|
||||
description='GitHub notifications alike app for Django.',
|
||||
|
||||
long_description=open('README.rst').read(),
|
||||
|
||||
author='django-notifications team',
|
||||
|
||||
author_email='yang@yangyubo.com',
|
||||
|
||||
url='http://github.com/django-notifications/django-notifications',
|
||||
|
||||
install_requires=[
|
||||
'django>=1.7',
|
||||
'django-model-utils>=2.0.3',
|
||||
'six>=1.9.0',
|
||||
'jsonfield>=1.0.3',
|
||||
],
|
||||
|
||||
test_requires=[
|
||||
'django>=1.7',
|
||||
'django-model-utils>=2.0.3',
|
||||
|
|
@ -31,33 +26,31 @@ setup(name='django-notifications-hq',
|
|||
'jsonfield>=1.0.3',
|
||||
'pytz'
|
||||
],
|
||||
|
||||
packages=['notifications',
|
||||
'notifications.templatetags',
|
||||
'notifications.migrations',
|
||||
],
|
||||
|
||||
package_data={'notifications': [
|
||||
'templates/notifications/*.html', 'static/notifications/*.js']},
|
||||
|
||||
classifiers=['Development Status :: 5 - Production/Stable',
|
||||
'Environment :: Web Environment',
|
||||
'Framework :: Django',
|
||||
'Intended Audience :: Developers',
|
||||
'License :: OSI Approved :: BSD License',
|
||||
'Operating System :: OS Independent',
|
||||
# Specify the Python versions you support here. In particular, ensure
|
||||
# that you indicate whether you support Python 2, Python 3 or both.
|
||||
'Programming Language :: Python',
|
||||
'Programming Language :: Python :: 2',
|
||||
'Programming Language :: Python :: 2.7',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Programming Language :: Python :: 3.3',
|
||||
'Programming Language :: Python :: 3.4',
|
||||
'Topic :: Utilities'
|
||||
],
|
||||
|
||||
packages=[
|
||||
'notifications',
|
||||
'notifications.templatetags',
|
||||
'notifications.migrations',
|
||||
],
|
||||
package_data={
|
||||
'notifications': ['templates/notifications/*.html', 'static/notifications/*.js']
|
||||
},
|
||||
classifiers=[
|
||||
'Development Status :: 5 - Production/Stable',
|
||||
'Environment :: Web Environment',
|
||||
'Framework :: Django',
|
||||
'Intended Audience :: Developers',
|
||||
'License :: OSI Approved :: BSD License',
|
||||
'Operating System :: OS Independent',
|
||||
# Specify the Python versions you support here. In particular, ensure
|
||||
# that you indicate whether you support Python 2, Python 3 or both.
|
||||
'Programming Language :: Python',
|
||||
'Programming Language :: Python :: 2',
|
||||
'Programming Language :: Python :: 2.7',
|
||||
'Programming Language :: Python :: 3',
|
||||
'Programming Language :: Python :: 3.3',
|
||||
'Programming Language :: Python :: 3.4',
|
||||
'Topic :: Utilities'
|
||||
],
|
||||
keywords='django notifications github action event stream',
|
||||
|
||||
license='MIT',
|
||||
)
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue