496 |
496 |
497 # try to get an existing SurveyRecord for the current user |
497 # try to get an existing SurveyRecord for the current user |
498 survey_record = self._getSurveyRecordFor(entity, request, params) |
498 survey_record = self._getSurveyRecordFor(entity, request, params) |
499 |
499 |
500 # get an instance of SurveyTakeForm to use |
500 # get an instance of SurveyTakeForm to use |
501 survey_form = self._getSurveyTakeForm(entity, survey_record, params) |
501 survey_form = self._getSurveyTakeForm(entity, survey_record, params, |
|
502 request.POST) |
502 |
503 |
503 # fill context with the survey_form and additional information |
504 # fill context with the survey_form and additional information |
504 context['survey_form'] = survey_form |
505 context['survey_form'] = survey_form |
505 self.setHelpAndStatus(context, entity, survey_record) |
506 self.setHelpAndStatus(context, entity, survey_record) |
506 |
507 |
532 filter = {'survey': survey, |
533 filter = {'survey': survey, |
533 'user': user_entity} |
534 'user': user_entity} |
534 |
535 |
535 return record_logic.getForFields(filter, unique=True) |
536 return record_logic.getForFields(filter, unique=True) |
536 |
537 |
537 def _getSurveyTakeForm(self, survey, record, params): |
538 def _getSurveyTakeForm(self, survey, record, params, post_dict=None): |
538 """Returns the specific SurveyTakeForm needed for the take view. |
539 """Returns the specific SurveyTakeForm needed for the take view. |
539 |
540 |
540 Args: |
541 Args: |
541 survey: a Survey entity |
542 survey: a Survey entity |
542 record: a SurveyRecord instance if any exist |
543 record: a SurveyRecord instance if any exist |
543 params: the params dict for the requesting View |
544 params: the params dict for the requesting View |
|
545 post_dict: POST data to be passed to form initialization |
544 |
546 |
545 Returns: |
547 Returns: |
546 An instance of SurveyTakeForm that can be used to take a Survey. |
548 An instance of SurveyTakeForm that can be used to take a Survey. |
547 """ |
549 """ |
548 |
550 |
549 return surveys.SurveyTakeForm(survey_content=survey.survey_content, |
551 return surveys.SurveyTakeForm(survey_content=survey.survey_content, |
550 survey_logic=params['logic']) |
552 survey_record=record, |
|
553 survey_logic=params['logic'], |
|
554 data=post_dict) |
|
555 |
551 |
556 |
552 def takeGet(self, request, template, context, params, survey_form, entity, |
557 def takeGet(self, request, template, context, params, survey_form, entity, |
553 record, **kwargs): |
558 record, **kwargs): |
554 """Handles the GET request for the Survey's take page. |
559 """Handles the GET request for the Survey's take page. |
555 |
560 |
559 entity: the Survey entity |
564 entity: the Survey entity |
560 record: a SurveyRecord entity iff any exists |
565 record: a SurveyRecord entity iff any exists |
561 rest: see base.View.public() |
566 rest: see base.View.public() |
562 """ |
567 """ |
563 |
568 |
564 # set the possible survey record as initial value |
|
565 # TODO: SurveyTakeForm should just work with post_data if available |
|
566 # and otherwise use the record supplied. |
|
567 survey_form.survey_record = record |
|
568 |
|
569 # fetch field contents |
|
570 survey_form.getFields() |
|
571 |
|
572 # call the hook method |
569 # call the hook method |
573 self._takeGet(request, template, context, params, entity, record, **kwargs) |
570 self._takeGet(request, template, context, params, entity, record, **kwargs) |
574 |
571 |
575 return responses.respond(request, template, context) |
572 return responses.respond(request, template, context) |
576 |
573 |
600 rest: see base.View.public() |
597 rest: see base.View.public() |
601 """ |
598 """ |
602 |
599 |
603 survey_logic = params['logic'] |
600 survey_logic = params['logic'] |
604 record_logic = survey_logic.getRecordLogic() |
601 record_logic = survey_logic.getRecordLogic() |
605 |
|
606 # fill form with request data |
|
607 survey_form.getFields(post_dict=request.POST) |
|
608 |
602 |
609 if not survey_form.is_valid(): |
603 if not survey_form.is_valid(): |
610 # show the form errors |
604 # show the form errors |
611 return self._constructResponse(request, entity=entity, context=context, |
605 return self._constructResponse(request, entity=entity, context=context, |
612 form=survey_form, params=params, |
606 form=survey_form, params=params, |