author | Lennard de Rijk <ljvderijk@gmail.com> |
Thu, 17 Sep 2009 21:59:30 +0200 | |
changeset 2942 | 996e64ba2db8 |
parent 2893 | 1adc6a815c71 |
child 2978 | 2b3c39483f6f |
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 |
'scope': fields['scope'], |
|
523 |
'status': 'active' |
|
524 |
} |
|
525 |
||
526 |
role_entity = ghop_org_admin_logic.logic.getForFields( |
|
527 |
filter, unique=True) |
|
528 |
||
529 |
if role_entity: |
|
530 |
# this user can publish/approve the task |
|
531 |
if fields.get('approved') and fields.get('published'): |
|
532 |
fields['status'] = 'Open' |
|
533 |
elif not fields.get('approved'): |
|
534 |
fields['status'] = 'Unapproved' |
|
535 |
else: |
|
536 |
fields['status'] = 'Unpublished' |
|
537 |
||
538 |
fields['mentors'] = [] |
|
539 |
if fields.get('mentors_list'): |
|
540 |
||
541 |
for mentor_link_id in fields['mentors_list']: |
|
542 |
properties = { |
|
543 |
'scope_path': fields['scope_path'], |
|
544 |
'link_id': mentor_link_id, |
|
545 |
} |
|
546 |
||
547 |
mentor_entity = ghop_mentor_logic.logic.getFromKeyFields(properties) |
|
548 |
fields['mentors'].append(mentor_entity.key()) |
|
549 |
else: |
|
550 |
role_entity = ghop_mentor_logic.logic.getForFields( |
|
551 |
filter, unique=True) |
|
552 |
if not entity: |
|
553 |
# creating a new task |
|
554 |
fields['status'] = 'Unapproved' |
|
555 |
||
556 |
# explicitly change the last_modified_on since the content has been edited |
|
557 |
fields['modified_on'] = datetime.datetime.now() |
|
558 |
||
559 |
if not entity: |
|
560 |
fields['link_id'] = 't%i' % (int(time.time()*100)) |
|
561 |
fields['modified_by'] = role_entity |
|
562 |
fields['created_by'] = role_entity |
|
563 |
fields['created_on'] = datetime.datetime.now() |
|
564 |
else: |
|
565 |
fields['link_id'] = entity.link_id |
|
566 |
fields['modified_by'] = role_entity |
|
567 |
if 'status' not in fields: |
|
568 |
fields['status'] = entity.status |
|
569 |
||
570 |
fields['difficulty'] = { |
|
571 |
'tags': fields['difficulty'], |
|
572 |
'scope': program_entity |
|
573 |
} |
|
574 |
||
575 |
fields['task_type'] = { |
|
576 |
'tags': fields['type_tags'], |
|
577 |
'scope': program_entity |
|
578 |
} |
|
579 |
||
580 |
fields['arbit_tag'] = { |
|
581 |
'tags': fields['arbit_tags'], |
|
582 |
'scope': program_entity |
|
583 |
} |
|
584 |
||
585 |
return |
|
586 |
||
587 |
@decorators.merge_params |
|
588 |
@decorators.check_access |
|
589 |
def suggestTask(self, request, access_type, page_name=None, |
|
590 |
params=None, **kwargs): |
|
591 |
"""View used to allow mentors to create or edit a task. |
|
592 |
||
593 |
Tasks created by mentors must be approved by org admins |
|
594 |
before they are published. |
|
595 |
""" |
|
596 |
||
597 |
params = dicts.merge(params, self._params) |
|
598 |
||
599 |
if 'link_id' in kwargs: |
|
600 |
# extend create_form to include difficulty levels |
|
601 |
params['mentor_form'] = self._getTagsForProgram( |
|
602 |
'base_edit_form', params, **kwargs) |
|
603 |
try: |
|
604 |
entity = self._logic.getFromKeyFieldsOr404(kwargs) |
|
605 |
except out_of_band.Error, error: |
|
606 |
return helper.responses.errorResponse( |
|
607 |
error, request, template=params['error_public']) |
|
608 |
else: |
|
609 |
# extend create_form to include difficulty levels |
|
610 |
params['mentor_form'] = self._getTagsForProgram( |
|
611 |
'base_create_form', params, **kwargs) |
|
612 |
entity = None |
|
613 |
||
614 |
context = helper.responses.getUniversalContext(request) |
|
615 |
helper.responses.useJavaScript(context, params['js_uses_all']) |
|
616 |
context['page_name'] = page_name |
|
617 |
||
618 |
if request.method == 'POST': |
|
619 |
return self.suggestTaskPost(request, context, |
|
620 |
params, entity) |
|
621 |
else: |
|
622 |
return self.suggestTaskGet(request, context, |
|
623 |
params, entity, **kwargs) |
|
624 |
||
625 |
def suggestTaskPost(self, request, context, params, entity): |
|
626 |
"""Handles the POST request for the suggest task view. |
|
627 |
""" |
|
628 |
||
629 |
form = params['mentor_form'](request.POST) |
|
630 |
||
631 |
if not form.is_valid(): |
|
632 |
return self._constructResponse(request, None, context, form, params) |
|
633 |
||
634 |
_, fields = helper.forms.collectCleanedFields(form) |
|
635 |
# TODO: this non-standard view shouldn't call _editPost but its own method |
|
636 |
self._editPost(request, entity, fields) |
|
637 |
||
638 |
logic = params['logic'] |
|
639 |
if entity: |
|
640 |
entity = logic.updateEntityProperties(entity, fields) |
|
641 |
else: |
|
642 |
entity = logic.updateOrCreateFromFields(fields) |
|
643 |
||
644 |
page_params = params['edit_params'] |
|
645 |
||
646 |
redirect = ghop_redirects.getSuggestTaskRedirect( |
|
647 |
entity, params) |
|
648 |
||
649 |
return http.HttpResponseRedirect(redirect) |
|
650 |
||
651 |
def suggestTaskGet(self, request, context, params, entity, **kwargs): |
|
652 |
"""Handles the GET request for the suggest task view. |
|
653 |
""" |
|
654 |
||
655 |
if entity: |
|
656 |
# populate form with the existing entity |
|
657 |
form = params['mentor_form'](instance=entity) |
|
658 |
||
659 |
self._editGet(request, entity, form) |
|
660 |
else: |
|
661 |
form = params['mentor_form']() |
|
662 |
||
663 |
return self._constructResponse(request, entity, context, form, params) |
|
664 |
||
665 |
@decorators.merge_params |
|
666 |
@decorators.check_access |
|
667 |
def listOrgTasks(self, request, access_type, |
|
668 |
page_name=None, params=None, **kwargs): |
|
669 |
"""See base.View.list() |
|
670 |
""" |
|
671 |
if request.method == 'POST': |
|
672 |
return self.listOrgTasksPost(request, params, **kwargs) |
|
673 |
else: # request.method == 'GET' |
|
674 |
return self.listOrgTasksGet(request, page_name, params, **kwargs) |
|
675 |
||
676 |
def listOrgTasksPost(self, request, params, **kwargs): |
|
677 |
"""Handles the POST request for the list tasks view. |
|
678 |
""" |
|
679 |
||
680 |
# update the status of task entities that have been approved |
|
681 |
# and published |
|
682 |
task_entities = [] |
|
683 |
for key_name, published in request.POST.items(): |
|
684 |
task_entity = ghop_task_logic.logic.getFromKeyName(key_name) |
|
685 |
if task_entity: |
|
686 |
task_entity.status = 'Open' |
|
687 |
||
688 |
task_entities.append(task_entity) |
|
689 |
||
690 |
# bulk update the task_entities |
|
691 |
# TODO: Have to be replaced by Task Queue APIs later |
|
692 |
db.put(task_entities) |
|
693 |
||
694 |
# redirect to the same page |
|
695 |
return http.HttpResponseRedirect('') |
|
696 |
||
697 |
def listOrgTasksGet(self, request, page_name, params, **kwargs): |
|
698 |
"""Handles the GET request for the list tasks view. |
|
699 |
""" |
|
700 |
||
701 |
from soc.modules.ghop.views.helper import list_info as list_info_helper |
|
702 |
||
703 |
org_entity = ghop_org_logic.logic.getFromKeyNameOr404( |
|
704 |
kwargs['scope_path']) |
|
705 |
||
706 |
contents = [] |
|
707 |
context = {} |
|
708 |
||
709 |
user_account = user_logic.logic.getForCurrentAccount() |
|
710 |
||
711 |
fields = { |
|
712 |
'user': user_account, |
|
713 |
'scope': org_entity, |
|
714 |
'status': 'active' |
|
715 |
} |
|
716 |
||
717 |
up_params = params.copy() |
|
718 |
# give the capability to approve tasks for the org_admins |
|
719 |
if ghop_org_admin_logic.logic.getForFields(fields, unique=True): |
|
720 |
up_params['list_template'] = 'modules/ghop/task/approve/approve.html' |
|
721 |
up_params['list_heading'] = 'modules/ghop/task/approve/heading.html' |
|
722 |
up_params['list_row'] = 'modules/ghop/task/approve/row.html' |
|
723 |
||
724 |
up_params['list_action'] = (redirects.getPublicRedirect, |
|
725 |
up_params) |
|
726 |
||
727 |
up_params['list_description'] = ugettext( |
|
728 |
'List of Unapproved or Unpublished tasks') |
|
729 |
||
730 |
filter = { |
|
731 |
'scope': org_entity, |
|
732 |
'status': ['Unapproved', 'Unpublished'], |
|
733 |
} |
|
734 |
||
2942
996e64ba2db8
Removed undefined order argument in GHOP Task View.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2893
diff
changeset
|
735 |
up_list = lists.getListContent(request, up_params, filter, idx=0, |
2885 | 736 |
need_content=True) |
737 |
||
738 |
if up_list: |
|
739 |
up_mentors_list = {} |
|
740 |
for task_entity in up_list['data']: |
|
741 |
up_mentors_list[task_entity.key()] = db.get(task_entity.mentors) |
|
742 |
||
743 |
up_list['info'] = (list_info_helper.getTasksInfo(up_mentors_list), None) |
|
744 |
||
745 |
contents.append(up_list) |
|
746 |
context['up_list'] = True |
|
747 |
||
748 |
ap_params = up_params.copy() |
|
749 |
ap_params['list_template'] = 'soc/models/list.html' |
|
750 |
ap_params['list_heading'] = 'modules/ghop/task/list/heading.html' |
|
751 |
ap_params['list_row'] = 'modules/ghop/task/list/row.html' |
|
752 |
||
753 |
ap_params['list_action'] = (redirects.getPublicRedirect, |
|
754 |
ap_params) |
|
755 |
||
756 |
ap_params['list_description'] = ugettext( |
|
757 |
'List of published tasks') |
|
758 |
||
759 |
filter = { |
|
760 |
'scope': org_entity, |
|
761 |
'status': ['Open', 'Reopened', 'ClaimRequested', 'Claimed', |
|
762 |
'ActionNeeded', 'Closed', 'AwaitingRegistration', |
|
763 |
'NeedsWork', 'NeedsReview'], |
|
764 |
} |
|
765 |
||
2942
996e64ba2db8
Removed undefined order argument in GHOP Task View.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
2893
diff
changeset
|
766 |
ap_list = lists.getListContent(request, ap_params, filter, idx=1, |
2885 | 767 |
need_content=True) |
768 |
||
769 |
if ap_list: |
|
770 |
ap_mentors_list = {} |
|
771 |
for task_entity in ap_list['data']: |
|
772 |
ap_mentors_list[task_entity.key()] = db.get(task_entity.mentors) |
|
773 |
||
774 |
ap_list['info'] = (list_info_helper.getTasksInfo(ap_mentors_list), None) |
|
775 |
||
776 |
contents.append(ap_list) |
|
777 |
||
778 |
# call the _list method from base to display the list |
|
779 |
return self._list(request, up_params, contents, page_name, context) |
|
780 |
||
781 |
@decorators.merge_params |
|
782 |
@decorators.check_access |
|
783 |
def public(self, request, access_type, |
|
784 |
page_name=None, params=None, **kwargs): |
|
785 |
"""See base.View.public(). |
|
786 |
""" |
|
787 |
||
788 |
# create default template context for use with any templates |
|
789 |
context = helper.responses.getUniversalContext(request) |
|
790 |
helper.responses.useJavaScript(context, params['js_uses_all']) |
|
791 |
context['page_name'] = page_name |
|
792 |
entity = None |
|
793 |
logic = params['logic'] |
|
794 |
||
795 |
try: |
|
796 |
entity, comment_entities, ws_entities = ( |
|
797 |
logic.getFromKeyFieldsWithCWSOr404(kwargs)) |
|
798 |
except out_of_band.Error, error: |
|
799 |
return helper.responses.errorResponse( |
|
800 |
error, request, template=params['error_public'], context=context) |
|
801 |
||
802 |
if entity.status in ['Claimed', 'NeedsReview', |
|
803 |
'ActionNeeded', 'NeedsWork']: |
|
804 |
entity, comment_entity, ws_entity = ( |
|
805 |
ghop_task_logic.logic.updateTaskStatus(entity)) |
|
806 |
if comment_entity: |
|
807 |
comment_entities.append(comment_entity) |
|
808 |
if ws_entity: |
|
809 |
ws_entities.append(ws_entity) |
|
810 |
||
811 |
context['entity'] = entity |
|
812 |
context['entity_key_name'] = entity.key().id_or_name() |
|
813 |
context['entity_type'] = params['name'] |
|
814 |
context['entity_type_url'] = params['url_name'] |
|
815 |
||
816 |
user_account = user_logic.logic.getForCurrentAccount() |
|
817 |
||
818 |
# get some entity specific context |
|
819 |
self.updatePublicContext(context, entity, comment_entities, |
|
820 |
ws_entities, user_account, params) |
|
821 |
||
822 |
validation = self._constructActionsList(context, entity, |
|
823 |
user_account, params) |
|
824 |
||
825 |
context = dicts.merge(params['context'], context) |
|
826 |
||
827 |
if request.method == 'POST': |
|
828 |
return self.publicPost(request, context, params, entity, |
|
829 |
user_account, validation, **kwargs) |
|
830 |
else: # request.method == 'GET' |
|
831 |
return self.publicGet(request, context, params, entity, |
|
832 |
user_account, **kwargs) |
|
833 |
||
834 |
def publicPost(self, request, context, params, entity, |
|
835 |
user_account=None, validation=None, **kwargs): |
|
836 |
"""Handles the POST request for the entity's public page. |
|
837 |
||
838 |
Args: |
|
839 |
entity: the task entity |
|
840 |
rest: see base.View.public() |
|
841 |
""" |
|
842 |
||
843 |
from soc.modules.ghop.logic.models import student as ghop_student_logic |
|
844 |
||
845 |
form = params['comment_form'](request.POST) |
|
846 |
||
847 |
if not form.is_valid(): |
|
848 |
template = params['public_template'] |
|
849 |
context['comment_form'] = form |
|
850 |
return self._constructResponse(request, entity, context, |
|
851 |
form, params, template=template) |
|
852 |
||
853 |
_, fields = helper.forms.collectCleanedFields(form) |
|
854 |
||
855 |
changes = [] |
|
856 |
||
857 |
action = fields['action'] |
|
858 |
||
859 |
properties = None |
|
860 |
ws_properties = None |
|
861 |
||
862 |
# TODO: this can be separated into several methods that handle the changes |
|
863 |
if validation == 'claim_request' and action == 'request': |
|
864 |
deadline = datetime.datetime.now() + datetime.timedelta( |
|
865 |
hours=entity.time_to_complete) |
|
866 |
||
867 |
properties = { |
|
868 |
'status': 'ClaimRequested', |
|
869 |
'user': user_account, |
|
870 |
'deadline': deadline, |
|
871 |
} |
|
872 |
||
873 |
st_filter = { |
|
874 |
'user': user_account, |
|
875 |
'scope': entity.program, |
|
876 |
'status': 'active' |
|
877 |
} |
|
878 |
student_entity = ghop_student_logic.logic.getForFields( |
|
879 |
st_filter, unique=True) |
|
880 |
||
881 |
if student_entity: |
|
882 |
properties['student'] = student_entity |
|
883 |
||
884 |
changes.extend([ugettext('User-Student'), |
|
885 |
ugettext('Action-Claim Requested'), |
|
886 |
ugettext('Status-%s' % (properties['status'])) |
|
887 |
]) |
|
888 |
elif (validation == 'claim_withdraw' or |
|
889 |
validation == 'needs_review') and action == 'withdraw': |
|
890 |
properties = { |
|
891 |
'user': None, |
|
892 |
'student': None, |
|
893 |
'status': 'Reopened', |
|
894 |
'deadline': None, |
|
895 |
} |
|
896 |
||
897 |
changes.extend([ugettext('User-Student'), |
|
898 |
ugettext('Action-Withdrawn'), |
|
899 |
ugettext('Status-%s' % (properties['status'])) |
|
900 |
]) |
|
901 |
elif validation == 'needs_review' and action == 'needs_review': |
|
902 |
properties = { |
|
903 |
'status': 'NeedsReview', |
|
904 |
} |
|
905 |
||
906 |
changes.extend([ |
|
907 |
ugettext('User-Student'), |
|
908 |
ugettext('Action-Submitted work and Requested for review'), |
|
909 |
ugettext('Status-%s' % (properties['status']))]) |
|
910 |
||
911 |
ws_properties = { |
|
912 |
'parent': entity, |
|
913 |
'link_id': 't%i' % (int(time.time()*100)), |
|
914 |
'scope_path': entity.key().name(), |
|
915 |
'scope': entity.scope, |
|
916 |
'user': user_account, |
|
917 |
'information': fields['comment'], |
|
918 |
'url_to_work': fields['work_submission'], |
|
919 |
'submitted_on': datetime.datetime.now(), |
|
920 |
} |
|
921 |
elif validation == 'accept_claim': |
|
922 |
if action == 'accept': |
|
923 |
deadline = datetime.datetime.now() + datetime.timedelta( |
|
924 |
hours=entity.time_to_complete) |
|
925 |
||
926 |
properties = { |
|
927 |
'status': 'Claimed', |
|
928 |
'deadline': deadline, |
|
929 |
} |
|
930 |
||
931 |
changes.extend([ugettext('User-Mentor'), |
|
932 |
ugettext('Action-Claim Accepted'), |
|
933 |
ugettext('Status-%s' % (properties['status'])) |
|
934 |
]) |
|
935 |
||
936 |
task_update.spawnUpdateTask(entity) |
|
937 |
if action == 'reject': |
|
938 |
properties = { |
|
939 |
'user': None, |
|
940 |
'student': None, |
|
941 |
'status': 'Reopened', |
|
942 |
'deadline': None, |
|
943 |
} |
|
944 |
||
945 |
changes.extend([ugettext('User-Mentor'), |
|
946 |
ugettext('Action-Claim Rejected'), |
|
947 |
ugettext('Status-%s' % (properties['status'])) |
|
948 |
]) |
|
949 |
elif validation == 'close': |
|
950 |
if action == 'needs_work': |
|
951 |
properties = { |
|
952 |
'status': 'NeedsWork', |
|
953 |
} |
|
954 |
||
955 |
changes.extend([ugettext('User-Mentor'), |
|
956 |
ugettext('Action-Requested more work'), |
|
957 |
ugettext('Status-%s' % (properties['status'])) |
|
958 |
]) |
|
959 |
||
960 |
if fields['extended_deadline'] > 0: |
|
961 |
deadline = entity.deadline + datetime.timedelta( |
|
962 |
hours=fields['extended_deadline']) |
|
963 |
||
964 |
properties['deadline'] = deadline |
|
965 |
||
966 |
changes.append(ugettext('DeadlineExtendedBy-%d hrs to %s' % ( |
|
967 |
fields['extended_deadline'], deadline.strftime( |
|
968 |
'%d %B %Y, %H :%M')))) |
|
969 |
||
970 |
task_update.spawnUpdateTask(entity) |
|
971 |
else: |
|
972 |
changes.append(ugettext('NoDeadlineExtensionGiven')) |
|
973 |
elif action == 'reopened': |
|
974 |
properties = { |
|
975 |
'user': None, |
|
976 |
'student': None, |
|
977 |
'status': 'Reopened', |
|
978 |
'deadline': None, |
|
979 |
} |
|
980 |
||
981 |
changes.extend([ugettext('User-Mentor'), |
|
982 |
ugettext('Action-Reopened'), |
|
983 |
ugettext('Status-%s' % (properties['status'])) |
|
984 |
]) |
|
985 |
elif action == 'closed': |
|
986 |
properties = { |
|
987 |
'deadline': None, |
|
988 |
} |
|
989 |
||
990 |
if entity.student: |
|
991 |
properties['status'] = 'Closed' |
|
992 |
else: |
|
993 |
properties['status'] = 'AwaitingRegistration' |
|
994 |
||
995 |
changes.extend([ugettext('User-Mentor'), |
|
996 |
ugettext('Action-Closed the task'), |
|
997 |
ugettext('Status-%s' % (properties['status'])) |
|
998 |
]) |
|
999 |
||
1000 |
comment_properties = { |
|
1001 |
'parent': entity, |
|
1002 |
'scope_path': entity.key().name(), |
|
1003 |
'created_by': user_account, |
|
1004 |
'changes': changes, |
|
1005 |
} |
|
1006 |
||
1007 |
if ws_properties: |
|
1008 |
comment_properties['content'] = self.DEF_WS_MSG_FMT |
|
1009 |
else: |
|
1010 |
comment_properties['content'] = fields['comment'] |
|
1011 |
||
1012 |
ghop_task_logic.logic.updateEntityPropertiesWithCWS( |
|
1013 |
entity, properties, comment_properties, ws_properties) |
|
1014 |
||
1015 |
# redirect to the same page |
|
1016 |
return http.HttpResponseRedirect('') |
|
1017 |
||
1018 |
def publicGet(self, request, context, params, entity, |
|
1019 |
user_account, **kwargs): |
|
1020 |
"""Handles the GET request for the entity's public page. |
|
1021 |
||
1022 |
Args: |
|
1023 |
entity: the task entity |
|
1024 |
rest see base.View.public() |
|
1025 |
""" |
|
1026 |
||
1027 |
context['comment_form'] = params['comment_form']() |
|
1028 |
||
1029 |
template = params['public_template'] |
|
1030 |
||
1031 |
return responses.respond(request, template, context=context) |
|
1032 |
||
1033 |
def updatePublicContext(self, context, entity, comment_entities, |
|
1034 |
ws_entities, user_account, params): |
|
1035 |
"""Updates the context for the public page with information. |
|
1036 |
||
1037 |
Args: |
|
1038 |
context: the context that should be updated |
|
1039 |
entity: a task used to set context |
|
1040 |
user_account: user entity of the logged in user |
|
1041 |
params: dict with params for the view using this context |
|
1042 |
""" |
|
1043 |
||
1044 |
mentor_entities = db.get(entity.mentors) |
|
1045 |
mentors_str = "" |
|
1046 |
for mentor in mentor_entities: |
|
1047 |
mentors_str += mentor.name() + ", " |
|
1048 |
||
1049 |
if mentors_str: |
|
1050 |
context['mentors_str'] = mentors_str[:-2] |
|
1051 |
else: |
|
1052 |
context['mentors_str'] = "Not Assigned" |
|
1053 |
||
1054 |
context['difficulty_str'] = entity.tags_string(entity.difficulty) |
|
1055 |
||
1056 |
context['task_type_str'] = entity.tags_string(entity.task_type) |
|
1057 |
||
1058 |
if entity.deadline: |
|
1059 |
# TODO: it should be able to abuse Django functionality for this |
|
1060 |
ttc = entity.deadline - datetime.datetime.now() |
|
1061 |
(ttc_min, ttc_hour) = ((ttc.seconds / 60), (ttc.seconds / 3600)) |
|
1062 |
if ttc_min >= 60: |
|
1063 |
ttc_min = ttc_min % 60 |
|
1064 |
if ttc_hour > 1: |
|
1065 |
if ttc_min == 0: |
|
1066 |
ttc_str = '%d hours' % (ttc_hour) |
|
1067 |
else: |
|
1068 |
ttc_str = '%d:%02d hours' % (ttc_hour, ttc_min) |
|
1069 |
if ttc.days == 1: |
|
1070 |
ttc_str = '%d day, %s' % (ttc.days, ttc_str) |
|
1071 |
elif ttc.days > 1: |
|
1072 |
ttc_str = '%d days, %s' % (ttc.days, ttc_str) |
|
1073 |
else: |
|
1074 |
ttc_str = '%d mins' % (ttc_min) |
|
1075 |
context['time_to_complete'] = ttc_str |
|
1076 |
else: |
|
1077 |
if entity.status == 'NeedsReview': |
|
1078 |
context['time_to_complete'] = 'No Time Left' |
|
1079 |
else: |
|
1080 |
context['time_to_complete'] = '%d hours' % (entity.time_to_complete) |
|
1081 |
||
1082 |
context['comments'] = comment_entities |
|
1083 |
||
1084 |
context['work_submissions'] = ws_entities |
|
1085 |
||
1086 |
def _constructActionsList(self, context, entity, |
|
1087 |
user_account, params): |
|
1088 |
"""Constructs a list of actions for the task page and extends |
|
1089 |
the comment form with this list. |
|
1090 |
||
1091 |
This method also returns the validation used by POST method to |
|
1092 |
validate the user input data. |
|
1093 |
||
1094 |
Args: |
|
1095 |
context: the context that should be updated |
|
1096 |
entity: a task used to set context |
|
1097 |
user_account: user entity of the logged in user |
|
1098 |
params: dict with params for the view using this context |
|
1099 |
""" |
|
1100 |
||
1101 |
# variable that holds what kind of validation this user |
|
1102 |
# and task combination pass. |
|
1103 |
validation = None |
|
1104 |
||
1105 |
# The following header messages are shown for non-logged in |
|
1106 |
# general public, logged in public and the student. |
|
1107 |
if entity.status is 'Closed': |
|
1108 |
context['header_msg'] = self.DEF_TASK_CLOSED_MSG |
|
1109 |
validation = 'closed' |
|
1110 |
||
1111 |
if entity.status == 'Open': |
|
1112 |
context['header_msg'] = self.DEF_TASK_OPEN_MSG |
|
1113 |
elif entity.status == 'Reopened': |
|
1114 |
context['header_msg'] = self.DEF_TASK_REOPENED_MSG |
|
1115 |
||
1116 |
if user_account: |
|
1117 |
actions = [('noaction', 'Comment without action')] |
|
1118 |
||
1119 |
# if the user is logged give him the permission to claim |
|
1120 |
# the task only if he none of program host, org admin or mentor |
|
1121 |
filter = { |
|
1122 |
'user': user_account, |
|
1123 |
} |
|
1124 |
||
1125 |
host_entity = host_logic.logic.getForFields(filter, unique=True) |
|
1126 |
||
1127 |
filter['scope_path'] = entity.scope_path |
|
1128 |
org_admin_entity = ghop_org_admin_logic.logic.getForFields( |
|
1129 |
filter, unique=True) |
|
1130 |
mentor_entity = ghop_mentor_logic.logic.getForFields( |
|
1131 |
filter, unique=True) |
|
1132 |
||
1133 |
if host_entity or org_admin_entity or mentor_entity: |
|
1134 |
validation, mentor_actions = self._constructMentorActions( |
|
1135 |
context, entity) |
|
1136 |
actions += mentor_actions |
|
1137 |
if entity.status in ['Unapproved', 'Unpublished', 'Open']: |
|
1138 |
if host_entity or org_admin_entity: |
|
1139 |
context['edit_link'] = redirects.getEditRedirect(entity, params) |
|
1140 |
elif mentor_entity: |
|
1141 |
context['suggest_link'] = ghop_redirects.getSuggestTaskRedirect( |
|
1142 |
entity, params) |
|
1143 |
else: |
|
1144 |
validation, student_actions = self._constructStudentActions( |
|
1145 |
context, entity, user_account) |
|
1146 |
actions += student_actions |
|
1147 |
||
1148 |
# create the difficultly level field containing the choices |
|
1149 |
# defined in the program entity |
|
1150 |
dynafields = [ |
|
1151 |
{'name': 'action', |
|
1152 |
'base': forms.ChoiceField, |
|
1153 |
'label': 'Action', |
|
1154 |
'required': False, |
|
1155 |
'passthrough': ['initial', 'required', 'choices'], |
|
1156 |
'choices': actions, |
|
1157 |
}, |
|
1158 |
] |
|
1159 |
||
1160 |
if validation == 'needs_review': |
|
1161 |
dynafields.append( |
|
1162 |
{'name': 'work_submission', |
|
1163 |
'base': forms.URLField, |
|
1164 |
'label': 'Submit Work', |
|
1165 |
'required': False, |
|
1166 |
'help_text': 'Provide a link to your work in this box. ' |
|
1167 |
'Please use the comment box if you need to explain ' |
|
1168 |
'of your work.', |
|
1169 |
}) |
|
1170 |
||
1171 |
if validation == 'close': |
|
1172 |
dynafields.append( |
|
1173 |
{'name': 'extended_deadline', |
|
1174 |
'base': forms.IntegerField, |
|
1175 |
'min_value': 1, |
|
1176 |
'label': 'Extend deadline by', |
|
1177 |
'required': False, |
|
1178 |
'passthrough': ['min_value', 'required', 'help_text'], |
|
1179 |
'help_text': 'Optional: Specify the number of hours by ' |
|
1180 |
'which you want to extend the deadline for the task ' |
|
1181 |
'for this student. ', |
|
1182 |
}) |
|
1183 |
||
1184 |
dynaproperties = params_helper.getDynaFields(dynafields) |
|
1185 |
if validation == 'needs_review': |
|
1186 |
dynaproperties['clean_work_submission'] = cleaning.clean_url( |
|
1187 |
'work_submission') |
|
1188 |
||
1189 |
extended_comment_form = dynaform.extendDynaForm( |
|
1190 |
dynaform=params['comment_form'], |
|
1191 |
dynaproperties=dynaproperties) |
|
1192 |
||
1193 |
params['comment_form'] = extended_comment_form |
|
1194 |
else: |
|
1195 |
# list of statuses a task can be in after it is requested to be |
|
1196 |
# claimed before closing or re-opening |
|
1197 |
claim_status = ['ClaimRequested', 'Claimed', 'ActionNeeded', |
|
1198 |
'NeedsWork', 'NeedsReview'] |
|
1199 |
if entity.status in claim_status: |
|
1200 |
context['header_msg'] = self.DEF_TASK_CLAIMED_MSG |
|
1201 |
elif entity.status in ['AwaitingRegistration', 'Closed']: |
|
1202 |
context['header_msg'] = self.DEF_TASK_CLOSED_MSG |
|
1203 |
||
1204 |
context['signin_comment_msg'] = self.DEF_SIGNIN_TO_COMMENT_MSG % ( |
|
1205 |
context['sign_in']) |
|
1206 |
||
1207 |
return validation |
|
1208 |
||
1209 |
def _constructMentorActions(self, context, entity): |
|
1210 |
"""Constructs the list of actions for mentors, org admins and |
|
1211 |
hosts. |
|
1212 |
""" |
|
1213 |
||
1214 |
# variable that holds what kind of validation this user |
|
1215 |
# and task combination pass. |
|
1216 |
validation = None |
|
1217 |
||
1218 |
actions = [] |
|
1219 |
||
1220 |
if entity.status in ['Unapproved', 'Unpublished']: |
|
1221 |
context['header_msg'] = self.DEF_TASK_UNPUBLISHED_MSG |
|
1222 |
context['comment_disabled'] = True |
|
1223 |
elif entity.status == 'Open': |
|
1224 |
context['header_msg'] = self.DEF_CAN_EDIT_TASK_MSG |
|
1225 |
elif entity.status == 'Reopened': |
|
1226 |
context['header_msg'] = self.DEF_TASK_MENTOR_REOPENED_MSG |
|
1227 |
elif entity.status == 'ClaimRequested': |
|
1228 |
actions.extend([('accept', 'Accept claim request'), |
|
1229 |
('reject', 'Reject claim request')]) |
|
1230 |
context['header_msg'] = self.DEF_TASK_CLAIM_REQUESTED_MSG |
|
1231 |
validation = 'accept_claim' |
|
1232 |
elif entity.status == 'Claimed': |
|
1233 |
context['header_msg'] = self.DEF_TASK_CLAIMED_BY_STUDENT_MSG |
|
1234 |
elif entity.status == 'NeedsReview': |
|
1235 |
context['header_msg'] = self.DEF_TASK_NEEDS_REVIEW_MSG |
|
1236 |
actions.extend([('needs_work', 'Needs More Work'), |
|
1237 |
('reopened', 'Reopen the task'), |
|
1238 |
('closed', 'Close the task')]) |
|
1239 |
validation = 'close' |
|
1240 |
elif entity.status in ['AwaitingRegistration', 'Closed']: |
|
1241 |
context['header_msg'] = self.DEF_TASK_CLOSED_MSG |
|
1242 |
||
1243 |
return validation, actions |
|
1244 |
||
1245 |
def _constructStudentActions(self, context, entity, user_account): |
|
1246 |
"""Constructs the list of actions for students. |
|
1247 |
""" |
|
1248 |
||
1249 |
# variable that holds what kind of validation this user |
|
1250 |
# and task combination pass. |
|
1251 |
validation = None |
|
1252 |
||
1253 |
actions = [] |
|
1254 |
||
1255 |
if entity.status in ['Open', 'Reopened']: |
|
1256 |
task_filter = { |
|
1257 |
'user': user_account, |
|
1258 |
'status': ['ClaimRequested', 'Claimed', 'ActionNeeded', |
|
1259 |
'NeedsWork', 'NeedsReview'] |
|
1260 |
} |
|
1261 |
task_entities = ghop_task_logic.logic.getForFields(task_filter) |
|
1262 |
||
1263 |
if len(task_entities) >= entity.program.nr_simultaneous_tasks: |
|
1264 |
context['header_msg'] = self.DEF_MAX_TASK_LIMIT_MSG_FMT % ( |
|
1265 |
entity.program.nr_simultaneous_tasks) |
|
1266 |
validation = 'claim_ineligible' |
|
1267 |
return validation, actions |
|
1268 |
||
1269 |
task_filter['status'] = 'AwaitingRegistration' |
|
1270 |
task_entities = ghop_task_logic.logic.getForFields(task_filter) |
|
1271 |
||
1272 |
if task_entities: |
|
1273 |
context['header_msg'] = self.DEF_AWAITING_REG_MSG |
|
1274 |
validation = 'claim_ineligible' |
|
1275 |
else: |
|
1276 |
actions.append(('request', 'Request to claim the task')) |
|
1277 |
validation = 'claim_request' |
|
1278 |
||
1279 |
# TODO: lot of double information here that can be simplified |
|
1280 |
if entity.user and user_account.key() == entity.user.key(): |
|
1281 |
if entity.status == 'ClaimRequested': |
|
1282 |
context['header_msg'] = self.DEF_TASK_REQ_CLAIMED_BY_YOU_MSG |
|
1283 |
actions.append(('withdraw', 'Withdraw from the task')) |
|
1284 |
validation = 'claim_withdraw' |
|
1285 |
elif entity.status in ['Claimed', 'NeedsWork', |
|
1286 |
'NeedsReview', 'ActionNeeded']: |
|
1287 |
context['header_msg'] = self.DEF_TASK_CLAIMED_BY_YOU_MSG |
|
1288 |
actions.extend([ |
|
1289 |
('withdraw', 'Withdraw from the task'), |
|
1290 |
('needs_review', 'Submit work and Request for review')]) |
|
1291 |
validation = 'needs_review' |
|
1292 |
elif entity.status == 'NeedsReview': |
|
1293 |
context['header_msg'] = self.DEF_TASK_NO_MORE_SUBMIT_MSG |
|
1294 |
actions.append(('withdraw', 'Withdraw from the task')) |
|
1295 |
if datetime.datetime.now < entity.deadline: |
|
1296 |
actions.append( |
|
1297 |
('needs_review', 'Submit work and Request for review')) |
|
1298 |
validation = 'needs_review' |
|
1299 |
elif entity.status == 'AwaitingRegistration': |
|
1300 |
context['header_msg'] = self.DEF_STUDENT_SIGNUP_MSG |
|
1301 |
elif entity.status == 'Closed': |
|
1302 |
context['header_msg'] = self.DEF_TASK_CMPLTD_BY_YOU_MSG |
|
1303 |
else: |
|
1304 |
if entity.status in ['ClaimRequested', 'Claimed', |
|
1305 |
'ActionNeeded', 'NeedsWork', |
|
1306 |
'NeedsReview']: |
|
1307 |
context['header_msg'] = self.DEF_TASK_CLAIMED_MSG |
|
1308 |
if entity.status in ['AwaitingRegistration', 'Closed']: |
|
1309 |
context['header_msg'] = self.DEF_TASK_CLOSED_MSG |
|
1310 |
||
1311 |
return validation, actions |
|
1312 |
||
1313 |
@decorators.merge_params |
|
1314 |
@decorators.check_access |
|
1315 |
def search(self, request, access_type, page_name=None, |
|
1316 |
params=None, filter=None, order=None,**kwargs): |
|
1317 |
"""View method to search for GHOP Tasks. |
|
1318 |
||
1319 |
Args: |
|
1320 |
request: the standard Django HTTP request object |
|
1321 |
access_type : the name of the access type which should be checked |
|
1322 |
page_name: the page name displayed in templates as page and header title |
|
1323 |
params: a dict with params for this View |
|
1324 |
kwargs: the Key Fields for the specified entity |
|
1325 |
""" |
|
1326 |
||
1327 |
from soc.modules.ghop.views.helper import list_info as list_info_helper |
|
1328 |
||
1329 |
get_params = request.GET |
|
1330 |
||
1331 |
contents = [] |
|
1332 |
context = {} |
|
1333 |
if not filter: |
|
1334 |
filter = {} |
|
1335 |
||
1336 |
public_status = ['Open', 'Reopened', 'ClaimRequested', 'Claimed', |
|
1337 |
'ActionNeeded', 'Closed', 'AwaitingRegistration', |
|
1338 |
'NeedsWork', 'NeedsReview'] |
|
1339 |
||
1340 |
task_params = params.copy() |
|
1341 |
task_params['list_template'] = 'modules/ghop/task/search/search.html' |
|
1342 |
task_params['list_heading'] = 'modules/ghop/task/search/heading.html' |
|
1343 |
task_params['list_row'] = 'modules/ghop/task/search/row.html' |
|
1344 |
||
1345 |
task_params['list_action'] = (redirects.getPublicRedirect, |
|
1346 |
task_params) |
|
1347 |
||
1348 |
task_params['list_description'] = ugettext( |
|
1349 |
'Search results: ') |
|
1350 |
||
1351 |
program_entity = ghop_program_logic.logic.getFromKeyFields(kwargs) |
|
1352 |
||
1353 |
org_fields = { |
|
1354 |
'scope': program_entity, |
|
1355 |
} |
|
1356 |
||
1357 |
org_entities = ghop_org_logic.logic.getForFields(org_fields) |
|
1358 |
org_names = [] |
|
1359 |
for org in org_entities: |
|
1360 |
org_names.append(org.name) |
|
1361 |
||
1362 |
df_entities = ghop_task_model.TaskDifficultyTag.get_by_scope( |
|
1363 |
program_entity) |
|
1364 |
difficulties = [] |
|
1365 |
for df_entity in df_entities: |
|
1366 |
difficulties.append(df_entity.tag) |
|
1367 |
||
1368 |
tt_entities = ghop_task_model.TaskTypeTag.get_by_scope(program_entity) |
|
1369 |
task_types = [] |
|
1370 |
for tt_entity in tt_entities: |
|
1371 |
task_types.append(tt_entity.tag) |
|
1372 |
||
1373 |
context['org_entities'] = org_names |
|
1374 |
context['public_status'] = public_status |
|
1375 |
context['difficulties'] = difficulties |
|
1376 |
context['tags'] = task_types |
|
1377 |
||
1378 |
org_filter = get_params.getlist('Organization') |
|
1379 |
status_filter = get_params.getlist('Status') |
|
1380 |
df_filter = get_params.getlist('Difficulty') |
|
1381 |
tag_filter = get_params.getlist('Tags') |
|
1382 |
||
1383 |
if org_filter: |
|
1384 |
org_fields = { |
|
1385 |
'scope': program_entity, |
|
1386 |
'name': org_filter, |
|
1387 |
} |
|
1388 |
org_entities = ghop_org_logic.logic.getForFields(org_fields) |
|
1389 |
filter['scope'] = org_entities |
|
1390 |
if status_filter: |
|
1391 |
filter['status']= status_filter |
|
1392 |
else: |
|
1393 |
filter['status'] = public_status |
|
1394 |
if df_filter: |
|
1395 |
filter['difficulty'] = df_filter |
|
1396 |
if tag_filter: |
|
1397 |
filter['task_type'] = tag_filter |
|
1398 |
||
1399 |
filter['program'] = program_entity |
|
1400 |
||
1401 |
task_list = lists.getListContent(request, task_params, filter, |
|
1402 |
order=order, idx=0, need_content=True) |
|
1403 |
||
1404 |
if task_list: |
|
1405 |
contents.append(task_list) |
|
1406 |
||
1407 |
# call the _list method from base to display the list |
|
1408 |
return self._list(request, task_params, contents, page_name, context) |
|
1409 |
||
1410 |
||
1411 |
view = View() |
|
1412 |
||
1413 |
create = decorators.view(view.create) |
|
1414 |
delete = decorators.view(view.delete) |
|
1415 |
edit = decorators.view(view.edit) |
|
1416 |
list = decorators.view(view.list) |
|
1417 |
list_org_tasks = decorators.view(view.listOrgTasks) |
|
1418 |
suggest_task = decorators.view(view.suggestTask) |
|
1419 |
public = decorators.view(view.public) |
|
1420 |
search = decorators.view(view.search) |