13.评论模块——peewee创建模型、tornado-peewee-async查询增加接口

1.模型建立与数据初始化

peewee创建模型:多表关系

# 创建评论表
class CommentModel(BaseModel):
    id = CharField(primary_key = True)
    content = CharField(verbose_name='内容')
    topic = ForeignKeyField(TopicModel, backref='comments')
    user = ForeignKeyField(UserModel, backref='comments')
    class Meta:
        table_name = 't_comments'

sql添加内容

INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e0e05dd0a380dbc1acbf', '2050-01-01 00:00:00', '挺好看的嘛', '5de0def05dd0a380dbc1acbb', '5ddf83c8b78aa66cfb5b025e');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e0f05dd0a380dbc1acc0', '2050-01-01 00:00:00', '大晚上,让我看这个!', '5de0dfdf5dd0a380dbc1acbd', '5ddf83c8b78aa66cfb5b025e');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e0f85dd0a380dbc1acc1', '2050-01-01 00:00:00', '真的是,饿到不行', '5de0dfdf5dd0a380dbc1acbd', '5ddf83c8b78aa66cfb5b025e');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e10d5dd0a380dbc1acc3', '2050-01-01 00:00:00', '王哥,', '5de0de315dd0a380dbc1acb9', '5ddf83c8b78aa66cfb5b025e');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e1205dd0a380dbc1acc4', '2050-01-01 00:00:00', '笑死我,继承我的花呗?', '5de0dd105dd0a380dbc1acb5', '5ddf83c8b78aa66cfb5b025e');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e1985dd0a380dbc1acca', '2050-01-01 00:00:00', '666,大晚上有点东西啊', '5de0dfdf5dd0a380dbc1acbd', '5de0b487b07c402cc7d1a00f');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e1a45dd0a380dbc1accb', '2050-01-01 00:00:00', '真帅!', '5de0def05dd0a380dbc1acbb', '5de0b487b07c402cc7d1a00f');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e1b35dd0a380dbc1accc', '2050-01-01 00:00:00', '加油!', '5de0dd8f5dd0a380dbc1acb7', '5de0b487b07c402cc7d1a00f');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e20d5dd0a380dbc1accd', '2050-01-01 00:00:00', '大家快举报得了,居心何在', '5de0dfdf5dd0a380dbc1acbd', '5de0d74a5dd0a380dbc1aca7');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e21d5dd0a380dbc1accf', '2050-01-01 00:00:00', '赞!', '5de0def05dd0a380dbc1acbb', '5de0d74a5dd0a380dbc1aca7');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e22e5dd0a380dbc1acd0', '2050-01-01 00:00:00', '搞笑!', '5de0dd105dd0a380dbc1acb5', '5de0d74a5dd0a380dbc1aca7');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e23b5dd0a380dbc1acd1', '2050-01-01 00:00:00', '哈哈啊哈,悲催', '5de0dc555dd0a380dbc1acb3', '5de0d74a5dd0a380dbc1aca7');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e2695dd0a380dbc1acd2', '2050-01-01 00:00:00', '吃饭去啦!', '5de0dfdf5dd0a380dbc1acbd', '5de0d8745dd0a380dbc1aca9');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e2745dd0a380dbc1acd4', '2050-01-01 00:00:00', '666', '5de0def05dd0a380dbc1acbb', '5de0d8745dd0a380dbc1aca9');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e2855dd0a380dbc1acd5', '2050-01-01 00:00:00', '牛逼!', '5de0dd105dd0a380dbc1acb5', '5de0d8745dd0a380dbc1aca9');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('5de0e28e5dd0a380dbc1acd6', '2050-01-01 00:00:00', '666', '5de0dc555dd0a380dbc1acb3', '5de0d8745dd0a380dbc1aca9');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('ad759fb9-899c-42a3-a9fc-3437b3f6640c', '2021-04-30 19:18:05', '123', '5de0d65b5dd0a380dbc1aca5', '5de0d2696e78ad79c2e2c64a');
INSERT INTO `t_comment` (`id`, `create_time`, `content`, `topic_id`, `user_id`) VALUES ('ff6ce34b-2793-4289-9986-b2a3f99ef088', '2021-04-30 19:23:38', '456', '5de0d65b5dd0a380dbc1aca5', '5de0d2696e78ad79c2e2c64a');

