Skip to content

Commit

Permalink
Don't require explicit backend import (Closes: #41)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Lamb <chris@chris-lamb.co.uk>
  • Loading branch information
lamby committed Apr 6, 2016
1 parent 6174e6b commit d7e6e47
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
29 changes: 12 additions & 17 deletions django_slack/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,17 @@ class DisabledBackend(Backend):
def send(self, url, data):
pass

try:
from celery import shared_task
except ImportError:
pass
else:
@shared_task
def celery_send(*args, **kwargs):
import_string(app_settings.BACKEND_FOR_QUEUE)().send(*args, **kwargs)

class CeleryBackend(Backend):
def __init__(self):
# Check we can import our specified backend up-front
import_string(app_settings.BACKEND_FOR_QUEUE)()

def send(self, *args, **kwargs):
# Send asynchronously via Celery
celery_send.delay(*args, **kwargs)
class CeleryBackend(Backend):
def __init__(self):
# Lazily import to avoid dependency
from .tasks import send
self.send = send

# Check we can import our specified backend up-front
import_string(app_settings.BACKEND_FOR_QUEUE)()

def send(self, *args, **kwargs):
# Send asynchronously via Celery
self.send.delay(*args, **kwargs)

Urllib2Backend = UrllibBackend # For backwards-compatibility
9 changes: 9 additions & 0 deletions django_slack/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from celery import task

from django.utils.module_loading import import_string

from .app_settings import app_settings

@task
def send(*args, **kwargs):
import_string(app_settings.BACKEND_FOR_QUEUE)().send(*args, **kwargs)

0 comments on commit d7e6e47

Please sign in to comment.