예제 #1
0
    def patch(cls, task_id: int):
        json = request.get_json()

        # Check if the task exists
        task = TaskModel.find_by_id(task_id)
        if not task:
            return generate_message_json(HttpStatusCode.NOT_FOUND.value,
                                         TASK_NOT_FOUND)

        # Check if client is trying to edit readonly fields
        readonly = {"id", "category_id"}
        keys = json.keys()
        forbidden = readonly & keys

        if forbidden:
            return generate_message_json(
                HttpStatusCode.BAD_REQUEST.value,
                FIELD_CANNOT_BE_EDITED.format(str(forbidden)[1:-1]),
            )

        # Check if the client specified non existing attrs

        try:
            check_attr(json.keys(), task)
            for key, value in json.items():
                setattr(task, key, value)

            task.save_to_db()  # Persist chages to db
            return task_schema.dump(task), HttpStatusCode.OK.value
        except AttributeError as ae:
            return generate_message_json(HttpStatusCode.BAD_REQUEST.value,
                                         str(ae))
        except SQLAlchemyError as se:
            return generate_message_json(HttpStatusCode.BAD_REQUEST.value,
                                         str(se))
예제 #2
0
    def patch(cls, category_id: int):
        json = request.get_json()

        # Check if the category exists
        category = CategoryModel.find_by_id(category_id)
        if not category:
            return generate_message_json(HttpStatusCode.NOT_FOUND.value,
                                         CATEGORY_NOT_FOUND)

        # Check if client is trying to edit readonly fields
        readonly = {"id", "user_id", "tasks"}
        keys = json.keys()
        forbidden = readonly & keys

        if forbidden:
            return generate_message_json(
                HttpStatusCode.BAD_REQUEST.value,
                FIELD_CANNOT_BE_EDITED.format(str(forbidden)[1:-1]),
            )

        # Check if the client specified non existing attrs

        try:
            check_attr(json.keys(), category)
            for key, value in json.items():
                setattr(category, key, value)

            category.save_to_db()
            return category_schema.dump(category), HttpStatusCode.OK.value
        except AttributeError as ae:
            return generate_message_json(HttpStatusCode.BAD_REQUEST.value,
                                         str(ae))
        except SQLAlchemyError as se:
            return generate_message_json(HttpStatusCode.BAD_REQUEST.value,
                                         str(se))
예제 #3
0
    def get(cls, task_id: int):
        task = TaskModel.find_by_id(task_id)
        if task:
            return task_schema.dump(task), HttpStatusCode.OK.value

        return generate_message_json(HttpStatusCode.NOT_FOUND.value,
                                     TASK_NOT_FOUND)
예제 #4
0
 def get(cls, category_id: int):
     return generate_message_json(
         HttpStatusCode.OK.value,
         task_list_schema.dump(
             TaskModel.query.filter_by(category_id=category_id)),
         "tasks",
     )
예제 #5
0
    def get(cls, user_id: int):
        user = UserModel.find_by_id(user_id)
        if not user:
            return generate_message_json(HttpStatusCode.NOT_FOUND.value,
                                         USER_NOT_FOUND)

        return user_schema.dump(user), HttpStatusCode.OK.value
예제 #6
0
    def get(cls, category_id: int):
        category = CategoryModel.find_by_id(category_id)
        if category:
            return category_schema.dump(category), HttpStatusCode.OK.value

        return generate_message_json(HttpStatusCode.NOT_FOUND.value,
                                     CATEGORY_NOT_FOUND)
예제 #7
0
 def get(cls, user_id: int):
     return generate_message_json(
         HttpStatusCode.OK.value,
         category_list_schema.dump(
             CategoryModel.query.filter_by(user_id=user_id)),
         "categories",
     )
예제 #8
0
    def delete(cls, task_id: int):
        task = TaskModel.find_by_id(task_id)
        if task:
            task.delete_from_db()
            return "", HttpStatusCode.NO_CONTENT.value

        return generate_message_json(HttpStatusCode.NOT_FOUND.value,
                                     TASK_NOT_FOUND)
예제 #9
0
    def delete(cls, user_id: int):
        user = UserModel.find_by_id(user_id)
        if not user:
            return generate_message_json(HttpStatusCode.NOT_FOUND.value,
                                         USER_NOT_FOUND)

        user.delete_from_db()
        return "", HttpStatusCode.NO_CONTENT.value
예제 #10
0
    def delete(cls, category_id: int):
        category = CategoryModel.find_by_id(category_id)
        if category:
            # NOTE: Deletion will currently fail if the category contains any tasks
            category.delete_from_db()
            return "", HttpStatusCode.NO_CONTENT.value

        return generate_message_json(HttpStatusCode.NOT_FOUND.value,
                                     CATEGORY_NOT_FOUND)
예제 #11
0
    def post(cls):
        json = request.get_json()
        user = user_schema.load(json)

        if UserModel.find_by_username(user.username):
            return generate_message_json(HttpStatusCode.BAD_REQUEST.value,
                                         USER_ALREADY_EXISTS)
        elif len(user.password) < 8:
            return generate_message_json(HttpStatusCode.BAD_REQUEST.value,
                                         PASSWORD_TOO_SHORT)

        # Hash password
        user.password = generate_password_hash(user.password)

        # Save user
        user.save_to_db()

        return generate_message_json(HttpStatusCode.CREATED.value,
                                     CREATED_SUCCESFULLY)
예제 #12
0
    def post(cls, user_id: int):
        json = request.get_json()
        json["user_id"] = user_id
        category = category_schema.load(json)

        if CategoryModel.find_by_user_id_and_title(category.user_id,
                                                   category.title):
            return generate_message_json(HttpStatusCode.BAD_REQUEST.value,
                                         CATEGORY_ALREADY_EXISTS)

        category.save_to_db()

        return category_schema.dump(category), HttpStatusCode.CREATED.value
예제 #13
0
    def post(cls):
        json = request.get_json()
        user = UserModel.find_by_username(json["username"])

        if user and check_password_hash(user.password, json["password"]):
            access_token = create_access_token(identity=user.id, fresh=True)
            refresh_token = create_refresh_token(identity=user.id)

            return (
                {
                    "access_token": access_token,
                    "refresh_token": refresh_token
                },
                HttpStatusCode.OK.value,
            )

        return generate_message_json(HttpStatusCode.UNAUTHORIZED.value,
                                     INVALID_CREDENTIALS)
예제 #14
0
 def post(cls):
     # JTI is the ID of the JWT
     jti = get_raw_jwt()["jti"]
     BLACKLIST.add(jti)
     return generate_message_json(HttpStatusCode.OK.value, "Logged out.")
예제 #15
0
 def post(cls):
     current_user = get_jwt_identity()
     new_token = create_access_token(identity=current_user, fresh=False)
     return generate_message_json(HttpStatusCode.OK.value, new_token,
                                  "access_token")