就像weixin朋友圈那样,用户可以发布信息到动态,也可以拉取好友发布的信息,要实现该功能我们要新建一个表,既然是仿朋友圈,那就叫friend_circle吧
朋友圈表
class Friend_Circle(db.Model):
__tablename__ = 'friend_circle'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
create_time = db.Column(db.DateTime, default=datetime.now)
user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # 发布者
user = db.relationship('User', backref=db.backref('friend_circles'))
text = db.Column(db.Text)
address = db.Column(db.String(200))
weather = db.Column(db.String(20))
发布动态的时候,除了发布纯文本,还有图片、视频等文件,并且拉取朋友圈的数据内,也需要包含点赞、评论等数据,所以在做朋友圈功能前还要实现文件上传、点赞评论等功能
文件表
class File(db.Model):
__tablename__ = 'file'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
create_time = db.Column(db.DateTime, default=datetime.now)
type = db.Column(db.String(50), nullable=False)
name = db.Column(db.String(255), nullable=False)
link = db.Column(db.String(255), nullable=False)
uploader_id = db.Column(db.Integer, db.ForeignKey('user.id')) # 上传者
uploader = db.relationship('User', backref=db.backref('files'))
source_id = db.Column(db.Integer) # 对应表id
source_type = db.Column(db.Integer) # 对应枚举类型
点赞表
class Like(db.Model):
__tablename__ = 'like'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
create_time = db.Column(db.DateTime, default=datetime.now)
friend_circle_id = db.Column(db.Integer,db.ForeignKey('friend_circle.id'))
friend_circle = db.relationship('Friend_Circle',backref=db.backref('likes'))
user_id = db.Column(db.Integer,nullable=False)
评论表
class Comment(db.Model):
__tablename__ = 'comment'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
create_time = db.Column(db.DateTime, default=datetime.now)
friend_circle_id = db.Column(db.Integer,db.ForeignKey('friend_circle.id'))
friend_circle = db.relationship('Friend_Circle',backref=db.backref('comments'))
text = db.Column(db.Text,nullable=False)
user_id = db.Column(db.Integer,nullable=False) # 发表评论者
target_id = db.Column(db.Integer) # 对谁的评论,默认空
别忘了修改数据库表之后执行两条终端命令
python3 manage.py db migrate
python3 manage.py db upgrade
然后就是朋友圈数据拉取:
@app.route('/friend_circle/', methods=['GET', 'POST'])
@login_required
def friend_circle():
if request.method == 'GET':
user_id = session.get('user_id')
friends = Friend_Bind.query.filter(or_(Friend_Bind.uid == user_id,
Friend_Bind.fid == user_id)).all()
friend_id_list = []
friend_id_list.append(user_id)
for friend in friends:
uid = friend.uid
if uid == user_id:
uid = friend.fid
friend_id_list.append(uid)
circles = Friend_Circle.query.filter(Friend_Circle.user_id.in_(friend_id_list)).order_by(
desc(Friend_Circle.create_time)).limit(100).all()
result = []
for circle in circles:
user = User.query.filter(User.id == circle.user_id).first()
content = {
'circle_id': circle.id,
'username': user.username,
'head_img_url': user.head_img_url,
'text': circle.text,
'user_id': user.id,
'timestamp': time.mktime(circle.create_time.timetuple()),
'images': None,
'like_status': '点赞'
}
result.append(content)
context = {'result': result}
return render_template('friend_circle.html', **context)
else:
return jsonify({'code': 1,
'result': 'null'})
点赞
@app.route('/like//')
def like(friend_circle_id):
my_user_id = session.get('user_id')
fc = Friend_Circle.query.filter(Friend_Circle.id == friend_circle_id).first()
exist = Like.query.filter(Like.user_id == my_user_id,
Like.friend_circle_id == friend_circle_id).first()
if exist is not None:
db.session.delete(exist)
db.session.commit()
return u'取消点赞成功'
like = Like(friend_circle=fc,
friend_circle_id=fc.id,
user_id=my_user_id)
db.session.add(like)
db.session.commit()
return u'点赞成功'
评论
@app.route('/comment/', methods=['GET', 'POST'])
@login_required
def comment():
user_id = session.get('user_id')
text = request.form.get('text')
circle_id = request.form.get('circle_id')
fc = Friend_Circle.query.filter(Friend_Circle.id == circle_id).first()
comment = Comment(friend_circle_id=circle_id,
user_id=user_id,
friend_circle=fc,
text=text)
db.session.add(comment)
db.session.commit()
return u'评论成功'
这样就完成了。
GitHub链接