521 'user': user_entity} |
522 'user': user_entity} |
522 |
523 |
523 return record_logic.getForFields(filter, unique=True) |
524 return record_logic.getForFields(filter, unique=True) |
524 |
525 |
525 def takeGet(self, request, template, context, params, entity, record, |
526 def takeGet(self, request, template, context, params, entity, record, |
|
527 form_data=None, **kwargs): |
|
528 """Handles the GET request for the Survey's take page. |
|
529 |
|
530 Args: |
|
531 template: the template used for this view |
|
532 entity: the Survey entity |
|
533 record: a SurveyRecord entity |
|
534 form_data: dict with form data that will be used for validation |
|
535 rest: see base.View.public() |
|
536 """ |
|
537 |
|
538 survey_form = surveys.SurveyForm(survey_content=entity.survey_content, |
|
539 survey_record=record, |
|
540 survey_logic=self._params['logic']) |
|
541 |
|
542 # fetch field contents and pass request data, if any |
|
543 survey_form.getFields(post_dict=form_data) |
|
544 |
|
545 # validate request data |
|
546 if form_data and not survey_form.is_valid(): |
|
547 survey_form.full_clean() |
|
548 context['survey_form'] = survey_form |
|
549 return self._constructResponse(request, entity=None, context=context, |
|
550 form=survey_form, params=params, template=template) |
|
551 |
|
552 # fill context with the survey and additional information |
|
553 context['survey_form'] = survey_form |
|
554 self.setHelpAndStatus(context, entity, record) |
|
555 |
|
556 # call the hook method |
|
557 self._takeGet(request, template, context, params, entity, record, **kwargs) |
|
558 |
|
559 return responses.respond(request, template, context) |
|
560 |
|
561 def _takeGet(self, request, template, context, params, entity, record, |
526 **kwargs): |
562 **kwargs): |
527 """Handles the GET request for the Survey's take page. |
563 """Hook for the GET request for the Survey's take page. |
|
564 |
|
565 This method is called just before the GET page is shown. |
528 |
566 |
529 Args: |
567 Args: |
530 template: the template used for this view |
568 template: the template used for this view |
531 entity: the Survey entity |
569 entity: the Survey entity |
532 record: a SurveyRecord entity |
570 record: a SurveyRecord entity |
533 rest: see base.View.public() |
571 rest: see base.View.public() |
534 """ |
572 """ |
535 |
573 pass |
536 survey_form = surveys.SurveyForm(survey_content=entity.survey_content, |
574 |
537 survey_record=record, |
575 def takePost(self, request, template, context, params, entity, record, |
538 survey_logic=self._params['logic']) |
576 **kwargs): |
539 |
577 """Handles the POST request for the Survey's take page. |
540 survey_form.getFields() |
|
541 |
|
542 # fill context with the survey and additional information |
|
543 context['survey_form'] = survey_form |
|
544 self.setHelpAndStatus(context, entity, record) |
|
545 |
|
546 # call the hook method |
|
547 self._takeGet(request, template, context, params, entity, record, **kwargs) |
|
548 |
|
549 return responses.respond(request, template, context) |
|
550 |
|
551 def _takeGet(self, request, template, context, params, entity, record, |
|
552 **kwargs): |
|
553 """Hook for the GET request for the Survey's take page. |
|
554 |
|
555 This method is called just before the GET page is shown. |
|
556 |
578 |
557 Args: |
579 Args: |
558 template: the template used for this view |
580 template: the template used for this view |
559 entity: the Survey entity |
581 entity: the Survey entity |
560 record: a SurveyRecord entity |
582 record: a SurveyRecord entity |
561 rest: see base.View.public() |
583 rest: see base.View.public() |
562 """ |
584 """ |
563 pass |
|
564 |
|
565 def takePost(self, request, template, context, params, entity, record, |
|
566 **kwargs): |
|
567 """Handles the POST request for the Survey's take page. |
|
568 |
|
569 Args: |
|
570 template: the template used for this view |
|
571 entity: the Survey entity |
|
572 record: a SurveyRecord entity |
|
573 rest: see base.View.public() |
|
574 """ |
|
575 |
585 |
576 survey_logic = params['logic'] |
586 survey_logic = params['logic'] |
577 record_logic = survey_logic.getRecordLogic() |
587 record_logic = survey_logic.getRecordLogic() |
578 |
588 |
579 # TODO: check the validity of the form data |
589 # create a form to validate |
580 properties = surveys.getSurveyResponseFromPost(entity, request.POST) |
590 survey_form = surveys.SurveyForm(survey_content=entity.survey_content, |
|
591 survey_record=None, |
|
592 survey_logic=self._params['logic']) |
|
593 # fill form with request data |
|
594 survey_form.getFields(post_dict=request.POST) |
|
595 |
|
596 if not survey_form.is_valid(): |
|
597 # redirect to takeGet so we can handle errors |
|
598 return self.takeGet(request, template, context, params, entity, record, |
|
599 form_data=properties) |
|
600 |
|
601 # retrieve the data from the form |
|
602 _, properties = forms_helper.collectCleanedFields(survey_form) |
581 |
603 |
582 # add the required SurveyRecord properties |
604 # add the required SurveyRecord properties |
583 properties['user'] = user_logic.getForCurrentAccount() |
605 properties['user'] = user_logic.getForCurrentAccount() |
584 properties['survey'] = entity |
606 properties['survey'] = entity |
585 |
607 |