Permalink
Browse files

allow user to define a callback for handling contacts upon selection

  • Loading branch information...
1 parent 9e373c5 commit f16bbdffb63d62d32cbe5c273a8be4cfc9026da7 @brosner brosner committed Apr 13, 2010
Showing with 46 additions and 11 deletions.
  1. +13 −4 contacts_import/settings.py
  2. +33 −7 contacts_import/views.py
@@ -6,8 +6,14 @@
from importlib import import_module
-def map_to_class(setting, default):
- path = getattr(settings, setting, default)
+def map_to_attr(setting, default=None):
+ if default is None:
+ try:
+ path = getattr(settings, setting)
+ except AttributeError:
+ raise ImproperlyConfigured("You must define '%s' in settings" % setting)
+ else:
+ path = getattr(settings, setting, default)
i = path.rfind(".")
module, attr = path[:i], path[i+1:]
try:
@@ -21,11 +27,14 @@ def map_to_class(setting, default):
return attr
-DEFAULT_PERSISTANCE = map_to_class(
+DEFAULT_PERSISTANCE = map_to_attr(
"CONTACTS_IMPORT_DEFAULT_PERSISTANCE",
"contacts_import.backends.persistance.ModelPersistance"
)
-RUNNER = map_to_class(
+RUNNER = map_to_attr(
"CONTACTS_IMPORT_RUNNER",
"contacts_import.backends.runners.SynchronousRunner"
+)
+CALLBACK = map_to_attr(
+ "CONTACTS_IMPORT_CALLBACK"
)
View
@@ -12,7 +12,8 @@
from contacts_import.forms import VcardImportForm
from contacts_import.backends.importers import GoogleImporter, YahooImporter
-from contacts_import.settings import RUNNER
+from contacts_import.models import Contact
+from contacts_import.settings import RUNNER, CALLBACK
GOOGLE_CONTACTS_URI = "http://www.google.com/m8/feeds/"
@@ -42,18 +43,46 @@ def _import_success(request, results):
@login_required
def import_contacts(request, template_name="contacts_import/import_contacts.html"):
runner_class = RUNNER
+ callback = CALLBACK
+
+ contacts = request.user.contacts.all()
+ try:
+ page_num = int(request.GET.get("page", 1))
+ except ValueError:
+ page_num = 1
+ page = Paginator(contacts, 50).page(page_num)
if request.method == "POST":
- if request.POST["action"] == "upload_vcard":
+ action = request.POST["action"]
+
+ if action == "upload_vcard":
form = VcardImportForm(request.POST)
if form.is_valid():
results = form.save(request.user, runner_class=runner_class)
return _import_success(request, results)
+ elif action == "import-contacts":
+ selected_post = set(request.POST.getlist("selected-contacts"))
+ selected_session = request.session.get("selected-contacts", set())
+ on_page = set([str(o.pk) for o in page.object_list])
+ selected = (
+ (selected_session - (on_page - selected_post)).union(selected_post)
+ )
+ request.session["selected-contacts"] = selected
+
+ if "next" in request.POST:
+ return HttpResponseRedirect("%s?page=%s" % (request.path, page_num+1))
+ elif "prev" in request.POST:
+ return HttpResponseRedirect("%s?page=%s" % (request.path, page_num-1))
+ elif "finish" in request.POST:
+ if not selected:
+ Contact.objects.filter(user=request.user).delete()
+ return HttpResponseRedirect(reverse("import_contacts"))
+ return callback(request, selected)
else:
form = VcardImportForm()
- if request.POST["action"] == "import_yahoo":
+ if action == "import_yahoo":
bbauth_token = request.session.pop("bbauth_token", None)
if bbauth_token:
runner = runner_class(YahooImporter,
@@ -63,7 +92,7 @@ def import_contacts(request, template_name="contacts_import/import_contacts.html
results = runner.import_contacts()
return _import_success(request, results)
- elif request.POST["action"] == "import_google":
+ elif action == "import_google":
authsub_token = request.session.pop("authsub_token", None)
if authsub_token:
runner = runner_class(GoogleImporter,
@@ -75,9 +104,6 @@ def import_contacts(request, template_name="contacts_import/import_contacts.html
else:
form = VcardImportForm()
- contacts = request.user.contacts.all()
- page = Paginator(contacts, 50).page(request.GET.get("page", 1))
-
ctx = {
"form": form,
"bbauth_token": request.session.get("bbauth_token"),

0 comments on commit f16bbdf

Please sign in to comment.