def viewRecord(self, request, access_type, page_name=None, params=None, **kwargs): """View that allows the user to see the contents of a single SurveyRecord. For params see base.View.public() """ survey_logic = params['logic'] record_logic = survey_logic.getRecordLogic() try: survey_entity = survey_logic.getFromKeyFieldsOr404(kwargs) except out_of_band.Error, error: return responses.errorResponse( error, request, template=params['error_public'])
def viewResults(self, request, access_type, page_name=None, params=None, **kwargs): """View that lists all SurveyRecords which are of interest to the user. For params see base.View.public(). """ # TODO: this view could also contain statistics for the Survey survey_logic = params['logic'] record_logic = survey_logic.getRecordLogic() try: entity = survey_logic.getFromKeyFieldsOr404(kwargs) except out_of_band.Error, error: return responses.errorResponse( error, request, template=params['error_public'])
def takePost(self, request, template, context, params, survey_form, entity, record, **kwargs): """Handles the POST request for the Survey's take page. Args: template: the template used for this view survey_form: instance of SurveyTakeForm entity: the Survey entity record: a SurveyRecord entity rest: see base.View.public() """ survey_logic = params['logic'] record_logic = survey_logic.getRecordLogic() if not survey_form.is_valid(): # show the form errors return self._constructResponse(request, entity=entity, context=context, form=survey_form, params=params, template=template) # retrieve the data from the form _, properties = forms_helper.collectCleanedFields(survey_form) # add the required SurveyRecord properties properties['user'] = user_logic.getCurrentUser() properties['survey'] = entity properties['modified'] = db.DateTimeProperty.now() # call the hook method before updating the SurveyRecord self._takePost(request, params, entity, record, properties) # update the record entity if any and clear all dynamic properties record = record_logic.updateOrCreateFromFields(record, properties, clear_dynamic=True) # get the path to redirect the user to path = self._getRedirectOnSuccessfulTake(request, params, entity, record) return http.HttpResponseRedirect(path)
def viewRecord(self, request, access_type, page_name=None, params=None, **kwargs): """View that allows the user to see the contents of a single SurveyRecord. For params see base.View.public() """ survey_logic = params['logic'] record_logic = survey_logic.getRecordLogic() try: survey_entity = survey_logic.getFromKeyFieldsOr404(kwargs) except out_of_band.Error, error: return responses.errorResponse(error, request, template=params['error_public'])
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.getCurrentUser() filter = {'survey': survey, 'user': user_entity} return record_logic.getForFields(filter, unique=True)
def viewResults(self, request, access_type, page_name=None, params=None, **kwargs): """View that lists all SurveyRecords which are of interest to the user. For params see base.View.public(). """ # TODO: this view could also contain statistics for the Survey survey_logic = params['logic'] record_logic = survey_logic.getRecordLogic() try: entity = survey_logic.getFromKeyFieldsOr404(kwargs) except out_of_band.Error, error: return responses.errorResponse(error, request, template=params['error_public'])
def takePost(self, request, template, context, params, survey_form, entity, record, **kwargs): """Handles the POST request for the Survey's take page. Args: template: the template used for this view survey_form: instance of SurveyTakeForm entity: the Survey entity record: a SurveyRecord entity rest: see base.View.public() """ survey_logic = params['logic'] record_logic = survey_logic.getRecordLogic() if not survey_form.is_valid(): # show the form errors return self._constructResponse(request, entity=entity, context=context, form=survey_form, params=params, template=template) # retrieve the data from the form _, properties = forms_helper.collectCleanedFields(survey_form) # add the required SurveyRecord properties properties['user'] = user_logic.getForCurrentAccount() properties['survey'] = entity # call the hook method before updating the SurveyRecord self._takePost(request, params, entity, record, properties) # update the record entity if any and clear all dynamic properties record_logic.updateOrCreateFromFields(record, properties, clear_dynamic=True) # TODO: add notice to page that the response has been saved successfully # redirect to the same page for now redirect = request.path return http.HttpResponseRedirect(redirect)
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 getMenusForScope(self, entity, params, id, user): """List featured surveys if after the survey_start date and before survey_end an iff the current user has the right taking access. Args: entity: entity which is the scope for a Survey params: params from the requesting View id: GAE user instance for the current user user: User entity from the current user """ # only list surveys for registered users if not user: return [] survey_params = self.getParams().copy() survey_logic = survey_params['logic'] record_logic = survey_logic.getRecordLogic() # filter all featured surveys for the given entity filter = { 'prefix': params['document_prefix'], 'scope_path': entity.key().id_or_name(), 'is_featured': True, } survey_entities = survey_logic.getForFields(filter) submenus = [] # get the rights checker rights = self._params['rights'] rights.setCurrentUser(id, user) # cache ACL survey_rights = {} # add a link to all featured active surveys the user can take for survey_entity in survey_entities: if survey_entity.taking_access not in survey_rights: # we have not determined if this user has the given type of access # check if the current user is allowed to visit the take Survey page allowed_to_take = False try: rights.checkIsSurveyTakeable( { 'key_name': survey_entity.key().name(), 'prefix': survey_entity.prefix, 'scope_path': survey_entity.scope_path, 'link_id': survey_entity.link_id, 'user': user }, survey_logic, check_time=False) allowed_to_take = True except: pass # cache ACL for a given entity.taking_access survey_rights[survey_entity.taking_access] = allowed_to_take if not allowed_to_take: # not allowed to take this survey continue elif not survey_rights[survey_entity.taking_access]: # we already determined that the user doens't have access to this type continue if not timeline.isActivePeriod(survey_entity, 'survey'): # this survey is not active right now continue # check if any SurveyRecord is available for this survey filter = {'survey': survey_entity, 'user': user} survey_record = record_logic.getForFields(filter, unique=True) if survey_record: taken_status = "" else: # no SurveyRecord available so we mark the survey as new taken_status = "(new)" submenu = (redirects.getTakeSurveyRedirect(survey_entity, survey_params), 'Survey ' + taken_status + ': ' + survey_entity.short_name, 'show') submenus.append(submenu) return submenus
def getMenusForScope(self, entity, params, id, user): """List featured surveys if after the survey_start date and before survey_end an iff the current user has the right taking access. Args: entity: entity which is the scope for a Survey params: params from the requesting View id: GAE user instance for the current user user: User entity from the current user """ # only list surveys for registered users if not user: return [] survey_params = self.getParams().copy() survey_logic = survey_params['logic'] record_logic = survey_logic.getRecordLogic() # filter all featured surveys for the given entity filter = { 'prefix' : params['url_name'], 'scope_path': entity.key().id_or_name(), 'is_featured': True, } survey_entities = survey_logic.getForFields(filter) submenus = [] # get the rights checker rights = self._params['rights'] rights.setCurrentUser(id, user) # cache ACL survey_rights = {} # add a link to all featured active surveys the user can take for survey_entity in survey_entities: if survey_entity.taking_access not in survey_rights: # we have not determined if this user has the given type of access # check if the current user is allowed to visit the take Survey page allowed_to_take = False try: rights.checkIsSurveyTakeable( {'key_name': survey_entity.key().name(), 'prefix': survey_entity.prefix, 'scope_path': survey_entity.scope_path, 'link_id': survey_entity.link_id, 'user': user}, survey_logic, check_time=False) allowed_to_take = True except: pass # cache ACL for a given entity.taking_access survey_rights[survey_entity.taking_access] = allowed_to_take if not allowed_to_take: # not allowed to take this survey continue elif not survey_rights[survey_entity.taking_access]: # we already determined that the user doens't have access to this type continue if not timeline.isActivePeriod(survey_entity, 'survey'): # this survey is not active right now continue # check if any SurveyRecord is available for this survey filter = {'survey': survey_entity, 'user': user} survey_record = record_logic.getForFields(filter, unique=True) if survey_record: taken_status = "" else: # no SurveyRecord available so we mark the survey as new taken_status = "(new)" submenu = (redirects.getTakeSurveyRedirect(survey_entity, survey_params), 'Survey ' + taken_status + ': ' + survey_entity.short_name, 'show') submenus.append(submenu) return submenus