import datetime 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 .utils import id2slug from notifications.signals import notify from model_utils import managers, Choices now = datetime.datetime.now if getattr(settings, 'USE_TZ'): try: from django.utils import timezone now = timezone.now except ImportError: pass 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 # 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): """ Action model describing the actor acting out a verb (on an optional target). Nomenclature based on http://activitystrea.ms/specs/atom/1.0/ Generalized Format::