class Vehicle(db.Model): __tablename__ = 'vehicle' id = db.Column(db.Integer, primary_key = True) plate_number = db.Column(db.String(10), unique = True, nullable = False) accelerometer = db.Column(db.String(10), nullable = False , default = '0') accidents = db.relationship('Accident', backref = 'vehicles', lazy = 'dynamic') def __repr__(self): return f"Vehicle('{self.id}','{self.plate_number}', '{self.accelerometer}')"
class Address(db.Model): __tablename__ = "address" id = db.Column(db.Integer, primary_key = True) postal_code = db.Column(db.String(10), nullable = False) country = db.Column(db.String(20), nullable = False) state = db.Column(db.String(20), nullable = False) city = db.Column(db.String(20), nullable = False) street = db.Column(db.String(20), nullable = False) def __repr__(self): return f"Address('{self.id}', '{self.postal_code}', '{self.country}', '{self.state}', '{self.city}', '{self.street}')"
class Vehicle(db.Model): __tablename__ = 'vehicle' id = db.Column(db.Integer, primary_key=True) plate_number = db.Column(db.String(10), unique=True, nullable=False) status = db.Column(db.String, nullable=False, default="Parked") accelerometer = db.Column(db.String(10), nullable=False, default='0') vehicle_model = db.Column(db.String, nullable=False) location = db.Column(db.String) latitude = db.Column(db.String) longitude = db.Column(db.String) update_date = db.Column(db.Date, nullable=False, default=datetime.today()) accidents = db.relationship('Accident', backref='vehicles', lazy='dynamic') def __repr__(self): return f"Vehicle('{self.id}','{self.plate_number}', '{self.accelerometer}')"
class Contact(db.Model): __tablename__ = "contact" id = db.Column(db.Integer, primary_key = True) cellphone = db.Column(db.String(20), unique = True, nullable = False) def __repr__(self): return f"Contact('{self.id}', '{self.cellphone}')"
class User(db.Model, UserMixin): __tablename__ = "user" id = db.Column(db.Integer, primary_key = True) username = db.Column(db.String(20), unique = True, nullable = False) email = db.Column(db.String(120), unique = True, nullable = False) first_name = db.Column(db.String(20), nullable = False) last_name = db.Column(db.String(20), nullable = False) image_file = db.Column(db.String(20), nullable = False, default = 'default.jpg') password = db.Column(db.String(60), nullable = False) phone_number = db.Column(db.String(20), unique = True ,nullable = False) admin = db.Column(db.Boolean, nullable = False, default = False) user_address = db.relationship('Address', secondary = User_Address, backref = 'users' ,lazy = 'dynamic') user_contact = db.relationship('Contact', secondary = User_Contact, backref = 'users',lazy = 'dynamic') user_vehicle = db.relationship('Vehicle', secondary = User_Vehicle, backref = 'users', lazy = 'dynamic') def get_reset_token(self, expires_sec=1800): s = Serializer(app.config['SECRET_KEY'], expires_sec) return s.dumps({'user_id': self.id}).decode('utf-8') @staticmethod def verify_reset_token(token): s = Serializer(app.config['SECRET_KEY']) print(s) try: user_id = s.loads(token)['user_id'] print(user_id) except: return None return User.query.get(user_id) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class Accident(db.Model): __tablename__ = 'accident' id = db.Column(db.Integer, primary_key=True) time_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) location = db.Column(db.String, nullable=False) accelerometer = db.Column(db.String(10), nullable=False, default='0') vehicle_id = db.Column(db.Integer, db.ForeignKey('vehicle.id'), nullable=False) db.UniqueConstraint(vehicle_id, location, time_date) def __repr__(self): return f"Accident('{self.id}', '{self.time_date}','{self.vehicle_id}')"
class Address(db.Model): __tablename__ = "address" id = db.Column(db.Integer, primary_key=True) country = db.Column(db.String(20), nullable=False) state = db.Column(db.String(20), nullable=False) city = db.Column(db.String(20), nullable=False) street = db.Column(db.String(20), nullable=False) postal_code = db.Column(db.String(10), nullable=False) users = db.relationship("User", backref='address', lazy=True) db.UniqueConstraint(country, state, city, street, postal_code) def __repr__(self): return f"Address('{self.id}', '{self.postal_code}', '{self.country}', '{self.state}', '{self.city}', '{self.street}')" def equals(self, address): if self.country == address.country \ and self.state == address.state \ and self.city == address.city \ and self.street == address.street \ and self.postal_code == address.postal_code: return True
class User(db.Model, UserMixin): __tablename__ = "user" id = db.Column(db.Integer, primary_key = True) username = db.Column(db.String(20), unique = True, nullable = False) email = db.Column(db.String(120), unique = True, nullable = False) first_name = db.Column(db.String(20), nullable = False) last_name = db.Column(db.String(20), nullable = False) image_file = db.Column(db.String(20), nullable = False, default = 'default.jpg') password = db.Column(db.String(60), nullable = False) phone_number = db.Column(db.String(20), unique = True ,nullable = False) admin = db.Column(db.Boolean, nullable = False, default = False) user_address = db.relationship('Address', secondary = User_Address, backref = 'users' ,lazy = 'dynamic') user_contact = db.relationship('Contact', secondary = User_Contact, backref = 'users',lazy = 'dynamic') user_vehicle = db.relationship('Vehicle', secondary = User_Vehicle, backref = 'users', lazy = 'dynamic') def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')"