Ejemplo n.º 1
0
    def post(self):
        json = request.json
        args = request.args

        RequestValidator.validate_request(
            PostResourceQueryParameterValidateSchema(), args)
        RequestValidator.validate_request(PostPostInputValidateSchema(), json)

        post_gallery = GalleryService.get_gallery_by_id(
            args.get(key="gallery-id"))
        uploader_account = AccountService.find_user_by_email(
            get_jwt_identity())

        created_post = PostListService.create_post(
            content=json["content"],
            title=json["title"],
            is_anonymous=json["is_anonymous"],
            upload_user=uploader_account,
            post_gallery=post_gallery,
            posted_datetime=datetime.now(),
        )

        for image_id in json["images"]:
            ImageService.set_foreign_key(image_id=image_id,
                                         key=created_post.id,
                                         location="post")

        return {}, 201
Ejemplo n.º 2
0
    def patch(self, post_id):
        request_account = AccountService.find_user_by_email(get_jwt_identity())
        post = PostService.get_post_by_post_id(post_id)
        json = request.json

        PostService.check_post_access_permission_of_account(
            post=post, account=request_account)
        RequestValidator.validate_request(PostPatchInputValidateSchema(), json)

        PostService.update_post(
            post=post,
            content=json.get("content", post.content),
            title=json.get("title", post.title),
        )

        new_images_ids, delete_images_ids = PostService.get_diff_of_images(
            post, json.get("image_ids", []))

        for image_id_to_delete in delete_images_ids:
            ImageService.delete_image_by_id(image_id_to_delete)
        for image_id_to_register in new_images_ids:
            ImageService.set_foreign_key(image_id_to_register, post.id, "post")

        db.session.commit()
        return {}, 200
Ejemplo n.º 3
0
    def get(self):
        RequestValidator.validate_request(GetUsernameDuplicationSchema(),
                                          request.args)
        username = request.args["username"]

        usable = UserService.get_user_by_username_or_none(username) is None

        return {"usable": usable}, 200
Ejemplo n.º 4
0
    def get(self):
        RequestValidator.validate_request(GetEmailDuplicationSchema(),
                                          request.args)
        email = request.args["email"]

        usable = UserService.get_user_by_email_or_none(email) is None

        return {"usable": usable}, 200
Ejemplo n.º 5
0
    def get(self):
        RequestValidator.validate_request(
            schema=GetGalleriesQueryParameterValidateSchema(),
            data=request.args,
        )

        galleries = GalleryListService.get_galleries(
            gallery_type=request.args.get("gallery-type", type=int))
        return galleries_schema.dump(galleries), 200
Ejemplo n.º 6
0
    def delete(self, username):
        RequestValidator.validate_request(DeleteUserSchema(), request.json)

        user = UserService.get_user_by_username(username)
        password = request.json.get("password")

        UserService.raise_401_if_not_password_verified(user, password=password)
        user.delete_user()

        return {}, 200
Ejemplo n.º 7
0
    def patch(self, gallery_id):
        RequestValidator.validate_request(PatchGalleryValidateSchema(),
                                          request.json)

        GalleryService.modify_gallery_info(
            gallery=GalleryService.get_gallery_by_id(gallery_id),
            name=request.json.get("name"),
            explain=request.json.get("explain"),
        )

        return {}, 200
Ejemplo n.º 8
0
    def post(self):
        RequestValidator.validate_request(PostEmailVerificationCodeSchema(),
                                          request.args)
        verification_code = request.args["verification-code"]

        user_to_register = self.find_user_by_verificatino_code(
            verification_code)
        self.delete_from_temporary_storage(verification_code)

        DBHelper.add_model(user_to_register)
        return {}, 200
Ejemplo n.º 9
0
    def post(self):
        RequestValidator.validate_request(PostTokenValidateSchema(),
                                          request.json)
        email = request.json.get("email")
        password = request.json.get("password")

        login_user = UserService.get_user_by_email_or_none(email)

        if login_user and login_user.verify_password(password):
            return {"access_token": login_user.generate_access_token()}, 201

        abort(401, "incorrect username or password")
Ejemplo n.º 10
0
    def put(self, username):
        RequestValidator.validate_request(AccountChangePasswordInputSchema(),
                                          request.json)

        json = request.json
        user = UserService.get_user_by_username(username)
        password = json["password"]
        new_password = json["new_password"]

        UserService.raise_401_if_not_password_verified(user, password)
        AccountService.change_account_password(user, new_password)

        return {}, 200