2.获取评论——tornado查询接口

2.1查看前端需要的接口和传递的数据

发现,他们通过文章的id为标识去查看评论
在这里插入图片描述
在这里插入图片描述

2.2代码展示

接口

# forum/handler/CommentHandler.py
from forum.handler.BaseHandler import BaseHandler
from forum import manager
from forum.models import UserModel, TopicModel, CommentModel

# 获取评论接口
class GetCommentHandler(BaseHandler):
    async def post(self):
        rs_data = {}
        id = self.get_body_argument('id')
        # 根据帖子ID获取所有的评论: 多表查询
        comments = await manager.execute(CommentModel.select().join(TopicModel).where(TopicModel.id==id))
        data = []
        # 遍历评论,转化成json
        for c in comments:
            # 把评论转为json
            tc = c.to_json()
            # 往转成的json对象种增加user属性
            tc['user'] = c.user.to_json()
            del tc['topic']
            data.append(tc)
        # 传递给前端
        rs_data['code'] = 200
        rs_data['msg'] = '获取评论成功'
        rs_data['comments'] = data
        print(rs_data['comments'])
        self.finish(rs_data)

挂载路由

# router.py
from forum.handler import BaseHandler,UserHandler,TopicHandler,CommentHandler
handlers = [
    ('/api/topic/my/?', TopicHandler.GetMyTopicHandler),
    ('/api/comment/get/tid/?',CommentHandler.GetCommentHandler)
]

3.发表评论——tornado增加接口

3.1查看前端需要的接口和传递的数据

在这里插入图片描述
在这里插入图片描述

3.2代码展示

接口

# forum/handler/CommentHandler.py
from uuid import uuid4

from forum.handler.BaseHandler import BaseHandler
from forum import manager
from forum.models import TopicModel, CommentModel, UserModel
from forum.decorators import login_required_async
# 增加评论接口
class AddCommentHandler(BaseHandler):
    @login_required_async
    async def post(self):
        # 前端传递来了两个数据,但是增加到评论表时,还得需要用户id,所以使用装饰器的方式获取评论用户的id
        content = self.get_body_argument('content')
        topic_id = self.get_body_argument('topic_id')
        # 获取帖子对象信息
        topic = await manager.get(TopicModel, id=topic_id)
        # 获取用户对象信息
        user = await manager.get(UserModel, id=self._user_id)
        # 创建一个Comment对象
        await manager.create(CommentModel, id=uuid4(), content=content, topic=topic, user=user)
        self.finish({'code':200,'msg':'评论成功!!!'})

挂载路由

# router.py
from forum.handler import BaseHandler,UserHandler,TopicHandler,CommentHandler
handlers = [
    ('/',BaseHandler.IndexHandler),
    ('/api/user/add/?', UserHandler.AddUserHandler),
    ('/api/comment/add/?',CommentHandler.AddCommentHandler)
]

4.获取个人评论——tornado查询接口

4.1查看前端需要的接口和传递的数据

在这里插入图片描述

4.2代码展示

接口

# forum/handler/CommentHandler.py
from uuid import uuid4

from forum.handler.BaseHandler import BaseHandler
from forum import manager
from forum.models import TopicModel, CommentModel, UserModel
from forum.decorators import login_required_async
# 查询自己的所有评论
class GetMyCommentHandler(BaseHandler):
    @login_required_async
    async def post(self):
        # 获取评论
        # peewee_async多表查询
        comments = await manager.execute(CommentModel.select().join(UserModel).where(UserModel.id == self._user_id))
        # 建立一个列表,用来存储所有的评论数据<json类型>
        data = []
        # 遍历数据,将数据转化成json类型
        for c in comments:
            tc = c.to_json()
            tc['user'] = c.user.to_json()
            del tc['topic']
            data.append(tc)
        self.finish({'code':200,'msg':'获取个人评论成功!','comments':data})

创建路由

# router.py
from forum.handler import BaseHandler,UserHandler,TopicHandler,CommentHandler
handlers = [
    ('/api/comment/add/?',CommentHandler.AddCommentHandler),
    ('/api/comment/my/?',CommentHandler.GetMyCommentHandler)
]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想成为数据分析师的开发工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值