Esempio n. 1
0
    def testGetFreeTitle(self):
        auth_user_id = connection.execute(auth_user_table.select().where(
            auth_user_table.c.email == 'test_email')).first().auth_user_id

        self.assertEqual(
            get_free_title(connection, 'test insert', auth_user_id),
            'test insert')

        stmt = survey_insert(survey_title='test insert',
                             survey_metadata={},
                             auth_user_id=auth_user_id)
        connection.execute(stmt)
        self.assertEqual(
            get_free_title(connection, 'test insert', auth_user_id),
            'test insert(1)')
        stmt2 = survey_insert(survey_title='test insert(1)',
                              survey_metadata={},
                              auth_user_id=auth_user_id)
        connection.execute(stmt2)
        self.assertEqual(
            get_free_title(connection, 'test insert', auth_user_id),
            'test insert(2)')
Esempio n. 2
0
def _create_survey(connection: Connection, data: dict) -> str:
    """
    Use the given connection to create a survey within a transaction. If
    this is an update to an existing survey, it will also copy over existing
    submissions.

    :param connection: the SQLAlchemy connection used for the transaction
    :param data: a JSON representation of the survey
    :return: the UUID of the survey in the database
    """
    is_update = 'survey_id' in data

    email = data['email']
    user_id = get_auth_user_by_email(connection, email).auth_user_id
    title = data['survey_title']
    data_q = data['questions']

    # First, create an entry in the survey table
    safe_title = get_free_title(connection, title, user_id)
    survey_values = {
        'auth_user_id': user_id,
        'survey_metadata': data['survey_metadata'],
        'survey_title': safe_title}
    executable = survey_insert(**survey_values)
    exc = [('survey_title_survey_owner_key',
            SurveyAlreadyExistsError(safe_title))]
    result = execute_with_exceptions(connection, executable, exc)
    survey_id = result.inserted_primary_key[0]

    # a map of old submission_id to new submission_id
    submission_map = None
    if is_update:
        submission_map = {
            entry[0]: entry[1] for entry in
            _copy_submission_entries(
                connection,
                data['survey_id'],
                survey_id,
                data['email'])
        }

    # Now insert questions.  Inserting branches has to come afterward so
    # that the question_id values actually exist in the tables.
    questions = list(_create_questions(connection, data_q, survey_id,
                                       submission_map=submission_map))
    if -1 not in set(q['question_to_sequence_number'] for q in questions):
        raise SurveyDoesNotEndError()
    _create_branches(connection, data_q, questions, survey_id)

    return survey_id