# HG changeset patch # User nishanth # Date 1271757681 -19800 # Node ID 81cd0140a0f2ac75481775a44cb3d19a344aab4a # Parent afc41af983e5e45c9c80303aab70bd1903d0c66a created the register user functionality. diff -r afc41af983e5 -r 81cd0140a0f2 event/forms.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'] + diff -r afc41af983e5 -r 81cd0140a0f2 quiz/__init__.py diff -r afc41af983e5 -r 81cd0140a0f2 quiz/forms.py --- /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'] + diff -r afc41af983e5 -r 81cd0140a0f2 quiz/models.py --- /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) diff -r afc41af983e5 -r 81cd0140a0f2 quiz/tests.py --- /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 +"""} + diff -r afc41af983e5 -r 81cd0140a0f2 quiz/utils.py --- /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)]) diff -r afc41af983e5 -r 81cd0140a0f2 quiz/views.py --- /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) diff -r afc41af983e5 -r 81cd0140a0f2 settings.py --- 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' diff -r afc41af983e5 -r 81cd0140a0f2 templates/register.html --- /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.as_p }} + +
+{% endblock %} diff -r afc41af983e5 -r 81cd0140a0f2 urls.py --- 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), )