Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in order to work on Django1.4 #50

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion notification/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = (0, 2, 0, "f") # following PEP 386
VERSION = (0, 2, 2, "f") # following PEP 386
DEV_N = None


Expand Down
100 changes: 50 additions & 50 deletions notification/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ class LanguageStoreNotAvailable(Exception):
pass

class NoticeType(models.Model):

label = models.CharField(_("label"), max_length=40)
display = models.CharField(_("display"), max_length=50)
description = models.CharField(_("description"), max_length=100)

# by default only on for media with sensitivity less than or equal to this number
default = models.IntegerField(_("default"))

def __unicode__(self):
return self.label

class Meta:
verbose_name = _("notice type")
verbose_name_plural = _("notice types")
Expand All @@ -60,12 +60,12 @@ class NoticeSetting(models.Model):
Indicates, for a given user, whether to send notifications
of a given type to a given medium.
"""

user = models.ForeignKey(User, verbose_name=_("user"))
notice_type = models.ForeignKey(NoticeType, verbose_name=_("notice type"))
medium = models.CharField(_("medium"), max_length=1, choices=NOTICE_MEDIA)
send = models.BooleanField(_("send"))
send = models.BooleanField(_("send"), default=False)

class Meta:
verbose_name = _("notice setting")
verbose_name_plural = _("notice settings")
Expand All @@ -87,14 +87,14 @@ def should_send(user, notice_type, medium):


class NoticeManager(models.Manager):

def notices_for(self, user, archived=False, unseen=None, on_site=None, sent=False):
"""
returns Notice objects for the given user.

If archived=False, it only include notices not archived.
If archived=True, it returns all notices for that user.

