def _create_submission(self, survey): # Unauthenticated submissions are only allowed if the survey_type is # 'public'. authenticated = super(self.__class__, self).is_authenticated() if not authenticated: if survey.survey_type == 'public': self._check_xsrf_cookie() else: raise exc.Unauthorized() # If logged in, add enumerator if self.current_user_model is not None: try: enumerator = self._get_model( self.data['enumerator_user_id'], model_cls=User ) except KeyError: self.data['enumerator'] = self.current_user_model else: self.data['enumerator'] = enumerator self.data['survey'] = survey with self.session.begin(): # create a list of Answer models if 'answers' in self.data: raw_answers = self.data['answers'] answers = [ _create_answer(self.session, answer) for answer in raw_answers ] self.data['answers'] = answers self.data['submission_type'] = survey.survey_type + '_submission' submission = construct_submission(**self.data) # add the answer models # if 'answers' in self.data: # submission.answers = answers # add the submission self.session.add(submission) self.session.flush() for answer in submission.answers: answer.main_answer = ( self.session .query(ANSWER_TYPES[answer.answer_type].main_answer) .filter_by(id=answer.id) .scalar() ) skipped_question = skipped_required(survey, submission.answers) if skipped_question is not None: raise RequiredQuestionSkipped( '{} skipped'.format(skipped_question) ) return submission
def _create_demo_user(session): with session.begin(): user = Administrator(name="demo_user", emails=[Email(address="*****@*****.**")]) survey = models.construct_survey( title={"English": "Demo Education Survey"}, survey_type="public", url_slug="demo", nodes=[ models.construct_survey_node( node=models.construct_node(type_constraint="photo", title={"English": "Photo of Facility Exterior"}) ), models.construct_survey_node( node=models.construct_node( type_constraint="facility", title={"English": "Facility"}, hint={"English": ("Select the facility from the list, or add" " a new one.")}, logic={"slat": -85, "nlat": 85, "wlng": -180, "elng": 180}, ) ), models.construct_survey_node( node=models.construct_node( type_constraint="multiple_choice", title={"English": "Education Type"}, choices=[ models.Choice(choice_text={"English": "public"}), models.Choice(choice_text={"English": "private"}), ], ) ), models.construct_survey_node( node=models.construct_node( type_constraint="multiple_choice", title={"English": "Education Level"}, allow_other=True, choices=[ models.Choice(choice_text={"English": "primary"}), models.Choice(choice_text={"English": "secondary"}), models.Choice(choice_text={"English": "both"}), ], ) ), models.construct_survey_node( node=models.construct_node( type_constraint="integer", title={"English": "Number of Students"}, logic={"min": 0} ) ), ], ) user.surveys.append(survey) session.add(user) session.flush() survey.submissions.extend( [ models.construct_submission( submission_type="public_submission", submitter_name="Demo Submitter 1", submission_time=(datetime.datetime.now() - datetime.timedelta(days=1)), save_time=(datetime.datetime.now() - datetime.timedelta(days=1)), answers=[ models.construct_answer( survey_node=survey.nodes[1], type_constraint="facility", answer={ "facility_id": 1, "lat": 40.8, "lng": -73.9, "facility_name": "Demo Facility 1", "facility_sector": "Demo", }, ), models.construct_answer( survey_node=survey.nodes[2], type_constraint="multiple_choice", answer=survey.nodes[2].node.choices[1].id, ), models.construct_answer( survey_node=survey.nodes[3], type_constraint="multiple_choice", other="Technical" ), models.construct_answer(survey_node=survey.nodes[4], type_constraint="integer", answer=200), ], ), models.construct_submission( submission_type="public_submission", submitter_name="Demo Submitter 2", submission_time=(datetime.datetime.now() - datetime.timedelta(days=4)), save_time=(datetime.datetime.now() - datetime.timedelta(days=4)), answers=[ models.construct_answer( survey_node=survey.nodes[1], type_constraint="facility", answer={ "facility_id": 2, "lat": 42, "lng": -74, "facility_name": "Demo Facility 2", "facility_sector": "Demo", }, ), models.construct_answer( survey_node=survey.nodes[2], type_constraint="multiple_choice", answer=survey.nodes[2].node.choices[0].id, ), models.construct_answer( survey_node=survey.nodes[3], type_constraint="multiple_choice", answer=survey.nodes[3].node.choices[0].id, ), models.construct_answer(survey_node=survey.nodes[4], type_constraint="integer", answer=300), ], ), ] ) session.add(survey) return user
def _create_demo_user(session): with session.begin(): user = Administrator( name='demo_user', emails=[Email(address='*****@*****.**')], ) survey = models.construct_survey( title={'English': 'Demo Education Survey'}, survey_type='public', url_slug='demo', nodes=[ models.construct_survey_node( node=models.construct_node( type_constraint='photo', title={'English': 'Photo of Facility Exterior'} ) ), models.construct_survey_node( node=models.construct_node( type_constraint='facility', title={'English': 'Facility'}, hint={'English': ( 'Select the facility from the list, or add' ' a new one.' )}, logic={ 'slat': -85, 'nlat': 85, 'wlng': -180, 'elng': 180, } ) ), models.construct_survey_node( node=models.construct_node( type_constraint='multiple_choice', title={'English': 'Education Type'}, choices=[ models.Choice( choice_text={ 'English': 'public', } ), models.Choice( choice_text={ 'English': 'private', } ) ] ) ), models.construct_survey_node( node=models.construct_node( type_constraint='multiple_choice', title={'English': 'Education Level'}, allow_other=True, choices=[ models.Choice( choice_text={ 'English': 'primary', } ), models.Choice( choice_text={ 'English': 'secondary', } ), models.Choice( choice_text={ 'English': 'both', } ) ] ) ), models.construct_survey_node( node=models.construct_node( type_constraint='integer', title={'English': 'Number of Students'}, logic={'min': 0} ) ), ], ) user.surveys.append(survey) session.add(user) session.flush() survey.submissions.extend([ models.construct_submission( submission_type='public_submission', submitter_name='Demo Submitter 1', submission_time=( datetime.datetime.now() - datetime.timedelta(days=1) ), save_time=( datetime.datetime.now() - datetime.timedelta(days=1) ), answers=[ models.construct_answer( survey_node=survey.nodes[1], type_constraint='facility', answer={ 'facility_id': 1, 'lat': 40.8, 'lng': -73.9, 'facility_name': 'Demo Facility 1', 'facility_sector': 'Demo', }, ), models.construct_answer( survey_node=survey.nodes[2], type_constraint='multiple_choice', answer=survey.nodes[2].node.choices[1].id, ), models.construct_answer( survey_node=survey.nodes[3], type_constraint='multiple_choice', other='Technical', ), models.construct_answer( survey_node=survey.nodes[4], type_constraint='integer', answer=200, ), ], ), models.construct_submission( submission_type='public_submission', submitter_name='Demo Submitter 2', submission_time=( datetime.datetime.now() - datetime.timedelta(days=4) ), save_time=( datetime.datetime.now() - datetime.timedelta(days=4) ), answers=[ models.construct_answer( survey_node=survey.nodes[1], type_constraint='facility', answer={ 'facility_id': 2, 'lat': 42, 'lng': -74, 'facility_name': 'Demo Facility 2', 'facility_sector': 'Demo', }, ), models.construct_answer( survey_node=survey.nodes[2], type_constraint='multiple_choice', answer=survey.nodes[2].node.choices[0].id, ), models.construct_answer( survey_node=survey.nodes[3], type_constraint='multiple_choice', answer=survey.nodes[3].node.choices[0].id, ), models.construct_answer( survey_node=survey.nodes[4], type_constraint='integer', answer=300, ), ], ), ]) session.add(survey) return user