views that do *not* require authentication need to inherit FreeAccessView
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Fri, 1 Nov 2013 14:40:37 +0000 (15:40 +0100)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Fri, 1 Nov 2013 14:40:37 +0000 (15:40 +0100)
portal/contactview.py
portal/homeview.py
portal/platformsview.py
portal/platformview.py
portal/registrationview.py
portal/resourceview.py
portal/views.py
unfold/loginrequired.py

index c07f397..796c8b0 100644 (file)
@@ -3,6 +3,7 @@ from django.template.loader     import render_to_string
 from django.views.generic       import View
 from django.core.mail           import send_mail
 
+from unfold.loginrequired       import FreeAccessView
 from ui.topmenu                 import topmenu_items, the_user
 
 from portal.forms               import ContactForm
@@ -10,7 +11,7 @@ from portal.forms               import ContactForm
 # splitting the 2 functions done here
 # GET is for displaying the empty form
 # POST is to process it once filled - or show the form again if anything is missing
-class ContactView (View):
+class ContactView (FreeAccessView):
     def post (self, request):
         form = ContactForm(request.POST) # A form bound to the POST data
         if form.is_valid(): # All validation rules pass
index 960c8f0..b6af064 100644 (file)
@@ -1,16 +1,17 @@
 # this somehow is not used anymore - should it not be ?
-from django.views.generic import View
 from django.core.context_processors import csrf
 from django.http import HttpResponseRedirect
 from django.contrib.auth import authenticate, login, logout
 from django.template import RequestContext
 from django.shortcuts import render_to_response
 
+from unfold.loginrequired import FreeAccessView
+
 from manifold.manifoldresult import ManifoldResult
 from ui.topmenu import topmenu_items, the_user
 from myslice.config import Config
 
-class HomeView (View):
+class HomeView (FreeAccessView):
 
     # expose this so we can mention the backend URL on the welcome page
     def default_env (self):
index 0a1e9ad..0a6677e 100644 (file)
@@ -1,14 +1,13 @@
-from django.views.generic.base   import TemplateView
-
 from manifold.core.query         import Query
 from unfold.page                 import Page
 
+from unfold.loginrequired       import FreeAccessView
 from ui.topmenu                  import topmenu_items, the_user
 
 from plugins.hazelnut            import Hazelnut
 
 # View for platforms
-class PlatformsView(TemplateView):
+class PlatformsView(FreeAccessView):
     template_name = "platforms.html"
 
     def get_context_data(self, **kwargs):
index 4d133e1..69fd1b0 100644 (file)
@@ -1,14 +1,13 @@
-from django.views.generic.base   import TemplateView
-
 from manifold.core.query         import Query
 from unfold.page                 import Page
 
+from unfold.loginrequired        import FreeAccessView
 from ui.topmenu                  import topmenu_items, the_user
 
 from plugins.hazelnut            import Hazelnut
 
 # View for 1 platform and its details
-class PlatformView(TemplateView):
+class PlatformView(FreeAccessView):
     template_name = "platform.html"
 
     def get_context_data(self, **kwargs):
index 9ecba0d..e27dfb4 100644 (file)
@@ -8,6 +8,7 @@ from django.template.loader     import render_to_string
 from django.shortcuts           import render
 
 from unfold.page                import Page
+from unfold.loginrequired       import FreeAccessView
 from ui.topmenu                 import topmenu_items
 
 from manifold.manifoldapi       import execute_admin_query
@@ -16,17 +17,18 @@ from manifold.core.query        import Query
 from portal.models              import PendingUser
 from portal.actions             import authority_get_pi_emails 
 
-# This is a rough porting from views.py
-# the former function-based view is now made a class
-# we redefine dispatch as it is simple
-# and coincidentally works since we do not need LoginRequiredAutoLogoutView
-# a second stab should redefine post and get instead
-# also this was not thoroughly tested either, might miss some imports
-# to be continued...
+# since we inherit from FreeAccessView we cannot redefine 'dispatch'
+# so let's override 'get' and 'post' instead
+#
+class RegistrationView (FreeAccessView):
 
-class RegistrationView (View):
+    def post (self, request):
+        return self.get_or_post (request, 'POST')
 
