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