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