created the register user functionality.
authornishanth
Tue, 20 Apr 2010 15:31:21 +0530
changeset 12 81cd0140a0f2
parent 11 afc41af983e5
child 13 ad193c5014b2
created the register user functionality.
event/forms.py
quiz/__init__.py
quiz/forms.py
quiz/models.py
quiz/tests.py
quiz/utils.py
quiz/views.py
settings.py
templates/register.html
urls.py
--- a/event/forms.py	Tue Apr 20 09:55:46 2010 +0530
+++ b/event/forms.py	Tue Apr 20 15:31:21 2010 +0530
@@ -20,3 +20,4 @@
             raise forms.ValidationError("Event cannot stop before it starts")
 
         return self.cleaned_data['stop_date']
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/quiz/forms.py	Tue Apr 20 15:31:21 2010 +0530
@@ -0,0 +1,13 @@
+from django import forms
+
+from offline.quiz.models import Profile
+
+class UserRegisterForm(forms.ModelForm):
+
+    first_name = forms.CharField(max_length=30)
+    last_name = forms.CharField(max_length=30)
+
+    class Meta:
+        model = Profile
+        fields = ['first_name', 'last_name', 'profession', 'affiliated_to']
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/quiz/models.py	Tue Apr 20 15:31:21 2010 +0530
@@ -0,0 +1,40 @@
+from django.db import models
+from django.contrib.auth.models import User
+
+from offline.event.models import Event
+
+class Profile(models.Model):
+
+    user = models.ForeignKey(User)
+    profession = models.CharField(max_length=20,help_text="(Ex: Faculty,Student etc.)")
+    affiliated_to = models.CharField(max_length=100, verbose_name="College/Company")
+
+class QuestionBank(models.Model):
+    """ A model for holding the database of questions.
+    """
+
+    quiz_num = models.CharField(max_length=2)
+
+    description = models.TextField()
+    type = models.CharField(max_length=1)
+    time_limit = models.PositiveSmallIntegerField()
+    expected_ans = models.TextField()
+
+class Answer(models.Model):
+    """ A model for holding answers submitted by users.
+    """
+
+    question = models.ForeignKey(Question)
+    submitted_ans = models.TextField()
+    is_correct = models.BoolenField()
+
+class Quiz(models.Model):
+    """ A model to hold the proceeding of a quiz.
+    """
+
+    user = models.ForeignKey(User)
+    event = models.ForeignKey(Event)
+    
+    quiz_num = models.CharField(max_length=2)
+    que_remaining = models.CharField(max_length=100)
+    que_answered = models.ManyToManyField(Answer)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/quiz/tests.py	Tue Apr 20 15:31:21 2010 +0530
@@ -0,0 +1,23 @@
+"""
+This file demonstrates two different styles of tests (one doctest and one
+unittest). These will both pass when you run "manage.py test".
+
+Replace these with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+class SimpleTest(TestCase):
+    def test_basic_addition(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        self.failUnlessEqual(1 + 1, 2)
+
+__test__ = {"doctest": """
+Another way to test that 1 + 1 is equal to 2.
+
+>>> 1 + 1 == 2
+True
+"""}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/quiz/utils.py	Tue Apr 20 15:31:21 2010 +0530
@@ -0,0 +1,6 @@
+import string
+import random
+
+def gen_key(no_of_chars):
+    allowed_chars = string.digits+string.uppercase
+    return ''.join([random.choice(allowed_chars) for i in range(no_of_chars)])
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/quiz/views.py	Tue Apr 20 15:31:21 2010 +0530
@@ -0,0 +1,56 @@
+from django.db import IntegrityError
+
+from django.contrib.auth.models import User
+from django.contrib.auth import login, logout, authenticate
+
+from django.shortcuts import redirect, render_to_response
+
+from offline.quiz.utils import gen_key
+from offline.quiz.models import Profile
+
+from offline.quiz.forms import UserRegisterForm
+
+def start_page(request):
+    """ first see if user is authenticated.
+    If he is, redirect to the page where quiz happens.
+    Else register the user
+    """
+
+    user = request.user
+    if user.is_authenticated():
+        return redirect("/quiz/start/%s"%user.username)
+
+    if request.method == "POST":
+        form = UserRegisterForm(request.POST)
+        if form.is_valid():
+            data = form.cleaned_data
+
+            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.save()
+
+            new_profile = Profile(user=new_user)
+            new_profile.profession = data['profession']
+            new_profile.affiliated_to = data['affiliated_to']
+            new_profile.save()
+
+            user = authenticate(username=username, password="123")
+            login(request, user)
+            return redirect("/quiz/start/%s"%username)
+
+        else:
+            return render_to_response('register.html',{'form':form})
+    else:
+        form = UserRegisterForm()
+        return render_to_response('register.html',{'form':form})
+
+def start_quiz(request, username):
+    logout(request)
--- a/settings.py	Tue Apr 20 09:55:46 2010 +0530
+++ b/settings.py	Tue Apr 20 15:31:21 2010 +0530
@@ -79,6 +79,8 @@
     'django.contrib.sites',
     'offline.event',
     'offline.feedback',
+    'offline.quiz',
 )
 
+AUTH_PROFILE_MODULE = "quiz.Profile"
 ADMIN_KEY = 'ditchax'
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/register.html	Tue Apr 20 15:31:21 2010 +0530
@@ -0,0 +1,8 @@
+{% extends "base.html" %}
+{% block content %}
+Please provide the following details before you start the test.
+<form action="" method="post">
+{{ form.as_p }}
+<input type="submit" value="submit">
+</form>
+{% endblock %}
--- a/urls.py	Tue Apr 20 09:55:46 2010 +0530
+++ b/urls.py	Tue Apr 20 15:31:21 2010 +0530
@@ -2,6 +2,7 @@
 
 from offline.feedback import views as feed_views
 from offline.event import views as event_views
+from offline.quiz import views as quiz_views
 
 # Uncomment the next two lines to enable the admin:
 # from django.contrib import admin
@@ -26,4 +27,6 @@
     (r'^feedback/close/(\w+)$', feed_views.close_feedback),
     (r'^feedback/list/(\w+)$', feed_views.list_feedbacks),
     (r'^feedback/report/(\w+)$', feed_views.view_report),
+    (r'^quiz$', quiz_views.start_page),
+    (r'^quiz/start/(\w+)$', quiz_views.start_quiz),
 )