def save_session(words, chars, accuracy): user = current_user try: session = Session.create(words=words, chars=chars, accuracy=accuracy, user_id=user.id) except IntegrityError: db.session.rollback() raise InvalidUsage.unknown_error() return session
def login(): data = request.json try: email = data.pop('email', None) password = data.pop('password', None) if email is None or password is None: return jsonify({'error': True, 'message':'Please supply your email and password!'}), 400 else: try: user = User.get(email=email) except: return jsonify({'error': True, 'message':'User not found!'}), 400 if not user.check_password(password): return jsonify({'error': True, 'message':'Invalid password!'}), 400 else: sesh = Session.create(user=user) sesh.save() session['session_token'] = sesh.token session.modified = True return jsonify({'error':False, 'message':'Successfully logged in!', 'data': user.to_dict}), 200 except: return jsonify({'error': True, 'message':'unknown error'}), 500
def register(): data = request.json password = data.pop("password") """ try: customer_id, account_id = create_account({ "title" : data["title"], "name" : data["name"], "last_name" : "", "dob" : data["dob"], "nationality" : data["nationality"], "email" : data["email"] }) except: pass # return jsonify({"message" : "EEEEEEE"}), 503 """ try: user = User(**data) except: return jsonify({'error':True, 'message': 'bad request'}), 400 user.set_password(password) user.save() starting_balance = 200 if (data['is_organisation']): starting_balance = 0 bank_acc = BankAccount(user=user, balance=starting_balance) bank_acc.save() sesh = Session.create(user=user) sesh.save() session['session_token'] = sesh.token return {'error': False, 'message':'User successfully created!', 'data': user.to_dict}, 201
def setup_general(session=None): """Runs the set-up needed for both local development and production. Also sets up first administrator user.""" if not session: session = boto3.Session() client = session.client('cognito-idp') # create a pool and print the id response = client.create_user_pool( PoolName='serverless-flask-test', Policies={ 'PasswordPolicy': { 'MinimumLength': 8, 'RequireUppercase': True, 'RequireLowercase': True, 'RequireNumbers': True, 'RequireSymbols': True } }, AliasAttributes=[ 'email', ], EmailVerificationMessage= 'Please use this code to verify your account with Serverless Flask: {####} ', EmailVerificationSubject='Email Verification Code for Serverless Flask', MfaConfiguration='OFF', DeviceConfiguration={ 'ChallengeRequiredOnNewDevice': False, 'DeviceOnlyRememberedOnUserPrompt': False }, AdminCreateUserConfig={ 'AllowAdminCreateUserOnly': False, 'UnusedAccountValidityDays': 1, 'InviteMessageTemplate': { 'EmailMessage': 'Welcome to this Serverless Flask example.', 'EmailSubject': 'Serverless Flask Welcomes You.' } }, Schema=[ { 'Name': 'email', 'AttributeDataType': 'String', 'DeveloperOnlyAttribute': False, 'Mutable': True, 'Required': True, }, { 'Name': 'family_name', 'AttributeDataType': 'String', 'DeveloperOnlyAttribute': False, 'Mutable': True, 'Required': True, }, { 'Name': 'given_name', 'AttributeDataType': 'String', 'DeveloperOnlyAttribute': False, 'Mutable': True, 'Required': True, }, ]) pool_name = response.get('UserPool')['Name'] print('The cognito user pool {0!s} has been created.'.format(pool_name)) pool_id = response.get('UserPool')['Id'] print( 'The cognito user pool id is {0!s}. Please save this and set it to your environment variable ' 'COGNITO_POOL_ID'.format(pool_id)) # create an app and print the id response = client.create_user_pool_client( UserPoolId=pool_id, ClientName='Serverless Flask Web App', GenerateSecret=False, # Boto3 doesn't support token auth yet. RefreshTokenValidity=30, ExplicitAuthFlows=[ 'ADMIN_NO_SRP_AUTH', ]) app_name = response.get('UserPoolClient')['ClientName'] print('The cognito app {0!s} has been created for your user pool'.format( app_name)) app_id = response.get('UserPoolClient')['ClientId'] print( 'The cognito app id is {0!s}. Please save this and set it to your environment variable ' 'COGNITO_APP_CLIENT_ID'.format(app_id)) # Create groups in cognito client.create_group(GroupName='admin', UserPoolId=pool_id, Description='Administrators', Precedence=1) print('The admin group has been created for the cognito user pool') client.create_group(GroupName='general', UserPoolId=pool_id, Description='All Users', Precedence=255) print('The general group has been created for the cognito user pool') print('Completed creating Cognito resources') # create the dynamo table if not EditableHTML.exists(): EditableHTML.create(read_capacity_units=1, write_capacity_units=1, wait=True) print("DynamoDB table for editors created") if not appSession.exists(): appSession.create(read_capacity_units=1, write_capacity_units=1, wait=True) print("DynamoDB table for sessions created")