class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(100), nullable=True) # 学号/职工号 name = db.Column(db.String(100), nullable=True) # 姓名 password = db.Column(db.String(30), nullable=True) # 密码 telephone = db.Column(db.String(100), nullable=True) # 用户电话 gender = db.Column(db.String(10), nullable=True) # 性别 age = db.Column(db.String(10), nullable=True) # 年龄 address = db.Column(db.String(10), nullable=True) # 籍贯 temperature = db.Column(db.String(100)) picture=db.Column(db.LargeBinary(length=4294967295)) status = db.Column(db.Enum('正常', '异常', '疑似'), server_default='正常') dormitory_id = db.Column(db.Integer, db.ForeignKey('dormitory.id'), nullable=True) # 宿舍楼-号 class_id = db.Column(db.Integer, db.ForeignKey('class.id'), nullable=True) # 班级 college_id = db.Column(db.Integer, db.ForeignKey('college.id'), nullable=True) # 学院 role_id = db.Column(db.Integer, db.ForeignKey('role.id'), nullable=True) # 角色信息 classa = db.relationship('Class', backref=db.backref('students1')) # 可以直接通过user._classa得到班级信息 role = db.relationship('Role', backref=db.backref('users')) # 可以直接通过user.role得到用户角色信息 college = db.relationship('College', backref=db.backref('students2')) # 直接通过user.college得到学院信息 dormitory = db.relationship('Dormitory', backref=db.backref('students3')) # 直接通过user.dormitory得到宿舍信息
class UserModel(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(80)) hashed_password_b64 = db.Column(db.LargeBinary(80)) def __init__(self, email: str, password: str) -> None: self.email = email self.hashed_password_b64 = base64.b64encode( bcrypt.hashpw(password.encode(), bcrypt.gensalt())) def save_to_db(self) -> None: db.session.add(self) db.session.commit() @classmethod def find_by_email(cls, email: str) -> Optional[UserModel]: return cls.query.filter_by(email=email).first() @classmethod def find_by_id(cls, _id: str) -> Optional[UserModel]: return cls.query.filter_by(id=_id).first()
class Profile(db.Model): id = db.Column(db.Integer, primary_key = True) img = db.Column(db.LargeBinary(length=2048), nullable=False) mimetype = db.Column(db.Text, nullable=False) username = db.Column(db.Text, nullable=False) description = db.Column(db.Text, nullable=False)
class UserModel(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), nullable=False) email = db.Column(db.String(80), nullable=False, unique=True) pw_salt = db.Column(db.LargeBinary(80), nullable=False) pw_hash = db.Column(db.LargeBinary(100), nullable=False) displayname = db.Column(db.String(100), nullable=False, unique=True) confirmation = db.relationship("ConfirmationModel", lazy="dynamic", cascade="all, delete-orphan") @property def most_recent_confirmation(self) -> "ConfirmationModel": # ordered by expiration time (in descending order) return self.confirmation.order_by(db.desc( ConfirmationModel.expire_at)).first() def save_to_db(self) -> None: """ Save the User record to the database """ db.session.add(self) db.session.commit() def delete_from_db(self) -> None: """ Delete the user record from the database """ db.session.delete(self) db.session.commit() def send_confirmation_email(self) -> Union[Response, None]: """ Send an address confirmation email to the user """ link = request.url_root[:-1] + url_for( "confirmation", confirmation_id=self.most_recent_confirmation.id) return Mailgun.send_email( from_email=msgs.FROM_EMAIL, from_title=msgs.FROM_TITLE, to_email=[self.email], subject=msgs.CONFIRMATION_MAIL_SUBJECT, text=msgs.CONFIRMATION_MAIL_BODY.format(name=self.username, link=link), html=msgs.CONFIRMATION_MAIL_BODY_HTML.format(name=self.username, link=link)) @classmethod def find_all(cls) -> List['UserModel']: """ :param """ return cls.query.all() @classmethod def find_by_username(cls, username: str) -> 'UserModel': return cls.query.filter_by(username=username).first() @classmethod def find_by_email(cls, email: str) -> 'UserModel': return cls.query.filter_by(email=email).first() @classmethod def find_by_id(cls, _id: int) -> 'UserModel': return cls.query.filter_by(id=_id).first() @classmethod def find_by_displayname(cls, displayname: str) -> 'UserModel': return cls.query.filter_by(displayname=displayname).first()