210 entity = self._logic.getFromKeyFieldsOr404(kwargs) |
213 entity = self._logic.getFromKeyFieldsOr404(kwargs) |
211 except out_of_band.Error, error: |
214 except out_of_band.Error, error: |
212 return responses.errorResponse( |
215 return responses.errorResponse( |
213 error, request, template=params['error_public']) |
216 error, request, template=params['error_public']) |
214 |
217 |
|
218 get_dict = request.GET |
|
219 |
|
220 if 'remove' in get_dict: |
|
221 # get the mentor to remove |
|
222 fields = {'link_id': get_dict['remove'], |
|
223 'scope': entity.scope} |
|
224 mentor = mentor_logic.logic.getForFields(fields, unique=True) |
|
225 |
|
226 additional_mentors = entity.additional_mentors |
|
227 if additional_mentors and mentor.key() in additional_mentors: |
|
228 # remove the mentor from the additional mentors list |
|
229 additional_mentors.remove(mentor.key()) |
|
230 fields= {'additional_mentors': additional_mentors} |
|
231 project_logic.updateEntityProperties(entity, fields) |
|
232 |
|
233 # redirect to the same page without GET arguments |
|
234 redirect = request.path |
|
235 return http.HttpResponseRedirect(redirect) |
|
236 |
|
237 template = params['manage_template'] |
|
238 |
215 # get the context for this webpage |
239 # get the context for this webpage |
216 context = responses.getUniversalContext(request) |
240 context = responses.getUniversalContext(request) |
217 responses.useJavaScript(context, params['js_uses_all']) |
241 responses.useJavaScript(context, params['js_uses_all']) |
218 context['page_name'] = "%s '%s' from %s" % (page_name, entity.title, |
242 context['page_name'] = "%s '%s' from %s" % (page_name, entity.title, |
219 entity.student.name()) |
243 entity.student.name()) |
220 |
244 context['entity'] = entity |
221 # use another template and make the cancel button goes to the public view |
|
222 params['edit_template'] = 'soc/student_project/manage.html' |
|
223 params['edit_cancel_redirect'] = redirects.getManageOverviewRedirect( |
|
224 entity.scope, params) |
|
225 |
245 |
226 # get all mentors for this organization |
246 # get all mentors for this organization |
227 fields = {'scope': entity.scope, |
247 fields = {'scope': entity.scope, |
228 'status': 'active'} |
248 'status': 'active'} |
229 mentors = mentor_logic.logic.getForFields(fields) |
249 mentors = mentor_logic.logic.getForFields(fields) |
248 dynaproperties = dynaproperties, |
268 dynaproperties = dynaproperties, |
249 ) |
269 ) |
250 |
270 |
251 params['mentor_edit_form'] = mentor_edit_form |
271 params['mentor_edit_form'] = mentor_edit_form |
252 |
272 |
|
273 additional_mentors = entity.additional_mentors |
|
274 |
|
275 # we want to show the names of the additional mentors in the context |
|
276 # therefore they need to be resolved to entities first |
|
277 additional_mentors_context = [] |
|
278 |
|
279 for mentor_key in additional_mentors: |
|
280 mentor_entity = mentor_logic.logic.getFromKeyName( |
|
281 mentor_key.id_or_name()) |
|
282 additional_mentors_context.append(mentor_entity) |
|
283 |
|
284 context['additional_mentors'] = additional_mentors_context |
|
285 |
|
286 # all mentors who are not already an additional mentor or |
|
287 # the primary mentor are allowed to become an additional mentor |
|
288 possible_additional_mentors = [m for m in mentors if |
|
289 (m.key() not in additional_mentors) and (m.key() != entity.mentor.key())] |
|
290 |
|
291 # create the information to be shown on the additional mentor form |
|
292 additional_mentor_choices = [ |
|
293 (mentor.link_id,'%s (%s)' %(mentor.name(), mentor.link_id)) |
|
294 for mentor in possible_additional_mentors] |
|
295 |
|
296 dynafields = [ |
|
297 {'name': 'mentor_id', |
|
298 'base': forms.ChoiceField, |
|
299 'label': 'Additional Mentors', |
|
300 'required': True, |
|
301 'passthrough': ['required', 'choices', 'label'], |
|
302 'choices': additional_mentor_choices, |
|
303 },] |
|
304 |
|
305 dynaproperties = params_helper.getDynaFields(dynafields) |
|
306 |
|
307 additional_mentor_form = dynaform.newDynaForm( |
|
308 dynabase = params['dynabase'], |
|
309 dynaproperties = dynaproperties, |
|
310 ) |
|
311 |
|
312 params['additional_mentor_form'] = additional_mentor_form |
|
313 |
253 if request.POST: |
314 if request.POST: |
254 return self.managePost(request, context, params, entity, **kwargs) |
315 return self.managePost(request, template, context, params, entity, |
|
316 **kwargs) |
255 else: #request.GET |
317 else: #request.GET |
256 return self.manageGet(request, context, params, entity, **kwargs) |
318 return self.manageGet(request, template, context, params, entity, |
257 |
319 **kwargs) |
258 def manageGet(self, request, context, params, entity, **kwargs): |
320 |
|
321 def manageGet(self, request, template, context, params, entity, **kwargs): |
259 """Handles the GET request for the project's manage page. |
322 """Handles the GET request for the project's manage page. |
260 |
323 |
261 Args: |
324 Args: |
|
325 template: the template used for this view |
262 entity: the student project entity |
326 entity: the student project entity |
263 rest: see base.View.public() |
327 rest: see base.View.public() |
264 """ |
328 """ |
265 |
329 |
266 # populate form with the current mentor |
330 # populate form with the current mentor |
267 initial = {'mentor_id': entity.mentor.link_id} |
331 initial = {'mentor_id': entity.mentor.link_id} |
268 form = params['mentor_edit_form'](initial=initial) |
332 context['mentor_edit_form'] = params['mentor_edit_form'](initial=initial) |
269 |
333 |
270 return self._constructResponse(request, entity, context, form, params) |
334 context['additional_mentor_form'] = params['additional_mentor_form']() |
271 |
335 |
272 def managePost(self, request, context, params, entity, **kwargs): |
336 return responses.respond(request, template, context) |
|
337 |
|
338 def managePost(self, request, template, context, params, entity, **kwargs): |
273 """Handles the POST request for the project's manage page. |
339 """Handles the POST request for the project's manage page. |
274 |
340 |
275 Args: |
341 Args: |
|
342 template: the template used for this view |
276 entity: the student project entity |
343 entity: the student project entity |
277 rest: see base.View.public() |
344 rest: see base.View.public() |
278 """ |
345 """ |
279 |
346 |
280 form = params['mentor_edit_form'](request.POST) |
347 post_dict = request.POST |
|
348 |
|
349 if 'set_mentor' in post_dict: |
|
350 form = params['mentor_edit_form'](post_dict) |
|
351 return self._manageSetMentor(request, template, context, params, entity, |
|
352 form) |
|
353 elif 'add_additional_mentor' in post_dict: |
|
354 form = params['additional_mentor_form'](post_dict) |
|
355 return self._manageAddAdditionalMentor(request, template, context, |
|
356 params, entity, form) |
|
357 else: |
|
358 # unexpected error return the normal page |
|
359 logging.warning('Unexpected POST data found') |
|
360 return self.manageGet(request, template, context, params, entity) |
|
361 |
|
362 def _manageSetMentor(self, request, template, context, params, entity, form): |
|
363 """Handles the POST request for changing a Projects's mentor. |
|
364 |
|
365 Args: |
|
366 template: the template used for this view |
|
367 entity: the student project entity |
|
368 form: instance of the form used to set the mentor |
|
369 rest: see base.View.public() |
|
370 """ |
281 |
371 |
282 if not form.is_valid(): |
372 if not form.is_valid(): |
283 return self._constructResponse(request, entity, context, form, params) |
373 context['mentor_edit_form'] = form |
|
374 |
|
375 # add an a fresh additional mentors form |
|
376 context['additional_mentor_form'] = params['additional_mentor_form']() |
|
377 |
|
378 return responses.respond(request, template, context) |
284 |
379 |
285 _, fields = forms_helper.collectCleanedFields(form) |
380 _, fields = forms_helper.collectCleanedFields(form) |
286 |
381 |
287 # get the mentor from the form |
382 # get the mentor from the form |
288 fields = {'link_id': fields['mentor_id'], |
383 fields = {'link_id': fields['mentor_id'], |
290 'status': 'active'} |
385 'status': 'active'} |
291 mentor = mentor_logic.logic.getForFields(fields, unique=True) |
386 mentor = mentor_logic.logic.getForFields(fields, unique=True) |
292 |
387 |
293 # update the project with the assigned mentor |
388 # update the project with the assigned mentor |
294 fields = {'mentor': mentor} |
389 fields = {'mentor': mentor} |
|
390 |
|
391 additional_mentors = entity.additional_mentors |
|
392 |
|
393 if additional_mentors and mentor.key() in additional_mentors: |
|
394 # remove the mentor that is now becoming the primary mentor |
|
395 additional_mentors.remove(mentor.key()) |
|
396 fields['additional_mentors'] = additional_mentors |
|
397 |
|
398 # update the project with the new mentor and possible |
|
399 # new set of additional mentors |
295 project_logic.updateEntityProperties(entity, fields) |
400 project_logic.updateEntityProperties(entity, fields) |
296 |
401 |
297 return self.manageGet(request, context, params, entity) |
402 # redirect to the same page |
|
403 redirect = request.path |
|
404 return http.HttpResponseRedirect(redirect) |
|
405 |
|
406 def _manageAddAdditionalMentor(self, request, template, context, params, entity, form): |
|
407 """Handles the POST request for changing a Projects's additional mentors. |
|
408 |
|
409 Args: |
|
410 template: the template used for this view |
|
411 entity: the student project entity |
|
412 form: instance of the form used to add an additional mentor |
|
413 rest: see base.View.public() |
|
414 """ |
|
415 |
|
416 if not form.is_valid(): |
|
417 context['additional_mentor_form'] = form |
|
418 |
|
419 # add a fresh edit mentor form |
|
420 initial = {'mentor_id': entity.mentor.link_id} |
|
421 context['mentor_edit_form'] = params['mentor_edit_form'](initial=initial) |
|
422 |
|
423 return responses.respond(request, template, context) |
|
424 |
|
425 _, fields = forms_helper.collectCleanedFields(form) |
|
426 |
|
427 # get the mentor from the form |
|
428 fields = {'link_id': fields['mentor_id'], |
|
429 'scope': entity.scope, |
|
430 'status': 'active'} |
|
431 mentor = mentor_logic.logic.getForFields(fields, unique=True) |
|
432 |
|
433 # add this mentor to the additional mentors |
|
434 if not entity.additional_mentors: |
|
435 additional_mentors = [mentor.key()] |
|
436 else: |
|
437 additional_mentors = additional_mentors.append(mentor.key()) |
|
438 |
|
439 fields = {'additional_mentors': additional_mentors} |
|
440 project_logic.updateEntityProperties(entity, fields) |
|
441 |
|
442 # redirect to the same page |
|
443 redirect = request.path |
|
444 return http.HttpResponseRedirect(redirect) |
298 |
445 |
299 @decorators.merge_params |
446 @decorators.merge_params |
300 @decorators.check_access |
447 @decorators.check_access |
301 def manageOverview(self, request, access_type, |
448 def manageOverview(self, request, access_type, |
302 page_name=None, params=None, **kwargs): |
449 page_name=None, params=None, **kwargs): |