Skip to content

Commit

Permalink
Renamed current send to send_now and made send a wrapper around send …
Browse files Browse the repository at this point in the history
…and queue. This allows for a global queue toggle as well as per-call overrides using `queue` and `now` kwargs.

git-svn-id: https://django-notification.googlecode.com/svn/trunk@106 590c3fc9-4838-0410-bb95-17a0c9b37ca9
  • Loading branch information
brosner committed Oct 13, 2008
1 parent c37f7a2 commit 0e4abd8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion notification/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def send_all():
logging.info("emitting notice to %s" % user)
# call this once per user to be atomic and allow for logging to
# accurately show how long each takes.
notification.send([user], label, extra_context, on_site)
notification.send_now([user], label, extra_context, on_site)
sent += 1
queued_batch.delete()
batches += 1
Expand Down
24 changes: 23 additions & 1 deletion notification/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
except ImportError:
from django.core.mail import send_mail

QUEUE_ALL = getattr(settings, "NOTIFICATION_QUEUE_ALL", False)

class LanguageStoreNotAvailable(Exception):
pass

Expand Down Expand Up @@ -225,7 +227,7 @@ def get_formatted_messages(formats, label, context):
'notification/%s' % format), context_instance=context)
return format_templates

def send(users, label, extra_context=None, on_site=True):
def send_now(users, label, extra_context=None, on_site=True):
"""
Creates a new notice.
Expand Down Expand Up @@ -302,6 +304,26 @@ def send(users, label, extra_context=None, on_site=True):
# reset environment to original language
activate(current_language)

def send(*args, **kwargs):
"""
A basic interface around both queue and send_now. This honors a global
flag NOTIFICATION_QUEUE_ALL that helps determine whether all calls should
be queued or not. A per call ``queue`` or ``now`` keyword argument can be
used to always override the default global behavior.
"""
queue_flag = kwargs.pop("queue", False)
now_flag = kwargs.pop("now", False)
assert not (queue_flag and now_flag), "'queue' and 'now' cannot both be True."
if queue_flag:
return queue(*args, **kwargs)
elif now_flag:
return send_now(*args, **kwargs)
else:
if QUEUE_ALL:
return queue(*args, **kwargs)
else:
return send_now(*args, **kwargs)

def queue(users, label, extra_context=None, on_site=True):
"""
Queue the notification in NoticeQueueBatch. This allows for large amounts
Expand Down

0 comments on commit 0e4abd8

Please sign in to comment.