2016-07-03 19:29:30 +02:00
|
|
|
from app import db
|
|
|
|
from flask_security import UserMixin, RoleMixin
|
2016-04-23 23:08:39 +02:00
|
|
|
|
2021-06-17 18:28:19 +02:00
|
|
|
roles_users = db.Table(
|
|
|
|
"roles_users",
|
|
|
|
db.Column("user_id", db.Integer(), db.ForeignKey("user.id")),
|
|
|
|
db.Column("role_id", db.Integer(), db.ForeignKey("role.id")),
|
|
|
|
)
|
2016-04-23 23:08:39 +02:00
|
|
|
|
2021-06-17 18:28:19 +02:00
|
|
|
vehicles_consumables = db.Table(
|
|
|
|
"vehicles_consumables",
|
|
|
|
db.Column("vehicle_id", db.Integer(), db.ForeignKey("vehicle.id")),
|
|
|
|
db.Column("consumable_id", db.Integer(), db.ForeignKey("consumable.id")),
|
|
|
|
)
|
2016-06-26 08:19:28 +02:00
|
|
|
|
2016-04-23 23:08:39 +02:00
|
|
|
|
2021-06-17 18:28:19 +02:00
|
|
|
users_fillingstations = db.Table(
|
|
|
|
"users_fillingstations",
|
|
|
|
db.Column("user_id", db.Integer(), db.ForeignKey("user.id")),
|
|
|
|
db.Column(
|
|
|
|
"fillingstation_id", db.Integer(), db.ForeignKey("filling_station.int_id")
|
|
|
|
),
|
|
|
|
)
|
2017-11-10 10:38:15 +01:00
|
|
|
|
|
|
|
|
2016-04-23 23:08:39 +02:00
|
|
|
class Role(db.Model, RoleMixin):
|
2016-07-04 20:19:59 +02:00
|
|
|
"""
|
|
|
|
Entity to handle different roles for users: Typically user and admin exist
|
|
|
|
"""
|
2021-06-17 18:28:19 +02:00
|
|
|
|
2016-04-23 23:08:39 +02:00
|
|
|
id = db.Column(db.Integer(), primary_key=True)
|
|
|
|
name = db.Column(db.String(80), unique=True)
|
|
|
|
description = db.Column(db.String(255))
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.name)
|
|
|
|
|
|
|
|
|
|
|
|
class User(db.Model, UserMixin):
|
2016-07-04 20:19:59 +02:00
|
|
|
"""
|
|
|
|
Entity to represent a user including login data and links to roles and vehicles.
|
|
|
|
"""
|
2021-06-17 18:28:19 +02:00
|
|
|
|
2016-04-23 23:08:39 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
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())
|
2017-11-24 09:08:04 +01:00
|
|
|
home_lat = db.Column(db.Numeric(8, 5), default=0)
|
|
|
|
home_long = db.Column(db.Numeric(8, 5), default=0)
|
2017-11-07 22:33:14 +01:00
|
|
|
home_zoom = db.Column(db.Integer(), default=0)
|
2016-07-04 20:19:59 +02:00
|
|
|
|
2021-06-17 18:28:19 +02:00
|
|
|
vehicles = db.relationship("Vehicle")
|
2016-04-23 23:08:39 +02:00
|
|
|
roles = db.relationship(
|
2021-06-17 18:28:19 +02:00
|
|
|
"Role", secondary=roles_users, backref=db.backref("users", lazy="dynamic")
|
2016-04-23 23:08:39 +02:00
|
|
|
)
|
2017-11-10 10:38:15 +01:00
|
|
|
favourite_filling_stations = db.relationship(
|
2021-06-17 18:28:19 +02:00
|
|
|
"FillingStation", secondary=users_fillingstations
|
2017-11-10 10:38:15 +01:00
|
|
|
)
|
2016-04-23 23:08:39 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
2016-04-28 06:32:51 +02:00
|
|
|
return '<User id="%r" email="%r" ' % (self.id, self.email)
|
2016-04-23 23:08:39 +02:00
|
|
|
|
|
|
|
|
2016-04-23 23:24:25 +02:00
|
|
|
class Vehicle(db.Model):
|
2016-07-04 20:19:59 +02:00
|
|
|
"""
|
|
|
|
Entity to represent a vehicle.
|
|
|
|
Attributes:
|
|
|
|
* name of the vehilce
|
|
|
|
* the id of the owner
|
|
|
|
* list of pitstops
|
|
|
|
* list of possible consumables
|
|
|
|
"""
|
2021-06-17 18:28:19 +02:00
|
|
|
|
2016-04-23 23:24:25 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2021-06-17 18:28:19 +02:00
|
|
|
owner_id = db.Column(db.Integer, db.ForeignKey("user.id"))
|
2016-05-16 18:46:24 +02:00
|
|
|
name = db.Column(db.String(255))
|
2021-06-17 18:28:19 +02:00
|
|
|
pitstops = db.relationship("Pitstop", order_by="asc(Pitstop.odometer)")
|
|
|
|
services = db.relationship("Service", order_by="asc(Service.odometer)")
|
|
|
|
regulars = db.relationship("RegularCost")
|
|
|
|
consumables = db.relationship("Consumable", secondary=vehicles_consumables)
|
2016-07-04 20:19:59 +02:00
|
|
|
# allow vehicle names to be duplicated between different owners but must still be uniq for each owner
|
2021-06-17 18:28:19 +02:00
|
|
|
__table_args__ = (db.UniqueConstraint("owner_id", "name", name="_owner_name_uniq"),)
|
2016-04-23 23:24:25 +02:00
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __repr__(self):
|
2021-06-17 18:28:19 +02:00
|
|
|
return '<Vehicle id="%r" owner_id="%r" name="%r" />' % (
|
|
|
|
self.id,
|
|
|
|
self.owner_id,
|
|
|
|
self.name,
|
|
|
|
)
|
2016-04-23 23:24:25 +02:00
|
|
|
|
|
|
|
|
2016-04-23 23:08:39 +02:00
|
|
|
class Pitstop(db.Model):
|
2016-07-04 20:19:59 +02:00
|
|
|
"""
|
|
|
|
Entity to represent a pitstop for a single consumable.
|
|
|
|
Attributes:
|
|
|
|
* the date of the pitstop
|
|
|
|
* the odometer of the pitstop
|
|
|
|
* the id of the fuelled consumable
|
|
|
|
* amount of consumable used
|
|
|
|
* the costs of the consumable
|
|
|
|
* the id of the vehicle that was refuelled
|
|
|
|
"""
|
2021-06-17 18:28:19 +02:00
|
|
|
|
2016-04-23 23:08:39 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
date = db.Column(db.Date)
|
|
|
|
odometer = db.Column(db.Integer)
|
2021-06-17 18:28:19 +02:00
|
|
|
consumable_id = db.Column(db.Integer, db.ForeignKey("consumable.id"))
|
2016-07-04 20:19:59 +02:00
|
|
|
amount = db.Column(db.Numeric(5, 2))
|
2016-06-21 21:06:41 +02:00
|
|
|
costs = db.Column(db.Numeric(5, 2), default=0)
|
2021-06-17 18:28:19 +02:00
|
|
|
vehicle_id = db.Column(db.Integer, db.ForeignKey("vehicle.id"))
|
2016-07-04 20:19:59 +02:00
|
|
|
# short cut to access the fuelled consumable of the pitstop
|
2021-06-17 18:28:19 +02:00
|
|
|
consumable = db.relationship("Consumable")
|
|
|
|
# this uniqueness constraint makes sure that for each consumable and each
|
|
|
|
# vehicle only one pitstop exists at the same odometer
|
|
|
|
__table_args__ = (
|
|
|
|
db.UniqueConstraint(
|
|
|
|
"odometer",
|
|
|
|
"consumable_id",
|
|
|
|
"vehicle_id",
|
|
|
|
name="_odometer_consumable_vehicle_uniq",
|
|
|
|
),
|
|
|
|
)
|
2016-07-04 20:19:59 +02:00
|
|
|
|
|
|
|
def __init__(self, odometer, amount, date, costs, consumable_id):
|
2016-04-23 23:08:39 +02:00
|
|
|
self.odometer = odometer
|
2016-07-04 20:19:59 +02:00
|
|
|
self.amount = amount
|
2016-04-23 23:08:39 +02:00
|
|
|
self.date = date
|
2016-06-21 21:06:41 +02:00
|
|
|
self.costs = costs
|
2016-06-27 21:35:00 +02:00
|
|
|
self.consumable_id = consumable_id
|
2016-04-23 23:08:39 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
2021-06-17 18:28:19 +02:00
|
|
|
return '<Pitstop odometer="%r" amount="%r" date="%r" vehicle_id="%r" consumable_id="%r">' % (
|
|
|
|
self.odometer,
|
|
|
|
self.amount,
|
|
|
|
self.date,
|
|
|
|
self.vehicle_id,
|
|
|
|
self.consumable_id,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class RegularCost(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
vehicle_id = db.Column(db.Integer, db.ForeignKey("vehicle.id"))
|
|
|
|
description = db.Column(db.String(4096), unique=True)
|
|
|
|
costs = db.Column(db.Numeric(10, 2), default=0)
|
|
|
|
days = db.Column(db.String(1024))
|
|
|
|
start_at = db.Column(db.Date)
|
|
|
|
ends_at = db.Column(db.Date)
|
|
|
|
|
|
|
|
def __init__(self, vehicle_id, description, costs, days, start_at, ends_at):
|
|
|
|
self.vehicle_id = vehicle_id
|
|
|
|
self.description = description
|
|
|
|
self.costs = costs
|
|
|
|
self.days = days
|
|
|
|
self.start_at = start_at
|
|
|
|
self.ends_at = ends_at
|
2016-06-26 08:19:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
class Consumable(db.Model):
|
2016-07-04 20:19:59 +02:00
|
|
|
"""
|
|
|
|
Entity to represent a material that be consumed by a vehilce.
|
|
|
|
Attributes:
|
|
|
|
* name (must be globally unique)
|
|
|
|
* unit
|
|
|
|
"""
|
2021-06-17 18:28:19 +02:00
|
|
|
|
2016-06-26 08:19:28 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2016-06-27 19:48:04 +02:00
|
|
|
name = db.Column(db.String(255), unique=True)
|
2017-11-12 10:17:36 +01:00
|
|
|
ext_id = db.Column(db.String(255))
|
2016-06-26 08:19:28 +02:00
|
|
|
unit = db.Column(db.String(255))
|
2016-07-04 20:19:59 +02:00
|
|
|
|
2021-06-17 18:28:19 +02:00
|
|
|
vehicles = db.relationship("Vehicle", secondary=vehicles_consumables)
|
2016-06-26 08:19:28 +02:00
|
|
|
|
2017-11-12 10:17:36 +01:00
|
|
|
def __init__(self, name, ext_id, unit):
|
2016-06-26 08:19:28 +02:00
|
|
|
self.name = name
|
2017-11-12 10:17:36 +01:00
|
|
|
self.ext_id = ext_id
|
2016-06-26 08:19:28 +02:00
|
|
|
self.unit = unit
|
2016-04-23 23:08:39 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
2016-06-26 08:19:28 +02:00
|
|
|
return '<Consumable name="%s" unit="%s" />' % (self.name, self.unit)
|
2016-11-01 11:17:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Service(db.Model):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
date = db.Column(db.Date)
|
|
|
|
odometer = db.Column(db.Integer)
|
2021-06-17 18:28:19 +02:00
|
|
|
vehicle_id = db.Column(db.Integer, db.ForeignKey("vehicle.id"))
|
2016-11-01 11:17:54 +01:00
|
|
|
costs = db.Column(db.Numeric(10, 2), default=0)
|
|
|
|
description = db.Column(db.String(4096))
|
|
|
|
|
|
|
|
def __init__(self, date, odometer, vehicle_id, costs, description):
|
|
|
|
self.description = description
|
|
|
|
self.costs = costs
|
|
|
|
self.date = date
|
|
|
|
self.odometer = odometer
|
|
|
|
self.vehicle_id = vehicle_id
|
|
|
|
|
|
|
|
def __repr__(self):
|
2021-06-17 18:28:19 +02:00
|
|
|
return (
|
|
|
|
'<Service odometer="%r" date="%r" vehicle_id="%r" costs="%r" description="%r">'
|
|
|
|
% (self.odometer, self.date, self.vehicle_id, self.costs, self.description)
|
|
|
|
)
|
2017-11-10 10:38:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class FillingStation(db.Model):
|
|
|
|
int_id = db.Column(db.Integer, primary_key=True)
|
|
|
|
id = db.Column(db.String(40), unique=True, nullable=False)
|
|
|
|
name = db.Column(db.Text(), nullable=False)
|
|
|
|
street = db.Column(db.Text(), nullable=False)
|
|
|
|
place = db.Column(db.Text(), nullable=False)
|
|
|
|
houseNumber = db.Column(db.Text())
|
|
|
|
postCode = db.Column(db.Integer(), nullable=False)
|
|
|
|
brand = db.Column(db.Text(), nullable=False)
|
2017-11-24 09:08:04 +01:00
|
|
|
lat = db.Column(db.Numeric(8, 5), nullable=False)
|
|
|
|
lng = db.Column(db.Numeric(8, 5), nullable=False)
|
2017-11-12 15:38:32 +01:00
|
|
|
last_update = db.Column(db.DateTime)
|
|
|
|
diesel = db.Column(db.Numeric(10, 3), default=0)
|
|
|
|
e5 = db.Column(db.Numeric(10, 3), default=0)
|
|
|
|
e10 = db.Column(db.Numeric(10, 3), default=0)
|
|
|
|
open = db.Column(db.Boolean())
|
2017-11-10 10:38:15 +01:00
|
|
|
|
|
|
|
def as_dict(self):
|
|
|
|
res = {}
|
|
|
|
for c in self.__table__.columns:
|
|
|
|
val = getattr(self, c.name)
|
|
|
|
import decimal
|
2021-06-17 18:28:19 +02:00
|
|
|
|
2017-11-10 10:38:15 +01:00
|
|
|
if isinstance(val, decimal.Decimal):
|
|
|
|
val = float(val)
|
|
|
|
val = str(val)
|
|
|
|
res[c.name] = val
|
|
|
|
return res
|