|
|
|
|
from datetime import datetime
|
|
|
|
|
import random
|
|
|
|
|
|
|
|
|
|
from flask import session
|
|
|
|
|
|
|
|
|
|
from main import db
|
|
|
|
|
from module.credit import Credit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Users(db.Model):
|
|
|
|
|
__tablename__ = 'user'
|
|
|
|
|
userid = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
username = db.Column(db.String(255), nullable=False)
|
|
|
|
|
password = db.Column(db.String(255), nullable=False)
|
|
|
|
|
avater = db.Column(db.String(255), nullable=True)
|
|
|
|
|
nickname = db.Column(db.String(255), nullable=False)
|
|
|
|
|
credit = db.Column(db.Integer, nullable=False)
|
|
|
|
|
role = db.Column(db.String(255), nullable=False)
|
|
|
|
|
createtime = db.Column(db.DateTime, default=datetime.utcnow)
|
|
|
|
|
updatetime = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 查询用户名,可用于注册时判断用户名是否已注册,也可用于登录校验
|
|
|
|
|
@classmethod
|
|
|
|
|
def find_by_username(cls, username):
|
|
|
|
|
result = cls.query.filter_by(username=username).first()
|
|
|
|
|
return result
|
|
|
|
|
# return cls.query.filter_by(username=username).first() is not None
|
|
|
|
|
@classmethod
|
|
|
|
|
def find_by_username_deledate(cls, username):
|
|
|
|
|
# result = cls.query.filter_by(username=username).all()
|
|
|
|
|
# return result
|
|
|
|
|
return cls.query.filter_by(username=username).first() is not None
|
|
|
|
|
# 如果查找不到返回空列表而不是None first()方法返回的才是None
|
|
|
|
|
'''
|
|
|
|
|
users = User.find_by_username('some_username')
|
|
|
|
|
if users:
|
|
|
|
|
# 处理找到的用户
|
|
|
|
|
for user in users:
|
|
|
|
|
print(user)
|
|
|
|
|
else:
|
|
|
|
|
# 没有找到用户
|
|
|
|
|
print("没有找到用户")
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 查找用户
|
|
|
|
|
@classmethod
|
|
|
|
|
def find_user_by_id(cls, userid):
|
|
|
|
|
'''
|
|
|
|
|
根据userid查找用户
|
|
|
|
|
:param userid: userid
|
|
|
|
|
:return: 查找到的对象
|
|
|
|
|
'''
|
|
|
|
|
return cls.query.filter(cls.userid == userid).first()
|
|
|
|
|
|
|
|
|
|
# 查找所有用户
|
|
|
|
|
@classmethod
|
|
|
|
|
def find_all_users(cls):
|
|
|
|
|
return cls.query.all()
|
|
|
|
|
|
|
|
|
|
# 减少用户表的剩余积分
|
|
|
|
|
@classmethod
|
|
|
|
|
def update_credit(cls,credit):
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
:param credit: 消耗的积分,默认加号,给出的积分应该已经附带+-符号
|
|
|
|
|
:return:
|
|
|
|
|
'''
|
|
|
|
|
cls.query.filter_by(userid = session.get('userid')).update({cls.credit: cls.credit + credit})
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
# 实现注册,首次注册时用户只需要输入用户名和密码,所以只需要两个参数
|
|
|
|
|
# 注册时,在模型类中为其他字段尽力生成一些可用的值,虽不全面,但可用
|
|
|
|
|
# 通常用户注册时不建议填写太多资料,影响体验,可待用户后续逐步完善
|
|
|
|
|
@classmethod
|
|
|
|
|
def do_register(cls, username, password):
|
|
|
|
|
nickname = username.split('@')[0]
|
|
|
|
|
user = Users(username=username, password=password, role='3', credit=50, nickname=nickname,avater = "2.png",createtime=datetime.utcnow(),updatetime=datetime.utcnow())
|
|
|
|
|
db.session.add(user)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
# 删除用户信息
|
|
|
|
|
@classmethod
|
|
|
|
|
def do_deletesign_user(cls, userid):
|
|
|
|
|
result = cls.query.filter_by(userid=userid).first()
|
|
|
|
|
if result:
|
|
|
|
|
db.session.delete(result)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
# 统计当前用户的数量
|
|
|
|
|
@classmethod
|
|
|
|
|
def get_total_count_user(cls):
|
|
|
|
|
return cls.query.count()
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def switch_role(cls, userid):
|
|
|
|
|
# print("数据库中articleid=", articleid)
|
|
|
|
|
row = cls.query.filter_by(userid=userid).first()
|
|
|
|
|
# print("数据库中row.checked=", row.checked)
|
|
|
|
|
if row.role == 'admin':
|
|
|
|
|
row.role = 'user'
|
|
|
|
|
else:
|
|
|
|
|
row.role = 'admin'
|
|
|
|
|
db.session.commit()
|
|
|
|
|
return row.role
|