app/soc/views/models/survey.py
changeset 2501 d612b48e6e12
parent 2497 996f381d458c
child 2502 2e096acc8720
equal deleted inserted replaced
2500:ecc16ffe174b 2501:d612b48e6e12
    41 from soc.models.survey_record import SurveyRecord
    41 from soc.models.survey_record import SurveyRecord
    42 from soc.models.user import User
    42 from soc.models.user import User
    43 from soc.views import out_of_band
    43 from soc.views import out_of_band
    44 from soc.views.helper import access
    44 from soc.views.helper import access
    45 from soc.views.helper import decorators
    45 from soc.views.helper import decorators
       
    46 from soc.views.helper import forms as forms_helper
    46 from soc.views.helper import redirects
    47 from soc.views.helper import redirects
    47 from soc.views.helper import responses
    48 from soc.views.helper import responses
    48 from soc.views.helper import surveys
    49 from soc.views.helper import surveys
    49 from soc.views.helper import widgets
    50 from soc.views.helper import widgets
    50 from soc.views.models import base
    51 from soc.views.models import base
   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 
   612     """Get help_text and status for template use.
   634     """Get help_text and status for template use.
   613 
   635 
   614     Args:
   636     Args:
   615         context: the context for the view to update
   637         context: the context for the view to update
   616         survey: a Survey entity
   638         survey: a Survey entity
   617         survey_record: a SurveyRecordEntity 
   639         survey_record: a SurveyRecordEntity
   618     """
   640     """
   619 
   641 
   620     if not survey.survey_end:
   642     if not survey.survey_end:
   621       survey_end_text = ""
   643       survey_end_text = ""
   622     else:
   644     else: