From aeb50765bdcb5e94bcd07d22f2cfa8f692e13911 Mon Sep 17 00:00:00 2001 From: Nishanth Amuluru Date: Thu, 10 Nov 2011 19:05:10 +0530 Subject: ENH: created user registration and login --- exam/views.py | 61 +++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 21 deletions(-) (limited to 'exam/views.py') diff --git a/exam/views.py b/exam/views.py index 05fe792..d8a0a45 100644 --- a/exam/views.py +++ b/exam/views.py @@ -9,7 +9,7 @@ from django.contrib.auth import login, logout, authenticate from django.shortcuts import render_to_response, get_object_or_404, redirect from django.template import RequestContext from exam.models import Question, Quiz, Profile, Answer -from exam.forms import UserRegisterForm +from exam.forms import UserRegisterForm, UserLoginForm def gen_key(no_of_chars): """Generate a random key of the number of characters.""" @@ -20,6 +20,16 @@ def index(request): """The start page. """ # Largely copied from Nishanth's quiz app. + user = request.user + if user.is_authenticated(): + return redirect("/exam/start/") + + return redirect("/exam/login/") + +def user_register(request): + """ Register a new user. + Create a user and corresponding profile and store roll_number also.""" + user = request.user if user.is_authenticated(): return redirect("/exam/start/") @@ -28,34 +38,43 @@ def index(request): form = UserRegisterForm(request.POST) if form.is_valid(): data = form.cleaned_data + new_user = form.save() - while True: - try: - username = gen_key(20) - new_user = User.objects.create_user(username, "temp@temp.com", "123") - break - except IntegrityError: - pass + context = {} + context['full_name'] = new_user.first_name.title() + " " + \ + new_user.last_name.title() + return render_to_response('exam/register.html', context, + context_instance=RequestContext(request)) + + else: + return render_to_response('exam/register.html',{'form':form}, + context_instance=RequestContext(request)) + else: + form = UserRegisterForm() + return render_to_response('exam/register.html',{'form':form}, + context_instance=RequestContext(request)) - new_user.first_name = data['first_name'] - new_user.last_name = data['last_name'] - new_user.email = data['email_address'] - new_user.save() +def user_login(request): + """Take the credentials of the user and log the user in.""" - new_profile = Profile(user=new_user) - new_profile.roll_number = data['roll_number'] - new_profile.save() + user = request.user + if user.is_authenticated(): + return redirect("/exam/start/") - user = authenticate(username=username, password="123") + if request.method == "POST": + form = UserLoginForm(request.POST) + if form.is_valid(): + user = form.cleaned_data login(request, user) return redirect("/exam/start/") - else: - return render_to_response('exam/register.html',{'form':form}, - context_instance=RequestContext(request)) + context = {"form": form,} + return render_to_response('exam/login.html', context, + context_instance=RequestContext(request)) else: - form = UserRegisterForm() - return render_to_response('exam/register.html',{'form':form}, + form = UserLoginForm() + context = {"form": form} + return render_to_response('exam/login.html', context, context_instance=RequestContext(request)) def show_question(request, q_id): -- cgit From 5b2d3d506e277d57bae9a8b90dd348225347d486 Mon Sep 17 00:00:00 2001 From: Nishanth Amuluru Date: Thu, 10 Nov 2011 20:25:03 +0530 Subject: Now the users cannot retake the test once they logout. --- exam/views.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'exam/views.py') diff --git a/exam/views.py b/exam/views.py index d8a0a45..43ac388 100644 --- a/exam/views.py +++ b/exam/views.py @@ -88,6 +88,8 @@ def start(request): user = request.user try: old_quiz = Quiz.objects.get(user=user) + if not old_quiz.is_active: + return redirect("/exam/complete/") q = old_quiz.current_question() return redirect('/exam/%s'%q) except Quiz.DoesNotExist: @@ -186,10 +188,14 @@ def quit(request): context_instance=RequestContext(request)) def complete(request): + user = request.user yes = True if request.method == 'POST': yes = request.POST.get('yes', None) if yes: + quiz = Quiz.objects.get(user=user) + quiz.is_active = False + quiz.save() logout(request) return render_to_response('exam/complete.html') else: -- cgit From f569c1f1cea65f744f42941b5d7e75b49350d442 Mon Sep 17 00:00:00 2001 From: Nishanth Amuluru Date: Thu, 10 Nov 2011 22:10:26 +0530 Subject: Users will be directed to the exam automatically after they register --- exam/views.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'exam/views.py') diff --git a/exam/views.py b/exam/views.py index 43ac388..7ee3c53 100644 --- a/exam/views.py +++ b/exam/views.py @@ -38,13 +38,11 @@ def user_register(request): form = UserRegisterForm(request.POST) if form.is_valid(): data = form.cleaned_data - new_user = form.save() + u_name, pwd = form.save() - context = {} - context['full_name'] = new_user.first_name.title() + " " + \ - new_user.last_name.title() - return render_to_response('exam/register.html', context, - context_instance=RequestContext(request)) + new_user = authenticate(username = u_name, password = pwd) + login(request, new_user) + return redirect("/exam/start/") else: return render_to_response('exam/register.html',{'form':form}, -- cgit