From 6910f1aa0c3f412050ffc7c84991395ae69a6c8c Mon Sep 17 00:00:00 2001 From: eric Date: Thu, 17 Oct 2013 12:44:47 -0400 Subject: [PATCH] cleanup of supporter view --- frontend/templates/supporter.html | 12 ++++----- frontend/urls.py | 2 +- frontend/views.py | 41 +++++++++++-------------------- libraryauth/__init__.py | 37 ---------------------------- libraryauth/views.py | 7 +++++- 5 files changed, 28 insertions(+), 71 deletions(-) diff --git a/frontend/templates/supporter.html b/frontend/templates/supporter.html index c6b32f33..383b2b29 100644 --- a/frontend/templates/supporter.html +++ b/frontend/templates/supporter.html @@ -72,8 +72,8 @@ function highlightTarget(targetdiv) { {% block topsection %}
{{ activetab }}
-{% if library %} -
{{ supporter.username }} is a Library participating in Unglue.it. Click here to use {{ supporter.username }}'s books. +{% if supporter.library %} +
{{ supporter.library }} is a Library participating in Unglue.it. Click here to use {{ supporter.library }}'s books.
{% endif %} @@ -195,9 +195,9 @@ function highlightTarget(targetdiv) {
{% if user.profile.goodreads_user_id %} - Update your GoodReads connection
or disconnect GoodReads: {{ profile_form.clear_goodreads }} + Update your GoodReads connection
or disconnect GoodReads: {{ profile_form.clear_goodreads }} {% else %} - Connect your GoodReads account to Unglue.it + Connect your GoodReads account to Unglue.it {% endif %}
@@ -208,7 +208,7 @@ function highlightTarget(targetdiv) {

Import your books

- {% if goodreads_id %} + {% if request.user.profile.goodreads_user_id %}
{% csrf_token %}
@@ -220,7 +220,7 @@ function highlightTarget(targetdiv) { {% else %}
Connect your GoodReads account to import from GoodReads.
{% endif %} - {% if librarything_id %} + {% if request.user.profile.librarything_id %} {% csrf_token %}
diff --git a/frontend/urls.py b/frontend/urls.py index 6e8ac641..45cab66d 100644 --- a/frontend/urls.py +++ b/frontend/urls.py @@ -48,7 +48,7 @@ urlpatterns = patterns( url(r"^next/$", "next", name="next"), url(r"^supporter/(?P[^/]+)/$", "supporter", {'template_name': 'supporter.html'}, name="supporter"), url(r"^supporter/(?P[^/]+)/marc/$", "marc", name="user_marc"), - url(r"^library/(?P[^/]+)/$", "supporter", {'template_name': 'libraryauth/library.html'}, name="library"), + url(r"^library/(?P[^/]+)/$", "library", name="library"), url(r"^accounts/manage/$", login_required(ManageAccount.as_view()), name="manage_account"), url(r"^search/$", "search", name="search"), url(r"^privacy/$", TemplateView.as_view(template_name="privacy.html"), diff --git a/frontend/views.py b/frontend/views.py index 7fe4789d..30ec70e7 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -1756,7 +1756,7 @@ def campaign_admin(request): return render(request, "campaign_admin.html", context) -def supporter(request, supporter_username, template_name): +def supporter(request, supporter_username, template_name, extra_context={}): supporter = get_object_or_404(User, username=supporter_username) wishlist = supporter.wishlist works = [] @@ -1836,29 +1836,10 @@ def supporter(request, supporter_username, template_name): else: profile_form= ProfileForm(instance=profile_obj) - if request.user.profile.goodreads_user_id is not None: - goodreads_id = request.user.profile.goodreads_user_id - else: - goodreads_id = None - - if request.user.profile.librarything_id is not None: - librarything_id = request.user.profile.librarything_id - else: - librarything_id = None else: profile_form = '' - goodreads_id = None - librarything_id = None process_kindle_email(request) - try: - # determine if the supporter is a library - authenticator = Authenticator(request,supporter.library) - library = supporter.library - except Library.DoesNotExist: - authenticator=None - library=None - context = { "supporter": supporter, "wishlist": wishlist, @@ -1872,16 +1853,24 @@ def supporter(request, supporter_username, template_name): "wished": wished, "profile_form": profile_form, "ungluers": userlists.other_users(supporter, 5 ), - "goodreads_auth_url": reverse('goodreads_auth'), - "goodreads_id": goodreads_id, - "librarything_id": librarything_id, "activetab": activetab, - "library":library, - "authenticator": authenticator } - + context.update(extra_context) return render(request, template_name, context) +def library(request,library): + context={} + try: + # determine if the supporter is a library + authenticator = Authenticator(request,library) + context['authenticator'] = authenticator + context['library'] = authenticator.library + except Library.DoesNotExist: + raise Http404 + return supporter(request,library,template_name='libraryauth/library.html', extra_context=context) + + + def edit_user(request): if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('superlogin')) diff --git a/libraryauth/__init__.py b/libraryauth/__init__.py index 83874b04..e69de29b 100644 --- a/libraryauth/__init__.py +++ b/libraryauth/__init__.py @@ -1,37 +0,0 @@ -'''import logging -from django.conf import settings -from django.http import HttpResponseRedirect -from .views import superlogin - -from . import backends - -logger = logging.getLogger(__name__) - -class Authenticator: - request=None - library=None - - def __init__(self, request, library): - self.request=request - self.library=library - - def process(self, success_url, deny_url): - logger.info('authenticator for %s at %s.'%(self.request.user, self.library)) - if self.library.has_user(self.request.user): - return HttpResponseRedirect(success_url) - backend_test= getattr(backends, self.library.backend + '_authenticate') - if backend_test(self.request, self.library): - if self.request.user.is_authenticated(): - self.library.add_user(self.request.user) - return HttpResponseRedirect(success_url) - else: - return superlogin(self.request, extra_context={'library':self.library}, template_name='libraryauth/library_login.html') - - else: - backend_authenticator= getattr(backends, self.library.backend + '_authenticator') - return backend_authenticator(self.request, self.library, success_url, deny_url) - - def allowed(self): - backend_test= getattr(backends, self.library.backend + '_authenticate') - return backend_test(self.request, self.library) -''' \ No newline at end of file diff --git a/libraryauth/views.py b/libraryauth/views.py index ff59eab1..45fd3cd3 100644 --- a/libraryauth/views.py +++ b/libraryauth/views.py @@ -36,7 +36,12 @@ class Authenticator: def __init__(self, request, library, *args, **kwargs): self.request = request - self.library = library + if isinstance(library , basestring): + self.library = Library.objects.get(user__username=library) + elif isinstance(library , Library): + self.library=library + else: + raise Exception try: form_class = getattr(backends, self.library.backend + '_form') self.form = form_class(request, library, *args, **kwargs)