Exemplo n.º 1
0
 def assert_user_exist(user_id: int) -> None:
     if user_id is None:
         raise ArgumentInvalid("user_id 不能为 None")
     user = User.query.filter_by(deleted=False).filter_by(
         id=user_id).first()
     if not user:
         raise ObjectsNotExist(f"Post <{user_id}> 不存在")
Exemplo n.º 2
0
 def assert_post_exist(self) -> None:
     self.assert_topic_exist()
     if self.post_id is None:
         raise ArgumentInvalid("post_id 不能为 None")
     post = Post.query.filter_by(deleted=False).filter_by(
         id=self.post_id).first()
     if not post:
         raise ObjectsNotExist(f"Post <{self.post_id}> 不存在")
Exemplo n.º 3
0
    def create_post(self, **kwargs) -> Optional[ResponsePostModel]:
        self.assert_topic_id_is_not_none()
        self.assert_topic_exist()

        user_id = kwargs["user_id"]
        self.assert_user_exist(user_id)

        content = kwargs["content"]
        if content is None or len(content.strip()) <= POST_MINIMUM_WORDS:
            raise ArgumentInvalid(f"Post 文章字数不能少于 <{POST_MINIMUM_WORDS}> 个")
        post = self._model.create(topic_id=self.topic_id, **kwargs)

        return ResponsePostModel(comments_count=0, **post.as_dict())
Exemplo n.º 4
0
    def update(self, **kwargs) -> Optional[ResponseUser]:

        user = self._get_sqlalchemy_instance()
        email = kwargs.get("email")
        password = kwargs.get("password")

        if email and not EmailChecker.is_allowed(email):
            raise ArgumentInvalid(EmailChecker.ERROR_MSG)
            # 验证邮箱

        if password and not PassWordChecker.is_allowed(password):
            raise ArgumentInvalid(PassWordChecker.ERROR_MSG)
            # 验证密码,验证权限

        if password:
            password_hash = generate_password_hash(password)
            kwargs.pop("password")
            kwargs["password_hash"] = password_hash

        ins = user.update(**kwargs)

        return ResponseUser(**ins.as_dict())
Exemplo n.º 5
0
    def create_comment(self, **kwargs) -> Optional[ResponseCommentModel]:

        self.assert_post_exist()
        user_id = kwargs.get("user_id")
        self.assert_user_exist(user_id)

        content = kwargs.get("content")
        if not ContentChecker.is_allowed(content):
            raise ArgumentInvalid(ContentChecker.ERROR_MSG)

        kwargs["post_id"] = self.post_id
        instance = self._model.create(**kwargs)

        return ResponseCommentModel(**instance.as_dict())
Exemplo n.º 6
0
    def create(**kwargs) -> Optional[ResponseUser]:
        email = kwargs.get("email")
        password = kwargs.get("password")

        if not email:
            raise ArgumentRequired("邮件地址不能为空")
        user = User.query.filter_by(deleted=False).filter_by(
            email=email).first()
        if user:
            raise ObjectsDuplicated("邮件为 <%s> 的用户已经注册" % email)
        if password is None:
            raise ArgumentRequired("密码不能为空")
        password = password.strip()

        if not EmailChecker.is_allowed(email):
            raise ArgumentInvalid(EmailChecker.ERROR_MSG)

        if not PassWordChecker.is_allowed(password):
            raise ArgumentInvalid(PassWordChecker.ERROR_MSG)

        kwargs["password_hash"] = generate_password_hash(password)
        ins = User.create(**kwargs)

        return ResponseUser(**ins.as_dict())
Exemplo n.º 7
0
    def parse_args(cls) -> T:
        parser = reqparse.RequestParser()

        for field in cls.__fields__:
            name = field.name
            location = field.location
            required = field.required
            nullable = field.nullable
            parser.add_argument(
                name, location=location, required=required, nullable=nullable, **field.parser_kwargs
            )

        parsed = parser.parse_args()

        for field in cls.__fields__:
            if field.enum_values and field.name in parsed:
                value = parsed[field.name]
                if value is not None and value not in field.enum_values:
                    raise ArgumentInvalid(
                        "参数 [{}] 的值必须在 [{}] 中".format(field.name, field.enum_values)
                    )

        instance = cls(**parsed)
        return instance
Exemplo n.º 8
0
 def assert_post_id_is_not_none(self) -> None:
     if self.id is None:
         raise ArgumentInvalid("post_id 不能为 None")
Exemplo n.º 9
0
 def assert_topic_id_is_not_none(self) -> None:
     if self.topic_id is None:
         raise ArgumentInvalid("topic_id 不能为 None")