def update(connection: Connection, data: dict): """ Update a survey (title, questions). You can also add or modify questions here. Note that this creates a new survey (with new submissions, etc), copying everything from the old survey. The old survey's title will be changed to end with "(new version created on <time>)". :param connection: a SQLAlchemy Connection :param data: JSON containing the UUID of the survey and fields to update. """ survey_id = data['survey_id'] email = data['email'] existing_survey = survey_select(connection, survey_id, email=email) if 'survey_metadata' not in data: data['survey_metadata'] = existing_survey.survey_metadata update_time = datetime.datetime.now() with connection.begin(): new_title = '{} (new version created on {})'.format( existing_survey.survey_title, update_time.isoformat()) executable = update_record(survey_table, 'survey_id', survey_id, survey_title=new_title) exc = [('survey_title_survey_owner_key', SurveyAlreadyExistsError(new_title))] execute_with_exceptions(connection, executable, exc) new_survey_id = _create_survey(connection, data) return get_one(connection, new_survey_id, email=email)
def testUpdateRecord(self): auth_user_id = connection.execute(auth_user_table.select().where( auth_user_table.c.email == 'test_email')).first().auth_user_id exec_stmt = connection.execute(survey_insert( survey_title='update me', survey_metadata={}, auth_user_id=auth_user_id)) survey_id = exec_stmt.inserted_primary_key[0] connection.execute(update_record(survey_table, 'survey_id', survey_id, survey_title='updated')) condition = survey_table.c.survey_id == survey_id new_record = connection.execute( survey_table.select().where(condition)).first() self.assertEqual(new_record.survey_title, 'updated') self.assertNotEqual(new_record.survey_last_update_time, new_record.created_on) connection.execute(update_record( survey_table, 'survey_id', survey_id, values_dict={'survey_title': 'update2'})) new_record = connection.execute(survey_table.select().where( condition)).first() self.assertEqual(new_record.survey_title, 'update2') self.assertRaises(TypeError, update_record, survey_table, 'survey_id', survey_id, values_dict={'survey_title': 'updated2'}, survey_title='updated3') self.assertRaises(TypeError, update_record, survey_table, 'survey_id', survey_id) connection.execute(delete_record(survey_table, 'survey_id', survey_id)) if __name__ == '__main__': unittest.main()
def set_api_token(*, expiration=timedelta(days=60), token: str, auth_user_id: str) -> Update: """ Set a new API token for the given user. :param expiration: how long the token will be valid, 60 days by default. :param token: the token to set. Use generate_api_token() :param auth_user_id: the id of the user :return: The Update object. Execute this! """ hashed_token = bcrypt_sha256.encrypt(token) expiration_time = datetime.now() + expiration return update_record(auth_user_table, 'auth_user_id', auth_user_id, token=hashed_token, expires_on=expiration_time)