If unseen=None, it includes all notices.
If unseen=True, return only unseen notices.
If unseen=False, return only seen notices.
Expand All @@ -111,21 +111,21 @@ def notices_for(self, user, archived=False, unseen=None, on_site=None, sent=Fals
if on_site is not None:
qs = qs.filter(on_site=on_site)
return qs

def unseen_count_for(self, recipient, **kwargs):
"""
returns the number of unseen notices for the given user but does not
mark them seen
"""
return self.notices_for(recipient, unseen=True, **kwargs).count()

def received(self, recipient, **kwargs):
"""
returns notices the given recipient has recieved.
"""
kwargs["sent"] = False
return self.notices_for(recipient, **kwargs)

def sent(self, sender, **kwargs):
"""
returns notices the given sender has sent
Expand All @@ -135,29 +135,29 @@ def sent(self, sender, **kwargs):


class Notice(models.Model):

recipient = models.ForeignKey(User, related_name="recieved_notices", verbose_name=_("recipient"))
sender = models.ForeignKey(User, null=True, related_name="sent_notices", verbose_name=_("sender"))
message = models.TextField(_("message"))
notice_type = models.ForeignKey(NoticeType, verbose_name=_("notice type"))
added = models.DateTimeField(_("added"), default=datetime.datetime.now)
unseen = models.BooleanField(_("unseen"), default=True)
archived = models.BooleanField(_("archived"), default=False)
on_site = models.BooleanField(_("on site"))
on_site = models.BooleanField(_("on site"), default=False)

objects = NoticeManager()

def __unicode__(self):
return self.message

def archive(self):
self.archived = True
self.save()

def is_unseen(self):
"""
returns value of self.unseen but also changes it to false.

Use this in a template to mark an unseen notice differently the first
time it is shown.
"""
Expand All @@ -166,12 +166,12 @@ def is_unseen(self):
self.unseen = False
self.save()
return unseen

class Meta:
ordering = ["-added"]
verbose_name = _("notice")
verbose_name_plural = _("notices")

def get_absolute_url(self):
return reverse("notification_notice", args=[str(self.pk)])

Expand All @@ -187,7 +187,7 @@ class NoticeQueueBatch(models.Model):
def create_notice_type(label, display, description, default=2, verbosity=1):
"""
Creates a new NoticeType.

This is intended to be used by other apps as a post_syncdb manangement step.
"""
try:
Expand Down Expand Up @@ -251,40 +251,40 @@ def get_formatted_messages(formats, label, context):
def send_now(users, label, extra_context=None, on_site=True, sender=None):
"""
Creates a new notice.

This is intended to be how other apps create new notices.

notification.send(user, "friends_invite_sent", {
"spam": "eggs",
"foo": "bar",
)

You can pass in on_site=False to prevent the notice emitted from being
displayed on the site.
"""
if extra_context is None:
extra_context = {}

notice_type = NoticeType.objects.get(label=label)

protocol = getattr(settings, "DEFAULT_HTTP_PROTOCOL", "http")
current_site = Site.objects.get_current()

notices_url = u"%s://%s%s" % (
protocol,
unicode(current_site),
reverse("notification_notices"),
)

current_language = get_language()

formats = (
"short.txt",
"full.txt",
"notice.html",
"full.html",
) # TODO make formats configurable

for user in users:
recipients = []
# get user language for user from language store defined in
Expand All @@ -293,11 +293,11 @@ def send_now(users, label, extra_context=None, on_site=True, sender=None):
language = get_notification_language(user)
except LanguageStoreNotAvailable:
language = None

if language is not None:
# activate the user's language
activate(language)

# update context with user specific translations
context = Context({
"recipient": user,
Expand All @@ -307,25 +307,25 @@ def send_now(users, label, extra_context=None, on_site=True, sender=None):
"current_site": current_site,
})
context.update(extra_context)

# get prerendered format messages
messages = get_formatted_messages(formats, label, context)

# Strip newlines from subject
subject = "".join(render_to_string("notification/email_subject.txt", {
"message": messages["short.txt"],
}, context).splitlines())

body = render_to_string("notification/email_body.txt", {
"message": messages["full.txt"],
}, context)

notice = Notice.objects.create(recipient=user, message=messages["notice.html"],
notice_type=notice_type, on_site=on_site, sender=sender)
if should_send(user, notice_type, "1") and user.email and user.is_active: # Email
recipients.append(user.email)
send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, recipients)

# reset environment to original language
activate(current_language)

Expand Down Expand Up @@ -370,7 +370,7 @@ def queue(users, label, extra_context=None, on_site=True, sender=None):


class ObservedItemManager(models.Manager):

def all_for(self, observed, signal):
"""
Returns all ObservedItems for an observed object,
Expand All @@ -379,35 +379,35 @@ def all_for(self, observed, signal):
content_type = ContentType.objects.get_for_model(observed)
observed_items = self.filter(content_type=content_type, object_id=observed.id, signal=signal)
return observed_items

def get_for(self, observed, observer, signal):
content_type = ContentType.objects.get_for_model(observed)
observed_item = self.get(content_type=content_type, object_id=observed.id, user=observer, signal=signal)
return observed_item


class ObservedItem(models.Model):

user = models.ForeignKey(User, verbose_name=_("user"))

content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
observed_object = generic.GenericForeignKey("content_type", "object_id")

notice_type = models.ForeignKey(NoticeType, verbose_name=_("notice type"))

added = models.DateTimeField(_("added"), default=datetime.datetime.now)

# the signal that will be listened to send the notice
signal = models.TextField(verbose_name=_("signal"))

objects = ObservedItemManager()

class Meta:
ordering = ["-added"]
verbose_name = _("observed item")
verbose_name_plural = _("observed items")

def send_notice(self, extra_context=None):
if extra_context is None:
extra_context = {}
Expand All @@ -418,7 +418,7 @@ def send_notice(self, extra_context=None):
def observe(observed, observer, notice_type_label, signal="post_save"):
"""
Create a new ObservedItem.

To be used by applications to register a user as an observer for some object.
"""
notice_type = NoticeType.objects.get(label=notice_type_label)
Expand Down
2 changes: 1 addition & 1 deletion notification/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django.conf.urls.defaults import *
from django.conf.urls import *

from notification.views import notices, mark_all_seen, feed_for_user, single, notice_settings

Expand Down
2 changes: 1 addition & 1 deletion notification/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.template import RequestContext

from django.contrib.auth.decorators import login_required
from django.contrib.syndication.views import feed
from django.contrib.syndication.views import Feed

from notification.models import *
from notification.decorators import basic_auth_required, simple_basic_auth_callback
Expand Down