Skip to content

Commit

Permalink
BACKWARD INCOMPATIBLE: added nullable Notice.sender and modified send…
Browse files Browse the repository at this point in the history
…_now and queue to take an optional sender

This allows notifications to record possibly request.user when sending the
notification.
  • Loading branch information
brosner committed Dec 11, 2009
1 parent 5202196 commit 57f4da8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Expand Up @@ -3,6 +3,8 @@ BI = backward incompatible change
0.2.0
-----

* BI: added nullable Notice.sender and modified send_now and queue to take
an optional sender
* BI: renamed Notice.user to Notice.recipient
* BI: renamed {{ user }} context variable in notification templates to {{ recipient }}

Expand Down
12 changes: 7 additions & 5 deletions notification/models.py
Expand Up @@ -123,7 +123,8 @@ def unseen_count_for(self, user, **kwargs):

class Notice(models.Model):

recipient = models.ForeignKey(User, verbose_name=_('recipient'))
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)
Expand Down Expand Up @@ -230,7 +231,7 @@ def get_formatted_messages(formats, label, context):
'notification/%s' % format), context_instance=context)
return format_templates

def send_now(users, label, extra_context=None, on_site=True):
def send_now(users, label, extra_context=None, on_site=True, sender=None):
"""
Creates a new notice.
Expand Down Expand Up @@ -283,6 +284,7 @@ def send_now(users, label, extra_context=None, on_site=True):
# update context with user specific translations
context = Context({
"recipient": user,
"sender": sender,
"notice": ugettext(notice_type.display),
"notices_url": notices_url,
"current_site": current_site,
Expand All @@ -302,7 +304,7 @@ def send_now(users, label, extra_context=None, on_site=True):
}, context)

notice = Notice.objects.create(recipient=user, message=messages['notice.html'],
notice_type=notice_type, on_site=on_site)
notice_type=notice_type, on_site=on_site, sender=sender)
if should_send(user, notice_type, "1") and user.email: # Email
recipients.append(user.email)
send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, recipients)
Expand Down Expand Up @@ -330,7 +332,7 @@ def send(*args, **kwargs):
else:
return send_now(*args, **kwargs)

def queue(users, label, extra_context=None, on_site=True):
def queue(users, label, extra_context=None, on_site=True, sender=None):
"""
Queue the notification in NoticeQueueBatch. This allows for large amounts
of user notifications to be deferred to a seperate process running outside
Expand All @@ -344,7 +346,7 @@ def queue(users, label, extra_context=None, on_site=True):
users = [user.pk for user in users]
notices = []
for user in users:
notices.append((user, label, extra_context, on_site))
notices.append((user, label, extra_context, on_site, sender))
NoticeQueueBatch(pickled_data=pickle.dumps(notices).encode("base64")).save()

class ObservedItemManager(models.Manager):
Expand Down

0 comments on commit 57f4da8

Please sign in to comment.