Merge pull request #42 from steppinstonez/master

Python3 compatibility (using six).
This commit is contained in:
Yang.Y 2015-02-04 23:37:37 +08:00
commit 44ef50eabd
2 changed files with 18 additions and 17 deletions

View file

@ -3,7 +3,7 @@ from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.db import models
from django.utils.timezone import utc
from six import text_type
from .utils import id2slug
from .signals import notify
@ -20,38 +20,38 @@ if getattr(settings, 'USE_TZ'):
class NotificationQuerySet(models.query.QuerySet):
def unread(self):
"Return only unread items in the current queryset"
return self.filter(unread=True)
def read(self):
"Return only read items in the current queryset"
return self.filter(unread=False)
def mark_all_as_read(self, recipient=None):
"""Mark as read any unread messages in the current queryset.
Optionally, filter these by recipient first.
"""
# We want to filter out read ones, as later we will store
# We want to filter out read ones, as later we will store
# the time they were marked as read.
qs = self.unread()
if recipient:
qs = qs.filter(recipient=recipient)
qs.update(unread=False)
def mark_all_as_unread(self, recipient=None):
"""Mark as unread any read messages in the current queryset.
Optionally, filter these by recipient first.
"""
qs = self.read()
if recipient:
qs = qs.filter(recipient=recipient)
qs.update(unread=True)
class Notification(models.Model):
@ -85,7 +85,7 @@ class Notification(models.Model):
"""
LEVELS = Choices('success', 'info', 'warning', 'error')
level = models.CharField(choices=LEVELS, default='info', max_length=20)
recipient = models.ForeignKey(settings.AUTH_USER_MODEL, blank=False, related_name='notifications')
unread = models.BooleanField(default=True, blank=False)
@ -112,7 +112,7 @@ class Notification(models.Model):
timestamp = models.DateTimeField(default=now)
public = models.BooleanField(default=True)
objects = managers.PassThroughManager.for_queryset_class(NotificationQuerySet)()
class Meta:
@ -162,7 +162,7 @@ if getattr(settings, 'NOTIFY_USE_JSONFIELD', False):
from jsonfield.fields import JSONField
except ImportError:
raise ImproperlyConfigured("You must have a suitable JSONField installed")
JSONField(blank=True, null=True).contribute_to_class(Notification, 'data')
EXTRA_DATA = True
@ -179,7 +179,7 @@ def notify_handler(verb, **kwargs):
recipient = recipient,
actor_content_type=ContentType.objects.get_for_model(actor),
actor_object_id=actor.pk,
verb=unicode(verb),
verb=text_type(verb),
public=bool(kwargs.pop('public', True)),
description=kwargs.pop('description', None),
timestamp=kwargs.pop('timestamp', now())
@ -191,7 +191,7 @@ def notify_handler(verb, **kwargs):
setattr(newnotify, '%s_object_id' % opt, obj.pk)
setattr(newnotify, '%s_content_type' % opt,
ContentType.objects.get_for_model(obj))
if len(kwargs) and EXTRA_DATA:
newnotify.data = kwargs

View file

@ -10,7 +10,8 @@ setup(name='django-notifications-hq',
url='http://github.com/brantyoung/django-notifications',
install_requires=[
'django>=1.4',
'django-model-utils>=2.0.3'
'django-model-utils>=2.0.3',
'six>=1.9.0'
],
packages=['notifications',
'notifications.templatetags',