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