quiz/views.py
author nishanth
Tue, 20 Apr 2010 15:44:43 +0530
changeset 14 ea7d372bfbff
parent 13 ad193c5014b2
child 15 99af908a4174
permissions -rw-r--r--
implemented more constraints on the register for test page
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     1
from django.db import IntegrityError
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     2
14
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
     3
from django.http import Http404
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
     4
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     5
from django.contrib.auth.models import User
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     6
from django.contrib.auth import login, logout, authenticate
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     7
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     8
from django.shortcuts import redirect, render_to_response
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     9
14
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    10
from offline.event.models import Event
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    11
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    12
from offline.quiz.utils import gen_key
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    13
from offline.quiz.models import Profile
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    14
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    15
from offline.quiz.forms import UserRegisterForm
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    16
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    17
def start_page(request):
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    18
    """ first see if user is authenticated.
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    19
    If he is, redirect to the page where quiz happens.
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    20
    Else register the user
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    21
    """
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    22
14
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    23
    try:
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    24
        event = Event.objects.all()[0]
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    25
    except IndexError:
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    26
        raise Http404
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    27
    
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    28
    if event.quiz_status == '00':
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    29
        raise Http404
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    30
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    31
    user = request.user
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    32
    if user.is_authenticated():
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    33
        return redirect("/quiz/start/%s"%user.username)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    34
13
ad193c5014b2 added ip field to model and used it in start page
nishanth
parents: 12
diff changeset
    35
    try:
14
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    36
        Quiz.objects.get(event=event,user_ip=ip,quiz_num=event.quiz_status)
13
ad193c5014b2 added ip field to model and used it in start page
nishanth
parents: 12
diff changeset
    37
        return redirect("/quiz/complete")
ad193c5014b2 added ip field to model and used it in start page
nishanth
parents: 12
diff changeset
    38
    except Quiz.DoesNotExist:
ad193c5014b2 added ip field to model and used it in start page
nishanth
parents: 12
diff changeset
    39
        pass
ad193c5014b2 added ip field to model and used it in start page
nishanth
parents: 12
diff changeset
    40
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    41
    if request.method == "POST":
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    42
        form = UserRegisterForm(request.POST)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    43
        if form.is_valid():
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    44
            data = form.cleaned_data
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    45
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    46
            while True:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    47
                try:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    48
                    username = gen_key(20)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    49
                    new_user = User.objects.create_user(username, "temp@temp.com", "123")
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    50
                    break
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    51
                except IntegrityError:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    52
                    pass
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    53
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    54
            new_user.first_name = data['first_name']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    55
            new_user.last_name = data['last_name']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    56
            new_user.save()
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    57
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    58
            new_profile = Profile(user=new_user)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    59
            new_profile.profession = data['profession']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    60
            new_profile.affiliated_to = data['affiliated_to']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    61
            new_profile.save()
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    62
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    63
            user = authenticate(username=username, password="123")
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    64
            login(request, user)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    65
            return redirect("/quiz/start/%s"%username)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    66
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    67
        else:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    68
            return render_to_response('register.html',{'form':form})
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    69
    else:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    70
        form = UserRegisterForm()
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    71
        return render_to_response('register.html',{'form':form})
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    72
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    73
def start_quiz(request, username):
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    74
    logout(request)