# HG changeset patch # User Lennard de Rijk # Date 1246615253 -7200 # Node ID 996f381d458cd760c04621df1357b39a89763e48 # Parent 03ada47366355ad1a4d641c1617626c9a719e7de Added _getSurveyRecordFor method to Survey View. This method is used by the Take view to get the right SurveyRecord for the current request. Subclassed Views that want to fetch their records in another way can override this method. diff -r 03ada4736635 -r 996f381d458c app/soc/views/models/survey.py --- a/app/soc/views/models/survey.py Thu Jul 02 18:35:57 2009 +0200 +++ b/app/soc/views/models/survey.py Fri Jul 03 12:00:53 2009 +0200 @@ -474,10 +474,9 @@ """ survey_logic = params['logic'] - record_logic = survey_logic.getRecordLogic() try: - entity = self._logic.getFromKeyFieldsOr404(kwargs) + entity = survey_logic.getFromKeyFieldsOr404(kwargs) except out_of_band.Error, error: return responses.errorResponse( error, request, template=params['error_public']) @@ -490,13 +489,8 @@ context['page_name'] = "%s titled '%s'" % (page_name, entity.title) context['entity'] = entity - user_entity = user_logic.getForCurrentAccount() - # try to get an existing SurveyRecord for the current user - filter = {'survey': entity, - 'user': user_entity} - - survey_record = record_logic.getForFields(filter, unique=True) + survey_record = self._getSurveyRecordFor(entity, request, params) if request.POST: return self.takePost(request, template, context, params, entity, @@ -505,6 +499,29 @@ return self.takeGet(request, template, context, params, entity, survey_record, **kwargs) + def _getSurveyRecordFor(self, survey, request, params): + """Returns the SurveyRecord for the given Survey and request. + + Args: + survey: a Survey entity + request: a Django HTTPRequest object + params: params for the requesting view + + Returns: + An existing SurveyRecord iff any exists for the given Survey, request + and any other conditions that must apply. + """ + + survey_logic = params['logic'] + record_logic = survey_logic.getRecordLogic() + + user_entity = user_logic.getForCurrentAccount() + + filter = {'survey': survey, + 'user': user_entity} + + return record_logic.getForFields(filter, unique=True) + def takeGet(self, request, template, context, params, entity, record, **kwargs): """Handles the GET request for the Survey's take page.