summaryrefslogtreecommitdiff
path: root/exam/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'exam/views.py')
-rw-r--r--exam/views.py65
1 files changed, 44 insertions, 21 deletions
diff --git a/exam/views.py b/exam/views.py
index 05fe792..7ee3c53 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."""
@@ -24,32 +24,26 @@ def index(request):
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/")
+
if request.method == "POST":
form = UserRegisterForm(request.POST)
if form.is_valid():
data = form.cleaned_data
+ u_name, pwd = form.save()
- while True:
- try:
- username = gen_key(20)
- new_user = User.objects.create_user(username, "temp@temp.com", "123")
- break
- except IntegrityError:
- pass
-
- new_user.first_name = data['first_name']
- new_user.last_name = data['last_name']
- new_user.email = data['email_address']
- new_user.save()
-
- new_profile = Profile(user=new_user)
- new_profile.roll_number = data['roll_number']
- new_profile.save()
-
- user = authenticate(username=username, password="123")
- login(request, user)
+ 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},
context_instance=RequestContext(request))
@@ -58,6 +52,29 @@ def index(request):
return render_to_response('exam/register.html',{'form':form},
context_instance=RequestContext(request))
+def user_login(request):
+ """Take the credentials of the user and log the user in."""
+
+ user = request.user
+ if user.is_authenticated():
+ return redirect("/exam/start/")
+
+ if request.method == "POST":
+ form = UserLoginForm(request.POST)
+ if form.is_valid():
+ user = form.cleaned_data
+ login(request, user)
+ return redirect("/exam/start/")
+ else:
+ context = {"form": form,}
+ return render_to_response('exam/login.html', context,
+ context_instance=RequestContext(request))
+ else:
+ form = UserLoginForm()
+ context = {"form": form}
+ return render_to_response('exam/login.html', context,
+ context_instance=RequestContext(request))
+
def show_question(request, q_id):
"""Show a question if possible."""
if len(q_id) == 0:
@@ -69,6 +86,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:
@@ -167,10 +186,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: