def post(self): data = UserParser.parser.parse_args() data['semester'] = StudentRegister.student_parser.parse_args( )['semester'] # ensure username is proper if not data['username'][0].isalpha(): return {"message": f"Invalid username."}, 400 if UserModel.find_by_username(data['username']): return { "message": f"User with username: {data['username']} already exists." }, 400 if UserModel.find_by_code(data['code']): return { "message": f"User with code: {data['code']} already exists." }, 400 if UserModel.find_by_email(data['email']): return { "message": f"User with email: {data['email']} already exists." }, 400 temp_pass = bcrypt.generate_password_hash(data['password'], 10).decode('UTF-8') data['password'] = temp_pass user = StudentModel(**data) # unpacking the dictionary user.save_to_db() return {"message": f"Student was created successfully."}, 201
def post(self): data = UserParser.parser.parse_args() if not data['username'][0].isalpha(): return {'message': 'Invalid username.'}, 400 if UserModel.find_by_username(data['username']): return { "message": f"User with username: {data['username']} already exists." }, 400 if UserModel.find_by_code(data['code']): return { "message": f"User with code: {data['code']} already exists." }, 400 if UserModel.find_by_email(data['email']): return { "message": f"User with email: {data['email']} already exists." }, 400 temp_pass = bcrypt.generate_password_hash(data['password'], 10).decode('UTF-8') data['password'] = temp_pass user = AdminModel(**data) # unpacking the dictionary user.save_to_db() return {"message": f"Admin was created successfully."}, 201
def get(self): data = RegisterUser.parser.parse_args() user = UserModel.find_by_email(data["email"]) if user: return user.json() else: return {"message": "no user with that email exists"}, 404
def get_user(user_email): user = UserModel.find_by_email(user_email) if not user: response = build_response({'error message': 'User not found'}, 404) return response response = build_response({'mail': user.email}) return response
def login_user(user_data): user_email = user_data['email'] expires = timedelta(seconds=int(os.environ.get('EXPIRE_TIME', '1200'))) user = UserModel.find_by_email(user_email) if user and safe_str_cmp(user.password, user_data['password']): access_token = create_access_token(identity=user.id, expires_delta=expires) response = build_response({'token': access_token}) return response response = build_response({'error message': 'Invalid credentials'}, 403) return response
def post(self): data = RegisterUser.parser.parse_args() user = UserModel(**data) if (UserModel.find_by_email(data["email"]) == None): try: user.save_to_db() return user.json(), 201 except: return { "message": "An error occurred while creating the user" }, 500 else: return {"message": "User with that email already exists"}, 400
def authorize(): google = oauth.create_client('google') token = google.authorize_access_token() resp = google.get('userinfo', token=token) user_info = resp.json() # do something with the token and profile session['email'] = user_info['email'] my_email = user_info['email'] usr = UserModel.find_by_email(my_email) code = None if usr: return redirect('https://booklick.me/students/' + str(usr.code)) else: return redirect('https://booklick.me/')
def post_user(user_data): if not user_data: response = build_response({'error message': 'Wrong data format'}, 400) return response user_email, user_password = user_data['email'], user_data['password'] if not user_facade.email_validation(user_email): response = build_response( {'error message': 'Email has an incorrect format'}, 400) return response if UserModel.find_by_email(user_email): response = build_response( {'error message': 'Email already exists'}, 409) return response user = UserModel(user_email, user_password) user.save() response = build_response({'email': user.email}) return response
def put_user(user_data, user_id): user = UserModel.find_by_id(user_id) # Buscamos el user por id if user: # Si existe ese user # Comprobamos si existe ya un user con ese email user_email = UserModel.find_by_email(user_data['email']) if not user_email: # Si el email está disponible: machacamos los datos if not user_facade.email_validation(user_data['email']): response = build_response( {'error message': 'Email has an incorrect format'}, 400) return response user.email = user_data['email'] user.password = user_data['password'] else: # El email NO está disponible: devolvemos un error 409 response = build_response( {'error message': 'Email alrady exists'}, 409) return response else: # Si no existe ese user, devolvemos un error 404 response = build_response({'error message': 'User not found'}, 404) return response user.save() # Guardo la información response = build_response({'email': user.email}) return response