Commits

Esteban Feldman committed 8c11c62

Registration AJAX working... need UI improvements and decisions
Added NonProf model
Added NonProf Form, not finished

  • Participants
  • Parent commits d36a382

Comments (0)

Files changed (1)

File registration/views.py

 """
 
 
+from django.core.serializers.json import DjangoJSONEncoder
+from django.http import HttpResponse
 from django.shortcuts import redirect
 from django.shortcuts import render_to_response
 from django.template import RequestContext
+from django.utils import simplejson
 
 from registration.backends import get_backend
 
         return redirect(disallowed_url)
     if form_class is None:
         form_class = backend.get_form_class(request)
-
+    
+    is_ajax_request = request.is_ajax()
+    
     if request.method == 'POST':
         form = form_class(data=request.POST, files=request.FILES)
         if form.is_valid():
             new_user = backend.register(request, **form.cleaned_data)
-            if success_url is None:
-                to, args, kwargs = backend.post_registration_redirect(request, new_user)
-                return redirect(to, *args, **kwargs)
+            # is a request made via AJAX?
+            if not is_ajax_request:
+                if success_url is None:
+                    to, args, kwargs = backend.post_registration_redirect(request, new_user)
+                    return redirect(to, *args, **kwargs)
+                else:
+                    return redirect(success_url)
             else:
-                return redirect(success_url)
+                res = {}
+                # see to add message dynamically
+                res.update({'status': 'OK', 'redirect': success_url, 'message': 'Hare Krishna'})
+                json = simplejson.dumps(res, cls=DjangoJSONEncoder)
+                print json
+                return HttpResponse(json)
+        else:
+            if is_ajax_request:
+                # import ipdb;ipdb.set_trace()
+                res = {}
+                res.update({'status': 'ERROR'})
+                # form.errors is not a python dictionary :P 
+                # http://groups.google.com/group/django-users/browse_thread/thread/d60e8d6d591f8eed#
+                form_errors = dict([(k, [unicode(e) for e in v]) for k,v in form.errors.items()]) 
+                res.update({'form_errors': form_errors})
+                res = simplejson.dumps(res, cls=DjangoJSONEncoder)
+                return HttpResponse(res, mimetype='application/javascript')
+            
     else:
         form = form_class()