class User(BaseModel, UserMixin): __tablename__ = 'user' nickname = db.Column(db.String(255)) email = db.Column(db.String(255), unique=True) password = db.Column(db.String(255)) active = db.Column(db.Boolean()) confirmed_at = db.Column(db.DateTime()) roles = db.relationship('Role', secondary=roles_users, backref=db.backref('role', lazy='dynamic'))
class Driver(BaseModel): __tablename__ = 'driver' fullName = db.Column(db.String(255), nullable=False) licenseNum = db.Column(db.String(255), unique=True, nullable=False) type_id = db.Column(db.Integer, ForeignKey('vehicle_type.id'), nullable=False) type = relationship('VehicleType') entries = relationship('ScheduleEntry') def __repr__(self): return self.fullName
class Role(db.Model, RoleMixin): __tablename__ = 'role' # Our Role has three fields, ID, name and description id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(80), unique=True) description = db.Column(db.String(255)) # __str__ is required by Flask-Admin, so we can have human-readable values for the Role when editing a User. # If we were using Python 2.7, this would be __unicode__ instead. def __str__(self): return self.name # __hash__ is required to avoid the exception TypeError: unhashable type: 'Role' when saving a User def __hash__(self): return hash(self.name)
class VehicleType(BaseModel): __tablename__ = 'vehicle_type' name = db.Column(db.String(255), unique=True) vehicles = relationship('Vehicle') drivers = relationship('Driver') def __repr__(self): return self.name
class Park(BaseModel): __tablename__ = 'park' name = db.Column(db.String(255)) locationLong = db.Column(db.Float(precision=64), nullable=False) locationLat = db.Column(db.Float(precision=64), nullable=False) vehicles = relationship('Vehicle') def __repr__(self): return self.name
class Stop(BaseModel): __tablename__ = 'stop' name = db.Column(db.String(255), nullable=False) locationLong = db.Column(db.Float(precision=64), nullable=False) locationLat = db.Column(db.Float(precision=64), nullable=False) routes = relationship('Route', secondary=routes_stops, back_populates='stops') def __repr__(self): return self.name
class Vehicle(BaseModel): __tablename__ = 'vehicle' factoryNum = db.Column(db.String(255), unique=True) year = db.Column(db.Integer(), nullable=False) model = db.Column(db.String(255), nullable=False) manufacturedBy = db.Column(db.String(255), nullable=False) type_id = db.Column(db.Integer, ForeignKey('vehicle_type.id'), nullable=False) type = relationship('VehicleType') park_id = db.Column(db.Integer, ForeignKey('park.id'), nullable=False) park = relationship('Park') entries = relationship('ScheduleEntry') __table_args__ = (CheckConstraint(f'1900 < year < {datetime.now().year}', name='is_year_valid_check'), {}) def __repr__(self): return f'{self.model} номер {self.factoryNum} ({self.year} года)'
class Route(BaseModel): __tablename__ = 'route' name = db.Column(db.String(255)) distanceKm = db.Column(db.Integer) entries = relationship('ScheduleEntry') stops = relationship('Stop', secondary=routes_stops, back_populates='routes') __table_args__ = (CheckConstraint('distanceKm > 0', name='is_distance_positive_check'), {}) def __repr__(self): return self.name
class ScheduleEntry(BaseModel): __tablename__ = 'schedule_entry' name = db.Column(db.String(255)) time_start = db.Column(db.DateTime, nullable=False) time_end = db.Column(db.DateTime, nullable=False) driver_id = db.Column(db.Integer, ForeignKey('driver.id'), nullable=False) driver = relationship('Driver', back_populates='entries') route_id = db.Column(db.Integer, ForeignKey('route.id'), nullable=False) route = relationship('Route', back_populates='entries') vehicle_id = db.Column(db.Integer, ForeignKey('vehicle.id'), nullable=False) vehicle = relationship('Vehicle', back_populates='entries') def __repr__(self): return f'{self.name}({self.id})'
class User(db.Model, TimestampMixin): query_class = UserQuery id = db.Column(db.Integer, primary_key=True) todos = db.relationship('Todo', backref='user', lazy=True) email = db.Column(db.String(255), unique=True, nullable=False) password_hash = db.Column(db.String(100)) first_name = db.Column(db.Unicode(100)) last_name = db.Column(db.Unicode(100)) auth_sub = db.Column(db.String(100)) notifications_enabled = db.Column(db.Boolean()) notifications_radius_meters = db.Column(db.Float()) phone = db.Column(db.String(100)) email = db.Column(db.String(100)) avatar = db.Column(db.String(100)) expires_at = db.Column(db.DateTime()) # Store last location of User. This properties is just used for postgis POC location = db.Column( fields.PointColumn(geometry_type='POINT', management=True, use_st_prefix=False)) # Distance from user current location to a coordinate distance = query_expression() lat = query_expression() lng = query_expression() @property def password(self): raise AttributeError('password is a write only field') @password.setter def password(self, password): self.password_hash = bcrypt \ .generate_password_hash(password) \ .decode('utf-8') def check_password(self, password): return bcrypt.check_password_hash(self.password_hash, password) @classmethod def create_new_user(cls, data): user = User.query.get_by_email(email=data['email']) if not user: new_user = User(email=data['email'], password=data['password'], first_name=data['first_name'], last_name=data['last_name']) db.session.add(new_user) db.session.commit() return new_user def update_location(self, lat, lng, **kwargs): point = 'POINT(%f %f)' % (lng, lat) wkb_element = WKTElement(point, srid=4326) self.location = wkb_element db.session.add(self) db.session.commit() return self