コード例 #1
0
    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')
コード例 #2
0
def user_verification(verification_token):
    """
    Verify user's email

    :param verification_token: Token for confirmation email
    """
    email = confirm_verification_token(verification_token)
    user = User.objects(email=email).get()
    if user.is_verified:
        response_with(response_code.INVALID_INPUT_422)
    else:
        user.update(is_verified=True)
        return response_with(response_code.SUCCESS_200, value={'message': 'Email verified, you can proceed login now'})
コード例 #3
0
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')
コード例 #4
0
def create_list():
    """
    Create list of board
    """
    data = request.get_json()
    if data:
        new_list = List(title=data['title'])
        new_list.save()
        board = Board.objects(id=data['id_board']).get()
        board.lists.append(new_list)
        board.save()
        list_schema = ListSchema()
        new_list = list_schema.dump(new_list)
        return response_with(response_code.SUCCESS_201, value={'list': new_list})
    else:
        return response_with(response_code.BAD_REQUEST_400, message='Could not get JSON or JSON empty')
コード例 #5
0
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})
コード例 #6
0
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')
コード例 #7
0
def delete_board(board_id):
    """
    Delete user
    :param board_id: The ID of board
    """
    board = Board.objects(id=board_id).get()
    board.delete()
    return response_with(response_code.SUCCESS_201)
コード例 #8
0
def update_board(board_id):
    data = request.get_json()
    if data:
        board = Board.objects(id=board_id).get()
        if data.get('title'):
            if data.get('title'):
                board.update(title=data['title'])
            board = Board.objects(id=board_id).get()
            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.MISSING_PARAMETERS_422,
                                 message='Check you JSON request')
    else:
        return response_with(response_code.BAD_REQUEST_400,
                             message='Could not get JSON or JSON empty')
コード例 #9
0
def delete_user(user_id):
    """
    Delete user

    :param user_id: User's ID
    """
    user = User.objects(id=user_id).get()
    user.delete()
    return response_with(response_code.SUCCESS_201)
コード例 #10
0
def get_board_by_id(board_id):
    """
    Get board by ID
    :param board_id: The ID of board
    """
    board = Board.objects(id=board_id).get()
    board_schema = BoardSchema(only=['id', 'title', 'lists'])
    board = board_schema.dump(board)
    return response_with(response_code.SUCCESS_200, value={'boards': board})
コード例 #11
0
def get_all_lists(id):
    """
    Get all lists's
    :param id: List ID
    """
    getted_list = List.objects(id=id).get()
    list_schema = ListSchema()
    getted_list = list_schema.dump(getted_list)
    return response_with(response_code.SUCCESS_200, value={'list': getted_list})
コード例 #12
0
 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})
コード例 #13
0
    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)
コード例 #14
0
def get_user_by_id(user_id):
    """
    Getting user's info by ID

    :param user_id: User's ID
    """
    user = User.objects(id=user_id).get()
    boards = Board.objects(user=user).all()
    user_schema = UserSchema(exclude=['password'])
    user = user_schema.dump(user)
    board_schema = BoardSchema(many=True, only=['id', 'title'])
    boards = board_schema.dump(boards)
    user['boards'] = boards
    return response_with(response_code.SUCCESS_200, value={'user': user})
コード例 #15
0
 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')
コード例 #16
0
 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')
コード例 #17
0
def update_user(user_id):
    """
    Update user's attributes

    :param user_id: User's ID
    """
    data = request.get_json()
    if data:
        user = User.objects(id=user_id).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(id=user_id).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')
コード例 #18
0
 def not_found(e):
     logging.error(e)
     return response_with(response_code.SERVER_ERROR_500)
コード例 #19
0
 def not_found(e):
     logging.error(e)
     return response_with(response_code.NOT_FOUND_404)
コード例 #20
0
 def not_found(e):
     logging.error(e)
     return response_with(response_code.UNAUTHORIZED_401)