class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, autoincrement=True, primary_key=True) name = db.Column(db.String(20), nullable=False, unique=True) password = db.Column(db.String(100), nullable=False) gender = db.Column(db.Enum("M", "N", "F"), nullable=False, default='N') birthday = db.Column(db.Date) recognition = db.Column(db.String(20)) is_admin = db.Column(db.Enum("Y", "N"), nullable=False, default='N') level = db.Column(db.Integer, nullable=False, default=1) exp = db.Column(db.Integer, nullable=False, default=0) pic = db.Column(db.String(100), nullable=False, default='images/userdefault.png') balance = db.Column(db.Integer, nullable=False, default=0)
class FrontUser(db.Model): __tablename__ = 'front_user' # 这里不要使用自增长的主键,否则可以很轻松获得该系统的用户数 id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid) telephone = db.Column(db.String(11), nullable=False, unique=True) username = db.Column(db.String(50), nullable=False) _password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True) realname = db.Column(db.String(50)) avatar = db.Column( db.String(100), default='https://www.yingjoy.cn/wp-content/uploads/2017/08/1.jpg') signature = db.Column(db.String(100), default='此人较懒...') gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOW) join_time = db.Column(db.DateTime, default=datetime.now) def __init__(self, *args, **kwargs): if 'password' in kwargs: self.password = kwargs.get('password') kwargs.pop('password') super(FrontUser, self).__init__(*args, **kwargs) @property def password(self): return self._password @password.setter def password(self, raw_password): self._password = generate_password_hash(raw_password) def check_password(self, raw_password): return check_password_hash(self.password, raw_password)
class Task(db.Model): __tablename__ = 'task' id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid) # task名称 title = db.Column(db.String(30), nullable=False) # task详情 content = db.Column(db.String(100)) # 全天时间,默认为否 all_day = db.Column(db.Boolean, default=False) # 开始时间,默认为创建时间 start = db.Column(db.DateTime, default=datetime.now) # 结束时间,可以为空,fullcalendar自动设置为开始时间1小时后 end = db.Column(db.DateTime) # 是否邮件提醒,默认提醒 email_remind = db.Column(db.Boolean, default=True) # 是否短信提醒,默认不提醒 message_remind = db.Column(db.Boolean, default=False) # 短信提醒时间 remindtime = db.Column(db.DateTime, default=datetime.now) # task完成状态, status = db.Column(db.Enum(TaskStatusEnum), default=TaskStatusEnum.UNFINISHED) # 对应用户 user_id = db.Column(db.String(100), db.ForeignKey('front_user.id'), nullable=False)
class FrontUser(db.Model): __tablename__ = 'front_user' # 用户id设置自增长的话容易被人猜出用户数量,使用生成随机数的方式,同时呢避免id重复 id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid) username = db.Column(db.String(50), nullable=False) tel = db.Column(db.String(11), nullable=False, unique=True) _password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True) realname = db.Column(db.String(50)) # 真实姓名 avatar = db.Column(db.String(100)) # 头像 signature = db.Column(db.String(100)) # 个性签名 gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKOWN) is_display = db.Column(db.Integer, default=1, comment='默认1允许用户发帖,0表示不允许用户发帖') join_time = db.Column(db.DateTime, default=datetime.now) def __init__(self, *args, **kwargs): if "password" in kwargs: self.password = kwargs.get('password') kwargs.pop('password') super().__init__(*args, **kwargs) @property def password(self): return self._password @password.setter def password(self, newpad): self._password = generate_password_hash(newpad) def check_password(self, rawpwd): result = check_password_hash(self.password, rawpwd) return result
class FrontUser(db.Model): __tablename__='front_user' id=db.Column(db.String(100),primary_key=True,default=shortuuid.uuid) telephone=db.Column(db.String(11),nullable=False,unique=True) username=db.Column(db.String(50),nullable=False) _password=db.Column(db.String(100),nullable=False) email=db.Column(db.String(50),unique=True) realname=db.Column(db.String(50)) avatar=db.Column(db.String(100)) signature=db.Column(db.String(100)) gender=db.Column(db.Enum(GenderEnum),default=GenderEnum.UNKNOWN) join_time=db.Column(db.DateTime,default=datetime.now) def __init__(self,*args,**kwargs): if 'password' in kwargs: self.password=kwargs.get('password') kwargs.pop('password') super(FrontUser, self).__init__(*args,**kwargs) @property def password(self): return self._password @password.setter def password(self,newpwd): self._password=generate_password_hash(newpwd) def check_password(self,rawpwd): return check_password_hash(self._password,rawpwd)
class FrontUser(db.Model): __tablename__ = 'front_user' id = db.Column(db.String(100), primary_key=100, default=shortuuid.uuid) telephone = db.Column(db.String(11), nullable=False, unique=True) username = db.Column(db.String(50), nullable=False) password_hash = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True) realname = db.Column(db.String(50)) avatar = db.Column(db.String(100)) signature = db.Column(db.String(100)) gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOWN) join_time = db.Column(db.DateTime, default=datetime.now) posts = db.relationship('PostModel', backref='front_users') comments = db.relationship('Comment', backref='front_users') def __init__(self, *args, **kwargs): if "password" in kwargs: self.password = kwargs.get("password") kwargs.pop("password") super(FrontUser, self).__init__(*args, **kwargs) @property def password(self): raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) def __repr__(self): return '<FrontUser {}>'.format(self.username)
class Front_user(db.Model): #创建一个前台用户类 __tablename__='front_user' #表明 id=db.Column(db.String(200),primary_key=True) #用户id username=db.Column(db.String(20),nullable=False) #用户名 telephone=db.Column(db.String(11),nullable=False,unique=True) #手机号 __passwd=db.Column(db.String(200),nullable=False) #密码 avatar=db.Column(db.String(200)) #头像 realname=db.Column(db.String(20)) #真实姓名 personal_introduction=db.Column(db.String(150)) #个人介绍 gender=db.Column(db.Enum(GenderEnum),default=GenderEnum.unknow) #性别 join_time=db.Column(db.DateTime,default=datetime.now()) #链接时间 is_delete=db.Column(db.Boolean,default=False,nullable=False) #用于加入黑名单 def __init__(self,*args,**kwargs): if 'passwd' in kwargs: #对接受过来的密码进行一段处理 passwd=kwargs.get('passwd') self.passwd=passwd kwargs.pop('passwd') #删除这个passwd super(Front_user, self).__init__(*args,**kwargs) #剩下的内容交个父类处理 @property #对密码进行加密 def passwd(self): return self.__passwd @passwd.setter def passwd(self,passwd): self.__passwd=generate_password_hash(passwd) def check_passwd(self,passwd): #查看用户输入的密码是否正确 return check_password_hash(self.passwd,passwd)
class FrontUser(db.Model): __tablename__ = 'front_user' id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid) # 自增长对商业信息不好,使用唯一字符串,shortuuid telephone = db.Column(db.String(11), nullable=False, unique=True) username = db.Column(db.String(50), nullable=False) _password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True) realname = db.Column(db.String(50)) avatar = db.Column(db.String(100)) signature = db.Column(db.String(100)) gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKONE) join_time = db.Column(db.DateTime, default=datetime.now) # 为了命令行中,_password找不到 def __init__(self, *args, **kwargs): if "password" in kwargs: self.password = kwargs.get('password') kwargs.pop("password") super(FrontUser, self).__init__(*args, **kwargs) @property def password(self): return self._password @password.setter def password(self, raw_pwd): # 进行加密 self._password = generate_password_hash(raw_pwd) def check_pwd(self, pwd): # 界面并对照 result = check_password_hash(self.password, pwd) return result
class User(db.Model): """前台用户""" __tablename__ = 'user' id = db.Column(db.String(50), primary_key=True, default=uuid) username = db.Column(db.String(50), nullable=False) telephone = db.Column(db.String(20), nullable=False, unique=True) _password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True) avatar = db.Column(db.String(100)) signature = db.Column(db.String(200)) realname = db.Column(db.String(50)) gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.DOKNOW) join_time = db.Column(db.DateTime(), default=datetime.now) def __init__(self, *args, **kwargs): if 'password' in kwargs: self.password = kwargs.pop('password') super(User, self).__init__(*args, **kwargs) @property def password(self): return self._password @password.setter def password(self, newpwd): self._password = generate_password_hash(newpwd) def check_password(self, password): return check_password_hash(self.password, password)
class FrontUser(db.Model): __tablename__ = 'front_user' #这里的id如果我们还使用自动增长,就会存在商业安全隐患,用户可以根据id推算出我们网站的人数 #不使用自动增长,又要保证id的唯一性,我们就可以使用uuid #虽然UUid好用,但是它太长了,查找效率就会降低 #这时我们就可以使用shortuuid这个库,它既满足了唯一性,又没有uuid那么长 id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid) telephone = db.Column(db.String(11), nullable=False) username = db.Column(db.String(50), nullable=False) _password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True) realname = db.Column(db.String(50)) avatar = db.Column(db.String(100)) signature = db.Column(db.String(100)) gender = db.Column(db.Enum(GenDerEnum), default=GenDerEnum.UNKNOW) join_time = db.Column(db.DateTime, default=datetime.now) #对password对应的是_password,所以要做处理 def __int__(self, *args, **kwargs): if 'password' in kwargs: self.password = kwargs.get('password') kwargs.pop('password') super(FrontUser, self).__init__(*args, **kwargs) @property def password(self): return self._password @password.setter def password(self, newpwd): self._password = generate_password_hash(newpwd) def check_password(self, rawpwd): return check_password_hash(self._password, rawpwd)
class FrontUser(db.Model): __tablename__ = "front_user" id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid) telephone = db.Column(db.String(11), nullable=False, unique=True) username = db.Column(db.String(30), nullable=False) _password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True) realname = db.Column(db.String(50)) avatar = db.Column(db.String(100)) # 头像 signature = db.Column(db.String(100)) # 签名 gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOW) join_time = db.Column(db.DateTime, default=datetime.datetime.now) # 因为要特殊处理password def __init__(self, password, **kwargs): self.password = password kwargs.pop('password', None) super(FrontUser, self).__init__(**kwargs) @property def password(self): return self._password @password.setter def password(self, frontpwd): # 1. 密码不希望外界访问 2.防止循环引用 self._password = generate_password_hash(frontpwd) def checkPwd(self, frontpwd): # return self.password == generate_password_hash(frontpwd) return check_password_hash(self._password, frontpwd)
class FrontUser(db.Model): __tablename__ = 'front_user' id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid) telephone = db.Column(db.String(11), nullable=False, unique=True) username = db.Column(db.String(50), nullable=False) _password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True) realname = db.Column(db.String(50)) avatar = db.Column(db.String(100)) signature = db.Column(db.String(100)) gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOW) join_time = db.Column(db.DateTime, default=datetime.now()) status = db.Column(db.Boolean, default=True) # comments = db.relationship("CommentModel",backref='author',cascade='all') def __init__(self, *args, **kwargs): if 'password' in kwargs: self.password = kwargs.get('password') kwargs.pop('password') super(FrontUser, self).__init__(*args, **kwargs) @property def password(self): return self._password @password.setter def password(self, raw_password): self._password = generate_password_hash(raw_password) def check_password(self, raw_password): result = check_password_hash(self.password, raw_password) return result
class FrontUser(db.Model): __tablename__ = "front_user" id = db.Column(db.INTEGER, autoincrement=True, primary_key=True) uid = db.Column(db.String(100), unique=True, default=shortuuid.uuid) telephone = db.Column(db.String(11), nullable=False, unique=True) username = db.Column(db.String(50), nullable=False) _password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True) realname = db.Column(db.String(20)) avatar = db.Column(db.String(100)) signature = db.Column(db.String(100)) gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.SECRET) join_time = db.Column(db.DATETIME, default=datetime.datetime.now) def __init__(self, *args, **kwargs): if "password" in kwargs: self.password = kwargs.get("password") super(FrontUser, self).__init__(*args, **kwargs) @property def password(self): return self._password @password.setter def password(self, password): self._password = generate_password_hash(password) def check_password(self, password): return check_password_hash(self._password, password)
class FrontUser(db.Model): __tablename__ = 'front_user' id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid) # telephone = db.Column(db.String(11), unique=True, nullable=False) username = db.Column(db.String(50), nullable=False) _password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True, nullable=False) # 真实姓名 realname = db.Column(db.String(50)) # 头像 avatar = db.Column(db.String(50)) signature = db.Column(db.String(50)) gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOW) join_time = db.Column(db.String(50), default=datetime.now) def __init__(self, *args, **kwargs): if 'password' in kwargs: self.password = kwargs.get('password') kwargs.pop('password') super(FrontUser, self).__init__(*args, **kwargs) # 这个装饰的作用是让函数可以通过属性访问 @property def password(self): return self._password @password.setter def password(self, raw_password): # 加密后的非明文密码才能存到数据库中 self._password = generate_password_hash(raw_password) def check_password(self, raw_password): result = check_password_hash(self.password, raw_password) return result
class FrontUser(db.Model): __tablename__ = "front_user" id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid) telephone = db.Column(db.String(11), nullable=False, unique=True) username = db.Column(db.String(50), nullable=False) _password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True) realname = db.Column(db.String(50)) avatar = db.Column(db.String(100)) signature = db.Column(db.String(100)) gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOW) join_time = db.Column(db.DateTime, default=datetime.now) def __init__(self, *args, **kwargs): # 如果传入的参数里面有‘password’,就单独处理 if "password" in kwargs: self.password = kwargs.get("password") # 处理完后把password pop出去 kwargs.pop("password") # 剩下的参数交给父类去处理 super(FrontUser, self).__init__(*args, **kwargs) @property def password(self): return self._password # 保存密码的时候加密 @password.setter def password(self, raw_password): self._password = generate_password_hash(raw_password) def check_password(self, raw_password): result = check_password_hash(self.password, raw_password) return result
class Employee(db.Model): __tablename__ = 'employee' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(100), nullable=False) age = db.Column(db.Integer, nullable=False) sex = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOW) hometown = db.Column(db.String(100)) working_age = db.Column(db.Integer) education = db.Column(db.Enum(EducationEnum), default=EducationEnum.UNKNOW) certificate = db.Column(db.String(1000)) resume_score = db.Column(db.Integer) interview_score = db.Column(db.Integer) total_score = db.Column(db.Integer) join_time = db.Column(db.DateTime, default=datetime.now()) #定义外键 project_id = db.Column(db.Integer, db.ForeignKey('project.id')) hr_id = db.Column(db.Integer, db.ForeignKey('hr_user.id')) #ORM层面外键映射 project = db.relationship('Project', backref=db.backref('emploees', cascade='all')) hr = db.relationship('HrUser', backref=db.backref('emploees', cascade='all'))
class Rule(db.Model): __tablename__ = "rule" id = db.Column(db.Integer, primary_key=True, autoincrement=True) type = db.Column(db.Enum("file", "menu"), nullable=False) pid = db.Column(db.Integer, nullable=False, index=True, default=0) name = db.Column(db.String(100), unique=True, nullable=False) title = db.Column(db.String(50), nullable=False) icon = db.Column(db.String(50), nullable=False) condition = db.Column(db.String(255), nullable=False) remark = db.Column(db.String(255), nullable=False) ismenu = db.Column(db.SmallInteger, nullable=False) createtime = db.Column(db.Integer, nullable=False) updatetime = db.Column(db.Integer, nullable=False) weigh = db.Column(db.Integer, nullable=False, index=True, default=0) status = db.Column(db.String(30), nullable=False)
class UserModel(db.Model): #用户表 __tablename__='user' id=db.Column(db.String(200),primary_key=True,default=shortuuid.uuid) #用户id username=db.Column(db.String(50),nullable=False) #用户名 __passwd=db.Column(db.String(255),nullable=False) #用户密码 mobile = db.Column(db.String(11), nullable=False, unique=True) #手机号码 is_delete=db.Column(db.String(255),nullable=True) #是否拉黑 avatar = db.Column(db.String(200)) #头像 personal_introduction = db.Column(db.String(150)) #个人介绍 Email = db.Column(db.String(100)) gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.unknow) #性别 create_time=db.Column(db.DateTime,default=datetime.now()) #创建时间 money=db.Column(db.Integer,default=0) #金钱 def __init__(self,*args,**kwargs): if 'passwd' in kwargs: #对接受过来的密码进行一段处理 passwd=kwargs.get('passwd') self.passwd=passwd kwargs.pop('passwd') #删除这个passwd super(UserModel, self).__init__(*args,**kwargs) #剩下的内容交个父类处理 def to_dic(self): d={ 'id':self.id, 'username':self.username, 'mobile':self.mobile, 'avatar':self.avatar, 'personal_introduction':self.personal_introduction, # 'gender':self.gender, 'create_time':self.create_time, 'money':self.money, 'email':self.Email } return d @property #对密码进行加密 def passwd(self): return self.__passwd @passwd.setter def passwd(self,passwd): self.__passwd=generate_password_hash(passwd) def check_passwd(self,passwd): #查看用户输入的密码是否正确 return check_password_hash(self.passwd,passwd)
class OrderModel(db.Model): #订单模型 __tablename__ = 'order' id = db.Column(db.Integer, primary_key=True, nullable=False) #订单编号 number = db.Column(db.Integer, nullable=False) #商品的数量 good_price = db.Column(db.Integer, nullable=False) #商品的总价 status = db.Column(db.Enum(StatusEnum), default=StatusEnum.WAIT_PAY) #订单状态 comment = db.Column(db.Text) #评论信息,或者拒绝理由 create_time = db.Column(db.DateTime, default=datetime.now(), nullable=False) #下单时间 remark = db.Column(db.Text) #下单备注 address_id = db.Column(db.Integer, db.ForeignKey('rcaddress.id'), nullable=False) #地址id user_id = db.Column(db.String(255), db.ForeignKey('user.id'), nullable=False) #下单客户 goods_id = db.Column(db.Integer, db.ForeignKey('goods.id'), nullable=False) #商品ID address = db.relationship('AddressModel', backref='orders') good = db.relationship('GoodsModel', backref='orders') user = db.relationship('UserModel', backref='orders') def to_dic(self): if self.status == StatusEnum.WAIT_PAY: status = '待支付' elif self.status == StatusEnum.PAID: status = '已支付' elif self.status == StatusEnum.WAIT_COMMENT: statue = '😳评论' elif self.status == StatusEnum.COMPLETE: status = '已完成' elif self.status == StatusEnum.CANCEL: status = '已取消' d = { 'id': self.id, 'number': self.number, 'goods_price': self.good_price, 'status': status, 'create_time': self.create_time, 'remark': self.remark, 'goodsName': self.good.title, } return d
class Comment(db.Model): __tablename__ = 'comments' id = db.Column(db.Integer, autoincrement=True, primary_key=True) content = db.Column(db.Text, nullable=False) author_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) post_id = db.Column(db.Integer, db.ForeignKey('posts.id'), nullable=False) floor = db.Column(db.Integer, nullable=False) parent = db.Column(db.Integer) com_time = db.Column(db.DateTime, nullable=False, default=datetime.now) like_num = db.Column(db.Integer, nullable=False, default=0) status = db.Column(db.Enum("normal", "blocked", "hot"), nullable=False, default="normal") author = db.relationship('User', backref=db.backref('comments')) article = db.relationship('Post', backref=db.backref('comments'))
class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, autoincrement=True, primary_key=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False) post_day = db.Column(db.Date, nullable=False, default=date.today()) author_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) board_id = db.Column(db.Integer, db.ForeignKey('boards.id'), nullable=False) com_num = db.Column(db.Integer, nullable=False, default=0) favor_num = db.Column(db.Integer, nullable=False, default=0) reward = db.Column(db.Integer, nullable=False, default=0) status = db.Column(db.Enum("reviewing", "normal", "blocked", "hot"), nullable=False, default="reviewing") author = db.relationship('User', backref=db.backref('articles')) board = db.relationship('Board', backref=db.backref('articles'))
class Front_User(db.Model): __tablename__ = "front_user" # id 类型不用db.Integer类型,使用String是为了防止爆破,同时使用shortuuid进行加密 id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid) telephone = db.Column(db.String(11), nullable=False, unique=True) # 非空唯一 username = db.Column(db.String(150), nullable=False) _password = db.Column(db.String(150), nullable=False) # 密码加密操作修改字段 email = db.Column(db.String(50), unique=True) realname = db.Column(db.String(50)) avatar = db.Column(db.String(150)) # 头像,二进制数据 signatrue = db.Column(db.String(500)) # 签名 gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOW) # 性别枚举类,默认未知 join_time = db.Column(db.DateTime, default=datetime.now) # 默认当前时间 # 修改密码加密操作,manage.py映射数据库时候,使用字段保持相同,由于字段太多,使用传参形式 def __init__(self, *args, **kwargs): if 'password' in kwargs: # 如果传参中包含有 self.password = kwargs.get('password') # 获取该参数值赋值给password kwargs.pop('password') # 模型参数中是_password,不是password,弹出 # super(FrontUser, self).__init__(*args, **kwargs) # python2的写法 super().__init__(*args, **kwargs) # 密码加密操作 @property def password(self): # 密码取值 return self._password @password.setter # 密码加密 def password(self, raw_password): self._password = generate_password_hash(raw_password) # 用于验证前台登录密码是否和数据库一致,raw_password是前台登录输入的密码 def check_password(self, raw_password): result = check_password_hash( self.password, raw_password) # 相当于用相同的hash加密算法加密raw_password,检测与数据库中是否一致 return result
class FrontUser(db.Model): __tablename__="front_user" id=db.Column(db.String(30),primary_key=True,default=shortuuid.uuid) telephone=db.Column(db.String(11),nullable=False,unique=True) username=db.Column(db.String(50),nullable=False) _password=db.Column(db.String(100),nullable=False) email=db.Column(db.String(50),unique=True) realname=db.Column(db.String(50)) avatar=db.Column(db.String(100)) signature=db.Column(db.String(100)) gender=db.Column(db.Enum(GenderEnum),default=GenderEnum.UNKNOW) join_time=db.Column(db.DateTime,default=datetime.now) @property def password(self): return self._password @password.setter def password(self,raw_password): self._password=generate_password_hash(raw_password) def check_password(self,raw_password): return check_password_hash(self.password,raw_password)
class FrontUser(db.Model, UserMixin): """ 定义一个类名,系统将类转换为表格,表名为front_user 定义类的属性就是表格的字段名 为了安全性,可以将用户表的password属性置为保护属性如 _password 为了方便用户操作保护属性像操作普通属性一样,需要装饰 _password 在设置密码的过程中,需要对密码加密——>调用generate_password_hash() 另外定义一个校验密码的方法check_password() 在校验密码的过程中,需要对密码解密——>调用check_password_hash() """ __tablename__ = "front_user" id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid) telephone = db.Column(db.String(11), unique=True, nullable=False) username = db.Column(db.String(50), nullable=False) _password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True) realname = db.Column(db.String(50)) avatar = db.Column(db.String(100)) signature = db.Column(db.String(100)) gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOWN) join_time = db.Column(db.DateTime, default=datetime.now) def __init__(self, *args, **kwargs): if "password" in kwargs: self.password = kwargs.get("password") kwargs.pop("password") super().__init__(*args, **kwargs) @property def password(self): return self._password @password.setter def password(self, newpwd): self._password = generate_password_hash(newpwd) def check_password(self, rawpwd): return check_password_hash(self._password, rawpwd)
class FrontUser(db.Model): __tablename__ = 'front_user' id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid) telephone = db.Column(db.String(11), nullable=True) username = db.Column(db.String(50), nullable=False) _password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True) realname = db.Column(db.String(50)) # 头像 avatar = db.Column(db.String(100)) signature = db.Column(db.String(100)) gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOW) join_time = db.Column(db.DateTime, default=datetime.now) works = db.relationship('WorkModel', backref='writer') def __init__(self, *args, **kwargs): if 'password' in kwargs: self.password = kwargs.get('password') # 把 password 取出来之后,从 kwargs 中去掉 kwargs.pop('password') super(FrontUser, self).__init__(*args, **kwargs) # password属性(方法)获取值 @property def password(self): return self._password # password属性(方法)赋值 @password.setter def password(self, raw_password): self._password = generate_password_hash(raw_password) def check_password(self, raw_password): result = check_password_hash(self.password, raw_password) return result
class FrontUser(db.Model): __tablename__ = 'front_user' # id不能为自增长,因为存在商业风险,特别是id关联的url id = db.Column( db.String(100), primary_key=True, default=shortuuid.uuid ) # 这里uuid不加(),如果给了就是每次都是把执行结果当作id,而现在的需求是每次执行产生一个 telephone = db.Column(db.String(11), nullable=False, unique=True) username = db.Column(db.String(50), nullable=False) _password = db.Column(db.String(100), nullable=False) # 加入_后,变成受保护属性 email = db.Column(db.String(50), nullable=True, unique=True) realname = db.Column(db.String(50)) avatar = db.Column(db.String(100)) signature = db.Column(db.String(100)) gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOWN) join_time = db.Column(db.DateTime, default=datetime.now) # 初始化 def __init__(self, *args, **kwargs): # kwargs是关键字参数,等同于字典 if "password" in kwargs: self.password = kwargs.get('password') kwargs.pop('password') # 其他参数可以用父类去处理 super(FrontUser, self).__init__(*args, **kwargs) @property def password(self): return self._password @password.setter def password(self, newpwd): self._password = generate_password_hash(newpwd) def check_password(self, rawpwd): return check_password_hash(self._password, rawpwd)
class FrontUser(db.Model): __tablename__ = 'front_user' id = db.Column(db.String(100), primary_key=True, default=shortuuid.uuid) telephone = db.Column(db.String(11), nullable=False, unique=True) username = db.Column(db.String(50), nullable=False) # 加密后的密码 _password = db.Column(db.String(100), nullable=False) email = db.Column(db.String(50), unique=True) realname = db.Column(db.String(50)) avatar = db.Column(db.String(100)) signature = db.Column(db.String(100)) # 使用枚举,设定默认值为类下的属性 gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOW) # 此处使datetime.now,而不是datetime.now(),实例化对象时会自动计算时间,而后 # 者在模型创建刚使用时的时间 join_time = db.Column(db.DateTime, default=datetime.now) def __init__(self, *args, **kwargs): if "password" in kwargs: # kwargs属于字典,这里可以调用字典的内建方法,get,pop,其他内建方法有: #调用password函数,返回的self._password self.password = kwargs.get('password') kwargs.pop("password") #super的作用:将除去password的数据提交给父类去处理 super(FrontUser, self).__init__(*args, **kwargs) # 使用装饰器,将函数定义为属性,这里一方面为上面初始化时调用,另一方面为后面给属性增加方法 @property def password(self): return self._password @password.setter def password(self, newpwd): self._password = generate_password_hash(newpwd) def check_password(self, rawpwd): return check_password_hash(self._password, rawpwd)
class Role(db.Model): __tablename__ = 'role' id = db.Column(db.Integer, primary_key=True, autoincrement=True) u_id = db.Column(db.String(100), db.ForeignKey('user.id')) group = db.Column(db.Enum(Group), default=Group.FrontUser, nullable=False) permissions = db.Column(db.Integer, nullable=False) user = db.relationship('User', backref=db.backref('role', uselist=False)) default_group = 'FrontUser' def __init__(self, **kwargs): super(Role, self).__init__(**kwargs) if self.group is None: self.group = self.default_group if self.permissions is None: self.permissions = sum(getattr(Group, self.group, 0).value) else: self.permissions = min(sum(getattr(Group, self.group).value), self.permissions) def add_permission(self, *perms): for perm in perms: if not self.has_permission(perm): self.permissions += perm def remove_permission(self, *perms): for perm in perms: if self.has_permission(perm): self.permissions -= perm def reset_permissions(self): self.permissions = sum(getattr(Group, self.group).value) def has_permission(self, perm): return self.permissions & perm == perm def __repr__(self): return '<Role:%r>' % self.group
class FrontUser(db.Model): __tablename__ = 'front_user' id = db.Column(db.String(25), primary_key=True, unique=True, default=shortuuid.uuid) name = db.Column(db.String(20), nullable=False) _password = db.Column(db.String(200), nullable=False) mobile = db.Column(db.String(15), nullable=False, unique=True) email = db.Column(db.String(30), unique=True) gender = db.Column(db.Enum(GenderEnum), default=GenderEnum.UNKNOWN) avatar = db.Column(db.String(150), default=DEFAULT_AVATAR) signature = db.Column(db.String(50)) realname = db.Column(db.String(10)) join_time = db.Column(db.DateTime, default=datetime.now) def __init__(self, *args, **kwargs): if 'password' in kwargs: self.password = kwargs.get('password') kwargs.pop('password') super(FrontUser, self).__init__(*args, **kwargs) @property def password(self): # 通过property将password转化为方法, 以便进行操作后再存储 return self._password @password.setter def password(self, raw_password): # setter装饰器可以在创建类时取代init self._password = generate_password_hash(raw_password) def check_password(self, raw_password): result = check_password_hash(self.password, raw_password) return result
class User(db.Model): __tablename__ = "user" id = db.Column(db.String(100),primary_key=True,default=shortuuid.uuid) zxbh = db.Column(db.String(50),nullable=False,unique=True) username = db.Column(db.String(50),nullable=False) _password = db.Column(db.String(100),nullable=False) realname = db.Column(db.String(50)) avatar = db.Column(db.String(100), default='image/avatar/default.png') signature = db.Column(db.String(100)) gender = db.Column(db.Enum(GenderEnum),default=GenderEnum.UNKNOW) join_time = db.Column(db.DateTime,default=datetime.now) def __init__(self, *args, **kwargs): # 如果传入的参数里面有‘password’,就单独处理 if "password" in kwargs: self.password = kwargs.get("password") # 处理完后把password pop出去 kwargs.pop("password") # 剩下的参数交给父类去处理 super(User, self).__init__(*args,**kwargs) @property def password(self): return self._password # 保存密码的时候加密 @password.setter def password(self, raw_password): self._password = generate_password_hash(raw_password) def check_password(self, raw_password): result = check_password_hash(self.password, raw_password) return result @property def permissions(self): # 用户拥有的权限 if not self.roles: return 0 all_permissions = 0 # 用户所有的角色 for role in self.roles: # 取出用户所有角色的所有权限 permissions = role.permissions # 把所有权限通过“|=”整合到all_permissions all_permissions |= permissions return all_permissions def has_permission(self, permission): # 判断用户是否有'xxx'权限 # 通过与操作判断用户是否有权限,结果相同返回true,不相等返回false return self.permissions & permission == permission @property def is_developer(self): return self.has_permission(Permission.ALL_PERMISSION) def is_voted(self, post_id): for post in self.postvote: if post.post_id == post_id: return True return False