app/soc/views/models/survey.py
changeset 2731 4d143278f9a0
parent 2727 19e985f09de8
child 2733 054a2227249c
equal deleted inserted replaced
2730:d1cfed8da027 2731:4d143278f9a0
   670 
   670 
   671   @decorators.merge_params
   671   @decorators.merge_params
   672   @decorators.check_access
   672   @decorators.check_access
   673   def viewResults(self, request, access_type, page_name=None,
   673   def viewResults(self, request, access_type, page_name=None,
   674                   params=None, **kwargs):
   674                   params=None, **kwargs):
   675     """View that will list the survey records.
   675     """View that lists all SurveyRecords which are of interest to the user.
   676 
   676 
   677     For params see base.View.public().
   677     For params see base.View.public().
   678     """
   678     """
   679 
   679 
   680     # TODO If read access then show all records, else show only mine +
   680     # TODO: this view could also contain statistics for the Survey
   681     # hook for subclasses. On both possibilities.
       
   682 
   681 
   683     survey_logic = params['logic']
   682     survey_logic = params['logic']
   684     record_logic = survey_logic.getRecordLogic()
   683     record_logic = survey_logic.getRecordLogic()
   685 
   684 
   686     try:
   685     try:
   693     context = responses.getUniversalContext(request)
   692     context = responses.getUniversalContext(request)
   694     responses.useJavaScript(context, params['js_uses_all'])
   693     responses.useJavaScript(context, params['js_uses_all'])
   695     context['page_name'] = "%s titled '%s'" % (page_name, entity.title)
   694     context['page_name'] = "%s titled '%s'" % (page_name, entity.title)
   696     context['entity'] = entity
   695     context['entity'] = entity
   697 
   696 
   698     # only show truncated preview of first answer
   697     # add the first question to the context show a preview can be shown
   699     context['first_question'] = entity.survey_content.orderedProperties()[0]
   698     context['first_question'] = entity.survey_content.orderedProperties()[0]
   700 
   699 
   701     filter = {'survey': entity}
   700     # get the rights checker
       
   701     user_entity = user_logic.getForCurrentAccount()
       
   702     rights = self._params['rights']
       
   703     rights.setCurrentUser(user_entity.account, user_entity)
       
   704 
       
   705     # check if the current user is allowed to visit the read the survey
       
   706     allowed_to_read = False
       
   707 
       
   708     try:
       
   709       rights.checkIsSurveyReadable(
       
   710           {'key_name': survey_entity.key().name(),
       
   711            'prefix': survey_entity.prefix,
       
   712            'scope_path': survey_entity.scope_path,
       
   713            'link_id': survey_entity.link_id,
       
   714            'user': user_entity},
       
   715           survey_logic)
       
   716       allowed_to_read = True
       
   717     except:
       
   718       pass
       
   719 
       
   720     # only show records for the retrieved survey
       
   721     fields = {'survey': entity}
       
   722 
       
   723     if not allowed_to_read:
       
   724       # this user is not allowed to view all the Records so only show their own
       
   725       fields['user'] = user_entity
       
   726 
       
   727     # call the hook for adjusting the SurveyRecord filter
       
   728     fields = self._getResultsViewRecordFields(fields, entity, allowed_to_read)
   702 
   729 
   703     list_params = params.copy()
   730     list_params = params.copy()
   704     list_params['logic'] = record_logic
   731     list_params['logic'] = record_logic
   705     list_params['list_heading'] = 'soc/survey/list/records_heading.html'
   732     list_params['list_heading'] = 'soc/survey/list/records_heading.html'
   706     list_params['list_row'] = 'soc/survey/list/records_row.html'
   733     list_params['list_row'] = 'soc/survey/list/records_row.html'
   707     list_params['list_description'] = \
   734     list_params['list_description'] = \
   708       'List of %(name_plural)s.' % list_params
   735         "List of Records for the %s titled '%s'." %(list_params['name'],
       
   736                                                     entity.title)
   709     list_params['list_action'] = (redirects.getViewSurveyRecordRedirect,
   737     list_params['list_action'] = (redirects.getViewSurveyRecordRedirect,
   710                                   list_params)
   738                                   list_params)
   711 
   739 
   712     record_list = lists.getListContent(request, list_params, filter, idx=0)
   740     record_list = lists.getListContent(request, list_params, fields, idx=0)
   713 
   741 
   714     contents = [record_list]
   742     contents = [record_list]
   715 
   743 
   716     return self._list(request, list_params, contents, page_name, context)
   744     return self._list(request, list_params, contents, page_name, context)
       
   745 
       
   746   def _getResultsViewRecordFields(self, fields, survey, allowed_to_read):
       
   747     """Hook for adjusting the Results View filter for SurveyRecords.
       
   748 
       
   749     Args:
       
   750       fields: filter dictionary to adjust
       
   751       survey: Survey instance for which the Records need to be shown
       
   752       allowed_to_read: specifies if the current User has read access
       
   753 
       
   754     Returns:
       
   755       Returns the dictionary containing the fields to filter on
       
   756     """
       
   757     return fields
   717 
   758 
   718   @decorators.merge_params
   759   @decorators.merge_params
   719   @decorators.check_access
   760   @decorators.check_access
   720   def viewRecord(self, request, access_type, page_name=None,
   761   def viewRecord(self, request, access_type, page_name=None,
   721                  params=None, **kwargs):
   762                  params=None, **kwargs):