app/soc/views/models/survey.py
changeset 2576 7a1138f8a0e2
parent 2560 a944c0169ad8
child 2582 df5939fecaff
equal deleted inserted replaced
2575:e319fd52cbc4 2576:7a1138f8a0e2
    43 from soc.models.user import User
    43 from soc.models.user import User
    44 from soc.views import out_of_band
    44 from soc.views import out_of_band
    45 from soc.views.helper import access
    45 from soc.views.helper import access
    46 from soc.views.helper import decorators
    46 from soc.views.helper import decorators
    47 from soc.views.helper import forms as forms_helper
    47 from soc.views.helper import forms as forms_helper
       
    48 from soc.views.helper import lists
    48 from soc.views.helper import redirects
    49 from soc.views.helper import redirects
    49 from soc.views.helper import responses
    50 from soc.views.helper import responses
    50 from soc.views.helper import surveys
    51 from soc.views.helper import surveys
    51 from soc.views.helper import widgets
    52 from soc.views.helper import widgets
    52 from soc.views.models import base
    53 from soc.views.models import base
   100     rights['create'] = ['checkIsUser']
   101     rights['create'] = ['checkIsUser']
   101     rights['edit'] = [('checkIsSurveyWritable', survey_logic)]
   102     rights['edit'] = [('checkIsSurveyWritable', survey_logic)]
   102     rights['delete'] = ['checkIsDeveloper'] # TODO: fix deletion of Surveys
   103     rights['delete'] = ['checkIsDeveloper'] # TODO: fix deletion of Surveys
   103     rights['list'] = ['checkDocumentList']
   104     rights['list'] = ['checkDocumentList']
   104     rights['pick'] = ['checkDocumentPick']
   105     rights['pick'] = ['checkDocumentPick']
       
   106     rights['results'] = [('checkIsSurveyWritable', survey_logic)]
   105     rights['take'] = [('checkIsSurveyTakeable', survey_logic)]
   107     rights['take'] = [('checkIsSurveyTakeable', survey_logic)]
   106 
   108 
   107     new_params = {}
   109     new_params = {}
   108     new_params['logic'] = survey_logic
   110     new_params['logic'] = survey_logic
   109     new_params['rights'] = rights
   111     new_params['rights'] = rights
   116          'soc.views.models.%(module_name)s.take',
   118          'soc.views.models.%(module_name)s.take',
   117          'Take %(name)s'),
   119          'Take %(name)s'),
   118          (r'^%(url_name)s/(?P<access_type>json)/%(scope)s$',
   120          (r'^%(url_name)s/(?P<access_type>json)/%(scope)s$',
   119          'soc.views.models.%(module_name)s.json',
   121          'soc.views.models.%(module_name)s.json',
   120          'Export %(name)s as JSON'),
   122          'Export %(name)s as JSON'),
   121         (r'^%(url_name)s/(?P<access_type>results)/%(scope)s$',
   123         (r'^%(url_name)s/(?P<access_type>results)/%(key_fields)s$',
   122          'soc.views.models.%(module_name)s.results',
   124          'soc.views.models.%(module_name)s.results',
   123          'View survey results for %(name)s'),
   125          'View survey results for %(name)s'),
   124         (r'^%(url_name)s/(?P<access_type>show)/user/(?P<link_id>)\w+$',
   126         (r'^%(url_name)s/(?P<access_type>show)/user/(?P<link_id>)\w+$',
   125          'soc.views.models.%(module_name)s.results',
   127          'soc.views.models.%(module_name)s.results',
   126          'View survey results for user'),
   128          'View survey results for user'),
   214     context['survey_form'] = survey_form
   216     context['survey_form'] = survey_form
   215 
   217 
   216     # return True to signal that the page may be displayed
   218     # return True to signal that the page may be displayed
   217     return True
   219     return True
   218 
   220 
   219   def _editContext(self, request, context):
       
   220     """Performs any required processing on the context for edit pages.
       
   221 
       
   222     Args:
       
   223       request: the django request object
       
   224       context: the context dictionary that will be used
       
   225 
       
   226       Adds list of SurveyRecord results as supplement to view.
       
   227 
       
   228       See surveys.SurveyResults for details.
       
   229     """
       
   230 
       
   231     if not getattr(self, '_entity', None):
       
   232       return
       
   233 
       
   234     results = surveys.SurveyResults()
       
   235 
       
   236     context['survey_records'] = results.render(self._entity, self._params,
       
   237                                                filter={})
       
   238 
       
   239     super(View, self)._editContext(request, context)
       
   240 
       
   241   def _editPost(self, request, entity, fields):
   221   def _editPost(self, request, entity, fields):
   242     """See base.View._editPost().
   222     """See base.View._editPost().
   243 
   223 
   244     Processes POST request items to add new dynamic field names,
   224     Processes POST request items to add new dynamic field names,
   245     question types, and default prompt values to SurveyContent model.
   225     question types, and default prompt values to SurveyContent model.
   694 
   674 
   695   @decorators.merge_params
   675   @decorators.merge_params
   696   @decorators.check_access
   676   @decorators.check_access
   697   def viewResults(self, request, access_type, page_name=None,
   677   def viewResults(self, request, access_type, page_name=None,
   698                   params=None, **kwargs):
   678                   params=None, **kwargs):
   699     """View for SurveyRecord and SurveyRecordGroup.
   679     """View for SurveyRecords.
   700     """
   680 
   701 
   681     For params see base.View.public().
   702     results_logic = params['logic'].getRecordLogic()
   682     """
       
   683 
       
   684     survey_logic = params['logic']
       
   685 
       
   686     try:
       
   687       entity = survey_logic.getFromKeyFieldsOr404(kwargs)
       
   688     except out_of_band.Error, error:
       
   689       return responses.errorResponse(
       
   690           error, request, template=params['error_public'])
       
   691 
       
   692     # get the context for this webpage
       
   693     context = responses.getUniversalContext(request)
       
   694     responses.useJavaScript(context, params['js_uses_all'])
       
   695     context['page_name'] = "%s titled '%s'" % (page_name, entity.title)
       
   696     context['entity'] = entity
       
   697 
       
   698     results_logic = survey_logic.getRecordLogic()
   703 
   699 
   704     user = user_logic.getForCurrentAccount()
   700     user = user_logic.getForCurrentAccount()
   705 
   701 
   706     # TODO(ajaksu) use the named parameter link_id from the re
   702     context['properties'] = entity.survey_content.orderedProperties()
   707     if request.path == '/survey/show/user/' + user.link_id:
   703 
   708       records = tuple(user.surveys_taken.run())
   704     filter = self._params.get('filter') or {}
   709       context = responses.getUniversalContext(request)
   705 
   710       context['content'] = records[0].survey.survey_content
   706     filter['survey'] = entity
   711       responses.useJavaScript(context, params['js_uses_all'])
   707 
   712       context['page_name'] = u'Your survey records.'
   708     list_params = params.copy()
   713     else:
   709     list_params['list_description'] = \
   714       entity, context = self.getContextEntity(request, page_name,
   710       'List of  %(name_plural)s.' % list_params
   715                                               params, kwargs)
   711 
   716 
   712     list_params['logic'] = results_logic
   717       if context is None:
   713 
   718         # user cannot see this page, return error response
   714     valid_list = lists.getListContent(request, list_params, filter, idx=0)
   719         return entity
   715 
   720       context['content'] = entity.survey_content
   716     valid_list['row'] = 'soc/survey/list/results_row.html'
   721       can_write = False
   717     valid_list['heading'] = 'soc/survey/list/results_heading.html'
   722       rights = self._params['rights']
   718     valid_list['description'] = 'Survey Results:'
   723       try:
   719 
   724         rights.checkIsSurveyWritable({'key_name': entity.key().name(),
   720     contents = []
   725                                       'prefix': entity.prefix,
   721     # fill contents with all the needed lists
   726                                       'scope_path': entity.scope_path,
   722     contents.append(valid_list)
   727                                       'link_id': entity.link_id,},
   723 
   728                                      'key_name')
   724     return self._list(request, list_params, contents, page_name, context)
   729         can_write = True
       
   730       except out_of_band.AccessViolation:
       
   731         pass
       
   732 
       
   733       filter = self._params.get('filter') or {}
       
   734 
       
   735       # if user can edit the survey, show everyone's results
       
   736       if can_write:
       
   737         filter['survey'] = entity
       
   738       else:
       
   739         filter.update({'user': user, 'survey': entity})
       
   740 
       
   741       limit = self._params.get('limit') or 1000
       
   742       offset = self._params.get('offset') or 0
       
   743       order = self._params.get('order') or []
       
   744       idx = self._params.get('idx') or 0
       
   745 
       
   746       records = results_logic.getForFields(filter=filter, limit=limit,
       
   747                                         offset=offset, order=order)
       
   748 
       
   749     updates = dicts.rename(params, params['list_params'])
       
   750     context.update(updates)
       
   751 
       
   752     context['results'] = records, records
       
   753 
       
   754     template = 'soc/survey/results_page.html'
       
   755     return responses.respond(request, template, context=context)
       
   756 
   725 
   757   @decorators.merge_params
   726   @decorators.merge_params
   758   @decorators.check_access
   727   @decorators.check_access
   759   def exportSerialized(self, request, access_type, page_name=None,
   728   def exportSerialized(self, request, access_type, page_name=None,
   760                        params=None, **kwargs):
   729                        params=None, **kwargs):