class Group(db.Model): __tablename__ = 'group' id = db.Column(db.Integer, primary_key=True, autoincrement=True) group_name = db.Column(db.String(128), unique=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) eui = db.Column(db.String(32), unique=True) group_id = db.Column(db.String(8), unique=True) @property def buses(self): return Bus.query.filter(Bus.group_id == self.id).all() def __init__(self, group_name, group_id, group_eui=None): self.group_name = group_name self.user_id = g.user.id self.group_id = group_id self.eui = group_eui def __repr__(self): return '<Group %s>' % self.group_name @classmethod def get_groups(cls): return cls.query.filter(Group.user_id == g.user.id).all() @classmethod def get(cls, group_id): return cls.query.filter(Group.id == group_id).first() @classmethod def delete(cls, group_id): buses = Bus.query.filter(Bus.group_id == group_id).all() del_obj = cls.get(group_id) db.session.delete(del_obj) if buses: for bus in buses: bus.group_id = None db.session.add(bus) db.session.commit() def save(self): db.session.add(self) db.session.commit() def update(self, **kwargs): pass def remove(self): buses = Bus.query.filter(Bus.group_id == self.id).all() db.session.delete(self) if buses: for bus in buses: bus.group_id = None db.session.add(bus) db.session.commit()
class Bus(db.Model): __tablename__ = 'bus' id = db.Column(db.Integer, primary_key=True) route_id = db.Column(db.Integer, db.ForeignKey('route.id')) group_id = db.Column(db.Integer, nullable=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) plate_number = db.Column(db.String(16), unique=True) # 车牌号 light_number = db.Column(db.String(16)) # 车灯号 eui = db.Column(db.String(64), unique=True) # 广告发送模块ID def __init__(self, route_id, plate_number, light_number=None, eui=None): self.route_id = route_id self.plate_number = plate_number self.user_id = g.user.id if light_number: self.light_number = light_number if eui: self.eui = eui def __repr__(self): return '<Bus %s>' % self.plate_number @classmethod def get_bus_list(cls): return cls.query.filter(Bus.user_id == g.user.id).all() @classmethod def get(cls, bus_id): return cls.query.filter(Bus.id == bus_id).first() @classmethod def get_buses_by_route(cls, route_id): return cls.query.filter(Bus.route_id == route_id).all() def update_bus(self, route_id=None, plate_number=None, light_number=None, eui=None): if route_id: self.route_id = route_id if plate_number: self.plate_number = plate_number if light_number: self.light_number = light_number if eui: self.eui = eui @classmethod def get_all(cls): return cls.query.filter(cls.user_id == g.user.id).all()
class District(db.Model): __tablename__ = 'district' id = db.Column(db.Integer, primary_key=True) district_name = db.Column(db.String(128), unique=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) # one-to-many routes = db.relationship('Route', backref=db.backref('district')) def __init__(self, district_name): self.district_name = district_name self.user_id = g.user.id def __repr__(self): return '<District %r>' % self.district_name # district list @classmethod def get_district_list(cls): if hasattr(g, 'user'): return cls.query.filter(District.user_id == g.user.id).all() @classmethod def get(cls, district_id): return cls.query.filter(District.id == district_id).first() def update_distrct(self, district_name): self.district_name = district_name
class Route(db.Model): __tablename__ = 'route' id = db.Column(db.Integer, primary_key=True) district_id = db.Column(db.Integer, db.ForeignKey('district.id', ondelete="CASCADE"), nullable=False) route_name = db.Column(db.String(128), unique=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) status = db.Column(db.Integer, default=1) # 可用标志位, 0 不可用, 1 可用(删除) # one-to-many buses = db.relationship('Bus', backref=db.backref('route')) def __init__(self, district_id, route_name): self.district_id = district_id self.route_name = route_name self.user_id = g.user.id def __repr__(self): return '<Route %s>' % self.route_name @classmethod def get_routes(cls): return cls.query.filter(Route.user_id == g.user.id).all() @classmethod def get(cls, route_id): return cls.query.filter(Route.id == route_id).first() @classmethod def delete(cls, route_id): bus = Bus.query.filter(Bus.route_id == route_id).first() if bus: raise IntegrityError(u'数据完整性异常') del_obj = cls.get(route_id) db.session.delete(del_obj) db.session.commit()
class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(128), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, info={'validators': Email()}) password = db.Column(db.String(80), nullable=False) role = db.Column(db.SmallInteger, default=1) # 超级管理员:0, 管理员:1, 一般员工:2 status = db.Column(db.SmallInteger, default=1) # 0 不可以, 1 可用 phone = db.Column(db.String(128)) company_id = db.Column(db.Integer, db.ForeignKey('company.id')) company_name = db.Column(db.String(128)) token = db.Column(db.String(128)) # progress_code = db.Column(db.String(128)) # redis记录进度标识码 def __init__(self, username, password): self.username = username self.password = bcrypt.generate_password_hash(password) def __repr__(self): return '<User %r>' % self.email def hash_password(self, password): self.password = bcrypt.generate_password_hash(password) def verify_password(self, password): return bcrypt.check_password_hash(self.password, password) def generate_auth_token(self, expiration=24 * 60 * 60): s = Serializer(SECRET_KEY, expires_in=expiration) return s.dumps({'id': self.id}) @classmethod def get_current_user_progress_code(cls): if hasattr(g, 'user'): return g.user.progress_code return None @classmethod def set_current_user_progress_code(cls, progress_code): if hasattr(g, 'user'): current_user = User.get(g.user.id) current_user.progress_code = progress_code current_user.save() def save(self, role=1): self.role = role db.session.add(self) db.session.commit() @staticmethod def virify_auth_token(token): s = Serializer(SECRET_KEY) try: data = s.loads(token) except SignatureExpired: return None # valid token, but expired except BadSignature: return None # invalid token user = User.query.get(data['id']) return user @classmethod def get_user_list(self): return self.query.filter(and_(User.status == 1, User.role == 1)).all() @classmethod def get(cls, user_id): return cls.query.filter(User.id == user_id).first() @classmethod def get_user_by_name(cls, username): return cls.query.filter(User.username == username).first()
class Company(db.Model): __tablename__ = 'company' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128), unique=True) # one-to-many users = db.relationship('User', backref=db.backref('company'))