class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(50), nullable=False, unique=True, index=True) show_name = db.Column(db.String(100)) default = db.Column(db.Boolean, default=False, index=True) permissions = db.Column(db.Integer) users = db.relationship('User', backref='role', lazy='dynamic') def __init__(self, *args, **kwargs): super(Role, self).__init__(*args, **kwargs) @staticmethod def insert_roles(): roles = { 'User': (Permission.USER, True, u'修学处义工'), # 修学处义工权限 'Moderator': (Permission.MODERATOR, False, u'辅导委义工'), # 辅导委义工权限 'Admin': (Permission.ADMIN, False, u'管理员') # 超级管理员权限 } 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] role.show_name = roles[r][2] db.session.add(role) db.session.commit() def __repr__(self): return '<Role %r>' % self.name
class LeadClassRecord(db.Model): __tablename__ = 'lead_class_records' id = db.Column(db.Integer, primary_key=True, autoincrement=True) from_date = db.Column(db.Date) to_date = db.Column(db.Date) counselor_id = db.Column(db.Integer, db.ForeignKey('counselors.id')) class_id = db.Column(db.Integer, db.ForeignKey('classes.id')) duty_id = db.Column(db.Integer, db.ForeignKey('duties.id')) update_user = db.Column(db.Integer) create_time = db.Column(db.DateTime, default=datetime.utcnow()) update_time = db.Column(db.DateTime, default=datetime.utcnow) def __init__(self, *args, **kwargs): super(LeadClassRecord, self).__init__(*args, **kwargs)
class EvaluationRecord(db.Model): __tablename__ = 'evaluation_records' id = db.Column(db.Integer, primary_key=True, autoincrement=True) item = db.Column(db.String(255), nullable=False, index=True) shiftdate = db.Column(db.Date, nullable=False) score = db.Column(db.Integer) counselor_id = db.Column(db.Integer, db.ForeignKey('counselors.id')) update_user = db.Column(db.Integer) create_time = db.Column(db.DateTime, default=datetime.utcnow) update_time = db.Column(db.DateTime, default=datetime.utcnow) def __init__(self, *args, **kwargs): super(EvaluationRecord, self).__init__(*args, **kwargs)
class Duty(db.Model): __tablename__ = 'duties' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), index=True, unique=True, nullable=False) desc = db.Column(db.Text) update_user = db.Column(db.Integer) create_time = db.Column(db.DateTime, default=datetime.utcnow) update_time = db.Column(db.DateTime, default=datetime.utcnow) counselors = db.relationship('Counselor', backref='duty', lazy='dynamic') lead_class_records = db.relationship('LeadClassRecord', backref='duty', lazy='dynamic') def __init__(self, *args, **kwargs): super(Duty, self).__init__(*args, **kwargs) @staticmethod def insert_duties(): duties = { u'辅导员': (u'岗位:辅导员',), u'实习辅导员': (u'岗位:辅导员',), u'辅助员': (u'岗位:辅助员',) } for r in duties: duty = Duty.query.filter_by(name=r).first() if duty is None: duty = Duty(name=r) duty.desc = duties[r][0] duty.create_time = datetime.utcnow() duty.update_time = datetime.utcnow() db.session.add(duty) db.session.commit()
class Class(db.Model): __tablename__ = 'classes' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), index=True, unique=True, nullable=False) number = db.Column(db.String(255), unique=True, nullable=False, index=True) # 班级编号 desc = db.Column(db.Text) update_user = db.Column(db.Integer) create_time = db.Column(db.DateTime, default=datetime.utcnow) update_time = db.Column(db.DateTime, default=datetime.utcnow) department_id = db.Column(db.Integer, db.ForeignKey('departments.id')) counselors = db.relationship('Counselor', backref='class_', lazy='dynamic') lead_class_records = db.relationship('LeadClassRecord', backref='class_', lazy='dynamic') def __init__(self, *args, **kwargs): super(Class, self).__init__(*args, **kwargs) def __repr__(self): return '<Class %r>' % self.name def to_json(self): return {'id': self.id, 'name': self.name, 'desc': self.desc, 'number': self.number}
class Division(db.Model): __tablename__ = 'divisions' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), index=True, unique=True, nullable=False) desc = db.Column(db.Text) update_user = db.Column(db.Integer) create_time = db.Column(db.DateTime, default=datetime.utcnow) update_time = db.Column(db.DateTime, default=datetime.utcnow) departments = db.relationship('Department', backref='division', lazy='dynamic') users = db.relationship('User', backref='division', lazy='dynamic') def __init__(self, *args, **kwargs): super(Division, self).__init__(*args, **kwargs) def __repr__(self): return '<Division %r>' % self.name
class Department(db.Model): __tablename__ = 'departments' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), index=True, unique=True, nullable=False) desc = db.Column(db.Text) update_user = db.Column(db.Integer) create_time = db.Column(db.DateTime, default=datetime.utcnow) update_time = db.Column(db.DateTime, default=datetime.utcnow) division_id = db.Column(db.Integer, db.ForeignKey('divisions.id')) classes = db.relationship('Class', backref='department', lazy='dynamic') def __init__(self, *args, **kwargs): super(Department, self).__init__(*args, **kwargs) def __repr__(self): return '<Department %r>' % self.name def to_json(self): return {'id': self.id, 'name': self.name, 'desc': self.desc}
class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(50), nullable=False, unique=True, index=True) _password_hash = db.Column('password', db.String(255), nullable=False, server_default=u'') realname = db.Column(db.String(255)) email = db.Column(db.String(255), nullable=False) last_login_time = db.Column(db.DateTime, default=datetime.now) last_login_ip = db.Column(db.String(50), default='') login_count = db.Column(db.Integer, nullable=False, default=0) is_active = db.Column(db.Boolean, nullable=False, default=True) role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) create_user = db.Column(db.Integer) create_time = db.Column(db.DateTime, default=datetime.utcnow) update_user = db.Column(db.Integer) update_time = db.Column(db.DateTime, default=datetime.utcnow) is_delete = db.Column(db.Integer, default=0) division_id = db.Column(db.Integer, db.ForeignKey('divisions.id')) def __init__(self, *args, **kwargs): super(User, self).__init__(*args, **kwargs) def __repr__(self): return '<User %r>' % self.username @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) @property def is_admin(self): return self.can(Permission.ADMIN) @property def is_moderator(self): return self.can(Permission.MODERATOR) def verify_password(self, password): return check_password_hash(self._password_hash, password) def can(self, permissions): return self.role is not None and (self.role.permissions | permissions) == permissions def cando(self, division_id): if self.role.permissions | Permission.USER == Permission.USER: if self.division_id != division_id: return False return True def update_login_info(self, last_login_ip): self.last_login_ip = last_login_ip self.login_count += 1 db.session.add(self) db.session.commit() @staticmethod def create_admin(): u = User() u.username = '******' u.password = '******' u.realname = 'Admin' u.email = 'admin@localhost' u.role = Role.query.filter_by(name='Admin').first() u.division_id = 0 db.session.add(u) db.session.commit()
class TrainingRecord(db.Model): __tablename__ = 'training_records' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), nullable=False) location = db.Column(db.String(255), nullable=False) content = db.Column(db.Text) remark = db.Column(db.Text) from_date = db.Column(db.Date) to_date = db.Column(db.Date) counselor_id = db.Column(db.Integer, db.ForeignKey('counselors.id')) update_user = db.Column(db.Integer) create_time = db.Column(db.DateTime, default=datetime.utcnow) update_time = db.Column(db.DateTime, default=datetime.utcnow) def __init__(self, *args, **kwargs): super(TrainingRecord, self).__init__(*args, **kwargs)
class Counselor(db.Model): __tablename__ = 'counselors' id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(100), nullable=False, index=True) religiousname = db.Column(db.String(100)) gender = db.Column(db.String(50)) birthday = db.Column(db.Date) mobile = db.Column(db.String(50)) email = db.Column(db.String(100)) class_id = db.Column(db.Integer, db.ForeignKey('classes.id')) duty_id = db.Column(db.Integer, db.ForeignKey('duties.id')) # 岗位 is_delete = db.Column(db.Integer, default=0) create_user = db.Column(db.Integer) create_time = db.Column(db.DateTime, default=datetime.utcnow) update_user = db.Column(db.Integer) update_time = db.Column(db.DateTime, default=datetime.utcnow) lead_class_records = db.relationship('LeadClassRecord', backref='counselor', lazy='dynamic') training_records = db.relationship('TrainingRecord', backref='counselor', lazy='dynamic') evaluation_records = db.relationship('EvaluationRecord', backref='counselor', lazy='dynamic') def __init__(self, *args, **kwargs): super(Counselor, self).__init__(*args, **kwargs) def __repr__(self): return '<Counselor %r>' % self.username if self.religiousname is None else self.religiousname