def get(self, email=""): try: get_auth_user_by_email(self.db, email) self.set_secure_cookie('user', email, expires_days=None, # secure=True, httponly=True) self.write('You are now logged in as {}'.format(email)) except UserDoesNotExistError: self.write('No such user')
def create_user(connection: Connection, data: dict) -> dict: """ Registers a new user account. :param connection: a SQLAlchemy Connection :param data: the user's e-mail :return: a response containing the e-mail and whether it was created or already exists in the database """ email = data['email'] try: get_auth_user_by_email(connection, email) except UserDoesNotExistError: with connection.begin(): connection.execute(create_auth_user(email=email)) return json_response({'email': email, 'response': 'Created'}) return json_response({'email': email, 'response': 'Already exists'})
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
def generate_token(connection: Connection, data: dict) -> dict: """ Generates a new API token for a user specified by e-mail address. You can supply a duration in seconds. :param connection: a SQLAlchemy Connection :param data: the user's e-mail and an optional duration :return: the generated token and the token's expiration time """ user = get_auth_user_by_email(connection, data['email']) token = generate_api_token() params = {'token': token, 'auth_user_id': user.auth_user_id} if 'duration' in data: duration = float(data['duration']) if duration > 31536000: raise TokenDurationTooLong(data['duration']) params['expiration'] = timedelta(seconds=duration) with connection.begin(): connection.execute(set_api_token(**params)) updated_user = get_auth_user_by_email(connection, data['email']) return json_response( {'token': token, 'expires_on': updated_user.expires_on.isoformat()})
def _get_user_id(connection: Connection, auth_user_id: str, email: str) -> str: """ Get the auth_user_id, whether it or the e-mail address is provided :param connection: a SQLAlchemy Connection :param auth_user_id: the auth_user_id, if provided :param email: the e-mail address, if provided :return: the auth_user_id """ user_id, user_email = _get_user(auth_user_id, email) # TODO: See if not doing a 3-table join is a performance problem if user_email is not None: user_id = get_auth_user_by_email(connection, user_email).auth_user_id return user_id
def testGetQuestions(self): survey_id = connection.execute(survey_table.select().where( survey_table.c.survey_title == 'test_title')).first().survey_id questions = get_questions(connection, survey_id, email='test_email') self.assertGreater(questions.rowcount, 0) auth_user_id = get_auth_user_by_email(connection, 'test_email').auth_user_id questions = get_questions(connection, survey_id, auth_user_id=auth_user_id) self.assertGreater(questions.rowcount, 0) self.assertRaises(TypeError, get_questions, connection, survey_id) self.assertRaises(TypeError, get_questions, connection, survey_id, auth_user_id='', email='')
def testGetAuthUserByEmail(self): connection.execute(auth_user_table.insert({'email': 'a'})) user = get_auth_user_by_email(connection, 'a') self.assertEqual(user.email, 'a')