author | anoop |
Mon, 01 Feb 2010 14:14:04 +0530 | |
changeset 17 | 9ca9f98af0eb |
parent 16 | d57e63325759 |
child 21 | 3e676fa948c4 |
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 |
16 | 4 |
from pytask.taskapp.forms.user import RegistrationForm, LoginForm, UserProfileForm, UserProfileEditForm |
5 |
from pytask.taskapp.events.user import createUser, updateProfile |
|
6 | 6 |
from django.contrib.auth import login, logout, authenticate |
7 |
from django.contrib.auth.models import User |
|
16 | 8 |
from pytask.taskapp.models import Profile |
2 | 9 |
|
14
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
10 |
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
|
11 |
""" simply redirect to homepage """ |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
12 |
|
14
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
13 |
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
|
14 |
|
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
15 |
def homepage(request): |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
16 |
""" check for authentication and display accordingly. """ |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
17 |
|
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
18 |
user = request.user |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
19 |
is_guest = False |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
20 |
is_mentor = False |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
21 |
can_create_task = False |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
22 |
task_list = [] |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
23 |
|
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
24 |
if not user.is_authenticated(): |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
25 |
is_guest = True |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
26 |
disp_num = 10 |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
27 |
tasks_count = Task.objects.count() |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
28 |
if tasks_count <= disp_num: |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
29 |
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
|
30 |
else: |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
31 |
task_list = Task.objects.order_by('id').reverse()[:10] |
14
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
32 |
|
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
33 |
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
|
34 |
|
5
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
35 |
else: |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
36 |
user_profile = user.get_profile() |
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
37 |
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
|
38 |
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
|
39 |
|
14
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
40 |
context = {'user':user, |
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
41 |
'is_guest':is_guest, |
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
42 |
'is_mentor':is_mentor, |
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
43 |
'task_list':task_list, |
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
44 |
'can_create_task':can_create_task, |
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 |
|
f2623fb8041a
implemented add another mentor functionality to a task.
nishanth
parents:
13
diff
changeset
|
47 |
return render_to_response('index.html', context) |
6 | 48 |
|
5
aea7e764c033
created views and templates for homepage,browse_task and added actions.
nishanth
parents:
2
diff
changeset
|
49 |
|
6 | 50 |
def register(request): |
51 |
""" user registration: gets the user details and create the user and userprofile if data entered is valid""" |
|
52 |
if request.method == 'POST': |
|
53 |
form = RegistrationForm(request.POST) |
|
54 |
if form.is_valid(): |
|
55 |
data = form.cleaned_data |
|
56 |
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
|
57 |
if data['username'].isalnum(): |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
58 |
try: |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
59 |
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
|
60 |
errors=['Choose some other username'] |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
61 |
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
|
62 |
except: |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
63 |
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
|
64 |
return redirect('/accounts/login/') |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
65 |
else: |
232d40a28118
imposed a filter on username that it contains only alphabets and numbers.
anoop
parents:
6
diff
changeset
|
66 |
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
|
67 |
return render_to_response('user/register.html',{'form':form,'errors':errors}) |
6 | 68 |
else: |
69 |
errors=['Password do not match'] |
|
70 |
form = RegistrationForm(request.POST) |
|
71 |
return render_to_response('user/register.html',{'form':form,'errors':errors})#HttpResponse('Password did not match') |
|
72 |
else: |
|
8 | 73 |
form = RegistrationForm(request.POST) |
6 | 74 |
else: |
75 |
form = RegistrationForm() |
|
76 |
return render_to_response('user/register.html', {'form': form}) |
|
77 |
||
78 |
def user_login(request): |
|
79 |
if request.method == 'POST': |
|
80 |
username = request.POST['username'] |
|
81 |
password = request.POST['password'] |
|
82 |
user = authenticate(username=username, password=password) |
|
83 |
if user is not None: |
|
84 |
if user.is_active: |
|
85 |
login(request, user) |
|
86 |
return redirect('/')# Redirect to a success page. |
|
87 |
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
|
88 |
return show_msg('username is not active, please contact the administrator')# Return a 'disabled account' error message |
6 | 89 |
else: |
90 |
errors = ['Please check your username and password'] |
|
91 |
form = LoginForm() |
|
92 |
return render_to_response('user/login.html',{'form':form,'errors':errors})# Return an 'invalid login' error message. |
|
93 |
return redirect('/') |
|
94 |
else: |
|
95 |
form = LoginForm() |
|
96 |
return render_to_response('user/login.html',{'form': form}) |
|
97 |
||
98 |
def user_logout(request): |
|
99 |
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
|
100 |
return show_msg('You have logged off successfully!!!') |
16 | 101 |
|
102 |
def view_my_profile(request,uid): |
|
103 |
""" allows the user to view the profiles of users """ |
|
104 |
edit_profile = True if request.user == User.objects.get(pk=uid) else False |
|
105 |
try: |
|
106 |
profile = Profile.objects.get(user = User.objects.get(pk=uid)) |
|
107 |
view_profile_form = UserProfileForm(instance = profile) |
|
108 |
except Profile.DoesNotExist: |
|
109 |
raise Http404 |
|
110 |
return render_to_response('user/my_profile.html', {'view_profile_form': view_profile_form,'edit_profile':edit_profile}) |
|
111 |
||
112 |
def edit_my_profile(request): |
|
113 |
""" enables the user to edit his/her user profile """ |
|
114 |
if str(request.user) == 'AnonymousUser': |
|
115 |
return show_msg('Please register yourself to activate the functionality') |
|
116 |
if request.method == 'POST': |
|
117 |
form = UserProfileEditForm(request.POST) |
|
118 |
# if not form.is_valid(): |
|
119 |
# edit_profile_form = UserProfileEditForm(instance = form) |
|
120 |
# return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form}) |
|
121 |
if request.user.is_authenticated() == True: |
|
122 |
profile = Profile.objects.get(user = request.user) |
|
123 |
data = request.POST#form.cleaned_data |
|
124 |
properties = {'aboutme':data['aboutme'], 'foss_comm':data['foss_comm'], 'phonenum':data['phonenum'], 'homepage':data['homepage'], 'street':data['street'], 'city':data['city'], 'country':data['country'], 'nick':data['nick']} |
|
125 |
#fields = ['dob','gender','credits','aboutme','foss_comm','phonenum','homepage','street','city','country','nick'] |
|
126 |
updateProfile(profile,properties) |
|
127 |
return redirect('/user/view/uid='+str(profile.user_id)) |
|
128 |
else: |
|
129 |
profile = Profile.objects.get(user = request.user) |
|
130 |
edit_profile_form = UserProfileEditForm(instance = profile) |
|
131 |
return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form}) |