summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--exam/forms.py77
-rw-r--r--exam/models.py3
-rw-r--r--exam/urls.py2
-rw-r--r--exam/views.py65
-rw-r--r--templates/exam/login.html12
-rw-r--r--templates/exam/register.html5
6 files changed, 135 insertions, 29 deletions
diff --git a/exam/forms.py b/exam/forms.py
index 164e334..38466e8 100644
--- a/exam/forms.py
+++ b/exam/forms.py
@@ -1,15 +1,80 @@
from django import forms
from exam.models import Profile
-class UserRegisterForm(forms.ModelForm):
+from django.contrib.auth import authenticate
+from django.contrib.auth.models import User
+from string import letters, punctuation
+
+UNAME_CHARS = letters + "."
+PWD_CHARS = letters + punctuation
+
+class UserRegisterForm(forms.Form):
+
+ username = forms.CharField(max_length=30)
+ email = forms.EmailField()
+ password = forms.CharField(max_length=30, widget=forms.PasswordInput())
+ confirm_password = forms.CharField(max_length=30, widget=forms.PasswordInput())
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
roll_number = forms.CharField(max_length=30)
- email_address = forms.EmailField()
- #password = forms.CharField(max_length=30, widget=forms.PasswordInput())
- class Meta:
- model = Profile
- fields = ['first_name', 'last_name', 'roll_number']
+ def clean_username(self):
+ u_name = self.cleaned_data["username"]
+
+ if u_name.strip(UNAME_CHARS):
+ raise forms.ValidationError("Only letters and period character are \
+ allowed in username")
+
+ try:
+ User.objects.get(username__iexact = u_name)
+ raise forms.ValidationError("Username already exists")
+ except User.DoesNotExist:
+ return u_name
+
+ def clean_password(self):
+ pwd = self.cleaned_data['password']
+ if pwd.strip(PWD_CHARS):
+ raise forms.ValidationError("Only letters and punctuation are \
+ allowed in password")
+ return pwd
+
+ def clean_confirm_password(self):
+ c_pwd = self.cleaned_data['confirm_password']
+ pwd = self.data['password']
+ if c_pwd != pwd:
+ raise forms.ValidationError("Passwords do not match")
+
+ return c_pwd
+
+ def save(self):
+ u_name = self.cleaned_data["username"]
+ u_name = u_name.lower()
+ pwd = self.cleaned_data["password"]
+ email = self.cleaned_data['email']
+ new_user = User.objects.create_user(u_name, email, pwd)
+
+ new_user.first_name = self.cleaned_data["first_name"]
+ new_user.last_name = self.cleaned_data["last_name"]
+ new_user.save()
+
+ new_profile = Profile(user=new_user)
+ new_profile.roll_number = self.cleaned_data["roll_number"]
+ new_profile.save()
+
+ return u_name, pwd
+
+class UserLoginForm(forms.Form):
+ username = forms.CharField(max_length = 30)
+ password = forms.CharField(max_length=30, widget=forms.PasswordInput())
+
+ def clean(self):
+ super(UserLoginForm, self).clean()
+ u_name, pwd = self.cleaned_data["username"], self.cleaned_data["password"]
+ user = authenticate(username = u_name, password = pwd)
+
+ if not user:
+ raise forms.ValidationError("Invalid username/password")
+
+ return user
diff --git a/exam/models.py b/exam/models.py
index 226146a..61de093 100644
--- a/exam/models.py
+++ b/exam/models.py
@@ -51,6 +51,9 @@ class Quiz(models.Model):
user_ip = models.CharField(max_length=15)
# Unused currently.
key = models.CharField(max_length=10)
+
+ # used to allow/stop a user from retaking the quiz
+ is_active = models.BooleanField(default = True)
# The questions (a list of ids separated by '|')
questions = models.CharField(max_length=128)
diff --git a/exam/urls.py b/exam/urls.py
index 1ca9755..e517adb 100644
--- a/exam/urls.py
+++ b/exam/urls.py
@@ -2,6 +2,8 @@ from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('exam.views',
url(r'^$', 'index'),
+ url(r'^login/$', 'user_login'),
+ url(r'^register/$', 'user_register'),
url(r'^start/$', 'start'),
url(r'^quit/$', 'quit'),
url(r'^complete/$', 'complete'),
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:
diff --git a/templates/exam/login.html b/templates/exam/login.html
new file mode 100644
index 0000000..c8e27e4
--- /dev/null
+++ b/templates/exam/login.html
@@ -0,0 +1,12 @@
+<p> Welcome to the Examination.
+Please login to proceed.</p>
+
+<form action="" method="post">
+{% csrf_token %}
+
+{{ form.as_p }}
+
+<input type="submit" value="Login" />
+</form>
+<!-- <a href="/exam/forgotpassword/">Forgot Password</a> <br /> -->
+<a href="/exam/register/">New User Registration</a>
diff --git a/templates/exam/register.html b/templates/exam/register.html
index 414da72..2ffc140 100644
--- a/templates/exam/register.html
+++ b/templates/exam/register.html
@@ -1,7 +1,8 @@
-Please provide the following details before you start the test.
+Please provide the following details.
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
-<input type="submit" value="submit">
+
+<input type="submit" value="Register" />
</form>