|
|
from datetime import datetime
|
|
|
|
|
|
from flask import session
|
|
|
|
|
|
from main import db
|
|
|
from module.thumb import Thumb
|
|
|
|
|
|
|
|
|
class Comment(db.Model):
|
|
|
__tablename__ = 'comment'
|
|
|
commentid = db.Column(db.Integer, primary_key=True)
|
|
|
userid = db.Column(db.String(64), db.ForeignKey('user.userid'))
|
|
|
articleid = db.Column(db.String(64), db.ForeignKey('article.articleid'))
|
|
|
content = db.Column(db.String(64), nullable = False) # 正文内容
|
|
|
ipaddr = db.Column(db.String(64), nullable = False) # 地址
|
|
|
replyid = db.Column(db.Integer, nullable=False) # 如果是普通评论,为0,如果是回复的评论,显示commentid
|
|
|
agreecount = db.Column(db.Integer, nullable=False) # 赞成该评论的数量
|
|
|
opposecount = db.Column(db.Integer, nullable=False) # 反对该评论的数量
|
|
|
hidden = db.Column(db.Integer, nullable=False) # 默认为0,不隐藏
|
|
|
createtime = db.Column(db.DateTime, nullable=False) # 使用 DateTime 类型,并设置默认值为当前 UTC 时间
|
|
|
updatetime = db.Column(db.DateTime, nullable=False)
|
|
|
|
|
|
def is_upComment(self,articleid,commentid):
|
|
|
return Thumb().is_upComment(articleid,commentid)
|
|
|
|
|
|
|
|
|
# 关联 Users 表
|
|
|
user = db.relationship('Users', backref=db.backref('comment_user', lazy=True))
|
|
|
|
|
|
|
|
|
|
|
|
# 新增评论
|
|
|
@classmethod
|
|
|
def add_comment(cls,articleid,content,ipaddr):
|
|
|
userid = session.get('userid')
|
|
|
comment = Comment(userid=userid,articleid=articleid,content=content,ipaddr=ipaddr,replyid=0,
|
|
|
agreecount=0,opposecount=0,hidden=0,createtime=datetime.utcnow(),updatetime=datetime.utcnow())
|
|
|
try:
|
|
|
db.session.add(comment)
|
|
|
db.session.commit()
|
|
|
return True
|
|
|
except Exception as e:
|
|
|
print("新增失败,文章id:", articleid, "错误:", e)
|
|
|
return False
|
|
|
|
|
|
# 根据文章编号查询所有的评论(未分页)
|
|
|
@classmethod
|
|
|
def find_comment_by_articleid(cls,articleid):
|
|
|
cls.query.filter_by(hidden=0).order_by(Comment.commentid.desc()).all()
|
|
|
|
|
|
|
|
|
# 查询用户和评论信息
|
|
|
@classmethod
|
|
|
def find_comment_with_user(cls,articleid):
|
|
|
options = db.joinedload(cls.user)
|
|
|
return cls.query.options(options).filter_by(articleid=articleid,hidden=0).order_by(Comment.createtime.desc()).all()
|
|
|
|
|
|
# 评论的赞同数量加一
|
|
|
@classmethod
|
|
|
def update_add_agreecount(cls, commentid):
|
|
|
cls.query.filter_by(commentid=commentid).update({cls.agreecount: cls.agreecount+1})
|
|
|
db.session.commit()
|
|
|
return True
|
|
|
|
|
|
# 评论的赞同数量减一
|
|
|
@classmethod
|
|
|
def update_sub_agreecount(cls, commentid):
|
|
|
cls.query.filter_by(commentid=commentid).update({cls.agreecount: cls.agreecount - 1})
|
|
|
db.session.commit()
|
|
|
|
|
|
# 评论的反对数量加一
|
|
|
@classmethod
|
|
|
def update_add_opposecount(cls, commentid):
|
|
|
cls.query.filter_by(commentid=commentid).update({cls.opposecount: cls.opposecount + 1})
|
|
|
db.session.commit()
|
|
|
|
|
|
# 评论的反对数量减一
|
|
|
@classmethod
|
|
|
def update_sub_opposecount(cls, commentid):
|
|
|
cls.query.filter_by(commentid=commentid).update({cls.opposecount: cls.opposecount - 1})
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
# 评论隐藏
|
|
|
@classmethod
|
|
|
def update_hide_comment(cls, commentid):
|
|
|
cls.query.filter_by(commentid=commentid).update({cls.hide:1})
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
def find_coment_by_id(cls, commentid,userid):
|
|
|
'''
|
|
|
根据userid查找用户
|
|
|
:param userid: userid
|
|
|
:return: 查找到的对象
|
|
|
'''
|
|
|
return cls.query.filter(cls.userid == userid,cls.commentid == commentid).first()
|
|
|
|
|
|
# 删除评论信息
|
|
|
@classmethod
|
|
|
def do_delete_comment(cls, commentid):
|
|
|
result = cls.query.filter_by(commentid=commentid).first()
|
|
|
if result:
|
|
|
db.session.delete(result)
|
|
|
db.session.commit() |