author | nishanth |
Sat, 30 Jan 2010 13:04:58 +0530 | |
changeset 12 | c823b42970a4 |
parent 11 | d28fcc644fbb |
child 13 | 4da58abdf6ff |
permissions | -rw-r--r-- |
5
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
1 |
from django.http import HttpResponse |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
2 |
from django.shortcuts import redirect, render_to_response |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
3 |
from pytask.taskapp.models import Task |
6 | 4 |
from pytask.taskapp.forms.user import RegistrationForm, LoginForm |
5 |
from pytask.taskapp.events.user import createUser |
|
6 |
from django.contrib.auth import login, logout, authenticate |
|
7 |
from django.contrib.auth.models import User |
|
2 | 8 |
|
11
d28fcc644fbb
implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents:
10
diff
changeset
|
9 |
def show_msg(error_msg): |
5
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
10 |
""" simply redirect to homepage """ |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
11 |
|
11
d28fcc644fbb
implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents:
10
diff
changeset
|
12 |
return render_to_response('error.html',{'error_msg':error_msg}) |
5
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
13 |
|
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
14 |
def homepage(request): |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
15 |
""" check for authentication and display accordingly. """ |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
16 |
|
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
17 |
user = request.user |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
18 |
is_guest = False |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
19 |
is_mentor = False |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
20 |
can_create_task = False |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
21 |
task_list = [] |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
22 |
|
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
23 |
if not user.is_authenticated(): |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
24 |
is_guest = True |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
25 |
disp_num = 10 |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
26 |
tasks_count = Task.objects.count() |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
27 |
if tasks_count <= disp_num: |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
28 |
task_list = Task.objects.order_by('id').reverse() |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
29 |
else: |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
30 |
task_list = Task.objects.order_by('id').reverse()[:10] |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
31 |
else: |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
32 |
user_profile = user.get_profile() |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
33 |
is_mentor = True if user.task_mentors.all() else False |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
34 |
can_create_task = False if user_profile.rights == u"CT" else True |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
35 |
|
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
36 |
context = {'user':user, |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
37 |
'is_guest':is_guest, |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
38 |
'is_mentor':is_mentor, |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
39 |
'task_list':task_list, |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
40 |
'can_create_task':can_create_task, |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
41 |
} |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
42 |
|
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
43 |
return render_to_response('index.html', context) |
6 | 44 |
|
5
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
45 |
|
6 | 46 |
def register(request): |
47 |
""" user registration: gets the user details and create the user and userprofile if data entered is valid""" |
|
48 |
if request.method == 'POST': |
|
49 |
form = RegistrationForm(request.POST) |
|
50 |
if form.is_valid(): |
|
51 |
data = form.cleaned_data |
|
52 |
if data['password'] == data['repeat_password']: |
|
7
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
53 |
if data['username'].isalnum(): |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
54 |
try: |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
55 |
if User.objects.get(username__exact = data['username']): |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
56 |
errors=['Choose some other username'] |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
57 |
return render_to_response('user/register.html',{'form':form,'errors':errors}) |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
58 |
except: |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
59 |
u = createUser(username=data['username'], email=data['email'], password=data['password'],dob = data['dob'],gender = data['gender']) |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
60 |
return redirect('/accounts/login/') |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
61 |
else: |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
62 |
errors = ['Username can contain only alphabets and numbers!'] |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
63 |
return render_to_response('user/register.html',{'form':form,'errors':errors}) |
6 | 64 |
else: |
65 |
errors=['Password do not match'] |
|
66 |
form = RegistrationForm(request.POST) |
|
67 |
return render_to_response('user/register.html',{'form':form,'errors':errors})#HttpResponse('Password did not match') |
|
68 |
else: |
|
8 | 69 |
form = RegistrationForm(request.POST) |
6 | 70 |
else: |
71 |
form = RegistrationForm() |
|
72 |
return render_to_response('user/register.html', {'form': form}) |
|
73 |
||
74 |
def user_login(request): |
|
75 |
if request.method == 'POST': |
|
76 |
username = request.POST['username'] |
|
77 |
password = request.POST['password'] |
|
78 |
user = authenticate(username=username, password=password) |
|
79 |
if user is not None: |
|
80 |
if user.is_active: |
|
81 |
login(request, user) |
|
82 |
return redirect('/')# Redirect to a success page. |
|
83 |
else: |
|
11
d28fcc644fbb
implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents:
10
diff
changeset
|
84 |
return show_msg('username is not active, please contact the administrator')# Return a 'disabled account' error message |
6 | 85 |
else: |
86 |
errors = ['Please check your username and password'] |
|
87 |
form = LoginForm() |
|
88 |
return render_to_response('user/login.html',{'form':form,'errors':errors})# Return an 'invalid login' error message. |
|
89 |
return redirect('/') |
|
90 |
else: |
|
91 |
form = LoginForm() |
|
92 |
return render_to_response('user/login.html',{'form': form}) |
|
93 |
||
94 |
def user_logout(request): |
|
95 |
logout(request) |
|
11
d28fcc644fbb
implemented create task view which needed task.py in events. added a method show_msg to views/user.py and used it in logoff view.
nishanth
parents:
10
diff
changeset
|
96 |
return show_msg('You have logged off successfully!!!') |