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