author | nishanth |
Mon, 01 Feb 2010 11:10:29 +0530 | |
changeset 14 | f2623fb8041a |
parent 13 | 4da58abdf6ff |
child 16 | d57e63325759 |
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 |
|
14
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
9 |
def show_msg(message, redirect_url=None, url_desc=None): |
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 |
|
14
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
12 |
return render_to_response('show_msg.html',{'message':message, 'redirect_url':redirect_url, 'url_desc':url_desc}) |
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] |
14
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
31 |
|
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
32 |
return render_to_response('index.html', {'is_guest':is_guest, 'task_list':task_list}) |
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
33 |
|
5
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
34 |
else: |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
35 |
user_profile = user.get_profile() |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
36 |
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
|
37 |
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
|
38 |
|
14
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
39 |
context = {'user':user, |
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
40 |
'is_guest':is_guest, |
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
41 |
'is_mentor':is_mentor, |
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
42 |
'task_list':task_list, |
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
43 |
'can_create_task':can_create_task, |
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
44 |
} |
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
45 |
|
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
46 |
return render_to_response('index.html', context) |
6 | 47 |
|
5
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
48 |
|
6 | 49 |
def register(request): |
50 |
""" user registration: gets the user details and create the user and userprofile if data entered is valid""" |
|
51 |
if request.method == 'POST': |
|
52 |
form = RegistrationForm(request.POST) |
|
53 |
if form.is_valid(): |
|
54 |
data = form.cleaned_data |
|
55 |
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
|
56 |
if data['username'].isalnum(): |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
57 |
try: |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
58 |
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
|
59 |
errors=['Choose some other username'] |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
60 |
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
|
61 |
except: |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
62 |
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
|
63 |
return redirect('/accounts/login/') |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
64 |
else: |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
65 |
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
|
66 |
return render_to_response('user/register.html',{'form':form,'errors':errors}) |
6 | 67 |
else: |
68 |
errors=['Password do not match'] |
|
69 |
form = RegistrationForm(request.POST) |
|
70 |
return render_to_response('user/register.html',{'form':form,'errors':errors})#HttpResponse('Password did not match') |
|
71 |
else: |
|
8 | 72 |
form = RegistrationForm(request.POST) |
6 | 73 |
else: |
74 |
form = RegistrationForm() |
|
75 |
return render_to_response('user/register.html', {'form': form}) |
|
76 |
||
77 |
def user_login(request): |
|
78 |
if request.method == 'POST': |
|
79 |
username = request.POST['username'] |
|
80 |
password = request.POST['password'] |
|
81 |
user = authenticate(username=username, password=password) |
|
82 |
if user is not None: |
|
83 |
if user.is_active: |
|
84 |
login(request, user) |
|
85 |
return redirect('/')# Redirect to a success page. |
|
86 |
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
|
87 |
return show_msg('username is not active, please contact the administrator')# Return a 'disabled account' error message |
6 | 88 |
else: |
89 |
errors = ['Please check your username and password'] |
|
90 |
form = LoginForm() |
|
91 |
return render_to_response('user/login.html',{'form':form,'errors':errors})# Return an 'invalid login' error message. |
|
92 |
return redirect('/') |
|
93 |
else: |
|
94 |
form = LoginForm() |
|
95 |
return render_to_response('user/login.html',{'form': form}) |
|
96 |
||
97 |
def user_logout(request): |
|
98 |
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
|
99 |
return show_msg('You have logged off successfully!!!') |