app/soc/views/models/survey.py
changeset 2722 1ec0af4ddd72
parent 2659 8df08a3b17db
child 2723 9e03061402ac
equal deleted inserted replaced
2721:e32118b84718 2722:1ec0af4ddd72
   104     rights['create'] = ['checkIsUser']
   104     rights['create'] = ['checkIsUser']
   105     rights['edit'] = [('checkIsSurveyWritable', survey_logic)]
   105     rights['edit'] = [('checkIsSurveyWritable', survey_logic)]
   106     rights['delete'] = ['checkIsDeveloper'] # TODO: fix deletion of Surveys
   106     rights['delete'] = ['checkIsDeveloper'] # TODO: fix deletion of Surveys
   107     rights['list'] = ['checkDocumentList']
   107     rights['list'] = ['checkDocumentList']
   108     rights['pick'] = ['checkDocumentPick']
   108     rights['pick'] = ['checkDocumentPick']
   109     rights['results'] = [('checkIsSurveyWritable', survey_logic)]
   109     rights['record'] = ['checkIsDeveloper'] # TODO: proper access check
       
   110     rights['results'] = ['checkIsDeveloper'] # TODO: proper access check
   110     rights['take'] = [('checkIsSurveyTakeable', survey_logic)]
   111     rights['take'] = [('checkIsSurveyTakeable', survey_logic)]
   111 
   112 
   112     new_params = {}
   113     new_params = {}
   113     new_params['logic'] = survey_logic
   114     new_params['logic'] = survey_logic
   114     new_params['rights'] = rights
   115     new_params['rights'] = rights
   121          'soc.views.models.%(module_name)s.take',
   122          'soc.views.models.%(module_name)s.take',
   122          'Take %(name)s'),
   123          'Take %(name)s'),
   123          (r'^%(url_name)s/(?P<access_type>json)/%(scope)s$',
   124          (r'^%(url_name)s/(?P<access_type>json)/%(scope)s$',
   124          'soc.views.models.%(module_name)s.json',
   125          'soc.views.models.%(module_name)s.json',
   125          'Export %(name)s as JSON'),
   126          'Export %(name)s as JSON'),
       
   127         (r'^%(url_name)s/(?P<access_type>record)/%(key_fields)s$',
       
   128          'soc.views.models.%(module_name)s.record',
       
   129          'View survey record for %(name)s'),
   126         (r'^%(url_name)s/(?P<access_type>results)/%(key_fields)s$',
   130         (r'^%(url_name)s/(?P<access_type>results)/%(key_fields)s$',
   127          'soc.views.models.%(module_name)s.results',
   131          'soc.views.models.%(module_name)s.results',
   128          'View survey results for %(name)s'),
   132          'View survey results for %(name)s'),
   129         (r'^%(url_name)s/(?P<access_type>show)/user/(?P<link_id>)\w+$',
   133         (r'^%(url_name)s/(?P<access_type>show)/user/(?P<link_id>)\w+$',
   130          'soc.views.models.%(module_name)s.results',
   134          'soc.views.models.%(module_name)s.results',
   140         'content', 'prefix','read_access','write_access']
   144         'content', 'prefix','read_access','write_access']
   141 
   145 
   142     new_params['edit_template'] = 'soc/survey/edit.html'
   146     new_params['edit_template'] = 'soc/survey/edit.html'
   143     new_params['create_template'] = 'soc/survey/edit.html'
   147     new_params['create_template'] = 'soc/survey/edit.html'
   144     new_params['public_template'] = 'soc/survey/public.html'
   148     new_params['public_template'] = 'soc/survey/public.html'
       
   149     new_params['record_template'] = 'soc/survey/view_record.html'
   145     new_params['take_template'] = 'soc/survey/take.html'
   150     new_params['take_template'] = 'soc/survey/take.html'
   146 
   151 
   147     # TODO: which one of these are leftovers from Document?
       
   148     new_params['no_create_raw'] = True
   152     new_params['no_create_raw'] = True
   149     new_params['no_create_with_scope'] = True
   153     new_params['no_create_with_scope'] = True
   150     new_params['no_create_with_key_fields'] = True
   154     new_params['no_create_with_key_fields'] = True
   151     new_params['no_list_raw'] = True
   155     new_params['no_list_raw'] = True
   152     new_params['sans_link_id_create'] = True
   156     new_params['sans_link_id_create'] = True
   211     # construct the form to be shown on the page
   215     # construct the form to be shown on the page
   212     # TODO(ljvderijk) Generate SurveyForm without passing along the logic
   216     # TODO(ljvderijk) Generate SurveyForm without passing along the logic
   213     survey_form = surveys.SurveyTakeForm(survey_content=entity.survey_content,
   217     survey_form = surveys.SurveyTakeForm(survey_content=entity.survey_content,
   214                                          survey_logic=self._params['logic'])
   218                                          survey_logic=self._params['logic'])
   215 
   219 
   216     # TODO(ljvderijk) pose question about the getFields method name and working
       
   217     survey_form.getFields()
   220     survey_form.getFields()
   218 
   221 
   219     context['survey_form'] = survey_form
   222     context['survey_form'] = survey_form
   220 
   223 
   221     # return True to signal that the page may be displayed
   224     # return True to signal that the page may be displayed
   669     """View for SurveyRecords.
   672     """View for SurveyRecords.
   670 
   673 
   671     For params see base.View.public().
   674     For params see base.View.public().
   672     """
   675     """
   673 
   676 
   674     survey_logic = params['logic']
   677     # TODO(ljvderijk) finish this method
       
   678     return http.HttpResponse("Work In Progress")
       
   679 
       
   680     # TODO If read access then show all records, else show only mine +
       
   681     # hook for subclasses. On both possibilities.
   675 
   682 
   676     try:
   683     try:
   677       entity = survey_logic.getFromKeyFieldsOr404(kwargs)
   684       entity = params['logic'].getFromKeyFieldsOr404(kwargs)
   678     except out_of_band.Error, error:
   685     except out_of_band.Error, error:
   679       return responses.errorResponse(
   686       return responses.errorResponse(
   680           error, request, template=params['error_public'])
   687           error, request, template=params['error_public'])
   681 
   688 
   682     # get the context for this webpage
   689     # get the context for this webpage
   683     context = responses.getUniversalContext(request)
   690     context = responses.getUniversalContext(request)
   684     responses.useJavaScript(context, params['js_uses_all'])
   691     responses.useJavaScript(context, params['js_uses_all'])
   685     context['page_name'] = "%s titled '%s'" % (page_name, entity.title)
   692     context['page_name'] = "%s titled '%s'" % (page_name, entity.title)
   686     context['entity'] = entity
   693     context['entity'] = entity
   687 
   694 
   688     results_logic = survey_logic.getRecordLogic()
   695     results_logic = params['logic'].getRecordLogic()
   689 
   696 
   690     user = user_logic.getForCurrentAccount()
   697     # only show truncated preview of first answer
   691 
   698     context['first_question'] = entity.survey_content.orderedProperties()[0]
   692     context['properties'] = entity.survey_content.orderedProperties()
   699 
   693 
   700     context['record_redirect'] = redirects.getSurveyRecordRedirect(
   694     filter = self._params.get('filter') or {}
   701         entity, params)
   695 
   702 
   696     filter['survey'] = entity
   703     filter = {'survey': entity}
   697 
   704 
   698     list_params = params.copy()
   705     list_params = params.copy()
   699     list_params['list_description'] = \
   706     list_params['list_description'] = \
   700       'List of  %(name_plural)s.' % list_params
   707       'List of %(name_plural)s.' % list_params
   701 
       
   702     list_params['logic'] = results_logic
   708     list_params['logic'] = results_logic
   703 
   709     list_params['list_heading'] = 'soc/survey/list/results_heading.html'
   704     valid_list = lists.getListContent(request, list_params, filter, idx=0)
   710     list_params['list_row'] = 'soc/survey/list/results_row.html'
   705 
   711 
   706     valid_list['row'] = 'soc/survey/list/results_row.html'
   712     record_list = lists.getListContent(request, list_params, filter, idx=0)
   707     valid_list['heading'] = 'soc/survey/list/results_heading.html'
   713 
   708     valid_list['description'] = 'Survey Results:'
   714     contents = [record_list]
   709 
       
   710     contents = []
       
   711     # fill contents with all the needed lists
       
   712     contents.append(valid_list)
       
   713 
   715 
   714     return self._list(request, list_params, contents, page_name, context)
   716     return self._list(request, list_params, contents, page_name, context)
       
   717 
       
   718   @decorators.merge_params
       
   719   @decorators.check_access
       
   720   def viewRecord(self, request, access_type, page_name=None,
       
   721                  params=None, **kwargs):
       
   722     """View that allows the user to see the contents of a single SurveyRecord.
       
   723 
       
   724     For params see base.View.public()
       
   725     """
       
   726 
       
   727     survey_logic = params['logic']
       
   728     record_logic = survey_logic.getRecordLogic()
       
   729 
       
   730     try:
       
   731       survey_entity = survey_logic.getFromKeyFieldsOr404(kwargs)
       
   732     except out_of_band.Error, error:
       
   733       return responses.errorResponse(
       
   734           error, request, template=params['error_public'])
       
   735 
       
   736     get_dict = request.GET
       
   737     record_id = get_dict.get('id')
       
   738 
       
   739     if record_id and record_id.isdigit():
       
   740       record_id = int(record_id)
       
   741       record_entity = record_logic.getFromIDOr404(record_id)
       
   742     else:
       
   743       raise out_of_band.Error('No valid Record ID given')
       
   744 
       
   745     if record_entity.survey.key() != survey_entity.key():
       
   746       # record does not match the retrieved survey
       
   747       raise out_of_band.Error('Record ID does not match the given survey')
       
   748 
       
   749     # get the context for this webpage
       
   750     context = responses.getUniversalContext(request)
       
   751     responses.useJavaScript(context, params['js_uses_all'])
       
   752     context['page_name'] = "%s titled '%s'" %(page_name, survey_entity.title)
       
   753     context['entity'] = survey_entity
       
   754     context['record'] = record_entity
       
   755 
       
   756     # store the read only survey form in the context
       
   757     survey_form = surveys.SurveyTakeForm(
       
   758        survey_content=survey_entity.survey_content,
       
   759        survey_record=record_entity,
       
   760        survey_logic=self._params['logic'],
       
   761        read_only=True)
       
   762     survey_form.getFields()
       
   763     context['survey_form'] = survey_form
       
   764 
       
   765     template = params['record_template']
       
   766     return responses.respond(request, template, context)
   715 
   767 
   716   @decorators.merge_params
   768   @decorators.merge_params
   717   @decorators.check_access
   769   @decorators.check_access
   718   def exportSerialized(self, request, access_type, page_name=None,
   770   def exportSerialized(self, request, access_type, page_name=None,
   719                        params=None, **kwargs):
   771                        params=None, **kwargs):
   877 delete = decorators.view(view.delete)
   929 delete = decorators.view(view.delete)
   878 json = decorators.view(view.exportSerialized)
   930 json = decorators.view(view.exportSerialized)
   879 list = decorators.view(view.list)
   931 list = decorators.view(view.list)
   880 public = decorators.view(view.public)
   932 public = decorators.view(view.public)
   881 pick = decorators.view(view.pick)
   933 pick = decorators.view(view.pick)
       
   934 record = decorators.view(view.viewRecord)
   882 results = decorators.view(view.viewResults)
   935 results = decorators.view(view.viewResults)
   883 take = decorators.view(view.take)
   936 take = decorators.view(view.take)