quiz/views.py
author nishanth
Tue, 20 Apr 2010 15:31:21 +0530
changeset 12 81cd0140a0f2
child 13 ad193c5014b2
permissions -rw-r--r--
created the register user functionality.
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
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     3
from django.contrib.auth.models import User
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     4
from django.contrib.auth import login, logout, authenticate
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     5
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     6
from django.shortcuts import redirect, render_to_response
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     7
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     8
from offline.quiz.utils import gen_key
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     9
from offline.quiz.models import Profile
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    10
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    11
from offline.quiz.forms import UserRegisterForm
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    13
def start_page(request):
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    14
    """ first see if user is authenticated.
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    15
    If he is, redirect to the page where quiz happens.
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    16
    Else register the user
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    17
    """
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    18
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    19
    user = request.user
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    20
    if user.is_authenticated():
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    21
        return redirect("/quiz/start/%s"%user.username)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    22
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    23
    if request.method == "POST":
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    24
        form = UserRegisterForm(request.POST)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    25
        if form.is_valid():
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    26
            data = form.cleaned_data
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    27
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    28
            while True:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    29
                try:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    30
                    username = gen_key(20)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    31
                    new_user = User.objects.create_user(username, "temp@temp.com", "123")
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    32
                    break
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    33
                except IntegrityError:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    34
                    pass
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    35
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    36
            new_user.first_name = data['first_name']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    37
            new_user.last_name = data['last_name']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    38
            new_user.save()
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    39
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    40
            new_profile = Profile(user=new_user)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    41
            new_profile.profession = data['profession']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    42
            new_profile.affiliated_to = data['affiliated_to']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    43
            new_profile.save()
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    44
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    45
            user = authenticate(username=username, password="123")
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    46
            login(request, user)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    47
            return redirect("/quiz/start/%s"%username)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    48
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    49
        else:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    50
            return render_to_response('register.html',{'form':form})
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    51
    else:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    52
        form = UserRegisterForm()
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    53
        return render_to_response('register.html',{'form':form})
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    54
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    55
def start_quiz(request, username):
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    56
    logout(request)