mirror of
https://github.com/Hopiu/django-notifications.git
synced 2026-04-30 09:54:42 +00:00
Tidy with flake8
This commit is contained in:
parent
055b607525
commit
6668474c65
5 changed files with 50 additions and 43 deletions
|
|
@ -22,6 +22,7 @@ from jsonfield.fields import JSONField
|
|||
|
||||
from django.contrib.auth.models import Group
|
||||
|
||||
|
||||
def now():
|
||||
# Needs to be be a function as USE_TZ can change based on if we are testing or not.
|
||||
_now = datetime.datetime.now
|
||||
|
|
@ -34,9 +35,9 @@ def now():
|
|||
return _now()
|
||||
|
||||
|
||||
#SOFT_DELETE = getattr(settings, 'NOTIFICATIONS_SOFT_DELETE', False)
|
||||
# SOFT_DELETE = getattr(settings, 'NOTIFICATIONS_SOFT_DELETE', False)
|
||||
def is_soft_delete():
|
||||
#TODO: SOFT_DELETE = getattr(settings, ...) doesn't work with "override_settings" decorator in unittest
|
||||
# TODO: SOFT_DELETE = getattr(settings, ...) doesn't work with "override_settings" decorator in unittest
|
||||
# But is_soft_delete is neither a very elegant way. Should try to find better approach
|
||||
return getattr(settings, 'NOTIFICATIONS_SOFT_DELETE', False)
|
||||
|
||||
|
|
@ -171,18 +172,14 @@ class Notification(models.Model):
|
|||
verb = models.CharField(max_length=255)
|
||||
description = models.TextField(blank=True, null=True)
|
||||
|
||||
target_content_type = models.ForeignKey(ContentType, related_name='notify_target',
|
||||
blank=True, null=True)
|
||||
target_content_type = models.ForeignKey(ContentType, related_name='notify_target', blank=True, null=True)
|
||||
target_object_id = models.CharField(max_length=255, blank=True, null=True)
|
||||
target = GenericForeignKey('target_content_type',
|
||||
'target_object_id')
|
||||
target = GenericForeignKey('target_content_type', 'target_object_id')
|
||||
|
||||
action_object_content_type = models.ForeignKey(ContentType,
|
||||
related_name='notify_action_object', blank=True, null=True)
|
||||
action_object_object_id = models.CharField(max_length=255, blank=True,
|
||||
null=True)
|
||||
action_object = GenericForeignKey('action_object_content_type',
|
||||
'action_object_object_id')
|
||||
action_object_content_type = models.ForeignKey(ContentType, blank=True, null=True,
|
||||
related_name='notify_action_object')
|
||||
action_object_object_id = models.CharField(max_length=255, blank=True, null=True)
|
||||
action_object = GenericForeignKey('action_object_content_type', 'action_object_object_id')
|
||||
|
||||
timestamp = models.DateTimeField(default=now)
|
||||
|
||||
|
|
@ -254,11 +251,14 @@ def notify_handler(verb, **kwargs):
|
|||
kwargs.pop('signal', None)
|
||||
recipient = kwargs.pop('recipient')
|
||||
actor = kwargs.pop('sender')
|
||||
optional_objs = [(kwargs.pop(opt, None), opt) for opt in ('target', 'action_object')]
|
||||
public=bool(kwargs.pop('public', True))
|
||||
description=kwargs.pop('description', None)
|
||||
timestamp=kwargs.pop('timestamp', now())
|
||||
level=kwargs.pop('level', Notification.LEVELS.info)
|
||||
optional_objs = [
|
||||
(kwargs.pop(opt, None), opt)
|
||||
for opt in ('target', 'action_object')
|
||||
]
|
||||
public = bool(kwargs.pop('public', True))
|
||||
description = kwargs.pop('description', None)
|
||||
timestamp = kwargs.pop('timestamp', now())
|
||||
level = kwargs.pop('level', Notification.LEVELS.info)
|
||||
|
||||
# Check if User or Group
|
||||
if isinstance(recipient, Group):
|
||||
|
|
@ -268,7 +268,7 @@ def notify_handler(verb, **kwargs):
|
|||
|
||||
for recipient in recipients:
|
||||
newnotify = Notification(
|
||||
recipient = recipient,
|
||||
recipient=recipient,
|
||||
actor_content_type=ContentType.objects.get_for_model(actor),
|
||||
actor_object_id=actor.pk,
|
||||
verb=text_type(verb),
|
||||
|
|
@ -279,8 +279,8 @@ def notify_handler(verb, **kwargs):
|
|||
)
|
||||
|
||||
# Set optional objects
|
||||
for obj,opt in optional_objs:
|
||||
if not obj is None:
|
||||
for obj, opt in optional_objs:
|
||||
if obj is not None:
|
||||
setattr(newnotify, '%s_object_id' % opt, obj.pk)
|
||||
setattr(newnotify, '%s_content_type' % opt,
|
||||
ContentType.objects.get_for_model(obj))
|
||||
|
|
|
|||
|
|
@ -37,7 +37,8 @@ class NotificationTest(TestCase):
|
|||
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.
|
||||
# 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.
|
||||
|
||||
@override_settings(USE_TZ=False)
|
||||
@override_settings(TIME_ZONE='Asia/Shanghai')
|
||||
|
|
@ -172,15 +173,21 @@ class NotificationTestPages(TestCase):
|
|||
|
||||
def test_next_pages(self):
|
||||
self.login('to', 'pwd')
|
||||
response = self.client.get(reverse('notifications:mark_all_as_read')+"?next="+reverse('notifications:unread'))
|
||||
response = self.client.get(reverse('notifications:mark_all_as_read'), data={
|
||||
"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_read', args=[slug])+"?next="+reverse('notifications:unread'))
|
||||
response = self.client.get(reverse('notifications:mark_as_read', args=[slug]), data={
|
||||
"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'))
|
||||
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):
|
||||
|
|
@ -247,14 +254,16 @@ class NotificationTestPages(TestCase):
|
|||
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")
|
||||
response = self.client.get(reverse('notifications:live_unread_notification_list'), data={"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)
|
||||
|
||||
# Test with a bad 'max' value
|
||||
response = self.client.get(reverse('notifications:live_unread_notification_list')+"?max=this_is_wrong")
|
||||
response = self.client.get(reverse('notifications:live_unread_notification_list'), data={
|
||||
"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)
|
||||
|
|
@ -284,6 +293,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})
|
||||
render(request, 'notifications/test_tags.html', {'request': request})
|
||||
|
||||
#TODO: Add more tests to check what is being output.
|
||||
# TODO: Add more tests to check what is being output.
|
||||
|
|
|
|||
|
|
@ -1,19 +1,18 @@
|
|||
from django.conf import settings
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
from django.shortcuts import render
|
||||
|
||||
from notifications import notify
|
||||
import random
|
||||
|
||||
|
||||
def live_tester(request):
|
||||
notify.send(sender=request.user, recipient=request.user, verb='you loaded the page')
|
||||
|
||||
data = {
|
||||
return render(request, 'test_live.html', {
|
||||
'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([
|
||||
|
|
@ -21,7 +20,7 @@ def make_notification(request):
|
|||
'cleaning the car',
|
||||
'jumping the shark',
|
||||
'testing the app',
|
||||
])
|
||||
|
||||
notify.send(sender=request.user, recipient=request.user, verb='you asked for a notification - you are '+the_notification)
|
||||
])
|
||||
|
||||
notify.send(sender=request.user, recipient=request.user,
|
||||
verb='you asked for a notification - you are ' + the_notification)
|
||||
|
|
|
|||
|
|
@ -16,4 +16,3 @@ urlpatterns = [
|
|||
url(r'^api/unread_count/$', views.live_unread_notification_count, name='live_unread_notification_count'),
|
||||
url(r'^api/unread_list/$', views.live_unread_notification_list, name='live_unread_notification_list'),
|
||||
]
|
||||
|
||||
|
|
|
|||
|
|
@ -131,8 +131,8 @@ def live_unread_notification_list(request):
|
|||
try:
|
||||
num_to_fetch = request.GET.get('max', 5) # If they don't specify, make it 5.
|
||||
num_to_fetch = int(num_to_fetch)
|
||||
num_to_fetch = max(1,num_to_fetch) # if num_to_fetch is negative, force at least one fetched notifications
|
||||
num_to_fetch = min(num_to_fetch,100) # put a sane ceiling on the number retrievable
|
||||
num_to_fetch = max(1, num_to_fetch) # if num_to_fetch is negative, force at least one fetched notifications
|
||||
num_to_fetch = min(num_to_fetch, 100) # put a sane ceiling on the number retrievable
|
||||
except ValueError:
|
||||
num_to_fetch = 5 # If casting to an int fails, just make it 5.
|
||||
|
||||
|
|
@ -148,7 +148,7 @@ def live_unread_notification_list(request):
|
|||
struct['action_object'] = str(n.action_object)
|
||||
unread_list.append(struct)
|
||||
data = {
|
||||
'unread_count':request.user.notifications.unread().count(),
|
||||
'unread_list':unread_list
|
||||
'unread_count': request.user.notifications.unread().count(),
|
||||
'unread_list': unread_list
|
||||
}
|
||||
return JsonResponse(data)
|
||||
|
|
|
|||
Loading…
Reference in a new issue