def test_get_query_string(self): request = self.factory.get('/login?next=frank.html&value=bob123') query_values = get_query_string(request) self.assertEqual(query_values['next'], 'frank.html') self.assertEqual(query_values['value'], 'bob123') request = self.factory.get('/login') query_values = get_query_string(request) self.assertRaises(KeyError, lambda: query_values['next'])
def login_view(request): query_strings = get_query_string(request) if request.POST: form = LoginForm(request.POST) if form.is_valid(): user = authenticate(username=form.cleaned_data["username"], password=form.cleaned_data["password"]) template_variables = {} template = "" if not user: template_variables = {"error": "Username/Password failed", "form": LoginForm()} template = "login.html" return render_to_response(template, template_variables, context_instance=RequestContext(request)) if not user.is_active: template_variables = {"error": "User account is not active", "form": LoginForm()} template = "login.html" else: login(request, user) request.session.set_expiry(900) ##Todo Refactor out the view logic and have both views call # the same function. Possibly rendering twice here. try: url = query_strings["next"] except KeyError: url = "/" return HttpResponseRedirect(url) else: template = "login.html" template_variables = {"error": "All fields are required", "form": form} else: template_variables = {"form": LoginForm()} template = "login.html" return render_to_response(template, template_variables, context_instance=RequestContext(request))