예제 #1
0
def _create_sub_survey(session, sub_survey_dict, parent_node):
    for bucket_dict in sub_survey_dict['buckets']:
        if bucket_dict['bucket_type'] == 'multiple_choice':
            choice_dict = bucket_dict['bucket']
            choice_number = choice_dict.pop('choice_number', None)
            if choice_number is not None:
                bucket_dict['bucket'] = parent_node.choices[choice_number]
            choice_id = choice_dict.pop('choice_id', None)
            if choice_id is not None:
                bucket_dict['bucket'] = session.query(Choice).get(choice_id)
    sub_survey_dict['buckets'] = [
        construct_bucket(**b) for b in sub_survey_dict['buckets']
    ]
    repeatable = sub_survey_dict.get('repeatable', None)
    _cogsn = _create_or_get_survey_node
    sub_survey_dict['nodes'] = [
        _cogsn(session, node, repeatable) for node in sub_survey_dict['nodes']
    ]
    return SubSurvey(**sub_survey_dict)
예제 #2
0
def _create_sub_survey(session, sub_survey_dict, parent_node):
    for bucket_dict in sub_survey_dict['buckets']:
        if bucket_dict['bucket_type'] == 'multiple_choice':
            choice_dict = bucket_dict['bucket']
            choice_number = choice_dict.pop('choice_number', None)
            if choice_number is not None:
                bucket_dict['bucket'] = parent_node.choices[choice_number]
            choice_id = choice_dict.pop('choice_id', None)
            if choice_id is not None:
                bucket_dict['bucket'] = session.query(Choice).get(choice_id)
    sub_survey_dict['buckets'] = [
        construct_bucket(**b) for b in sub_survey_dict['buckets']
    ]
    repeatable = sub_survey_dict.get('repeatable', None)
    _cogsn = _create_or_get_survey_node
    sub_survey_dict['nodes'] = [
        _cogsn(session, node, repeatable) for node in sub_survey_dict['nodes']
    ]
    return SubSurvey(**sub_survey_dict)
예제 #3
0
                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(
예제 #4
0
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)
예제 #5
0
         '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',
예제 #6
0
     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(
         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=(
예제 #8
0
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)