-    def dispatch (self, request):
+    def get (self, request):
+        return self.get_or_post (request, 'GET')
+
+    def get_or_post  (self, request, method):
         errors = []
 
         #authorities_query = Query.get('authority').\
@@ -49,9 +51,9 @@ class RegistrationView (View):
         page.add_js_files  ( [ "js/jquery.validate.js", "js/my_account.register.js" ] )
         page.add_css_files ( [ "css/onelab.css", "css/registration.css" ] )
 
-        print 'registration view, method',request.method
+        print 'registration view, method',method
 
-        if request.method == 'POST':
+        if method == 'POST':
             # We shall use a form here
 
             #get_email = PendingUser.objects.get(email)
index 0ba8a26..c9fa5ba 100644 (file)
@@ -1,8 +1,7 @@
-from django.views.generic.base   import TemplateView
-
 from manifold.core.query         import Query
 from unfold.page                 import Page
 
+from unfold.loginrequired        import FreeAccessView
 from ui.topmenu                  import topmenu_items, the_user
 
 from plugins.googlemap           import GoogleMap
@@ -11,7 +10,7 @@ from plugins.lists.simplelist    import SimpleList
 from plugins.slicestat           import SliceStat
 
 # View for 1 platform and its details
-class ResourceView(TemplateView):
+class ResourceView(FreeAccessView):
     template_name = "resource.html"
 
     def get_context_data(self, **kwargs):
index deb4fad..167f324 100644 (file)
 import json
 
 from django.http                import HttpResponseRedirect, HttpResponse
-from django.views.generic.base  import TemplateView
 from django.shortcuts           import render
 from django.template.loader     import render_to_string
 
+from unfold.loginrequired       import FreeAccessView
 from ui.topmenu                 import topmenu_items, the_user
 
 from portal.event               import Event
@@ -49,7 +49,7 @@ from unfold.page                import Page
 # all the other ones have now migrated into separate classes/files for more convenience
 # I'm leaving these ones here for now as I could not exactly figure what the purpose was 
 # (i.e. what the correct name should be, as presviewview was a bit cryptic)
-class PresViewView(TemplateView):
+class PresViewView(FreeAccessView):
     template_name = "view-unfold1.html"
 
     def get_context_data(self, **kwargs):
@@ -222,7 +222,7 @@ def pres_view_static(request, constraints, id):
     json_answer = json.dumps(cmd)
     return HttpResponse (json_answer, mimetype="application/json")
 
-class ValidatePendingView(TemplateView):
+class ValidatePendingView(FreeAccessView):
     template_name = "validate_pending.html"
 
     def get_context_data(self, **kwargs):
index ebe33d5..92f9ddb 100644 (file)
@@ -6,6 +6,13 @@ from django.views.generic.base          import TemplateView
 
 from manifold.manifoldresult            import ManifoldException
 
+###
+# IMPORTANT NOTE
+# the implementation of the classes in this file rely on redefining 'dispatch'
+# for this reason if you inherit any of these, please do not redefine 'dispatch' yourself,
+# but rather redefine 'get' and 'post' instead
+###
+
 ########## the base class for views that require a login
 class LoginRequiredView (TemplateView):
 
@@ -26,7 +33,7 @@ class LoginRequiredView (TemplateView):
 
 def logout_on_manifold_exception (fun_that_returns_httpresponse):
     def wrapped (request, *args, **kwds):
-        print 'wrapped by logout_on_manifold_exception'
+#        print 'wrapped by logout_on_manifold_exception'
         try:
             return fun_that_returns_httpresponse(request,*args, **kwds)
         except ManifoldException, manifold_result:
@@ -53,3 +60,11 @@ class LoginRequiredAutoLogoutView (TemplateView):
     @method_decorator(login_required)
     def dispatch (self, request, *args, **kwds):
         return super(LoginRequiredAutoLogoutView,self).dispatch(request,*args,**kwds)
+
+# we have more and more views that actually send manifold queries
+# so for these we need to protect against manifold exceptions
+# even though login is not required
+class FreeAccessView (TemplateView):
+
+    def dispatch (self, request, *args, **kwds):
+        return super(FreeAccessView,self).dispatch(request,*args,**kwds)