def create(self): """Create a new survey. Uses the current_user_model (i.e. logged-in user) as creator. """ with self.session.begin(): # create a list of Node models _node = _create_or_get_survey_node self.data['nodes'] = [ _node(self.session, node) for node in self.data['nodes'] ] self.data['creator'] = self.current_user_model # pass survey props as kwargs survey = construct_survey(**self.data) self.session.add(survey) return survey
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