Browse files

Renamed current send to send_now and made send a wrapper around send …

…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...
1 parent c37f7a2 commit 0e4abd88b7b9c7fa29e642f1a680834fcaaa2d8e @brosner brosner committed Oct 13, 2008
Showing with 24 additions and 2 deletions.
  1. +1 −1 notification/engine.py
  2. +23 −1 notification/models.py
View
2 notification/engine.py
@@ -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
View
24 notification/models.py
@@ -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
@@ -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.
@@ -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

0 comments on commit 0e4abd8

Please sign in to comment.