1.peewee模型类建立——自关联的多对多关系
由于关注和粉丝之间的关系都是来源于用户表,并且是多对多的关系
代码展示
# forum/models.py
# 用于创建数据表模型
from peewee import *
from forum import database
from datetime import datetime
# 创建基类:减少重复代码
class BaseModel(Model):
create_time = DateTimeField(default = datetime.now)
# 创建函数,用于返回用户的信息位字典形式
def to_json(self) -> dict:
r = {}
for k in self.__data__.keys():
# 判断数据是否是create_time(时间不是字符串)
if k == 'create_time':
r[k] = str(getattr(self,k))
else:
r[k] = getattr(self,k)
return r
class Meta:
database = database
# 创建用户表
class UserModel(BaseModel):
id = CharField(primary_key = True)
email = CharField(max_length=32, verbose_name='账号')
nick_name = CharField(max_length=32, verbose_name='昵称',null=True)
password = CharField(max_length=128, verbose_name='密码')
gender = CharField(verbose_name='性别',null=True)
signatrue = CharField(max_length=32, verbose_name='签名',null=True)
pic = CharField(max_length=512, verbose_name='头像',null=True)
status = CharField(verbose_name='账号状态', default=1)
class Meta:
table_name = 't_user'
# 创建关注表——自关联的多对多关系
class FollowModel(BaseModel):
to_user = ForeignKeyField(UserModel, backref='to_user')
from_user = ForeignKeyField(UserModel, backref='from_user')
class Meta:
table_name = 't_follow'
primary_key = CompositeKey('to_user','from_user')
创建代码
FollowModel.create_table(True)
2.用户关注——tornado增加接口
2.1查看前端需要的接口和传递的数据
2.2代码展示
接口
# /forum/handler/FollowHandler.py
from forum.handler.BaseHandler import BaseHandler
from forum import manager
from forum.models import UserModel, FollowModel
from forum.decorators import login_required_async
# 增加关注
class AddFollowHandler(BaseHandler):
@login_required_async
async def post(self):
# 获取关注的用户id
uid = self.get_body_argument('id')
print("uid:",uid,"----------",type(uid))
to_user = await manager.get(UserModel, id= uid)
print("to_user:",to_user,"----------",type(to_user))
# 获取自己的id
from_user = await manager.get(UserModel, id = self._user_id)
await manager.create(FollowModel, to_user=to_user, from_user=from_user)
self.finish({'code':200,'msg':'关注成功!!'})
2.3 获取关注状态概述
当我们点击完关注,应该更换成一个取消关注的图标。此时,查看前端代码,前端是通过flag这个变量进行控制。
2.4查看前端需要的接口和传递的数据——tornado查询接口
我们发现,在前端,还有一个接口是专门控制查看是否关注。
2.5代码展示
# /forum/handler/FollowHandler.py
from forum.handler.BaseHandler import BaseHandler
from forum import manager
from forum.models import UserModel, FollowModel
from forum.decorators import login_required_async
# 查看是否关注接口
class GetFollowHandler(BaseHandler):
@login_required_async
async def post(self):
try:
# 获取关注的用户id
uid = self.get_body_argument('id')
fid = self._user_id
# 在数据库查询是否已关注
follow = await manager.execute(FollowModel.select().where(FollowModel.to_user==uid,FollowModel.from_user==fid))
if follow:
self.finish({'code':200,'msg':'已关注','flag':True})
else:
self.finish({'code':200,'msg':'未关注','flag':False})
except Exception as e:
print(e)
self.finish({'code':200,'msg':'未关注','flag':False})
3.取消关注——tornado删除接口
3.1查看前端需要的接口和传递的数据
3.2代码展示
接口
# 取消关注接口
class DeleteFollowHandler(BaseHandler):
@login_required_async
async def delete(self):
try:
# 获取请求体里的字典参数
data = loads(self.request.body)
uid = data.get('id')
to_user = await manager.get(UserModel, id= uid)
from_user = await manager.get(UserModel, self._user_id)
# 方法1:传递对象
# rs = await manager.execute(FollowModel.delete().where(FollowModel.to_user==to_user, FollowModel.from_user==from_user))
# 方法2:传递字符串
rs = await manager.execute(FollowModel.delete().where(FollowModel.to_user==uid, FollowModel.from_user==self._user_id))
self.finish({'code':200,'msg':'取消关注成功'})
except Exception as e:
self.finish({'code':500,'msg':'取消关注失败'})
4.我的关注列表——tornado查询接口
4.1查看前端需要的接口和传递的数据
4.2代码展示
# 查看是否关注接口
class GetFollowHandler(BaseHandler):
# 获取全部关注的列表
@login_required_async
async def get(self):
# 需要字段:user.id nick_name create_time
follows = await manager.execute(FollowModel.select(FollowModel.create_time,UserModel.id,UserModel.pic,UserModel.nick_name).join(UserModel,on=FollowModel.to_user).where(FollowModel.from_user==self._user_id))
data = [{'create_time':str(f[0]),'id':f[1], 'pic':f[2], 'nick_name':f[3] } for f in follows._rows ]
self.finish({'code':200,'msg':'获取关注列表成功!!','friends':data})
5.显示关注与粉丝数——tornado查询接口
5.1查看前端需要的接口和传递的数据
5.2代码展示
# 获取关注数与粉丝数
class FollowNumHandler(BaseHandler):
@login_required_async
async def post(self):
# to 关注数
# from 粉丝数
type_ = self.get_body_argument('type_')
if type_ == 'to':
to_user = await manager.execute(FollowModel.select().where(FollowModel.from_user == self._user_id))
self.finish({'code':200,'msg':'获取关注数成功!!!','num':len(to_user._rows)})
elif type_ == 'from':
from_user = await manager.execute(FollowModel.select().where(FollowModel.to_user == self._user_id))
self.finish({'code':200,'msg':'获取粉丝数成功!!!','num':len(from_user._rows)})