From d8567930b4b21178d3b155db677ed8ad62613a54 Mon Sep 17 00:00:00 2001 From: Matthew Schinckel Date: Tue, 23 Oct 2012 17:20:29 +1030 Subject: [PATCH] Change 'readed' field to unread. Add a better manager/queryset. (required django-model-utils). --- notifications/managers.py | 9 -------- notifications/models.py | 21 +++++++++++++++---- .../templates/notifications/list.html | 2 +- .../templatetags/notifications_tags.py | 4 +++- setup.py | 5 ++++- 5 files changed, 25 insertions(+), 16 deletions(-) delete mode 100644 notifications/managers.py diff --git a/notifications/managers.py b/notifications/managers.py deleted file mode 100644 index c4a85d3..0000000 --- a/notifications/managers.py +++ /dev/null @@ -1,9 +0,0 @@ -from django.db import models - - -class NotificationManager(models.Manager): - def unread_count(self, user): - return self.filter(recipient=user, readed=False).count() - - def mark_all_as_read(self, recipient): - return self.filter(recipient=recipient, readed=False).update(readed=True) diff --git a/notifications/models.py b/notifications/models.py index 4fd5bff..d4dedfb 100644 --- a/notifications/models.py +++ b/notifications/models.py @@ -6,15 +6,28 @@ from django.contrib.contenttypes import generic from django.db import models from django.utils.timezone import utc from .utils import id2slug -from notifications.managers import NotificationManager + from notifications.signals import notify +from model_utils import managers + try: from django.utils import timezone now = timezone.now except ImportError: now = datetime.datetime.now +class NotificationQuerySet(models.query.QuerySet): + def unread(self): + return self.filter(unread=True) + + # Should we return self on these? + def mark_all_as_read(self): + self.update(unread=False) + + def mark_all_as_unread(self): + self.update(unread=True) + class Notification(models.Model): """ Action model describing the actor acting out a verb (on an optional @@ -43,9 +56,9 @@ class Notification(models.Model): brosner commented on pinax/pinax 2 hours ago - """ + """ recipient = models.ForeignKey(User, blank=False, related_name='notifications') - readed = models.BooleanField(default=False, blank=False) + unread = models.BooleanField(default=True, blank=False) actor_content_type = models.ForeignKey(ContentType, related_name='notify_actor') actor_object_id = models.CharField(max_length=255) @@ -71,7 +84,7 @@ class Notification(models.Model): public = models.BooleanField(default=True) - objects = NotificationManager() + objects = managers.PassThroughManager.for_queryset_class(NotificationQuerySet)() class Meta: ordering = ('-timestamp', ) diff --git a/notifications/templates/notifications/list.html b/notifications/templates/notifications/list.html index 910243a..21b895f 100644 --- a/notifications/templates/notifications/list.html +++ b/notifications/templates/notifications/list.html @@ -17,7 +17,7 @@
- + {% if action.actor.get_absolute_url %} {{ action.actor }} {% endif %} diff --git a/notifications/templatetags/notifications_tags.py b/notifications/templatetags/notifications_tags.py index 7ea54e4..31de030 100644 --- a/notifications/templatetags/notifications_tags.py +++ b/notifications/templatetags/notifications_tags.py @@ -6,6 +6,8 @@ from django.template import Node register = Library() +# TODO: Simplify this: it's a really simple tag! + class InboxCountNode(Node): "For use in the notifications_unread tag" def __init__(self, asvar=None): @@ -21,7 +23,7 @@ class InboxCountNode(Node): if user.is_anonymous(): count = '' else: - count = Notification.objects.unread_count(user) + count = user.notifications.unread().count() except (KeyError, AttributeError): count = '' if self.asvar: diff --git a/setup.py b/setup.py index 764de9d..0f6e380 100644 --- a/setup.py +++ b/setup.py @@ -1,13 +1,16 @@ from distutils.core import setup from notifications import __version__ -setup(name='django-notifications-hq', +setup(name='django-notifications', version=__version__, description='GitHub notifications alike app for Django.', long_description=open('README.rst').read(), author='Brant Young', author_email='brant.young@gmail.com', url='http://github.com/brantyoung/django-notifications', + install_requires=[ + 'django-model-utils>=1.1.0' + ], packages=['notifications', 'notifications.templatetags'], package_data={'notifications': [