Ejemplo n.º 11
0
    def get(self):
        RequestValidator.validate_request(
            PostGetQueryParameterValidateSchema(), request.args)
        page = request.args.get(key="page", default=1, type=int)
        per_page = request.args.get(key="per_page", default=20, type=int)

        posts = PostListService.get_posts_for_days(7)
        PostListService.sort_posts_by_hot_score(posts)
        posts, number_of_pages = PostListService.paging_posts(
            posts=posts, page=page, per_page=per_page)

        return {
            "posts": posts_schema.dump(posts),
            "number_of_page": number_of_pages,
        }, 200
Ejemplo n.º 12
0
    def validate_json_body(self, json):
        RequestValidator.validate_request(ReportInputSchema(), json)

        content_type = json["reported_content_type"]
        if content_type == ContentType.POST.value:
            validate_schema = ReportPostInputSchema()
        elif content_type == ContentType.COMMENT.value:
            validate_schema = ReportCommentInputSchema()

        RequestValidator.validate_request(validate_schema, json)

        if json.get("comment_id") and json.get("post_id"):
            abort(400, "comment id and post id can be together")

        return json
Ejemplo n.º 13
0
    def put(self, username):
        user = UserService.get_user_by_username(username)
        json = request.json

        RequestValidator.validate_request(UserPutInputSchema(), json)
        AccountService.check_exist_same_username(json["username"])

        UserService.update_user(
            user=user,
            username=json["username"],
            explain=json["user_explain"],
            email=user.email,
            profile_image=user.profile_image,
            password_hash=user.password_hash,
        )

        return {}, 200
Ejemplo n.º 14
0
    def patch(self, comment_id):
        json = request.json
        RequestValidator.validate_request(CommentPatchInputSchema(), json)

        comment = CommentService.get_comment_by_id(comment_id)
        request_account = AccountService.find_user_by_email(
            email=get_jwt_identity())

        CommentService.check_comment_access_permission_of_account(
            comment=comment, account=request_account)

        new_content = json.get("content", None)
        if new_content is None:
            new_content = comment.content

        CommentService.modify_comment(comment=comment, new_content=new_content)

        return {}, 200
Ejemplo n.º 15
0
    def post(self):
        post_account = AccountService.find_user_by_email(get_jwt_identity())

        RequestValidator.validate_request(PostGalleryValidateSchema(),
                                          request.json)
        gallery_type = request.json["gallery_type"]

        if self.is_gallery_type_for_admin(gallery_type):
            verify_admin_jwt_in_request()

        GalleryListService.create_new_gallery(
            gallery_id=request.json["gallery_id"],
            name=request.json["name"],
            explain=request.json["explain"],
            gallery_type=gallery_type,
            manager_user=post_account,
        )

        return {}, 201
Ejemplo n.º 16
0
    def get(self):
        RequestValidator.validate_request(
            PostGetQueryParameterValidateSchema(), request.args)

        gallery_id = request.args.get(key="gallery-id", default=None, type=str)
        username = request.args.get(key="username", default=None, type=str)
        page = request.args.get(key="page", default=1, type=int)
        per_page = request.args.get(key="per-page", default=20, type=int)

        gallery = GalleryService.get_gallery_by_id(
            gallery_id) if gallery_id else None
        user = UserService.get_user_by_username(username) if username else None

        paged_posts = PostListService.get_paginated_posts(gallery=gallery,
                                                          user=user,
                                                          page=page,
                                                          per_page=per_page)

        return {
            "posts": posts_schema.dump(paged_posts.items),
            "number_of_pages": paged_posts.pages,
        }, 200
Ejemplo n.º 17
0
    def post(self):
        RequestValidator.validate_request(AccountRegisterSchema(),
                                          request.json)

        email = request.json.get("email")
        username = request.json.get("username")

        AccountService.check_exist_same_email(email)
        AccountService.check_exist_same_username(username)

        new_user = self.create_new_user(
            username=username,
            email=email,
            password=request.json.get("password"),
        )
        verification_code = AccountService.generate_verification_code()

        self.store_account_data_with_verification_code(verification_code,
                                                       new_user)
        self.send_verification_code_by_email(verification_code, email)

        return {}, 201
Ejemplo n.º 18
0
def validate_post_id(post_id):
    RequestValidator.validate_request(
        data={"post_id": post_id},
        schema=RequestPostlikeApiQueryParameterVaidateSchema())
Ejemplo n.º 19
0
 def validate_request():
     RequestValidator.validate_request(PostCommentParameterSchema(),
                                       request.args)
     RequestValidator.validate_request(CommentInputSchema(),
                                       request.json)
Ejemplo n.º 20
0
 def validate_image_file(image):
     RequestValidator.validate_request(
         PostImageValidateSchema(), {"image": image}
     )
     return image