Example #1
0
    async def before_insert(self, raw_post: Dict,
                            values_lst: List[SQLValuesToWrite]):
        values = values_lst[0]
        relate_type = values.get('related_type', None)
        if not (relate_type and relate_type in POST_TYPES.values()):
            return self.finish(RETCODE.INVALID_POSTDATA, "被评论的内容不存在")

        try:
            cid = config.POST_ID_GENERATOR(values['related_id'])
            post = POST_TYPES.get_post(relate_type, cid)

            if not post:
                return self.finish(RETCODE.INVALID_POSTDATA, "被评论的内容不存在")

            if relate_type == POST_TYPES.TOPIC:
                if post.state == POST_STATE.CLOSE:
                    return self.finish(RETCODE.INVALID_POSTDATA, "无法评论指定内容")
                elif post.visible in (POST_VISIBLE.HIDE, ):
                    return self.finish(RETCODE.INVALID_POSTDATA, "被评论的内容不存在")

        except TypeError:
            return self.finish(RETCODE.INVALID_POSTDATA, "被评论的内容不存在")

        if 'content' not in values or not values['content']:
            return self.finish(RETCODE.INVALID_POSTDATA, "评论内容不能为空")

        if 'reply_to_cmt_id' in values:
            try:
                rtid = config.POST_ID_GENERATOR(values['reply_to_cmt_id'])
            except TypeError:
                return self.finish(RETCODE.INVALID_POSTDATA, "指定被回复的内容不存在")
            c: Comment = Comment.get_by_pk(rtid.to_bin())

            if not c:
                return self.finish(RETCODE.INVALID_POSTDATA, "指定被回复的内容不存在")
            if c.related_id != post.id:
                return self.finish(RETCODE.INVALID_POSTDATA, "指定被回复的内容不存在")

            values['reply_to_cmt_id'] = rtid.to_bin()

        if not isinstance(config.LONG_ID_GENERATOR, config.AutoGenerator):
            values['id'] = config.LONG_ID_GENERATOR().to_bin()
        values['related_id'] = cid.to_bin()
        values['related_type'] = int(values['related_type'])
        values['user_id'] = self.current_user.id
        values['time'] = int(time.time())
        values['content'], self.do_mentions = check_content_mention(
            values['content'])

        if relate_type == POST_TYPES.TOPIC:
            post: Topic
            await post.weight_inc()
Example #2
0
    async def before_insert(self, raw_post: Dict, values_lst: List[SQLValuesToWrite]):
        values = values_lst[0]
        form = TopicNewForm(**raw_post)
        if not form.validate():
            return self.finish(RETCODE.FAILED, form.errors)
        values['user_id'] = self.current_user.id

        # 以下通用
        if not config.POST_ID_GENERATOR == config.AutoGenerator:
            values['id'] = config.POST_ID_GENERATOR().digest()
        values['time'] = int(time.time())
        values['weight'] = await Topic.weight_gen()
        values['update_time'] = int(time.time())
        values['content'], self.do_mentions = check_content_mention(values['content'])