author | nishanth |
Sun, 28 Feb 2010 01:15:15 +0530 | |
changeset 133 | 34187a80d279 |
parent 127 | 71888e23f323 |
child 137 | e56b95298254 |
permissions | -rw-r--r-- |
61 | 1 |
import os |
83 | 2 |
|
42
9b5b8c997598
started using django-registration default backend, removed browse users functionality.
anoop
parents:
40
diff
changeset
|
3 |
from django.http import HttpResponse, Http404 |
17
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
4 |
from django.shortcuts import redirect, render_to_response |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
5 |
from django.contrib.auth.models import User |
39 | 6 |
from django.contrib.auth.decorators import login_required |
7 |
||
83 | 8 |
from pytask.taskapp.models import Task, Profile, Request |
123 | 9 |
|
83 | 10 |
from pytask.taskapp.events.user import createUser, updateProfile |
123 | 11 |
from pytask.taskapp.events.request import reply_to_request |
12 |
||
83 | 13 |
from pytask.taskapp.forms.user import UserProfileEditForm |
123 | 14 |
|
122
daee11bdfbaa
now rid is not the position argument. it is request id.
nishanth
parents:
111
diff
changeset
|
15 |
from pytask.taskapp.utilities.request import get_request |
123 | 16 |
from pytask.taskapp.utilities.notification import get_notification |
17
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
17 |
|
127
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
18 |
about = { |
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
19 |
"addmentors":"about/addmentors.html", |
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
20 |
"mentor":"about/mentor.html", |
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
21 |
} |
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
22 |
|
108
131554cc3434
updated show_msg method to also use the user variable in the context.
nishanth
parents:
104
diff
changeset
|
23 |
def show_msg(user, message, redirect_url=None, url_desc=None): |
17
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
24 |
""" simply redirect to homepage """ |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
25 |
|
111 | 26 |
return render_to_response('show_msg.html',{'user':user, 'message':message, 'redirect_url':redirect_url, 'url_desc':url_desc}) |
17
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
27 |
|
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
28 |
def homepage(request): |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
29 |
""" check for authentication and display accordingly. """ |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
30 |
|
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
31 |
user = request.user |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
32 |
is_guest = False |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
33 |
is_mentor = False |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
34 |
can_create_task = False |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
35 |
task_list = [] |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
36 |
|
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
37 |
if not user.is_authenticated(): |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
38 |
is_guest = True |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
39 |
disp_num = 10 |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
40 |
tasks_count = Task.objects.count() |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
41 |
if tasks_count <= disp_num: |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
42 |
task_list = Task.objects.order_by('id').reverse() |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
43 |
else: |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
44 |
task_list = Task.objects.order_by('id').reverse()[:10] |
21
c28774fe7ffd
implemented "add another mentor" functionality to a task.
nishanth
parents:
20
diff
changeset
|
45 |
|
c28774fe7ffd
implemented "add another mentor" functionality to a task.
nishanth
parents:
20
diff
changeset
|
46 |
return render_to_response('index.html', {'is_guest':is_guest, 'task_list':task_list}) |
c28774fe7ffd
implemented "add another mentor" functionality to a task.
nishanth
parents:
20
diff
changeset
|
47 |
|
17
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
48 |
else: |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
49 |
user_profile = user.get_profile() |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
50 |
is_mentor = True if user.task_mentors.all() else False |
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
51 |
can_create_task = False if user_profile.rights == u"CT" else True |
123 | 52 |
notifications = user.notification_sent_to.filter(is_deleted=False,is_read=False) |
97 | 53 |
requests = user.request_sent_to.filter(is_replied=False) |
17
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
54 |
|
21
c28774fe7ffd
implemented "add another mentor" functionality to a task.
nishanth
parents:
20
diff
changeset
|
55 |
context = {'user':user, |
c28774fe7ffd
implemented "add another mentor" functionality to a task.
nishanth
parents:
20
diff
changeset
|
56 |
'is_guest':is_guest, |
c28774fe7ffd
implemented "add another mentor" functionality to a task.
nishanth
parents:
20
diff
changeset
|
57 |
'is_mentor':is_mentor, |
c28774fe7ffd
implemented "add another mentor" functionality to a task.
nishanth
parents:
20
diff
changeset
|
58 |
'task_list':task_list, |
c28774fe7ffd
implemented "add another mentor" functionality to a task.
nishanth
parents:
20
diff
changeset
|
59 |
'can_create_task':can_create_task, |
97 | 60 |
'notifications':notifications, |
61 |
'requests':requests, |
|
21
c28774fe7ffd
implemented "add another mentor" functionality to a task.
nishanth
parents:
20
diff
changeset
|
62 |
} |
125 | 63 |
|
64 |
context["unpublished_tasks"] = user.task_mentors.filter(status="UP") |
|
21
c28774fe7ffd
implemented "add another mentor" functionality to a task.
nishanth
parents:
20
diff
changeset
|
65 |
|
c28774fe7ffd
implemented "add another mentor" functionality to a task.
nishanth
parents:
20
diff
changeset
|
66 |
return render_to_response('index.html', context) |
17
aa45fec40e7e
renamed users.py to user.py and tasks to task in views folder and updated urls.py accordingly .
nishanth
parents:
diff
changeset
|
67 |
|
39 | 68 |
@login_required |
127
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
69 |
def learn_more(request, what): |
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
70 |
""" depending on what was asked for, we render different pages. |
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
71 |
""" |
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
72 |
|
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
73 |
disp_template = about.get(what, None) |
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
74 |
if not disp_template: |
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
75 |
raise Http404 |
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
76 |
else: |
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
77 |
return render_to_response(disp_template) |
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
78 |
|
71888e23f323
created a view for about pages and added about pages for mentor and addmentors.
nishanth
parents:
125
diff
changeset
|
79 |
@login_required |
42
9b5b8c997598
started using django-registration default backend, removed browse users functionality.
anoop
parents:
40
diff
changeset
|
80 |
def view_my_profile(request,uid=None): |
23 | 81 |
""" allows the user to view the profiles of users """ |
42
9b5b8c997598
started using django-registration default backend, removed browse users functionality.
anoop
parents:
40
diff
changeset
|
82 |
if uid == None: |
9b5b8c997598
started using django-registration default backend, removed browse users functionality.
anoop
parents:
40
diff
changeset
|
83 |
edit_profile = True |
9b5b8c997598
started using django-registration default backend, removed browse users functionality.
anoop
parents:
40
diff
changeset
|
84 |
profile = Profile.objects.get(user = request.user) |
69
49532a0f5071
added user context variable in view_profile and edit_profile.
nishanth
parents:
68
diff
changeset
|
85 |
return render_to_response('user/my_profile.html', {'edit_profile':edit_profile,'profile':profile, 'user':request.user}) |
23 | 86 |
edit_profile = True if request.user == User.objects.get(pk=uid) else False |
87 |
try: |
|
88 |
profile = Profile.objects.get(user = User.objects.get(pk=uid)) |
|
89 |
except Profile.DoesNotExist: |
|
90 |
raise Http404 |
|
69
49532a0f5071
added user context variable in view_profile and edit_profile.
nishanth
parents:
68
diff
changeset
|
91 |
return render_to_response('user/my_profile.html', {'edit_profile':edit_profile,'profile':profile, 'user':request.user}) |
23 | 92 |
|
45 | 93 |
@login_required |
23 | 94 |
def edit_my_profile(request): |
95 |
""" enables the user to edit his/her user profile """ |
|
96 |
if request.method == 'POST': |
|
97 |
form = UserProfileEditForm(request.POST) |
|
98 |
# if not form.is_valid(): |
|
99 |
# edit_profile_form = UserProfileEditForm(instance = form) |
|
100 |
# return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form}) |
|
101 |
if request.user.is_authenticated() == True: |
|
102 |
profile = Profile.objects.get(user = request.user) |
|
103 |
data = request.POST#form.cleaned_data |
|
61 | 104 |
properties = {'aboutme':data['aboutme'], |
105 |
'foss_comm':data['foss_comm'], |
|
106 |
'phonenum':data['phonenum'], |
|
107 |
'homepage':data['homepage'], |
|
108 |
'street':data['street'], |
|
109 |
'city':data['city'], |
|
110 |
'country':data['country'], |
|
111 |
'nick':data['nick']} |
|
112 |
uploaded_photo = request.FILES.get('photo',None) |
|
113 |
prev_photo = profile.photo |
|
114 |
if uploaded_photo: |
|
115 |
if prev_photo: |
|
116 |
os.remove(prev_photo.path) |
|
117 |
properties['photo'] = uploaded_photo |
|
23 | 118 |
#fields = ['dob','gender','credits','aboutme','foss_comm','phonenum','homepage','street','city','country','nick'] |
119 |
updateProfile(profile,properties) |
|
120 |
return redirect('/user/view/uid='+str(profile.user_id)) |
|
121 |
else: |
|
122 |
profile = Profile.objects.get(user = request.user) |
|
123 |
edit_profile_form = UserProfileEditForm(instance = profile) |
|
69
49532a0f5071
added user context variable in view_profile and edit_profile.
nishanth
parents:
68
diff
changeset
|
124 |
return render_to_response('user/edit_profile.html',{'edit_profile_form' : edit_profile_form, 'user':request.user}) |
28
e137b605b888
added browse users functionality, added user/browse.html, fixed view my profile template.
anoop
parents:
27
diff
changeset
|
125 |
|
83 | 126 |
@login_required |
127 |
def browse_requests(request): |
|
128 |
||
129 |
user = request.user |
|
122
daee11bdfbaa
now rid is not the position argument. it is request id.
nishanth
parents:
111
diff
changeset
|
130 |
active_reqs = user.request_sent_to.filter(is_replied=False).exclude(is_valid=False) |
83 | 131 |
reqs = active_reqs.order_by('creation_date').reverse() |
122
daee11bdfbaa
now rid is not the position argument. it is request id.
nishanth
parents:
111
diff
changeset
|
132 |
|
83 | 133 |
context = { |
134 |
'user':user, |
|
135 |
'reqs':reqs, |
|
136 |
} |
|
137 |
||
138 |
return render_to_response('user/browse_requests.html', context) |
|
139 |
||
140 |
@login_required |
|
141 |
def view_request(request, rid): |
|
142 |
""" please note that request variable in this method is that of django. |
|
143 |
our app request is called user_request. |
|
144 |
""" |
|
145 |
||
146 |
user = request.user |
|
133
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
147 |
newest, newer, user_request, older, oldest = get_request(rid, user) |
122
daee11bdfbaa
now rid is not the position argument. it is request id.
nishanth
parents:
111
diff
changeset
|
148 |
if not user_request: |
daee11bdfbaa
now rid is not the position argument. it is request id.
nishanth
parents:
111
diff
changeset
|
149 |
raise Http404 |
daee11bdfbaa
now rid is not the position argument. it is request id.
nishanth
parents:
111
diff
changeset
|
150 |
|
100
2275886511df
now admin can accept a request for assigning credits.
nishanth
parents:
97
diff
changeset
|
151 |
user_request.is_read = True |
2275886511df
now admin can accept a request for assigning credits.
nishanth
parents:
97
diff
changeset
|
152 |
user_request.save() |
83 | 153 |
|
154 |
context = { |
|
155 |
'user':user, |
|
156 |
'req':user_request, |
|
133
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
157 |
'sent_users':user_request.sent_to.all(), |
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
158 |
'newest':newest, |
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
159 |
'newer':newer, |
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
160 |
'older':older, |
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
161 |
'oldest':oldest, |
83 | 162 |
} |
163 |
||
164 |
return render_to_response('user/view_request.html', context) |
|
165 |
||
166 |
@login_required |
|
167 |
def process_request(request, rid, reply): |
|
168 |
""" check if the method is post and then update the request. |
|
169 |
if it is get, display a 404 error. |
|
170 |
""" |
|
171 |
||
100
2275886511df
now admin can accept a request for assigning credits.
nishanth
parents:
97
diff
changeset
|
172 |
user = request.user |
122
daee11bdfbaa
now rid is not the position argument. it is request id.
nishanth
parents:
111
diff
changeset
|
173 |
browse_request_url= '/user/requests' |
133
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
174 |
newest, newer, req_obj, older, oldest = get_request(rid, user) |
122
daee11bdfbaa
now rid is not the position argument. it is request id.
nishanth
parents:
111
diff
changeset
|
175 |
|
daee11bdfbaa
now rid is not the position argument. it is request id.
nishanth
parents:
111
diff
changeset
|
176 |
if not req_obj: |
daee11bdfbaa
now rid is not the position argument. it is request id.
nishanth
parents:
111
diff
changeset
|
177 |
return show_msg(user, "Your reply has been processed", browse_request_url, "view other requests") |
100
2275886511df
now admin can accept a request for assigning credits.
nishanth
parents:
97
diff
changeset
|
178 |
|
83 | 179 |
if request.method=="POST": |
100
2275886511df
now admin can accept a request for assigning credits.
nishanth
parents:
97
diff
changeset
|
180 |
reply = True if reply == "yes" else False |
104 | 181 |
req_obj.remarks = request.POST.get('remarks', "") |
182 |
req_obj.save() |
|
183 |
||
100
2275886511df
now admin can accept a request for assigning credits.
nishanth
parents:
97
diff
changeset
|
184 |
reply_to_request(req_obj, reply, user) |
133
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
185 |
|
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
186 |
if older: |
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
187 |
return redirect('/user/requests/rid=%s'%older.id) |
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
188 |
else: |
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
189 |
return redirect(browse_request_url) |
108
131554cc3434
updated show_msg method to also use the user variable in the context.
nishanth
parents:
104
diff
changeset
|
190 |
return show_msg(user, "Your reply has been processed", browse_request_url, "view other requests") |
83 | 191 |
else: |
108
131554cc3434
updated show_msg method to also use the user variable in the context.
nishanth
parents:
104
diff
changeset
|
192 |
return show_msg(user, "You are not authorised to do this", browse_request_url, "view other requests") |
101 | 193 |
|
194 |
@login_required |
|
195 |
def browse_notifications(request): |
|
196 |
""" get the list of notifications that are not deleted and display in datetime order. |
|
197 |
""" |
|
198 |
||
199 |
user = request.user |
|
200 |
||
123 | 201 |
active_notifications = user.notification_sent_to.filter(is_deleted=False).order_by('sent_date').reverse() |
101 | 202 |
|
203 |
context = { |
|
204 |
'user':user, |
|
205 |
'notifications':active_notifications, |
|
206 |
} |
|
207 |
||
208 |
return render_to_response('user/browse_notifications.html', context) |
|
103 | 209 |
|
210 |
@login_required |
|
211 |
def view_notification(request, nid): |
|
212 |
""" get the notification depending on nid. |
|
213 |
Display it. |
|
214 |
""" |
|
215 |
||
216 |
user = request.user |
|
133
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
217 |
newest, newer, notification, older, oldest = get_notification(nid, user) |
123 | 218 |
if not notification: |
219 |
raise Http404 |
|
220 |
||
103 | 221 |
notification.is_read = True |
222 |
notification.save() |
|
223 |
||
224 |
context = { |
|
225 |
'user':user, |
|
226 |
'notification':notification, |
|
133
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
227 |
'newest':newest, |
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
228 |
'newer':newer, |
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
229 |
'older':older, |
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
230 |
'oldest':oldest, |
103 | 231 |
} |
232 |
||
233 |
return render_to_response('user/view_notification.html', context) |
|
234 |
||
235 |
@login_required |
|
236 |
def edit_notification(request, nid, action): |
|
237 |
""" if action is delete, set is_deleted. |
|
238 |
if it is unread, unset is_read. |
|
239 |
save the notification and redirect to browse_notifications. |
|
240 |
""" |
|
241 |
||
242 |
user = request.user |
|
133
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
243 |
newest, newer, notification, older, oldest = get_notification(nid, user) |
123 | 244 |
|
245 |
if not notification: |
|
246 |
raise Http404 |
|
247 |
||
103 | 248 |
notifications_url = "/user/notifications/" |
249 |
||
250 |
if request.method == "POST": |
|
251 |
if action == "delete": |
|
123 | 252 |
notification.is_deleted = True |
103 | 253 |
elif action == "unread": |
254 |
notification.is_read = False |
|
255 |
||
256 |
notification.save() |
|
133
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
257 |
if older: |
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
258 |
return redirect('/user/notifications/nid=%s'%older.id) |
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
259 |
else: |
34187a80d279
added next and previous capabilities to requests and notifications.
nishanth
parents:
127
diff
changeset
|
260 |
return redirect(notifications_url) |
103 | 261 |
else: |
108
131554cc3434
updated show_msg method to also use the user variable in the context.
nishanth
parents:
104
diff
changeset
|
262 |
return show_msg(user, 'This is wrong', notification_url, "view the notification") |
103 | 263 |