def ws_register(request): """ Parses and responds to a webservices login request. """ from swarm.utils.wsu import return_json_response timer_start = datetime.now() context = { "action": "%s" % reverse("registration_ws_register"), "result": { "success": 0, "message": "Not authorized" } } user = request.user try: if user.is_authenticated(): # Let the system know the person is already authenticated. context['result']['success'] = 1 context['result']['message'] = "Already authenticated" else: if request.method == "POST": form = RegistrationForm(data=request.POST) if form.is_valid(): new_user = form.save() from django.contrib.auth import login, authenticate new_user = authenticate(username=new_user.username, password=form.cleaned_data['password1']) login(request, new_user) if request.session.test_cookie_worked(): request.session.delete_test_cookie() context['result']['success'] = 1 context['result']['message'] = "Registered. Logged in. Redirecting." utype = form.cleaned_data['user_type'] if utype == 'bus': redirect = "/businesses/new/" elif utype == 'ins': redirect = "/institutions/new/" elif utype == 'pro': redirect = "/professionals/new/" else: redirect = "/" context['result']['redirect'] = redirect else: # TODO: process errors into json response context['result']['message'] = "Please check for errors and try again." context['result']['errors'] = form.errors except: # Admins need to know personally if there's an error happening subject = "Authentication Error" import sys, traceback import cStringIO from django.core.mail import mail_admins exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() message = "%s\n\n%s" % (request.raw_post_data, traceback.format_exc(),) mail_admins(subject=subject,message=message) timer_end = datetime.now() context['delta'] = timer_end - timer_start return return_json_response(context)
def ws_login(request): """ Parses and responds to a webservices login request. """ from swarm.utils.wsu import return_json_response timer_start = datetime.now() context = { "action": "%s" % reverse("auth_ws_login"), "result": { "success": 0, "message": "Invalid username or password" } } user = request.user try: if user.is_authenticated(): # Let the system know the person is already authenticated. context['result']['success'] = 1 context['result']['message'] = "Already authenticated" else: if request.method == "POST": form = AuthenticationForm(data=request.POST) if form.is_valid(): from django.contrib.auth import login login(request, form.get_user()) if request.session.test_cookie_worked(): request.session.delete_test_cookie() context['result']['success'] = 1 context['result']['message'] = "Logged in. Redirecting." else: # TODO: process errors into json response print form._errors except: # Admins need to know personally if there's an error happening subject = "Authentication Error" import sys, traceback import cStringIO from django.core.mail import mail_admins exceptionType, exceptionValue, exceptionTraceback = sys.exc_info() message = "%s\n\n%s" % (request.raw_post_data, traceback.format_exc(),) mail_admins(subject=subject,message=message) timer_end = datetime.now() context['delta'] = timer_end - timer_start return return_json_response(context)