def _create_or_get_survey_node(session, survey_node_dict, repeatable=None): node_dict = survey_node_dict['node'] if 'id' in node_dict: node = get_model(session, Node, node_dict['id']) else: choices = node_dict.get('choices', None) if choices is not None: node_dict['choices'] = [Choice(**choice) for choice in choices] node = construct_node(**node_dict) survey_node_dict['node'] = node if repeatable is not None: survey_node_dict['repeatable'] = repeatable _css = _create_sub_survey sub_survey_data = survey_node_dict.get('sub_surveys', None) if sub_survey_data is not None: survey_node_dict['sub_surveys'] = [ _css(session, ssd, node) for ssd in sub_survey_data ] return construct_survey_node(**survey_node_dict)
def _create_or_get_survey_node(session, survey_node_dict, repeatable=None): node_dict = survey_node_dict['node'] if 'id' in node_dict: node = get_model(session, Node, node_dict['id']) else: choices = node_dict.get('choices', None) if choices is not None: node_dict['choices'] = [ Choice(**choice) for choice in choices ] node = construct_node(**node_dict) survey_node_dict['node'] = node if repeatable is not None: survey_node_dict['repeatable'] = repeatable _css = _create_sub_survey sub_survey_data = survey_node_dict.get('sub_surveys', None) if sub_survey_data is not None: survey_node_dict['sub_surveys'] = [ _css(session, ssd, node) for ssd in sub_survey_data ] return construct_survey_node(**survey_node_dict)
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
models.construct_survey_node( allow_dont_know=True, node=models.construct_node( type_constraint='integer', title={'English': 'integer' + ' node'}, hint={ 'English': ( 'fill in response for ' + 'integer' + ' node' ) }, allow_multiple=False, ), sub_surveys=[ models.SubSurvey( buckets=[ models.construct_bucket( bucket_type='integer', bucket='(, 2]' ), ], nodes=[ models.construct_survey_node( node=models.construct_node( title={ 'English': 'date sub node (nested)' }, type_constraint='date', ), sub_surveys=[ models.SubSurvey( buckets=[ models.construct_bucket( bucket_type='date', bucket=( '(2011-01-01, 2014-01-02]' ) ), ], nodes=[ models.construct_survey_node( required=True, node=models.construct_node( title={ 'English': ( 'integer sub' ' suuubb node' ' (required)' ) }, type_constraint='integer', ), ), models.construct_survey_node( node=models.construct_node( title={ 'English': ( 'date sub suuubb ' 'node' ) }, type_constraint='date', ), ), models.construct_survey_node( node=models.construct_node( title={ 'English': ( 'text sub suuubb' ' node' ) }, type_constraint='text', ), ) ], ), ], ) ], ), models.SubSurvey( buckets=[ models.construct_bucket( bucket_type='integer', bucket='(2, 5]' ), ], nodes=[ models.construct_survey_node( node=models.construct_node( title={'English': 'text sub 1'}, type_constraint='text', ), ), models.construct_survey_node( node=models.construct_node( title={'English': 'text sub 2'}, type_constraint='text', ), ), models.construct_survey_node( node=models.construct_node( title={'English': 'text sub 3'}, type_constraint='text', ), ), ], ), ], ),
def load_fixtures(engine): """Create test users, surveys, and submissions.""" # creates db schema session = Session(bind=engine, autocommit=True) with session.begin(): creator = models.Administrator( name='test_user', # known ID against which we can test id='b7becd02-1a3f-4c1d-a0e1-286ba121aef4', emails=[models.Email(address='*****@*****.**')], ) creator_b = models.Administrator( name='test_user_b', # known ID against which we can test id='e7becd02-1a3f-4c1d-a0e1-286ba121aef1', emails=[models.Email(address='*****@*****.**')], ) enumerator = models.User( name='test_user', # known ID against which we can test id='a7becd02-1a3f-4c1d-a0e1-286ba121aef3', emails=[models.Email(address='*****@*****.**')], ) node_types = list(models.NODE_TYPES) for node_type in node_types: node_dict = { 'title': { 'English': node_type + '_node' }, 'type_constraint': node_type, } if node_type == 'facility': node_dict['logic'] = { 'slat': 39, 'nlat': 41, 'wlng': -71, 'elng': -69, } survey = models.Survey( title={'English': node_type + '_survey'}, nodes=[ models.construct_survey_node( node=models.construct_node(**node_dict), ), ], ) creator.surveys.append(survey) # Add a single submission per survey regular_submission = models.PublicSubmission( survey=survey, submitter_name='regular') session.add(regular_submission) # Add another survey with known ID single_survey = models.Survey( id='b0816b52-204f-41d4-aaf0-ac6ae2970923', title={'English': 'single_survey'}, nodes=[ models.construct_survey_node( id="60e56824-910c-47aa-b5c0-71493277b43f", node=models.construct_node( id="60e56824-910c-47aa-b5c0-71493277b43f", title={'English': 'integer node'}, type_constraint='integer', ), sub_surveys=[ models.SubSurvey( buckets=[ models.construct_bucket(bucket_type='integer', bucket='(1, 2]'), ], nodes=[ models.construct_survey_node( node=models.construct_node( title={'English': 'decimal node'}, type_constraint='decimal', ), sub_surveys=[ models.SubSurvey(buckets=[ models.construct_bucket( bucket_type='decimal', bucket='(1.3, 2.3]'), ], ), ], ), models.construct_survey_node( node=models.construct_node( title={'English': 'integer node'}, type_constraint='integer', ), sub_surveys=[ models.SubSurvey(buckets=[ models.construct_bucket( bucket_type='integer', bucket='(1, 2]'), ]) ], ), models.construct_survey_node( node=models.construct_node( title={'English': 'date node'}, type_constraint='date', ), sub_surveys=[ models.SubSurvey(buckets=[ models.construct_bucket( bucket_type='date', bucket=( '(2015-1-1, 2015-2-2]')), ], ), ], ) ]), ], ), models.construct_survey_node( id="80e56824-910c-47aa-b5c0-71493277b439", allow_dont_know=True, node=models.construct_node( id="80e56824-910c-47aa-b5c0-71493277b439", title={'English': 'Mutliple Choices'}, type_constraint='multiple_choice', allow_other=True, choices=[ models.Choice( id="99956824-910c-47aa-b5c0-71493277b439", choice_text={"English": "first choice"}), models.Choice( id="11156824-910c-47aa-b5c0-71493277b439", choice_text={"English": "second choice"}), ])), ], ) # Add another survey with known ID for creator_b single_survey_c_b = models.Survey( id='d0816b52-204f-41d4-aaf0-ac6ae2970923', title={'English': 'single_survey_c_b'}, nodes=[], ) # Add an enumerator only survey with known ID single_enum_survey = models.EnumeratorOnlySurvey( id='c0816b52-204f-41d4-aaf0-ac6ae2970925', languages=['English', 'Español', 'Russian'], default_language='Russian', title={ 'English': 'enumerator_only_single_survey', 'Español': 'ENUMERATOR_ONLY_SINGLE_SURVEY', 'Russian': 'enumerator_only_single_survey (Russian)' }, nodes=[ models.construct_survey_node( allow_dont_know=True, node=models.construct_node( title={ 'English': 'Engine Room Photo', 'Russian': 'Engine Room Photo (Russian)', 'Español': 'Photo de Engine Room' }, hint={ 'English': ('Tap the image to capture a photo.'), 'Español': ('Marca la imagen para grabar una photo.'), 'Russian': ('Tap the image to capture a photo.' ' (Russian)') }, type_constraint='photo', allow_multiple=True)), ], enumerators=[creator]) # Add another public submission with a known ID single_regular_submission = models.PublicSubmission( id='b0816b52-204f-41d4-aaf0-ac6ae2970924', survey=single_survey, submitter_name='regular_singular', answers=[ models.construct_answer( survey_node=single_survey.nodes[0], type_constraint='integer', answer=3, ), ]) session.add(single_regular_submission) # Add 100 public submissions over the past 100 days today = datetime.date.today() for i in range(0, 100): sub_time = today - datetime.timedelta(days=i) sub = models.PublicSubmission( survey=single_survey, submitter_name='regular', save_time=sub_time, submission_time=sub_time, ) session.add(sub) # Add surveys to creator and enumerator creator.surveys.append(single_survey) creator_b.surveys.append(single_survey_c_b) creator.surveys.append(single_enum_survey) enumerator.allowed_surveys.append(single_enum_survey) # Finally save the creator and enumerator session.add(creator) session.add(creator_b) session.add(enumerator)
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
models.construct_survey_node( allow_dont_know=True, node=models.construct_node( type_constraint=node_type, title={'English': node_type + ' node'}, logic={ 'wlng': -72, 'elng': -74, 'nlat': 85, 'slat': -85, }, hint={ 'English': ( 'fill in response for ' + node_type + ' node' ) }, allow_multiple=node_type != 'integer', ), sub_surveys=[ models.SubSurvey( buckets=[ models.construct_bucket( bucket_type='integer', bucket='(1, 3]' ), ], nodes=[ models.construct_survey_node( node=models.construct_node( title={ 'English': 'integer sub node (nested)' }, type_constraint='integer', ), sub_surveys=[ models.SubSurvey( buckets=[ models.construct_bucket( bucket_type='integer', bucket='(3, 5]' ), ], nodes=[ models.construct_survey_node( node=models.construct_node( title={ 'English': ( 'integer sub' ' suuubb node' ) }, type_constraint='integer', ), ) ], ), ], ) ], ) for i in range(1) if node_type == 'integer'], ) for node_type in node_types
models.construct_survey_node( allow_dont_know=True, node=models.construct_node( type_constraint=node_type, title={'English': node_type + ' node'}, logic={ 'wlng': -72, 'elng': -74, 'nlat': 85, 'slat': -85, }, hint={ 'English': ('fill in response for ' + node_type + ' node') }, allow_multiple=node_type != 'integer', ), sub_surveys=[ models.SubSurvey( buckets=[ models.construct_bucket(bucket_type='integer', bucket='(1, 3]'), ], nodes=[ models.construct_survey_node( node=models.construct_node( title={ 'English': 'integer sub node (nested)' }, type_constraint='integer', ), sub_surveys=[ models.SubSurvey( buckets=[ models.construct_bucket( bucket_type='integer', bucket='(3, 5]'), ], nodes=[ models.construct_survey_node( node=models.construct_node( title={ 'English': ('integer sub' ' suuubb node') }, type_constraint='integer', ), ) ], ), ], ) ], ) for i in range(1) if node_type == 'integer' ], ) for node_type in node_types
session = Session(bind=engine, autocommit=True) with session.begin(): creator = models.Administrator( name='Stet Tarcoea', emails=[models.Email(address='*****@*****.**')], ) node_types = list(models.NODE_TYPES) education_survey = models.Survey( id='b0816b52-204f-41d4-aaf0-ac6ae2970923', title={'English': 'Education Facility Survey'}, 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='location', title={'English': 'Location (GPS Coordinates)'} ) ), models.construct_survey_node( allow_dont_know=True, node=models.construct_node( type_constraint='text', title={'English': 'Community Name'} ) ),
models.construct_survey_node( allow_dont_know=True, node=models.construct_node( type_constraint='integer', title={'English': 'integer' + ' node'}, hint={ 'English': ('fill in response for ' + 'integer' + ' node') }, allow_multiple=False, ), sub_surveys=[ models.SubSurvey( buckets=[ models.construct_bucket(bucket_type='integer', bucket='(, 2]'), ], nodes=[ models.construct_survey_node( node=models.construct_node( title={ 'English': 'date sub node (nested)' }, type_constraint='date', ), sub_surveys=[ models.SubSurvey( buckets=[ models.construct_bucket( bucket_type='date', bucket=( '(2011-01-01, 2014-01-02]' )), ], nodes=[ models.construct_survey_node( required=True, node=models.construct_node( title={ 'English': ('integer sub' ' suuubb node' ' (required)') }, type_constraint='integer', ), ), models.construct_survey_node( node=models.construct_node( title={ 'English': ('date sub suuubb ' 'node') }, type_constraint='date', ), ), models.construct_survey_node( node=models.construct_node( title={ 'English': ('text sub suuubb' ' node') }, type_constraint='text', ), ) ], ), ], ) ], ), models.SubSurvey( buckets=[ models.construct_bucket(bucket_type='integer', bucket='(2, 5]'), ], nodes=[ models.construct_survey_node( node=models.construct_node( title={'English': 'text sub 1'}, type_constraint='text', ), ), models.construct_survey_node( node=models.construct_node( title={'English': 'text sub 2'}, type_constraint='text', ), ), models.construct_survey_node( node=models.construct_node( title={'English': 'text sub 3'}, type_constraint='text', ), ), ], ), ], ),
def load_fixtures(engine): """Create test users, surveys, and submissions.""" # creates db schema session = Session(bind=engine, autocommit=True) with session.begin(): creator = models.Administrator( name='test_user', # known ID against which we can test id='b7becd02-1a3f-4c1d-a0e1-286ba121aef4', emails=[models.Email(address='*****@*****.**')], ) creator_b = models.Administrator( name='test_user_b', # known ID against which we can test id='e7becd02-1a3f-4c1d-a0e1-286ba121aef1', emails=[models.Email(address='*****@*****.**')], ) enumerator = models.User( name='test_user', # known ID against which we can test id='a7becd02-1a3f-4c1d-a0e1-286ba121aef3', emails=[models.Email(address='*****@*****.**')], ) node_types = list(models.NODE_TYPES) for node_type in node_types: node_dict = { 'title': {'English': node_type + '_node'}, 'type_constraint': node_type, } if node_type == 'facility': node_dict['logic'] = { 'slat': 39, 'nlat': 41, 'wlng': -71, 'elng': -69, } survey = models.Survey( title={'English': node_type + '_survey'}, nodes=[ models.construct_survey_node( node=models.construct_node(**node_dict), ), ], ) creator.surveys.append(survey) # Add a single submission per survey regular_submission = models.PublicSubmission( survey=survey, submitter_name='regular' ) session.add(regular_submission) # Add another survey with known ID single_survey = models.Survey( id='b0816b52-204f-41d4-aaf0-ac6ae2970923', title={'English': 'single_survey'}, nodes=[ models.construct_survey_node( id="60e56824-910c-47aa-b5c0-71493277b43f", node=models.construct_node( id="60e56824-910c-47aa-b5c0-71493277b43f", title={'English': 'integer node'}, type_constraint='integer', ), sub_surveys=[ models.SubSurvey( buckets=[ models.construct_bucket( bucket_type='integer', bucket='(1, 2]' ), ], nodes=[ models.construct_survey_node( node=models.construct_node( title={'English': 'decimal node'}, type_constraint='decimal', ), sub_surveys=[ models.SubSurvey( buckets=[ models.construct_bucket( bucket_type='decimal', bucket='(1.3, 2.3]' ), ], ), ], ), models.construct_survey_node( node=models.construct_node( title={'English': 'integer node'}, type_constraint='integer', ), sub_surveys=[ models.SubSurvey( buckets=[ models.construct_bucket( bucket_type='integer', bucket='(1, 2]' ), ] ) ], ), models.construct_survey_node( node=models.construct_node( title={'English': 'date node'}, type_constraint='date', ), sub_surveys=[ models.SubSurvey( buckets=[ models.construct_bucket( bucket_type='date', bucket=( '(2015-1-1, 2015-2-2]' ) ), ], ), ], ) ] ), ], ), models.construct_survey_node( id="80e56824-910c-47aa-b5c0-71493277b439", allow_dont_know=True, node=models.construct_node( id="80e56824-910c-47aa-b5c0-71493277b439", title={'English': 'Mutliple Choices'}, type_constraint='multiple_choice', allow_other=True, choices=[ models.Choice( id="99956824-910c-47aa-b5c0-71493277b439", choice_text={"English": "first choice"}), models.Choice( id="11156824-910c-47aa-b5c0-71493277b439", choice_text={"English": "second choice"}), ] ) ), ], ) # Add another survey with known ID for creator_b single_survey_c_b = models.Survey( id='d0816b52-204f-41d4-aaf0-ac6ae2970923', title={'English': 'single_survey_c_b'}, nodes=[], ) # Add an enumerator only survey with known ID single_enum_survey = models.EnumeratorOnlySurvey( id='c0816b52-204f-41d4-aaf0-ac6ae2970925', languages=['English', 'Español', 'Russian'], default_language='Russian', title={ 'English': 'enumerator_only_single_survey', 'Español': 'ENUMERATOR_ONLY_SINGLE_SURVEY', 'Russian': 'enumerator_only_single_survey (Russian)' }, nodes=[ models.construct_survey_node( allow_dont_know=True, node=models.construct_node( title={ 'English': 'Engine Room Photo', 'Russian': 'Engine Room Photo (Russian)', 'Español': 'Photo de Engine Room' }, hint={'English': ( 'Tap the image to capture a photo.' ), 'Español': ( 'Marca la imagen para grabar una photo.' ), 'Russian': ( 'Tap the image to capture a photo.' ' (Russian)' )}, type_constraint='photo', allow_multiple=True ) ), ], enumerators=[ creator ] ) # Add another public submission with a known ID single_regular_submission = models.PublicSubmission( id='b0816b52-204f-41d4-aaf0-ac6ae2970924', survey=single_survey, submitter_name='regular_singular', answers=[ models.construct_answer( survey_node=single_survey.nodes[0], type_constraint='integer', answer=3, ), ] ) session.add(single_regular_submission) # Add 100 public submissions over the past 100 days today = datetime.date.today() for i in range(0, 100): sub_time = today - datetime.timedelta(days=i) sub = models.PublicSubmission( survey=single_survey, submitter_name='regular', save_time=sub_time, submission_time=sub_time, ) session.add(sub) # Add surveys to creator and enumerator creator.surveys.append(single_survey) creator_b.surveys.append(single_survey_c_b) creator.surveys.append(single_enum_survey) enumerator.allowed_surveys.append(single_enum_survey) # Finally save the creator and enumerator session.add(creator) session.add(creator_b) session.add(enumerator)