class SystemVersion(SurrogatePK, Model): """系统更新版本号列表 暂定不用了 表名:system_versions parameters参数: number版本号 title标题 summary描述 context内容 created_at创建时间 """ __tablename__ = 'system_versions' number = Column(db.String(20)) title = Column(db.String(100)) summary = Column(db.String(200)) context = Column(db.UnicodeText) created_at = Column(db.DateTime, nullable=False, default=dt.datetime.now)
class School(SurrogatePK, Model): """学校列表 表名称:schools parameters参数: name:学校名称 active:是否激活 指是否可以添加下级操作 state:是否可使用,暂定,无作用 created_at:创建时间 grades:外键年级表 user:外键用户表,,这里设置校管员 student_parents外键家长表 doorkeepers这里外键到门卫表,用于判断门卫归属那个学校 """ __tablename__ = 'schools' name = Column(db.String(80), nullable=False) active = Column(db.Boolean(), default=False) state = Column(db.Boolean(), default=True) created_at = Column(db.DateTime, nullable=False, default=dt.datetime.now) # phone = Column(db.String(80), nullable=False) grades = relationship('Grade', backref='schools', lazy='joined') student_parents = relationship('StudentParent', backref='schools') doorkeepers = relationship('Doorkeeper', backref='schools') user = reference_col('users')
class ChargeTeacher(SurrogatePK, Model): """教师表 表名:charge_teacher parameters参数: number编码 name姓名 phone手机号 id_number身份证号 address住址 tmp_classes_id 临时存储 classes_id班级字段 发送 微信通知到校管员 同意后变更为classes_id classes_id外键班级表,只要对应班级表后即可查询 所属学校、所属年级 user外键用户表 """ __tablename__ = 'charge_teacher' number = Column(db.String(80), nullable=False) phone = Column(db.String(80)) name = Column(db.String(80)) id_number = Column(db.String(80)) address = Column(db.String(200)) tmp_classes_id = Column(db.String(80)) user = reference_col('users') student_entry_exit_id = relationship('Classes', backref='teacher', lazy='dynamic')
class StudentParent(SurrogatePK, Model): """学生家长表 表名称:student_parents parameters参数: phone手机号 name姓名 id_number身份证号 address住址 child_name子女姓名 child_number子女学号 student外键学生表 user外键用户表 school_id外键学校表 """ __tablename__ = 'student_parents' student = relationship('Student', backref='parents') phone = Column(db.String(80)) name = Column(db.String(80)) id_number = Column(db.String(80)) address = Column(db.String(200)) child_name = Column(db.String(80)) child_number = Column(db.String(80)) user = reference_col('users') school_id = reference_col('schools')
class Doorkeeper(SurrogatePK, Model): """门卫表 表名:doorkeepers parameters参数: number编号 user外键用户表 school_id这里外键到学校表 方式变更 原代码门卫发起请假代码需要更改 """ __tablename__ = 'doorkeepers' number = Column(db.String(80), nullable=False) user = reference_col('users') school_id = reference_col('schools')
class Classes(SurrogatePK, Model): """班级表 表名称:classesd parameters参数: name班级名称 student学生表 charge_teachers班主任表 grades年级表 """ __tablename__ = 'classesd' name = Column(db.String(80), nullable=False) student = relationship('Student', backref='classes') charge_teachers = reference_col('charge_teacher') grades = reference_col('grades')
class Grade(SurrogatePK, Model): """年级表 表名称:grades parameters参数: name:年级名称 created_at:创建时间 classes_id外键 school外键学校表 """ __tablename__ = 'grades' name = Column(db.String(80), nullable=False) created_at = Column(db.DateTime, nullable=False, default=dt.datetime.now) classes_id = relationship('Classes', backref='grade') school = reference_col('schools')
class Student(SurrogatePK, Model): """学生表 表名称:students parameters参数: number学号 sex性别#男女 默认男 name姓名 img照片 patriarch_name家长姓名 id_number身份证号 address住址 phone联系电话 user外键用户 默认空 parent外键家长表 默认空 classesd外键班级 ask_student外键请假表 entry_exit 外键学生出入表 """ __tablename__ = 'students' number = Column(db.String(80), nullable=False) sex = Column(db.Boolean(), default=True) #男女 name = Column(db.String(80)) img = Column(db.String(100)) patriarch_name = Column(db.String(80)) patriarch_phone = Column(db.String(50)) id_number = Column(db.String(80)) address = Column(db.String(200)) phone = Column(db.String(50)) user = reference_col('users') parent = reference_col('student_parents') classesd = reference_col('classesd') ask_student = relationship('AskLeave', backref='ask_student', primaryjoin="Student.id == AskLeave.ask_users") entry_exit = relationship('StudentEntryExit', backref='student')
class Role(SurrogatePK, Model): """A role for a user.""" __tablename__ = 'roles' name = Column(db.String(80), unique=True, nullable=False) permissions = db.Column(db.Integer) default = db.Column(db.Boolean, default=False, index=True) user = relationship('User', backref='roles') def __init__(self, name, **kwargs): """Create instance.""" db.Model.__init__(self, name=name, **kwargs) def __repr__(self): """Represent instance as a unique string.""" return '<Role({name})>'.format(name=self.name) @staticmethod def insert_roles(): roles = { 'Others': (0, True), #其他 'Students': (Permission.LEAVE, True), #学生 'Doorkeeper': (Permission.LEAVE | Permission.RETURN_LEAVE, False), #门卫 'Patriarch': (Permission.LEAVE, False), #家长 'Teacher': (Permission.ALLOW_LEAVE, False), #教师 'Principal': (0xfff, False), #校长 'ADMIN': (0xffff, False) #管理员 } for r in roles: role = Role.query.filter_by(name=r).first() if role is None: role = Role(name=r) role.permissions = roles[r][0] role.default = roles[r][1] db.session.add(role) db.session.commit() """
class User(UserMixin, SurrogatePK, Model): """ username:用户名自动生成 password密码 created_at密码 active是否激活 is_admin是否管理员 wechat_id微信id phone手机号 name真实姓名 id_number身份证号码 address住址 car_number车牌号 q_number用户编号。用户识别 用于出入等场景 格式:Q+ID """ __tablename__ = 'users' username = Column(db.String(80), unique=True, nullable=False) #: The hashed password password = Column(db.Binary(128), nullable=True) created_at = Column(db.DateTime, nullable=False, default=dt.datetime.now) active = Column(db.Boolean(), default=False) is_admin = Column(db.Boolean(), default=False) wechat_id = Column(db.String(80), nullable=True) #默认这里的手机号等信息用于其他人员, 班主任 学生家长身份的信息在各个表中 phone = Column(db.String(80)) name = Column(db.String(80)) id_number = Column(db.String(80)) address = Column(db.String(200)) car_number = Column(db.String(80)) q_number = Column(db.String(80), unique=True) role = reference_col('roles') #学校表 # school = reference_col('schools') #教师表 一对一 teacher = relationship('ChargeTeacher', backref='users', uselist=False) #学生表 一对一 student = relationship('Student', backref='users', uselist=False) #学生表家长 一对一 parents = relationship('StudentParent', backref='users', uselist=False) #门卫 一对一 doorkeeper = relationship('Doorkeeper', backref='users', uselist=False) #请假发起人 send_users = relationship('AskLeave', backref='send_ask_user', primaryjoin="User.id == AskLeave.send_users") #批准请假人 charge_users = relationship('AskLeave', backref='charge_ask_user', primaryjoin="User.id == AskLeave.charge_users") schools = relationship('School', backref='users') def __init__(self, username, password=None, **kwargs): """Create instance.""" db.Model.__init__(self, username=username, **kwargs) if password: self.set_password(password) else: self.password = None def set_password(self, password): """Set password.""" self.password = bcrypt.generate_password_hash(password) def check_password(self, value): """Check password.""" return bcrypt.check_password_hash(self.password, value) @property def full_name(self): """Full user name.""" return '{0} {1}'.format(self.first_name, self.last_name) def __repr__(self): """Represent instance as a unique string.""" return '<User({username!r})>'.format(username=self.username) def can(self, permissions): return self.roles is not None and \ (self.roles.permissions & permissions) == permissions