Skip to content

Commit

Permalink
Use import_string
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 Feb 20, 2016
1 parent 598d8b7 commit c124c44
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 20 deletions.
4 changes: 2 additions & 2 deletions django_slack/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

from django.conf import settings
from django.template import Engine
from django.utils.module_loading import import_string

from . import app_settings
from .utils import from_dotted_path

engine = Engine(
app_dirs=True,
builtins=['django_slack.templatetags.escapeslack'],
)

backend = from_dotted_path(app_settings.BACKEND)()
backend = import_string(app_settings.BACKEND)()

def slack_message(template, context=None, attachments=None, fail_silently=app_settings.FAIL_SILENTLY):
data = {}
Expand Down
8 changes: 5 additions & 3 deletions django_slack/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from six.moves import urllib

from django.utils.module_loading import import_string

from . import app_settings
from .utils import Backend, from_dotted_path
from .utils import Backend

class UrllibBackend(Backend):
def send(self, url, data):
Expand Down Expand Up @@ -45,12 +47,12 @@ def send(self, url, data):
else:
@shared_task
def celery_send(*args, **kwargs):
from_dotted_path(app_settings.BACKEND_FOR_QUEUE)().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
from_dotted_path(app_settings.BACKEND_FOR_QUEUE)()
import_string(app_settings.BACKEND_FOR_QUEUE)()

def send(self, *args, **kwargs):
# Send asynchronously via Celery
Expand Down
15 changes: 0 additions & 15 deletions django_slack/utils.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import json

def from_dotted_path(fullpath):
"""
Returns the specified attribute of a module, specified by a string.
``from_dotted_path('a.b.c.d')`` is roughly equivalent to::
from a.b.c import d
except that ``d`` is returned and not entered into the current namespace.
"""

module, attr = fullpath.rsplit('.', 1)

return getattr(__import__(module, {}, {}, (attr,)), attr)

class Backend(object):
def send(self, url, data):
raise NotImplementedError()
Expand Down

0 comments on commit c124c44

Please sign in to comment.