Commits

tejinderss committed 9be294f

Adding support for ajax in registration view

Comments (0)

Files changed (1)

registration/views.py

 
 """
 
+from django.http import HttpResponse, HttpResponseBadRequest
 from django.shortcuts import redirect
+from django.utils import simplejson as json
 from django.views.generic.base import TemplateView
 from django.views.generic.edit import FormView
 
     A version of FormView which passes extra arguments to certain
     methods, notably passing the HTTP request nearly everywhere, to
     enable finer-grained processing.
-    
+
     """
     def get(self, request, *args, **kwargs):
         # Pass request to get_form_class and get_form for per-request
 class RegistrationView(_RequestPassingFormView):
     """
     Base class for user registration views.
-    
+
     """
     disallowed_url = 'registration_disallowed'
     form_class = RegistrationForm
         """
         Check that user signup is allowed before even bothering to
         dispatch or do other processing.
-        
+
         """
         if not self.registration_allowed(request):
             return redirect(self.disallowed_url)
     def form_valid(self, request, form):
         new_user = self.register(request, **form.cleaned_data)
         success_url = self.get_success_url(request, new_user)
-        
+
         # success_url may be a simple string, or a tuple providing the
         # full argument set for redirect(). Attempting to unpack it
         # tells us which one it is.
+        splitted = False
+        to = success_url
         try:
             to, args, kwargs = success_url
+            splitted = True
+        except ValueError:
+            pass
+
+        if request.is_ajax():
+            context = {
+                'redirect_url': to,
+            }
+            return HttpResponse(json.dumps(context),
+                                mimetype="application/json")
+        if splitted:
             return redirect(to, *args, **kwargs)
-        except ValueError:
-            return redirect(success_url)
+        else:
+            return redirect(to)
+
+    def form_invalid(self, form):
+        """
+        We haz errors in the form. If ajax, return them as json.
+        Otherwise, proceed as normal.
+        """
+        if self.request.is_ajax():
+            return HttpResponseBadRequest(json.dumps(form.errors),
+                                          mimetype="application/json")
+        return super(RegistrationView, self).form_invalid(form)
 
     def registration_allowed(self, request):
         """
         Override this to enable/disable user registration, either
         globally or on a per-request basis.
-        
+
         """
         return True
 
         Implement user-registration logic here. Access to both the
         request and the full cleaned_data of the registration form is
         available here.
-        
+
         """
         raise NotImplementedError
-                
+
 
 class ActivationView(TemplateView):
     """
     Base class for user activation views.
-    
+
     """
     http_method_names = ['get']
     template_name = 'registration/activate.html'
     def activate(self, request, *args, **kwargs):
         """
         Implement account-activation logic here.
-        
+
         """
         raise NotImplementedError