Modified tasks.py to use celery only if available

Please see the comments inside tasks.py.
TL;DR: Defines a fake "task" decorator - the fake
decorator was needed to add the delay() attribute
to the send_notification function
pull/30/head
Serafeim Papastefanos 2014-02-11 16:48:53 +02:00
rodzic e7bb62d8ec
commit a2319a897a
1 zmienionych plików z 23 dodań i 4 usunięć

Wyświetl plik

@ -1,5 +1,3 @@
from celery.decorators import task
from django.template.loader import render_to_string
from django.core.mail import send_mail
from django.conf import settings
@ -8,6 +6,27 @@ from django.db.models import Q
from wagtail.wagtailcore.models import PageRevision, GroupPagePermission
# The following will check to see if we can import task from celery -
# if not then we definitely haven't installed it
try:
from celery.decorators import task
NO_CELERY = False
except:
NO_CELERY = True
# However, we could have installed celery for other projects. So we will also
# check if we have defined the BROKER_URL setting. If not then definitely we
# haven't configured it.
if NO_CELERY or not hasattr(settings, 'BROKER_URL'):
# So if we enter here we will define a different "task" decorator that
# just returns the original function and sets its delay attribute to
# point to the original function: This way, the send_notification
# function will be actually called instead of the the
# send_notification.delay()
def task(f):
f.delay=f
return f
def users_with_page_permission(page, permission_type, include_superusers=True):
# Get user model
@ -25,11 +44,11 @@ def users_with_page_permission(page, permission_type, include_superusers=True):
return User.objects.filter(q).distinct()
@task()
@task
def send_notification(page_revision_id, notification, excluded_user_id):
# Get revision
revision = PageRevision.objects.get(id=page_revision_id)
# Get list of recipients
if notification == 'submitted':
# Get list of publishers