author | Madhusudan.C.S <madhusudancs@gmail.com> |
Sat, 03 Oct 2009 22:17:58 +0530 | |
changeset 3002 | 2c5e9cd4ba4d |
parent 2981 | 8a99de852dc2 |
child 3017 | 6689a4c8f02e |
permissions | -rw-r--r-- |
2885 | 1 |
#!/usr/bin/python2.5 |
2 |
# |
|
3 |
# Copyright 2009 the Melange authors. |
|
4 |
# |
|
5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 |
# you may not use this file except in compliance with the License. |
|
7 |
# You may obtain a copy of the License at |
|
8 |
# |
|
9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
|
10 |
# |
|
11 |
# Unless required by applicable law or agreed to in writing, software |
|
12 |
# distributed under the License is distributed on an "AS IS" BASIS, |
|
13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 |
# See the License for the specific language governing permissions and |
|
15 |
# limitations under the License. |
|
16 |
||
17 |
"""Views for Tasks. |
|
18 |
""" |
|
19 |
||
20 |
__authors__ = [ |
|
2942
996e64ba2db8
Removed undefined order argument in GHOP Task View.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2893
diff
changeset
|
21 |
'"Madhusudan.C.S" <madhusudancs@gmail.com>', |
996e64ba2db8
Removed undefined order argument in GHOP Task View.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2893
diff
changeset
|
22 |
'"Lennard de Rijk" <ljvderijk@gmail.com>', |
2885 | 23 |
] |
24 |
||
25 |
||
26 |
import datetime |
|
27 |
import time |
|
28 |
||
29 |
from google.appengine.ext import db |
|
30 |
||
31 |
from django import forms |
|
32 |
from django import http |
|
33 |
from django.utils.translation import ugettext |
|
34 |
||
35 |
from soc.logic import cleaning |
|
36 |
from soc.logic import dicts |
|
37 |
from soc.logic.models import host as host_logic |
|
38 |
from soc.logic.models import student as student_logic |
|
39 |
from soc.logic.models import user as user_logic |
|
40 |
from soc.views import helper |
|
41 |
from soc.views import out_of_band |
|
42 |
from soc.views.helper import decorators |
|
43 |
from soc.views.helper import dynaform |
|
44 |
from soc.views.helper import lists |
|
45 |
from soc.views.helper import params as params_helper |
|
46 |
from soc.views.helper import requests |
|
47 |
from soc.views.helper import redirects |
|
48 |
from soc.views.helper import responses |
|
49 |
from soc.views.helper import widgets |
|
50 |
from soc.views.models import base |
|
51 |
||
52 |
from soc.modules.ghop.logic import cleaning as ghop_cleaning |
|
53 |
from soc.modules.ghop.logic.models import comment as ghop_comment_logic |
|
54 |
from soc.modules.ghop.logic.models import mentor as ghop_mentor_logic |
|
55 |
from soc.modules.ghop.logic.models import organization as ghop_org_logic |
|
56 |
from soc.modules.ghop.logic.models import org_admin as ghop_org_admin_logic |
|
57 |
from soc.modules.ghop.logic.models import program as ghop_program_logic |
|
58 |
from soc.modules.ghop.logic.models import task as ghop_task_logic |
|
59 |
from soc.modules.ghop.models import task as ghop_task_model |
|
60 |
from soc.modules.ghop.views.helper import access |
|
61 |
from soc.modules.ghop.views.helper import redirects as ghop_redirects |
|
62 |
from soc.modules.ghop.views.models import organization as ghop_org_view |
|
63 |
from soc.modules.ghop.tasks import task_update |
|
64 |
||
65 |
import soc.modules.ghop.logic.models.task |
|
66 |
||
67 |
||
68 |
class View(base.View): |
|
69 |
"""View methods for the Tasks. |
|
70 |
""" |
|
71 |
||
72 |
DEF_AWAITING_REG_MSG = ugettext( |
|
73 |
'The task is open but you cannot claim this task since you ' |
|
74 |
'have not completed the student signup after finishing your ' |
|
75 |
'first task.') |
|
76 |
||
77 |
DEF_CAN_EDIT_TASK_MSG = ugettext( |
|
78 |
'The task can be edited by clicking on the edit link ' |
|
79 |
'next to the title above.') |
|
80 |
||
81 |
DEF_MAX_TASK_LIMIT_MSG_FMT = ugettext( |
|
82 |
'The task is open but you cannot claim this task since you ' |
|
83 |
'either have already claimed %d task.') |
|
84 |
||
85 |
DEF_SIGNIN_TO_COMMENT_MSG = ugettext( |
|
86 |
'<a href=%s>Sign in</a> to perform any action or comment on ' |
|
87 |
'this task.') |
|
88 |
||
89 |
DEF_STUDENT_SIGNUP_MSG = ugettext( |
|
90 |
'You have successfully completed this task. Sign up as a student ' |
|
91 |
'before you proceed further.') |
|
92 |
||
93 |
DEF_TASK_CLAIMED_BY_YOU_MSG = ugettext( |
|
94 |
'This task has been claimed by you!') |
|
95 |
||
96 |
DEF_TASK_CLAIMED_BY_STUDENT_MSG = ugettext( |
|
97 |
'This task has been claimed by a student!') |
|
98 |
||
99 |
DEF_TASK_CLAIMED_MSG = ugettext( |
|
100 |
'The task is already claimed and the work is in progress.') |
|
101 |
||
102 |
DEF_TASK_CLAIM_REQUESTED_MSG = ugettext( |
|
103 |
'A student has requested to claim this task. Accept or ' |
|
104 |
'Reject the request.') |
|
105 |
||
106 |
DEF_TASK_CLOSED_MSG = ugettext('The task is closed.') |
|
107 |
||
108 |
DEF_TASK_CMPLTD_BY_YOU_MSG = ugettext( |
|
109 |
'You have successfully completed this task!') |
|
110 |
||
111 |
DEF_TASK_NO_MORE_SUBMIT_MSG = ugettext( |
|
112 |
'You have submitted the work to this task, but deadline has passed ' |
|
113 |
'You cannot submit any more work until your mentor extends the ' |
|
114 |
'deadline.') |
|
115 |
||
116 |
DEF_TASK_MENTOR_REOPENED_MSG = ugettext( |
|
117 |
'The task has been reopened.') |
|
118 |
||
119 |
DEF_TASK_NEEDS_REVIEW_MSG = ugettext( |
|
120 |
'Student has submitted his work for this task! It needs review.') |
|
121 |
||
122 |
DEF_TASK_OPEN_MSG = ugettext( |
|
123 |
'This task is open. If you are GHOP student, you can claim it!') |
|
124 |
||
125 |
DEF_TASK_REOPENED_MSG = ugettext( |
|
126 |
'This task has been reopened. If you are a GHOP student, ' |
|
127 |
'you can clam it!') |
|
128 |
||
129 |
DEF_TASK_REQ_CLAIMED_BY_YOU_MSG = ugettext( |
|
130 |
'You have requested to claim this task!') |
|
131 |
||
132 |
DEF_TASK_UNPUBLISHED_MSG = ugettext( |
|
133 |
'The task is not yet published. It can be edited by clicking on ' |
|
134 |
'the edit button below.') |
|
135 |
||
136 |
DEF_WS_MSG_FMT = ugettext( |
|
137 |
'(To see the work submitted <a href=#ws%d>click here</a>.)') |
|
138 |
||
139 |
def __init__(self, params=None): |
|
140 |
"""Defines the fields and methods required for the task View class |
|
141 |
to provide the user with the necessary views. |
|
142 |
||
143 |
Params: |
|
144 |
params: a dict with params for this View |
|
145 |
""" |
|
146 |
||
147 |
rights = access.GHOPChecker(params) |
|
148 |
# TODO: create and suggest_task don't need access checks which use state |
|
149 |
# also feels like roles are being checked twice? |
|
150 |
rights['create'] = [ |
|
151 |
('checkCanOrgAdminOrMentorEdit', ['scope_path', True]), |
|
152 |
('checkRoleAndStatusForTask', |
|
153 |
[['ghop/org_admin'], ['active'], |
|
154 |
[]])] |
|
155 |
rights['edit'] = [ |
|
156 |
('checkCanOrgAdminOrMentorEdit', ['scope_path', False]), |
|
157 |
('checkRoleAndStatusForTask', |
|
158 |
[['ghop/org_admin'], ['active'], |
|
159 |
['Unapproved', 'Unpublished', 'Open']])] |
|
160 |
rights['delete'] = ['checkIsDeveloper'] |
|
161 |
rights['show'] = ['checkStatusForTask'] |
|
162 |
rights['list_org_tasks'] = [ |
|
163 |
('checkCanOrgAdminOrMentorEdit', ['scope_path', False])] |
|
164 |
rights['suggest_task'] = [ |
|
165 |
('checkCanOrgAdminOrMentorEdit', ['scope_path', True]), |
|
166 |
('checkRoleAndStatusForTask', |
|
167 |
[['ghop/org_admin', 'ghop/mentor'], ['active'], |
|
168 |
[]])] |
|
169 |
rights['search'] = ['allow'] |
|
170 |
||
171 |
new_params = {} |
|
172 |
new_params['logic'] = soc.modules.ghop.logic.models.task.logic |
|
173 |
new_params['rights'] = rights |
|
174 |
||
175 |
new_params['name'] = "Task" |
|
176 |
new_params['module_name'] = "task" |
|
177 |
new_params['sidebar_grouping'] = 'Tasks' |
|
178 |
||
179 |
new_params['module_package'] = 'soc.modules.ghop.views.models' |
|
180 |
new_params['url_name'] = 'ghop/task' |
|
181 |
||
182 |
new_params['scope_view'] = ghop_org_view |
|
183 |
new_params['scope_redirect'] = redirects.getCreateRedirect |
|
184 |
||
185 |
new_params['list_heading'] = 'modules/ghop/task/list/heading.html' |
|
186 |
new_params['list_row'] = 'modules/ghop/task/list/row.html' |
|
187 |
||
188 |
new_params['extra_dynaexclude'] = ['task_type', 'mentors', 'user', |
|
189 |
'student', 'program', 'status', |
|
190 |
'deadline', 'created_by', |
|
191 |
'created_on', 'modified_by', |
|
192 |
'modified_on', 'history', |
|
193 |
'link_id', 'difficulty'] |
|
194 |
||
195 |
patterns = [] |
|
196 |
patterns += [ |
|
197 |
(r'^%(url_name)s/(?P<access_type>suggest_task)/%(scope)s$', |
|
198 |
'%(module_package)s.%(module_name)s.suggest_task', |
|
199 |
'Mentors suggest %(name)s'), |
|
200 |
(r'^%(url_name)s/(?P<access_type>suggest_task)/%(key_fields)s$', |
|
201 |
'%(module_package)s.%(module_name)s.suggest_task', |
|
202 |
'Mentors edit a %(name)s'), |
|
203 |
(r'^%(url_name)s/(?P<access_type>list_org_tasks)/%(scope)s$', |
|
204 |
'%(module_package)s.%(module_name)s.list_org_tasks', |
|
205 |
'List Organization %(name)s'), |
|
206 |
(r'^%(url_name)s/(?P<access_type>search)/' |
|
207 |
'(?P<scope_path>%(ulnp)s)/%(lnp)s$', |
|
208 |
'%(module_package)s.%(module_name)s.search', |
|
209 |
'Search for %(name)ss'), |
|
210 |
] |
|
211 |
||
212 |
new_params['extra_django_patterns'] = patterns |
|
213 |
||
214 |
new_params['create_dynafields'] = [ |
|
215 |
{'name': 'arbit_tags', |
|
216 |
'base': forms.fields.CharField, |
|
217 |
'label': 'Arbitrary Tags', |
|
218 |
'required': False, |
|
219 |
'group': ugettext('Tags'), |
|
220 |
'help_text': ugettext( |
|
221 |
'Enter arbitrary Tags for this task separated by comma.\n' |
|
222 |
'<b>Note:</b> Tag names are case sensitive. If the tag is same ' |
|
223 |
'as the program mandated tag, it will be considered as a ' |
|
224 |
'mandatory tag.') |
|
225 |
}, |
|
226 |
] |
|
227 |
||
228 |
new_params['create_extra_dynaproperties'] = { |
|
229 |
'description': forms.fields.CharField(required=True, |
|
230 |
widget=widgets.FullTinyMCE(attrs={'rows': 25, 'cols': 100})), |
|
231 |
'scope_path': forms.CharField(widget=forms.HiddenInput, |
|
232 |
required=True), |
|
233 |
'time_to_complete': forms.IntegerField(min_value=1, |
|
234 |
required=True), |
|
235 |
'clean_description': cleaning.clean_html_content('description'), |
|
236 |
'clean_arbit_tags': cleaning.str2set('arbit_tags'), |
|
237 |
} |
|
238 |
||
239 |
new_params['edit_extra_dynaproperties'] = { |
|
240 |
'link_id': forms.CharField(widget=forms.HiddenInput) |
|
241 |
} |
|
242 |
||
243 |
new_params['public_template'] = 'modules/ghop/task/public.html' |
|
244 |
||
245 |
params = dicts.merge(params, new_params, sub_merge=True) |
|
246 |
||
247 |
super(View, self).__init__(params=params) |
|
248 |
||
249 |
# holds the base form for the task creation and editing |
|
250 |
self._params['base_create_form'] = self._params['create_form'] |
|
251 |
self._params['base_edit_form'] = self._params['edit_form'] |
|
252 |
||
253 |
# extend create and edit form for org admins |
|
254 |
dynafields = [ |
|
255 |
{'name': 'mentors_list', |
|
256 |
'required': True, |
|
257 |
'base': forms.fields.CharField, |
|
258 |
'label': 'Assign mentors', |
|
259 |
'help_text': 'Assign mentors to this task by ' |
|
260 |
'giving their link_ids separated by comma.', |
|
261 |
}, |
|
262 |
{'name': 'published', |
|
263 |
'required': False, |
|
264 |
'initial': False, |
|
265 |
'base': forms.fields.BooleanField, |
|
266 |
'label': 'Publish the task', |
|
267 |
'help_text': ugettext('By ticking this box, the task will be' |
|
268 |
'made public and can be claimed by students.'), |
|
269 |
} |
|
270 |
] |
|
271 |
||
272 |
dynaproperties = params_helper.getDynaFields(dynafields) |
|
273 |
||
274 |
dynaproperties['clean_mentors_list'] = ghop_cleaning.cleanMentorsList( |
|
275 |
'mentors_list') |
|
276 |
||
277 |
create_form = dynaform.extendDynaForm( |
|
278 |
dynaform=self._params['create_form'], |
|
279 |
dynaproperties=dynaproperties) |
|
280 |
||
281 |
self._params['create_form'] = create_form |
|
282 |
||
283 |
edit_form = dynaform.extendDynaForm( |
|
284 |
dynaform=self._params['edit_form'], |
|
285 |
dynaproperties=dynaproperties) |
|
286 |
||
287 |
self._params['edit_form'] = edit_form |
|
288 |
||
289 |
# create the comment form |
|
290 |
dynafields = [ |
|
291 |
{'name': 'comment', |
|
292 |
'base': forms.CharField, |
|
293 |
'widget': widgets.FullTinyMCE(attrs={'rows': 10, 'cols': 40}), |
|
294 |
'label': 'Comment', |
|
295 |
'required': False, |
|
296 |
'example_text': 'Comment is optional.<br/>' |
|
297 |
'Choose an appropriate Action below and Save your ' |
|
298 |
'changes.<br /><br />Caution, you will not be able ' |
|
299 |
'to edit your comment!', |
|
300 |
}, |
|
301 |
] |
|
302 |
||
303 |
dynaproperties = params_helper.getDynaFields(dynafields) |
|
304 |
dynaproperties['clean_comment'] = cleaning.clean_html_content('comment') |
|
2893
1adc6a815c71
Several style and import fixes regarding the GHOP module.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2885
diff
changeset
|
305 |
dynaproperties['clean'] = ghop_cleaning.cleanTaskComment( |
2885 | 306 |
'comment', 'action', 'work_submission') |
307 |
||
308 |
comment_form = dynaform.newDynaForm(dynamodel=None, |
|
309 |
dynabase=helper.forms.BaseForm, dynainclude=None, |
|
310 |
dynaexclude=None, dynaproperties=dynaproperties) |
|
311 |
self._params['comment_form'] = comment_form |
|
312 |
||
313 |
def _getTagsForProgram(self, form_name, params, **kwargs): |
|
314 |
"""Extends form dynamically from difficulty levels in program entity. |
|
315 |
||
316 |
Args: |
|
317 |
form_name: the Form entry in params to extend |
|
318 |
params: the params for the view |
|
319 |
""" |
|
320 |
||
321 |
# obtain program_entity using scope_path which holds |
|
322 |
# the org_entity key_name |
|
323 |
org_entity = ghop_org_logic.logic.getFromKeyName(kwargs['scope_path']) |
|
324 |
program_entity = ghop_program_logic.logic.getFromKeyName( |
|
325 |
org_entity.scope_path) |
|
326 |
||
327 |
# get a list difficulty levels stored for the program entity |
|
328 |
tds = ghop_task_model.TaskDifficultyTag.get_by_scope( |
|
329 |
program_entity) |
|
330 |
||
331 |
difficulties = [] |
|
332 |
for td in tds: |
|
333 |
difficulties.append((td.tag, td.tag)) |
|
334 |
||
335 |
# get a list of task type tags stored for the program entity |
|
336 |
tts = ghop_task_model.TaskTypeTag.get_by_scope(program_entity) |
|
337 |
||
338 |
type_tags = [] |
|
339 |
for tt in tts: |
|
340 |
type_tags.append((tt.tag, tt.tag)) |
|
341 |
||
342 |
# create the difficultly level field containing the choices |
|
343 |
# defined in the program entity |
|
344 |
dynafields = [ |
|
345 |
{'name': 'difficulty', |
|
346 |
'base': forms.ChoiceField, |
|
347 |
'label': 'Difficulty level', |
|
348 |
'required': True, |
|
349 |
'passthrough': ['initial', 'required', 'choices', |
|
350 |
'label', 'help_text'], |
|
351 |
'choices': difficulties, |
|
352 |
'group': ugettext('Tags'), |
|
353 |
'help_text': ugettext('Difficulty Level of this task.'), |
|
354 |
}, |
|
355 |
{'name': 'type_tags', |
|
356 |
'base': forms.MultipleChoiceField, |
|
357 |
'label': 'Task Types', |
|
358 |
'required': True, |
|
359 |
'passthrough': ['initial', 'required', 'choices', |
|
360 |
'label', 'help_text'], |
|
361 |
'choices': type_tags, |
|
362 |
'group': ugettext('Tags'), |
|
363 |
'help_text': ugettext('Task Type tags mandated by the program. You ' |
|
364 |
'must select one or more of them.'), |
|
365 |
}, |
|
366 |
] |
|
367 |
||
368 |
dynaproperties = params_helper.getDynaFields(dynafields) |
|
369 |
||
370 |
extended_form = dynaform.extendDynaForm( |
|
371 |
dynaform=params[form_name], |
|
372 |
dynaproperties=dynaproperties) |
|
373 |
||
374 |
return extended_form |
|
375 |
||
376 |
@decorators.check_access |
|
377 |
def create(self, request, access_type, |
|
378 |
page_name=None, params=None, **kwargs): |
|
379 |
"""Replaces the create Form with the dynamic one. |
|
380 |
||
381 |
For args see base.View.create(). |
|
382 |
""" |
|
383 |
||
384 |
params = dicts.merge(params, self._params) |
|
385 |
||
386 |
# extend create_form to include difficulty levels |
|
387 |
params['create_form'] = self._getTagsForProgram( |
|
388 |
'create_form', params, **kwargs) |
|
389 |
||
390 |
return super(View, self).create(request, 'allow', page_name=page_name, |
|
391 |
params=params, **kwargs) |
|
392 |
||
393 |
@decorators.merge_params |
|
394 |
@decorators.check_access |
|
395 |
def edit(self, request, access_type, |
|
396 |
page_name=None, params=None, seed=None, **kwargs): |
|
397 |
"""See base.View.edit(). |
|
398 |
""" |
|
399 |
||
400 |
logic = params['logic'] |
|
401 |
||
402 |
context = helper.responses.getUniversalContext(request) |
|
403 |
helper.responses.useJavaScript(context, params['js_uses_all']) |
|
404 |
context['page_name'] = page_name |
|
405 |
||
406 |
try: |
|
407 |
entity = logic.getFromKeyFieldsOr404(kwargs) |
|
408 |
except out_of_band.Error, error: |
|
409 |
msg = self.DEF_CREATE_NEW_ENTITY_MSG_FMT % { |
|
410 |
'entity_type_lower' : params['name'].lower(), |
|
411 |
'entity_type' : params['name'], |
|
412 |
'create' : params['missing_redirect'] |
|
413 |
} |
|
414 |
error.message_fmt = error.message_fmt + msg |
|
415 |
return helper.responses.errorResponse( |
|
416 |
error, request, context=context) |
|
417 |
||
418 |
user_account = user_logic.logic.getForCurrentAccount() |
|
419 |
||
420 |
filter = { |
|
421 |
'user': user_account, |
|
422 |
} |
|
423 |
||
424 |
# extend edit_form to include difficulty levels |
|
425 |
params['edit_form'] = self._getTagsForProgram( |
|
426 |
'edit_form', params, **kwargs) |
|
427 |
||
428 |
org_admin_entities = ghop_org_admin_logic.logic.getForFields(filter) |
|
429 |
||
430 |
if entity and entity.status == 'Unapproved': |
|
431 |
approval_req = True |
|
432 |
for org_admin_entity in org_admin_entities: |
|
433 |
if org_admin_entity.key().name() == entity.created_by.key().name(): |
|
434 |
approval_req = False |
|
435 |
break |
|
436 |
||
437 |
if approval_req: |
|
438 |
dynafields = [ |
|
439 |
{'name': 'approved', |
|
440 |
'required': False, |
|
441 |
'initial': False, |
|
442 |
'base': forms.fields.BooleanField, |
|
443 |
'label': 'Approve the task', |
|
444 |
'help_text': 'By ticking this box, the task will be' |
|
445 |
'will be approved for publishing.', |
|
446 |
} |
|
447 |
] |
|
448 |
||
449 |
dynaproperties = params_helper.getDynaFields(dynafields) |
|
450 |
||
451 |
edit_form = dynaform.extendDynaForm( |
|
452 |
dynaform=params['edit_form'], |
|
453 |
dynaproperties=dynaproperties) |
|
454 |
||
455 |
params['edit_form'] = edit_form |
|
456 |
||
457 |
if request.method == 'POST': |
|
458 |
return self.editPost(request, entity, context, params=params) |
|
459 |
else: |
|
460 |
return self.editGet(request, entity, context, params=params) |
|
461 |
||
462 |
def _editGet(self, request, entity, form): |
|
463 |
"""See base.View._editGet(). |
|
464 |
""" |
|
465 |
||
466 |
if entity.task_type: |
|
467 |
form.fields['type_tags'].initial = entity.tags_string( |
|
468 |
entity.task_type, ret_list=True) |
|
469 |
if entity.arbit_tag: |
|
470 |
form.fields['arbit_tags'].initial = entity.tags_string( |
|
471 |
entity.arbit_tag) |
|
472 |
||
473 |
if entity.difficulty: |
|
474 |
form.fields['difficulty'].initial = entity.tags_string( |
|
475 |
entity.difficulty) |
|
476 |
||
477 |
if entity.mentors and 'mentors_list' in form.fields: |
|
478 |
mentor_entities = db.get(entity.mentors) |
|
479 |
mentors_list = [] |
|
480 |
for mentor in mentor_entities: |
|
481 |
mentors_list.append(mentor.link_id) |
|
482 |
form.fields['mentors_list'].initial = ', '.join(mentors_list) |
|
483 |
||
484 |
form.fields['link_id'].initial = entity.link_id |
|
485 |
||
486 |
# checks if the task is already approved or not and sets |
|
487 |
# the form approved field |
|
488 |
if 'approved' in form.fields: |
|
489 |
if entity.status == 'Unapproved': |
|
490 |
form.fields['approved'].initial = False |
|
491 |
else: |
|
492 |
form.fields['approved'].initial = True |
|
493 |
||
494 |
# checks if the task is already published or not and sets |
|
495 |
# the form published field |
|
496 |
if 'published' in form.fields: |
|
497 |
if entity.status == 'Unapproved' or entity.status == 'Unpublished': |
|
498 |
form.fields['published'].initial = False |
|
499 |
else: |
|
500 |
form.fields['published'].initial = True |
|
501 |
||
502 |
return super(View, self)._editGet(request, entity, form) |
|
503 |
||
504 |
def _editPost(self, request, entity, fields): |
|
505 |
"""See base._editPost(). |
|
506 |
""" |
|
507 |
||
508 |
super(View, self)._editPost(request, entity, fields) |
|
509 |
||
510 |
# TODO: this method can be made more clear, it seems a bit of a mess |
|
511 |
||
512 |
if not entity: |
|
513 |
program_entity = fields['scope'].scope |
|
514 |
fields['program'] = program_entity |
|
515 |
else: |
|
516 |
program_entity = entity.program |
|
517 |
||
518 |
user_account = user_logic.logic.getForCurrentAccount() |
|
519 |
||
520 |
filter = { |
|
521 |
'user': user_account, |
|
522 |
'status': 'active' |
|
523 |
} |
|
2978
2b3c39483f6f
Fixes the problem of GHOP Task editing after it is created.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2942
diff
changeset
|
524 |
if not entity: |
2b3c39483f6f
Fixes the problem of GHOP Task editing after it is created.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2942
diff
changeset
|
525 |
filter['scope'] = fields['scope'] |
2b3c39483f6f
Fixes the problem of GHOP Task editing after it is created.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2942
diff
changeset
|
526 |
else: |
2b3c39483f6f
Fixes the problem of GHOP Task editing after it is created.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
2942
diff
changeset
|
527 |
filter['scope'] = entity.program |
2885 | 528 |
|
529 |
role_entity = ghop_org_admin_logic.logic.getForFields( |
|
530 |
filter, unique=True) |
|
531 |
||
532 |
if role_entity: |
|
533 |
# this user can publish/approve the task |
|
534 |
if fields.get('approved') and fields.get('published'): |
|
535 |
fields['status'] = 'Open' |
|
536 |
elif not fields.get('approved'): |
|
537 |
fields['status'] = 'Unapproved' |
|
538 |
else: |
|
539 |
fields['status'] = 'Unpublished' |
|
540 |
||
541 |
fields['mentors'] = [] |
|
542 |
if fields.get('mentors_list'): |
|
543 |
||
544 |
for mentor_link_id in fields['mentors_list']: |
|
545 |
properties = { |
|
546 |
'scope_path': fields['scope_path'], |
|
547 |
'link_id': mentor_link_id, |
|
548 |
} |
|
549 |
||
550 |
mentor_entity = ghop_mentor_logic.logic.getFromKeyFields(properties) |
|
551 |
fields['mentors'].append(mentor_entity.key()) |
|
552 |
else: |
|
553 |
role_entity = ghop_mentor_logic.logic.getForFields( |
|
554 |
filter, unique=True) |
|
555 |
if not entity: |
|
556 |
# creating a new task |
|
557 |
fields['status'] = 'Unapproved' |
|
558 |
||
559 |
# explicitly change the last_modified_on since the content has been edited |
|
560 |
fields['modified_on'] = datetime.datetime.now() |
|
561 |
||
562 |
if not entity: |
|
563 |
fields['link_id'] = 't%i' % (int(time.time()*100)) |
|
564 |
fields['modified_by'] = role_entity |
|
565 |
fields['created_by'] = role_entity |
|
566 |
fields['created_on'] = datetime.datetime.now() |
|
567 |
else: |
|
568 |
fields['link_id'] = entity.link_id |
|
569 |
fields['modified_by'] = role_entity |
|
570 |
if 'status' not in fields: |
|
571 |
fields['status'] = entity.status |
|
572 |
||
573 |
fields['difficulty'] = { |
|
574 |
'tags': fields['difficulty'], |
|
575 |
'scope': program_entity |
|
576 |
} |
|
577 |
||
578 |
fields['task_type'] = { |
|
579 |
'tags': fields['type_tags'], |
|
580 |
'scope': program_entity |
|
581 |
} |
|
582 |
||
583 |
fields['arbit_tag'] = { |
|
584 |
'tags': fields['arbit_tags'], |
|
585 |
'scope': program_entity |
|
586 |
} |
|
587 |
||
588 |
return |
|
589 |
||
590 |
@decorators.merge_params |
|
591 |
@decorators.check_access |
|
592 |
def suggestTask(self, request, access_type, page_name=None, |
|
593 |
params=None, **kwargs): |
|
594 |
"""View used to allow mentors to create or edit a task. |
|
595 |
||
596 |
Tasks created by mentors must be approved by org admins |
|
597 |
before they are published. |
|
598 |
""" |
|
599 |
||
600 |
params = dicts.merge(params, self._params) |
|
601 |
||
602 |
if 'link_id' in kwargs: |
|
603 |
# extend create_form to include difficulty levels |
|
604 |
params['mentor_form'] = self._getTagsForProgram( |
|
605 |
'base_edit_form', params, **kwargs) |
|
606 |
try: |
|
607 |
entity = self._logic.getFromKeyFieldsOr404(kwargs) |
|
608 |
except out_of_band.Error, error: |
|
609 |
return helper.responses.errorResponse( |
|
610 |
error, request, template=params['error_public']) |
|
611 |
else: |
|
612 |
# extend create_form to include difficulty levels |
|
613 |
params['mentor_form'] = self._getTagsForProgram( |
|
614 |
'base_create_form', params, **kwargs) |
|
615 |
entity = None |
|
616 |
||
617 |
context = helper.responses.getUniversalContext(request) |
|
618 |
helper.responses.useJavaScript(context, params['js_uses_all']) |
|
619 |
context['page_name'] = page_name |
|
620 |
||
621 |
if request.method == 'POST': |
|
622 |
return self.suggestTaskPost(request, context, |
|
623 |
params, entity) |
|
624 |
else: |
|
625 |
return self.suggestTaskGet(request, context, |
|
626 |
params, entity, **kwargs) |
|
627 |
||
628 |
def suggestTaskPost(self, request, context, params, entity): |
|
629 |
"""Handles the POST request for the suggest task view. |
|
630 |
""" |
|
631 |
||
632 |
form = params['mentor_form'](request.POST) |
|
633 |
||
634 |
if not form.is_valid(): |
|
635 |
return self._constructResponse(request, None, context, form, params) |
|
636 |
||
637 |
_, fields = helper.forms.collectCleanedFields(form) |
|
638 |
# TODO: this non-standard view shouldn't call _editPost but its own method |
|
639 |
self._editPost(request, entity, fields) |
|
640 |
||
641 |
logic = params['logic'] |
|
642 |
if entity: |
|
643 |
entity = logic.updateEntityProperties(entity, fields) |
|
644 |
else: |
|
645 |
entity = logic.updateOrCreateFromFields(fields) |
|
646 |
||
647 |
page_params = params['edit_params'] |
|
648 |
||
649 |
redirect = ghop_redirects.getSuggestTaskRedirect( |
|
650 |
entity, params) |
|
651 |
||
652 |
return http.HttpResponseRedirect(redirect) |
|
653 |
||
654 |
def suggestTaskGet(self, request, context, params, entity, **kwargs): |
|
655 |
"""Handles the GET request for the suggest task view. |
|
656 |
""" |
|
657 |
||
658 |
if entity: |
|
659 |
# populate form with the existing entity |
|
660 |
form = params['mentor_form'](instance=entity) |
|
661 |
||
662 |
self._editGet(request, entity, form) |
|
663 |
else: |
|
664 |
form = params['mentor_form']() |
|
665 |
||
666 |
return self._constructResponse(request, entity, context, form, params) |
|
667 |
||
668 |
@decorators.merge_params |
|
669 |
@decorators.check_access |
|
670 |
def listOrgTasks(self, request, access_type, |
|
671 |
page_name=None, params=None, **kwargs): |
|
672 |
"""See base.View.list() |
|
673 |
""" |
|
674 |
if request.method == 'POST': |
|
675 |
return self.listOrgTasksPost(request, params, **kwargs) |
|
676 |
else: # request.method == 'GET' |
|
677 |
return self.listOrgTasksGet(request, page_name, params, **kwargs) |
|
678 |
||
679 |
def listOrgTasksPost(self, request, params, **kwargs): |
|
680 |
"""Handles the POST request for the list tasks view. |
|
681 |
""" |
|
682 |
||
683 |
# update the status of task entities that have been approved |
|
684 |
# and published |
|
685 |
task_entities = [] |
|
686 |
for key_name, published in request.POST.items(): |
|
687 |
task_entity = ghop_task_logic.logic.getFromKeyName(key_name) |
|
688 |
if task_entity: |
|
689 |
task_entity.status = 'Open' |
|
690 |
||
691 |
task_entities.append(task_entity) |
|
692 |
||
693 |
# bulk update the task_entities |
|
694 |
# TODO: Have to be replaced by Task Queue APIs later |
|
695 |
db.put(task_entities) |
|
696 |
||
697 |
# redirect to the same page |
|
698 |
return http.HttpResponseRedirect('') |
|
699 |
||
700 |
def listOrgTasksGet(self, request, page_name, params, **kwargs): |
|
701 |
"""Handles the GET request for the list tasks view. |
|
702 |
""" |
|
703 |
||
704 |
from soc.modules.ghop.views.helper import list_info as list_info_helper |
|
705 |
||
706 |
org_entity = ghop_org_logic.logic.getFromKeyNameOr404( |
|
707 |
kwargs['scope_path']) |
|
708 |
||
709 |
contents = [] |
|
710 |
context = {} |
|
711 |
||
712 |
user_account = user_logic.logic.getForCurrentAccount() |
|
713 |
||
714 |
fields = { |
|
715 |
'user': user_account, |
|
716 |
'scope': org_entity, |
|
717 |
'status': 'active' |
|
718 |
} |
|
719 |
||
720 |
up_params = params.copy() |
|
721 |
# give the capability to approve tasks for the org_admins |
|
722 |
if ghop_org_admin_logic.logic.getForFields(fields, unique=True): |
|
723 |
up_params['list_template'] = 'modules/ghop/task/approve/approve.html' |
|
724 |
up_params['list_heading'] = 'modules/ghop/task/approve/heading.html' |
|
725 |
up_params['list_row'] = 'modules/ghop/task/approve/row.html' |
|
726 |
||
727 |
up_params['list_action'] = (redirects.getPublicRedirect, |
|
728 |
up_params) |
|
729 |
||
730 |
up_params['list_description'] = ugettext( |
|
731 |
'List of Unapproved or Unpublished tasks') |
|
732 |
||
733 |
filter = { |
|
734 |
'scope': org_entity, |
|
735 |
'status': ['Unapproved', 'Unpublished'], |
|
736 |
} |
|
737 |
||
2942
996e64ba2db8
Removed undefined order argument in GHOP Task View.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2893
diff
changeset
|
738 |
up_list = lists.getListContent(request, up_params, filter, idx=0, |
2885 | 739 |
need_content=True) |
740 |
||
741 |
if up_list: |
|
742 |
up_mentors_list = {} |
|
743 |
for task_entity in up_list['data']: |
|
744 |
up_mentors_list[task_entity.key()] = db.get(task_entity.mentors) |
|
745 |
||
746 |
up_list['info'] = (list_info_helper.getTasksInfo(up_mentors_list), None) |
|
747 |
||
748 |
contents.append(up_list) |
|
749 |
context['up_list'] = True |
|
750 |
||
751 |
ap_params = up_params.copy() |
|
752 |
ap_params['list_template'] = 'soc/models/list.html' |
|
753 |
ap_params['list_heading'] = 'modules/ghop/task/list/heading.html' |
|
754 |
ap_params['list_row'] = 'modules/ghop/task/list/row.html' |
|
755 |
||
756 |
ap_params['list_action'] = (redirects.getPublicRedirect, |
|
757 |
ap_params) |
|
758 |
||
759 |
ap_params['list_description'] = ugettext( |
|
760 |
'List of published tasks') |
|
761 |
||
762 |
filter = { |
|
763 |
'scope': org_entity, |
|
764 |
'status': ['Open', 'Reopened', 'ClaimRequested', 'Claimed', |
|
765 |
'ActionNeeded', 'Closed', 'AwaitingRegistration', |
|
766 |
'NeedsWork', 'NeedsReview'], |
|
767 |
} |
|
768 |
||
2942
996e64ba2db8
Removed undefined order argument in GHOP Task View.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2893
diff
changeset
|
769 |
ap_list = lists.getListContent(request, ap_params, filter, idx=1, |
2885 | 770 |
need_content=True) |
771 |
||
772 |
if ap_list: |
|
773 |
ap_mentors_list = {} |
|
774 |
for task_entity in ap_list['data']: |
|
775 |
ap_mentors_list[task_entity.key()] = db.get(task_entity.mentors) |
|
776 |
||
777 |
ap_list['info'] = (list_info_helper.getTasksInfo(ap_mentors_list), None) |
|
778 |
||
779 |
contents.append(ap_list) |
|
780 |
||
781 |
# call the _list method from base to display the list |
|
782 |
return self._list(request, up_params, contents, page_name, context) |
|
783 |
||
784 |
@decorators.merge_params |
|
785 |
@decorators.check_access |
|
786 |
def public(self, request, access_type, |
|
787 |
page_name=None, params=None, **kwargs): |
|
788 |
"""See base.View.public(). |
|
789 |
""" |
|
790 |
||
791 |
# create default template context for use with any templates |
|
792 |
context = helper.responses.getUniversalContext(request) |
|
793 |
helper.responses.useJavaScript(context, params['js_uses_all']) |
|
794 |
context['page_name'] = page_name |
|
795 |
entity = None |
|
796 |
logic = params['logic'] |
|
797 |
||
798 |
try: |
|
799 |
entity, comment_entities, ws_entities = ( |
|
800 |
logic.getFromKeyFieldsWithCWSOr404(kwargs)) |
|
801 |
except out_of_band.Error, error: |
|
802 |
return helper.responses.errorResponse( |
|
803 |
error, request, template=params['error_public'], context=context) |
|
804 |
||
2981
8a99de852dc2
Fixed Task public view to always call the update method.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2978
diff
changeset
|
805 |
# because we are not sure if the Task API has called this for us we do it |
8a99de852dc2
Fixed Task public view to always call the update method.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2978
diff
changeset
|
806 |
entity, comment_entity = ghop_task_logic.logic.updateTaskStatus(entity) |
8a99de852dc2
Fixed Task public view to always call the update method.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2978
diff
changeset
|
807 |
if comment_entity: |
8a99de852dc2
Fixed Task public view to always call the update method.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2978
diff
changeset
|
808 |
comment_entities.append(comment_entity) |
2885 | 809 |
|
810 |
context['entity'] = entity |
|
811 |
context['entity_key_name'] = entity.key().id_or_name() |
|
812 |
context['entity_type'] = params['name'] |
|
813 |
context['entity_type_url'] = params['url_name'] |
|
814 |
||
815 |
user_account = user_logic.logic.getForCurrentAccount() |
|
816 |
||
817 |
# get some entity specific context |
|
818 |
self.updatePublicContext(context, entity, comment_entities, |
|
819 |
ws_entities, user_account, params) |
|
820 |
||
821 |
validation = self._constructActionsList(context, entity, |
|
822 |
user_account, params) |
|
823 |
||
824 |
context = dicts.merge(params['context'], context) |
|
825 |
||
826 |
if request.method == 'POST': |
|
827 |
return self.publicPost(request, context, params, entity, |
|
828 |
user_account, validation, **kwargs) |
|
829 |
else: # request.method == 'GET' |
|
830 |
return self.publicGet(request, context, params, entity, |
|
831 |
user_account, **kwargs) |
|
832 |
||
833 |
def publicPost(self, request, context, params, entity, |
|
834 |
user_account=None, validation=None, **kwargs): |
|
835 |
"""Handles the POST request for the entity's public page. |
|
836 |
||
837 |
Args: |
|
838 |
entity: the task entity |
|
839 |
rest: see base.View.public() |
|
840 |
""" |
|
841 |
||
842 |
from soc.modules.ghop.logic.models import student as ghop_student_logic |
|
843 |
||
844 |
form = params['comment_form'](request.POST) |
|
845 |
||
846 |
if not form.is_valid(): |
|
847 |
template = params['public_template'] |
|
848 |
context['comment_form'] = form |
|
849 |
return self._constructResponse(request, entity, context, |
|
850 |
form, params, template=template) |
|
851 |
||
852 |
_, fields = helper.forms.collectCleanedFields(form) |
|
853 |
||
854 |
changes = [] |
|
855 |
||
856 |
action = fields['action'] |
|
857 |
||
858 |
properties = None |
|
859 |
ws_properties = None |
|
860 |
||
861 |
# TODO: this can be separated into several methods that handle the changes |
|
862 |
if validation == 'claim_request' and action == 'request': |
|
863 |
properties = { |
|
864 |
'status': 'ClaimRequested', |
|
865 |
'user': user_account, |
|
866 |
} |
|
867 |
||
868 |
st_filter = { |
|
869 |
'user': user_account, |
|
870 |
'scope': entity.program, |
|
871 |
'status': 'active' |
|
872 |
} |
|
873 |
student_entity = ghop_student_logic.logic.getForFields( |
|
874 |
st_filter, unique=True) |
|
875 |
||
876 |
if student_entity: |
|
877 |
properties['student'] = student_entity |
|
878 |
||
879 |
changes.extend([ugettext('User-Student'), |
|
880 |
ugettext('Action-Claim Requested'), |
|
881 |
ugettext('Status-%s' % (properties['status'])) |
|
882 |
]) |
|
883 |
elif (validation == 'claim_withdraw' or |
|
884 |
validation == 'needs_review') and action == 'withdraw': |
|
885 |
properties = { |
|
886 |
'user': None, |
|
887 |
'student': None, |
|
888 |
'status': 'Reopened', |
|
889 |
'deadline': None, |
|
890 |
} |
|
891 |
||
892 |
changes.extend([ugettext('User-Student'), |
|
893 |
ugettext('Action-Withdrawn'), |
|
894 |
ugettext('Status-%s' % (properties['status'])) |
|
895 |
]) |
|
896 |
elif validation == 'needs_review' and action == 'needs_review': |
|
897 |
properties = { |
|
898 |
'status': 'NeedsReview', |
|
899 |
} |
|
900 |
||
901 |
changes.extend([ |
|
902 |
ugettext('User-Student'), |
|
903 |
ugettext('Action-Submitted work and Requested for review'), |
|
904 |
ugettext('Status-%s' % (properties['status']))]) |
|
905 |
||
906 |
ws_properties = { |
|
907 |
'parent': entity, |
|
908 |
'link_id': 't%i' % (int(time.time()*100)), |
|
909 |
'scope_path': entity.key().name(), |
|
910 |
'scope': entity.scope, |
|
911 |
'user': user_account, |
|
912 |
'information': fields['comment'], |
|
913 |
'url_to_work': fields['work_submission'], |
|
914 |
'submitted_on': datetime.datetime.now(), |
|
915 |
} |
|
916 |
elif validation == 'accept_claim': |
|
917 |
if action == 'accept': |
|
918 |
deadline = datetime.datetime.now() + datetime.timedelta( |
|
919 |
hours=entity.time_to_complete) |
|
920 |
||
921 |
properties = { |
|
922 |
'status': 'Claimed', |
|
923 |
'deadline': deadline, |
|
924 |
} |
|
925 |
||
926 |
changes.extend([ugettext('User-Mentor'), |
|
927 |
ugettext('Action-Claim Accepted'), |
|
928 |
ugettext('Status-%s' % (properties['status'])) |
|
929 |
]) |
|
930 |
||
931 |
task_update.spawnUpdateTask(entity) |
|
932 |
if action == 'reject': |
|
933 |
properties = { |
|
934 |
'user': None, |
|
935 |
'student': None, |
|
936 |
'status': 'Reopened', |
|
937 |
'deadline': None, |
|
938 |
} |
|
939 |
||
940 |
changes.extend([ugettext('User-Mentor'), |
|
941 |
ugettext('Action-Claim Rejected'), |
|
942 |
ugettext('Status-%s' % (properties['status'])) |
|
943 |
]) |
|
944 |
elif validation == 'close': |
|
945 |
if action == 'needs_work': |
|
946 |
properties = { |
|
947 |
'status': 'NeedsWork', |
|
948 |
} |
|
949 |
||
950 |
changes.extend([ugettext('User-Mentor'), |
|
951 |
ugettext('Action-Requested more work'), |
|
952 |
ugettext('Status-%s' % (properties['status'])) |
|
953 |
]) |
|
954 |
||
955 |
if fields['extended_deadline'] > 0: |
|
956 |
deadline = entity.deadline + datetime.timedelta( |
|
957 |
hours=fields['extended_deadline']) |
|
958 |
||
959 |
properties['deadline'] = deadline |
|
960 |
||
961 |
changes.append(ugettext('DeadlineExtendedBy-%d hrs to %s' % ( |
|
962 |
fields['extended_deadline'], deadline.strftime( |
|
963 |
'%d %B %Y, %H :%M')))) |
|
964 |
||
965 |
task_update.spawnUpdateTask(entity) |
|
966 |
else: |
|
967 |
changes.append(ugettext('NoDeadlineExtensionGiven')) |
|
968 |
elif action == 'reopened': |
|
969 |
properties = { |
|
970 |
'user': None, |
|
971 |
'student': None, |
|
972 |
'status': 'Reopened', |
|
973 |
'deadline': None, |
|
974 |
} |
|
975 |
||
976 |
changes.extend([ugettext('User-Mentor'), |
|
977 |
ugettext('Action-Reopened'), |
|
978 |
ugettext('Status-%s' % (properties['status'])) |
|
979 |
]) |
|
980 |
elif action == 'closed': |
|
981 |
properties = { |
|
982 |
'deadline': None, |
|
983 |
} |
|
984 |
||
985 |
if entity.student: |
|
986 |
properties['status'] = 'Closed' |
|
987 |
else: |
|
988 |
properties['status'] = 'AwaitingRegistration' |
|
989 |
||
990 |
changes.extend([ugettext('User-Mentor'), |
|
991 |
ugettext('Action-Closed the task'), |
|
992 |
ugettext('Status-%s' % (properties['status'])) |
|
993 |
]) |
|
994 |
||
995 |
comment_properties = { |
|
996 |
'parent': entity, |
|
997 |
'scope_path': entity.key().name(), |
|
998 |
'created_by': user_account, |
|
999 |
'changes': changes, |
|
1000 |
} |
|
1001 |
||
1002 |
if ws_properties: |
|
1003 |
comment_properties['content'] = self.DEF_WS_MSG_FMT |
|
1004 |
else: |
|
1005 |
comment_properties['content'] = fields['comment'] |
|
1006 |
||
1007 |
ghop_task_logic.logic.updateEntityPropertiesWithCWS( |
|
1008 |
entity, properties, comment_properties, ws_properties) |
|
1009 |
||
1010 |
# redirect to the same page |
|
1011 |
return http.HttpResponseRedirect('') |
|
1012 |
||
1013 |
def publicGet(self, request, context, params, entity, |
|
1014 |
user_account, **kwargs): |
|
1015 |
"""Handles the GET request for the entity's public page. |
|
1016 |
||
1017 |
Args: |
|
1018 |
entity: the task entity |
|
1019 |
rest see base.View.public() |
|
1020 |
""" |
|
1021 |
||
1022 |
context['comment_form'] = params['comment_form']() |
|
1023 |
||
1024 |
template = params['public_template'] |
|
1025 |
||
1026 |
return responses.respond(request, template, context=context) |
|
1027 |
||
1028 |
def updatePublicContext(self, context, entity, comment_entities, |
|
1029 |
ws_entities, user_account, params): |
|
1030 |
"""Updates the context for the public page with information. |
|
1031 |
||
1032 |
Args: |
|
1033 |
context: the context that should be updated |
|
1034 |
entity: a task used to set context |
|
1035 |
user_account: user entity of the logged in user |
|
1036 |
params: dict with params for the view using this context |
|
1037 |
""" |
|
1038 |
||
1039 |
mentor_entities = db.get(entity.mentors) |
|
1040 |
mentors_str = "" |
|
1041 |
for mentor in mentor_entities: |
|
1042 |
mentors_str += mentor.name() + ", " |
|
1043 |
||
1044 |
if mentors_str: |
|
1045 |
context['mentors_str'] = mentors_str[:-2] |
|
1046 |
else: |
|
1047 |
context['mentors_str'] = "Not Assigned" |
|
1048 |
||
1049 |
context['difficulty_str'] = entity.tags_string(entity.difficulty) |
|
1050 |
||
1051 |
context['task_type_str'] = entity.tags_string(entity.task_type) |
|
1052 |
||
1053 |
if entity.deadline: |
|
1054 |
# TODO: it should be able to abuse Django functionality for this |
|
1055 |
ttc = entity.deadline - datetime.datetime.now() |
|
1056 |
(ttc_min, ttc_hour) = ((ttc.seconds / 60), (ttc.seconds / 3600)) |
|
1057 |
if ttc_min >= 60: |
|
1058 |
ttc_min = ttc_min % 60 |
|
1059 |
if ttc_hour > 1: |
|
1060 |
if ttc_min == 0: |
|
1061 |
ttc_str = '%d hours' % (ttc_hour) |
|
1062 |
else: |
|
1063 |
ttc_str = '%d:%02d hours' % (ttc_hour, ttc_min) |
|
1064 |
if ttc.days == 1: |
|
1065 |
ttc_str = '%d day, %s' % (ttc.days, ttc_str) |
|
1066 |
elif ttc.days > 1: |
|
1067 |
ttc_str = '%d days, %s' % (ttc.days, ttc_str) |
|
1068 |
else: |
|
1069 |
ttc_str = '%d mins' % (ttc_min) |
|
1070 |
context['time_to_complete'] = ttc_str |
|
1071 |
else: |
|
1072 |
if entity.status == 'NeedsReview': |
|
1073 |
context['time_to_complete'] = 'No Time Left' |
|
1074 |
else: |
|
1075 |
context['time_to_complete'] = '%d hours' % (entity.time_to_complete) |
|
1076 |
||
1077 |
context['comments'] = comment_entities |
|
1078 |
||
1079 |
context['work_submissions'] = ws_entities |
|
1080 |
||
1081 |
def _constructActionsList(self, context, entity, |
|
1082 |
user_account, params): |
|
1083 |
"""Constructs a list of actions for the task page and extends |
|
1084 |
the comment form with this list. |
|
1085 |
||
1086 |
This method also returns the validation used by POST method to |
|
1087 |
validate the user input data. |
|
1088 |
||
1089 |
Args: |
|
1090 |
context: the context that should be updated |
|
1091 |
entity: a task used to set context |
|
1092 |
user_account: user entity of the logged in user |
|
1093 |
params: dict with params for the view using this context |
|
1094 |
""" |
|
1095 |
||
1096 |
# variable that holds what kind of validation this user |
|
1097 |
# and task combination pass. |
|
1098 |
validation = None |
|
1099 |
||
1100 |
# The following header messages are shown for non-logged in |
|
1101 |
# general public, logged in public and the student. |
|
1102 |
if entity.status is 'Closed': |
|
1103 |
context['header_msg'] = self.DEF_TASK_CLOSED_MSG |
|
1104 |
validation = 'closed' |
|
1105 |
||
1106 |
if entity.status == 'Open': |
|
1107 |
context['header_msg'] = self.DEF_TASK_OPEN_MSG |
|
1108 |
elif entity.status == 'Reopened': |
|
1109 |
context['header_msg'] = self.DEF_TASK_REOPENED_MSG |
|
1110 |
||
1111 |
if user_account: |
|
1112 |
actions = [('noaction', 'Comment without action')] |
|
1113 |
||
1114 |
# if the user is logged give him the permission to claim |
|
1115 |
# the task only if he none of program host, org admin or mentor |
|
1116 |
filter = { |
|
1117 |
'user': user_account, |
|
1118 |
} |
|
1119 |
||
1120 |
host_entity = host_logic.logic.getForFields(filter, unique=True) |
|
1121 |
||
1122 |
filter['scope_path'] = entity.scope_path |
|
1123 |
org_admin_entity = ghop_org_admin_logic.logic.getForFields( |
|
1124 |
filter, unique=True) |
|
1125 |
mentor_entity = ghop_mentor_logic.logic.getForFields( |
|
1126 |
filter, unique=True) |
|
1127 |
||
1128 |
if host_entity or org_admin_entity or mentor_entity: |
|
1129 |
validation, mentor_actions = self._constructMentorActions( |
|
1130 |
context, entity) |
|
1131 |
actions += mentor_actions |
|
1132 |
if entity.status in ['Unapproved', 'Unpublished', 'Open']: |
|
1133 |
if host_entity or org_admin_entity: |
|
1134 |
context['edit_link'] = redirects.getEditRedirect(entity, params) |
|
1135 |
elif mentor_entity: |
|
1136 |
context['suggest_link'] = ghop_redirects.getSuggestTaskRedirect( |
|
1137 |
entity, params) |
|
1138 |
else: |
|
1139 |
validation, student_actions = self._constructStudentActions( |
|
1140 |
context, entity, user_account) |
|
1141 |
actions += student_actions |
|
1142 |
||
1143 |
# create the difficultly level field containing the choices |
|
1144 |
# defined in the program entity |
|
1145 |
dynafields = [ |
|
1146 |
{'name': 'action', |
|
1147 |
'base': forms.ChoiceField, |
|
1148 |
'label': 'Action', |
|
1149 |
'required': False, |
|
1150 |
'passthrough': ['initial', 'required', 'choices'], |
|
1151 |
'choices': actions, |
|
1152 |
}, |
|
1153 |
] |
|
1154 |
||
1155 |
if validation == 'needs_review': |
|
1156 |
dynafields.append( |
|
1157 |
{'name': 'work_submission', |
|
1158 |
'base': forms.URLField, |
|
1159 |
'label': 'Submit Work', |
|
1160 |
'required': False, |
|
1161 |
'help_text': 'Provide a link to your work in this box. ' |
|
1162 |
'Please use the comment box if you need to explain ' |
|
1163 |
'of your work.', |
|
1164 |
}) |
|
1165 |
||
1166 |
if validation == 'close': |
|
1167 |
dynafields.append( |
|
1168 |
{'name': 'extended_deadline', |
|
1169 |
'base': forms.IntegerField, |
|
1170 |
'min_value': 1, |
|
1171 |
'label': 'Extend deadline by', |
|
1172 |
'required': False, |
|
1173 |
'passthrough': ['min_value', 'required', 'help_text'], |
|
1174 |
'help_text': 'Optional: Specify the number of hours by ' |
|
1175 |
'which you want to extend the deadline for the task ' |
|
1176 |
'for this student. ', |
|
1177 |
}) |
|
1178 |
||
1179 |
dynaproperties = params_helper.getDynaFields(dynafields) |
|
1180 |
if validation == 'needs_review': |
|
1181 |
dynaproperties['clean_work_submission'] = cleaning.clean_url( |
|
1182 |
'work_submission') |
|
1183 |
||
1184 |
extended_comment_form = dynaform.extendDynaForm( |
|
1185 |
dynaform=params['comment_form'], |
|
1186 |
dynaproperties=dynaproperties) |
|
1187 |
||
1188 |
params['comment_form'] = extended_comment_form |
|
1189 |
else: |
|
1190 |
# list of statuses a task can be in after it is requested to be |
|
1191 |
# claimed before closing or re-opening |
|
1192 |
claim_status = ['ClaimRequested', 'Claimed', 'ActionNeeded', |
|
1193 |
'NeedsWork', 'NeedsReview'] |
|
1194 |
if entity.status in claim_status: |
|
1195 |
context['header_msg'] = self.DEF_TASK_CLAIMED_MSG |
|
1196 |
elif entity.status in ['AwaitingRegistration', 'Closed']: |
|
1197 |
context['header_msg'] = self.DEF_TASK_CLOSED_MSG |
|
1198 |
||
1199 |
context['signin_comment_msg'] = self.DEF_SIGNIN_TO_COMMENT_MSG % ( |
|
1200 |
context['sign_in']) |
|
1201 |
||
1202 |
return validation |
|
1203 |
||
1204 |
def _constructMentorActions(self, context, entity): |
|
1205 |
"""Constructs the list of actions for mentors, org admins and |
|
1206 |
hosts. |
|
1207 |
""" |
|
1208 |
||
1209 |
# variable that holds what kind of validation this user |
|
1210 |
# and task combination pass. |
|
1211 |
validation = None |
|
1212 |
||
1213 |
actions = [] |
|
1214 |
||
1215 |
if entity.status in ['Unapproved', 'Unpublished']: |
|
1216 |
context['header_msg'] = self.DEF_TASK_UNPUBLISHED_MSG |
|
1217 |
context['comment_disabled'] = True |
|
1218 |
elif entity.status == 'Open': |
|
1219 |
context['header_msg'] = self.DEF_CAN_EDIT_TASK_MSG |
|
1220 |
elif entity.status == 'Reopened': |
|
1221 |
context['header_msg'] = self.DEF_TASK_MENTOR_REOPENED_MSG |
|
1222 |
elif entity.status == 'ClaimRequested': |
|
1223 |
actions.extend([('accept', 'Accept claim request'), |
|
1224 |
('reject', 'Reject claim request')]) |
|
1225 |
context['header_msg'] = self.DEF_TASK_CLAIM_REQUESTED_MSG |
|
1226 |
validation = 'accept_claim' |
|
1227 |
elif entity.status == 'Claimed': |
|
1228 |
context['header_msg'] = self.DEF_TASK_CLAIMED_BY_STUDENT_MSG |
|
1229 |
elif entity.status == 'NeedsReview': |
|
1230 |
context['header_msg'] = self.DEF_TASK_NEEDS_REVIEW_MSG |
|
1231 |
actions.extend([('needs_work', 'Needs More Work'), |
|
1232 |
('reopened', 'Reopen the task'), |
|
1233 |
('closed', 'Close the task')]) |
|
1234 |
validation = 'close' |
|
1235 |
elif entity.status in ['AwaitingRegistration', 'Closed']: |
|
1236 |
context['header_msg'] = self.DEF_TASK_CLOSED_MSG |
|
1237 |
||
1238 |
return validation, actions |
|
1239 |
||
1240 |
def _constructStudentActions(self, context, entity, user_account): |
|
1241 |
"""Constructs the list of actions for students. |
|
1242 |
""" |
|
1243 |
||
1244 |
# variable that holds what kind of validation this user |
|
1245 |
# and task combination pass. |
|
1246 |
validation = None |
|
1247 |
||
1248 |
actions = [] |
|
1249 |
||
1250 |
if entity.status in ['Open', 'Reopened']: |
|
1251 |
task_filter = { |
|
1252 |
'user': user_account, |
|
1253 |
'status': ['ClaimRequested', 'Claimed', 'ActionNeeded', |
|
1254 |
'NeedsWork', 'NeedsReview'] |
|
1255 |
} |
|
1256 |
task_entities = ghop_task_logic.logic.getForFields(task_filter) |
|
1257 |
||
1258 |
if len(task_entities) >= entity.program.nr_simultaneous_tasks: |
|
1259 |
context['header_msg'] = self.DEF_MAX_TASK_LIMIT_MSG_FMT % ( |
|
1260 |
entity.program.nr_simultaneous_tasks) |
|
1261 |
validation = 'claim_ineligible' |
|
1262 |
return validation, actions |
|
1263 |
||
1264 |
task_filter['status'] = 'AwaitingRegistration' |
|
1265 |
task_entities = ghop_task_logic.logic.getForFields(task_filter) |
|
1266 |
||
1267 |
if task_entities: |
|
1268 |
context['header_msg'] = self.DEF_AWAITING_REG_MSG |
|
1269 |
validation = 'claim_ineligible' |
|
1270 |
else: |
|
1271 |
actions.append(('request', 'Request to claim the task')) |
|
1272 |
validation = 'claim_request' |
|
1273 |
||
1274 |
# TODO: lot of double information here that can be simplified |
|
1275 |
if entity.user and user_account.key() == entity.user.key(): |
|
1276 |
if entity.status == 'ClaimRequested': |
|
1277 |
context['header_msg'] = self.DEF_TASK_REQ_CLAIMED_BY_YOU_MSG |
|
1278 |
actions.append(('withdraw', 'Withdraw from the task')) |
|
1279 |
validation = 'claim_withdraw' |
|
1280 |
elif entity.status in ['Claimed', 'NeedsWork', |
|
1281 |
'NeedsReview', 'ActionNeeded']: |
|
1282 |
context['header_msg'] = self.DEF_TASK_CLAIMED_BY_YOU_MSG |
|
1283 |
actions.extend([ |
|
1284 |
('withdraw', 'Withdraw from the task'), |
|
1285 |
('needs_review', 'Submit work and Request for review')]) |
|
1286 |
validation = 'needs_review' |
|
1287 |
elif entity.status == 'NeedsReview': |
|
1288 |
context['header_msg'] = self.DEF_TASK_NO_MORE_SUBMIT_MSG |
|
1289 |
actions.append(('withdraw', 'Withdraw from the task')) |
|
1290 |
if datetime.datetime.now < entity.deadline: |
|
1291 |
actions.append( |
|
1292 |
('needs_review', 'Submit work and Request for review')) |
|
1293 |
validation = 'needs_review' |
|
1294 |
elif entity.status == 'AwaitingRegistration': |
|
1295 |
context['header_msg'] = self.DEF_STUDENT_SIGNUP_MSG |
|
1296 |
elif entity.status == 'Closed': |
|
1297 |
context['header_msg'] = self.DEF_TASK_CMPLTD_BY_YOU_MSG |
|
1298 |
else: |
|
1299 |
if entity.status in ['ClaimRequested', 'Claimed', |
|
1300 |
'ActionNeeded', 'NeedsWork', |
|
1301 |
'NeedsReview']: |
|
1302 |
context['header_msg'] = self.DEF_TASK_CLAIMED_MSG |
|
1303 |
if entity.status in ['AwaitingRegistration', 'Closed']: |
|
1304 |
context['header_msg'] = self.DEF_TASK_CLOSED_MSG |
|
1305 |
||
1306 |
return validation, actions |
|
1307 |
||
1308 |
@decorators.merge_params |
|
1309 |
@decorators.check_access |
|
1310 |
def search(self, request, access_type, page_name=None, |
|
1311 |
params=None, filter=None, order=None,**kwargs): |
|
1312 |
"""View method to search for GHOP Tasks. |
|
1313 |
||
1314 |
Args: |
|
1315 |
request: the standard Django HTTP request object |
|
1316 |
access_type : the name of the access type which should be checked |
|
1317 |
page_name: the page name displayed in templates as page and header title |
|
1318 |
params: a dict with params for this View |
|
1319 |
kwargs: the Key Fields for the specified entity |
|
1320 |
""" |
|
1321 |
||
1322 |
from soc.modules.ghop.views.helper import list_info as list_info_helper |
|
1323 |
||
1324 |
get_params = request.GET |
|
1325 |
||
1326 |
contents = [] |
|
1327 |
context = {} |
|
1328 |
if not filter: |
|
1329 |
filter = {} |
|
1330 |
||
1331 |
public_status = ['Open', 'Reopened', 'ClaimRequested', 'Claimed', |
|
1332 |
'ActionNeeded', 'Closed', 'AwaitingRegistration', |
|
1333 |
'NeedsWork', 'NeedsReview'] |
|
1334 |
||
1335 |
task_params = params.copy() |
|
1336 |
task_params['list_template'] = 'modules/ghop/task/search/search.html' |
|
1337 |
task_params['list_heading'] = 'modules/ghop/task/search/heading.html' |
|
1338 |
task_params['list_row'] = 'modules/ghop/task/search/row.html' |
|
1339 |
||
1340 |
task_params['list_action'] = (redirects.getPublicRedirect, |
|
1341 |
task_params) |
|
1342 |
||
1343 |
task_params['list_description'] = ugettext( |
|
1344 |
'Search results: ') |
|
1345 |
||
1346 |
program_entity = ghop_program_logic.logic.getFromKeyFields(kwargs) |
|
1347 |
||
1348 |
org_fields = { |
|
1349 |
'scope': program_entity, |
|
1350 |
} |
|
1351 |
||
1352 |
org_entities = ghop_org_logic.logic.getForFields(org_fields) |
|
1353 |
org_names = [] |
|
1354 |
for org in org_entities: |
|
1355 |
org_names.append(org.name) |
|
1356 |
||
1357 |
df_entities = ghop_task_model.TaskDifficultyTag.get_by_scope( |
|
1358 |
program_entity) |
|
1359 |
difficulties = [] |
|
1360 |
for df_entity in df_entities: |
|
1361 |
difficulties.append(df_entity.tag) |
|
1362 |
||
1363 |
tt_entities = ghop_task_model.TaskTypeTag.get_by_scope(program_entity) |
|
1364 |
task_types = [] |
|
1365 |
for tt_entity in tt_entities: |
|
1366 |
task_types.append(tt_entity.tag) |
|
1367 |
||
1368 |
context['org_entities'] = org_names |
|
1369 |
context['public_status'] = public_status |
|
1370 |
context['difficulties'] = difficulties |
|
1371 |
context['tags'] = task_types |
|
1372 |
||
1373 |
org_filter = get_params.getlist('Organization') |
|
1374 |
status_filter = get_params.getlist('Status') |
|
1375 |
df_filter = get_params.getlist('Difficulty') |
|
1376 |
tag_filter = get_params.getlist('Tags') |
|
1377 |
||
1378 |
if org_filter: |
|
1379 |
org_fields = { |
|
1380 |
'scope': program_entity, |
|
1381 |
'name': org_filter, |
|
1382 |
} |
|
1383 |
org_entities = ghop_org_logic.logic.getForFields(org_fields) |
|
1384 |
filter['scope'] = org_entities |
|
1385 |
if status_filter: |
|
1386 |
filter['status']= status_filter |
|
1387 |
else: |
|
1388 |
filter['status'] = public_status |
|
1389 |
if df_filter: |
|
1390 |
filter['difficulty'] = df_filter |
|
1391 |
if tag_filter: |
|
1392 |
filter['task_type'] = tag_filter |
|
1393 |
||
1394 |
filter['program'] = program_entity |
|
1395 |
||
1396 |
task_list = lists.getListContent(request, task_params, filter, |
|
1397 |
order=order, idx=0, need_content=True) |
|
1398 |
||
1399 |
if task_list: |
|
1400 |
contents.append(task_list) |
|
1401 |
||
1402 |
# call the _list method from base to display the list |
|
1403 |
return self._list(request, task_params, contents, page_name, context) |
|
1404 |
||
1405 |
||
1406 |
view = View() |
|
1407 |
||
1408 |
create = decorators.view(view.create) |
|
1409 |
delete = decorators.view(view.delete) |
|
1410 |
edit = decorators.view(view.edit) |
|
1411 |
list = decorators.view(view.list) |
|
1412 |
list_org_tasks = decorators.view(view.listOrgTasks) |
|
1413 |
suggest_task = decorators.view(view.suggestTask) |
|
1414 |
public = decorators.view(view.public) |
|
1415 |
search = decorators.view(view.search) |