13.评论模块——peewee创建模型、tornado

阅读: 评论:0

13.评论模块——peewee创建模型、tornado

13.评论模块——peewee创建模型、tornado

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
dels import UserModel, TopicModel, CommentModel# 获取评论接口
class GetCommentHandler(BaseHandler):async def post(self):rs_data = {}id = _body_argument('id')# 根据帖子ID获取所有的评论: 多表查询comments = ute(CommentModel.select().join(TopicModel).where(TopicModel.id==id))data = []# 遍历评论,转化成jsonfor c in comments:# 把评论转为jsontc = c.to_json()# 往转成的json对象种增加user属性tc['user'] = _json()del tc['topic']data.append(tc)# 传递给前端rs_data['code'] = 200rs_data['msg'] = '获取评论成功'rs_data['comments'] = dataprint(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 uuid4from forum.handler.BaseHandler import BaseHandler
from forum import manager
dels import TopicModel, CommentModel, UserModel
from forum.decorators import login_required_async
# 增加评论接口
class AddCommentHandler(BaseHandler):@login_required_asyncasync def post(self):# 前端传递来了两个数据,但是增加到评论表时,还得需要用户id,所以使用装饰器的方式获取评论用户的idcontent = _body_argument('content')topic_id = _body_argument('topic_id')# 获取帖子对象信息topic = (TopicModel, id=topic_id)# 获取用户对象信息user = (UserModel, id=self._user_id)# 创建一个Comment对象ate(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 uuid4from forum.handler.BaseHandler import BaseHandler
from forum import manager
dels import TopicModel, CommentModel, UserModel
from forum.decorators import login_required_async
# 查询自己的所有评论
class GetMyCommentHandler(BaseHandler):@login_required_asyncasync def post(self):# 获取评论# peewee_async多表查询comments = ute(CommentModel.select().join(UserModel).where(UserModel.id == self._user_id))# 建立一个列表,用来存储所有的评论数据<json类型>data = []# 遍历数据,将数据转化成json类型for c in comments:tc = c.to_json()tc['user'] = _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)
]

本文发布于:2024-02-04 07:53:44,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170702418453690.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:模块   模型   tornado   peewee
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23