def post(self): """Create user by POST request and send email confirmation""" data = request.get_json() if data: user_schema = UserSchema() data['password'] = User.generate_password(data['password']) user = user_schema.load(data) role = Role.objects(title='user').get() user.roles.append(role) user.save() # Generate email confirmation token and sent it to user token = generate_verification_token(data['email']) logo = base64.b64encode( open("./todo/static/images/logo.png", "rb").read()).decode() html = render_template('email_confirmation.html', logo=logo, token=token) subject = "Please verify your email" send_email.apply_async(args=(user.email, subject, html)) # Get this user information for response user_schema = UserSchema(exclude=['password']) user = user_schema.dump(user) return response_with(response_code.SUCCESS_201, value={'user': user}) else: return response_with(response_code.BAD_REQUEST_400, message='Could not get JSON or JSON empty')
def get_all_boards(): """Get all user's boards""" user = User.objects(email=get_jwt_identity()).get() boards = Board.objects(user=user).all() board_schema = BoardSchema(many=True, only=['id', 'title', 'lists']) boards = board_schema.dump(boards) return response_with(response_code.SUCCESS_200, value={'boards': boards})
def wrapper_func(*args, **kwargs): user = User.objects(email=get_jwt_identity()).get() for role in roles: role = Role.objects(title=role).get() if role in user.roles: return f(*args, **kwargs) return response_with(response_code.FORBIDDEN_403, message='Access denied')
def get(self): """Getting user's profile""" user_email = get_jwt_identity() user = User.objects(email=user_email).get() boards = Board.objects(user=user).all() user_schema = UserSchema(exclude=['password']) user = user_schema.dump(user) board_schema = BoardSchema(many=True, exclude=['user', 'lists']) boards = board_schema.dump(boards) user['boards'] = boards return response_with(response_code.SUCCESS_200, value={'user': user})
def create_board(): """Create board""" data = request.get_json() if data: user = User.objects(email=get_jwt_identity()).get() board = Board(title=data['title'], user=user) board.save() board_schema = BoardSchema(only=['id', 'title', 'user']) board = board_schema.dump(board) return response_with(response_code.SUCCESS_201, value={'board': board}) else: return response_with(response_code.BAD_REQUEST_400, message='Could not get JSON or JSON empty')
def get(self): """ Getting user's avatar :return: Avatar image """ user_email = get_jwt_identity() user = User.objects(email=user_email).get() if user.avatar: return send_from_directory( f"{current_app.root_path}{current_app.config['UPLOAD_FOLDER']}", user.avatar) else: return response_with(response_code.NOT_FOUND_404)
def put(self): data = request.get_json() if data: user_email = get_jwt_identity() user = User.objects(email=user_email).get() if data.get('email') or data.get('first_name') or data.get( 'last_name'): if data.get('email'): user.update(email=data['email']) if data.get('first_name'): user.update(first_name=data['first_name']) if data.get('last_name'): user.update(last_name=data['last_name']) user = User.objects(email=user_email).get() user_schema = UserSchema(exclude=['password']) user = user_schema.dump(user) return response_with(response_code.SUCCESS_201, value={'user': user}) else: return response_with(response_code.MISSING_PARAMETERS_422, message='Check you JSON request') else: return response_with(response_code.MISSING_PARAMETERS_422, message='Could not get JSON or JSON empty')
def login(): data = request.get_json() if data: user = User.objects(email=data['email']).get() if not user: return response_with(response_code.UNAUTHORIZED_401, message='Invalid email or password') if user and not user.is_verified: return response_with(response_code.BAD_REQUEST_400, message='Please confirm your email') if user.verify_password(data['password'], user.password): access_token = create_access_token(identity=user.email) return response_with(response_code.SUCCESS_200, value={'message': f'Logged in as {user.email}', 'access_token': access_token}) else: return response_with(response_code.UNAUTHORIZED_401, message='Invalid email or password') else: return response_with(response_code.MISSING_PARAMETERS_422, message='Could not get JSON or JSON empty')
def post(self): """Upload user's avatar""" file = request.files['avatar'] user_email = get_jwt_identity() user = User.objects(email=user_email).get() if file and allowed_file(file): filename = hashlib.md5( user.email.lower().encode('utf-8')).hexdigest() file.save( f"{current_app.root_path}{current_app.config['UPLOAD_FOLDER']}{filename}" ) if current_app.config['AVATAR_SERVER']: user.avatar = current_app.config['AVATAR_SERVER'] + filename else: user.avatar = filename user.save() user_schema = UserSchema(exclude=['password']) user = user_schema.dump(user) return response_with(response_code.SUCCESS_201, value={'user': user}) else: return response_with( response_code.INVALID_INPUT_422, message='No image in request or image format is not accepted')
def test_user_get_absolute_url(user: User): assert user.get_absolute_url() == f"/users/{user.username}/"