Skip to content
Browse files

removed direct celery dependency

  • Loading branch information...
1 parent a06a593 commit 32515e4b712df0068f1800857515c8b32aeb19d3 @brosner brosner committed Apr 13, 2010
View
10 contacts_import/backends/importers.py
@@ -5,7 +5,15 @@
from django.conf import settings
from django.utils import simplejson as json
-from celery.task import Task
+from contacts_import.backends.runners import AsyncRunner
+from contacts_import.settings import RUNNER
+
+
+# determine the base class based on what type of importing should be done
+if issubclass(RUNNER, AsyncRunner):
+ from celery.task import Task
+else:
+ Task = object
class BaseImporter(Task):
View
13 contacts_import/backends/runners.py
@@ -1,18 +1,11 @@
import sys
-from django.conf import settings
-
-
-DEFAULT_CONTACT_PERSISTANCE = getattr(settings, "DEFAULT_CONTACT_PERSISTANCE",
- "contacts_import.backends.persistance.ModelPersistance")
-
class BaseRunner(object):
def __init__(self, importer, persistance=None, **credentials):
+ from contacts_import.settings import DEFAULT_PERSISTANCE
if persistance is None:
- module, klass = DEFAULT_CONTACT_PERSISTANCE.rsplit(".", 1)
- __import__(module)
- persistance = getattr(sys.modules[module], klass)
+ persistance = DEFAULT_PERSISTANCE
self.importer = importer
self.persistance = persistance
self.credentials = credentials
@@ -23,7 +16,7 @@ def import_contacts(self):
class SynchronousRunner(BaseRunner):
def import_contacts(self):
- return self.importer.apply(args=[self.credentials, self.persistance()])
+ return self.importer.run(self.credentials, self.persistance())
class AsyncRunner(BaseRunner):
View
31 contacts_import/settings.py
@@ -0,0 +1,31 @@
+from django.conf import settings
+from django.core.exceptions import ImproperlyConfigured
+try:
+ from django.utils.importlib import import_module
+except ImportError:
+ from importlib import import_module
+
+
+def map_to_class(setting, default):
+ path = getattr(settings, setting, default)
+ i = path.rfind(".")
+ module, attr = path[:i], path[i+1:]
+ try:
+ mod = import_module(module)
+ except ImportError, e:
+ raise ImproperlyConfigured("Error importing %s: '%s'" % (module, e))
+ try:
+ attr = getattr(mod, attr)
+ except AttributeError:
+ raise ImproperlyConfigured("Module '%s' does not define a '%s'" % (module, attr))
+ return attr
+
+
+DEFAULT_PERSISTANCE = map_to_class(
+ "CONTACTS_IMPORT_DEFAULT_PERSISTANCE",
+ "contacts_import.backends.persistance.ModelPersistance"
+)
+RUNNER = map_to_class(
+ "CONTACTS_IMPORT_RUNNER",
+ "contacts_import.backends.runners.SynchronousRunner"
+)
View
7 contacts_import/views.py
@@ -12,9 +12,8 @@
from contacts_import.forms import VcardImportForm
from contacts_import.backends.importers import GoogleImporter, YahooImporter
-from contacts_import.backends.runners import SynchronousRunner, AsyncRunner
-from contacts_import.oauth_consumer import oAuthConsumer
# from contacts_import.oauth_consumer import oAuthConsumer
+from contacts_import.settings import RUNNER
GOOGLE_CONTACTS_URI = "http://www.google.com/m8/feeds/"
@@ -38,12 +37,12 @@ def _import_success(request, results):
@login_required
-def import_contacts(request, runner_class=AsyncRunner):
+def import_contacts(request):
if request.method == "POST":
if request.POST["action"] == "upload_vcard":
form = VcardImportForm(request.POST)
if form.is_valid():
- results = form.save(request.user, runner_class=runner_class)
+ results = form.save(request.user, runner_class=RUNNER)
return _import_success(request, results)
else:
form = VcardImportForm()

0 comments on commit 32515e4

Please sign in to comment.
Something went wrong with that request. Please try again.