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