rollerverbrauch/app/rollerverbrauch/entities.py

71 lines
2.2 KiB
Python
Raw Normal View History

2016-04-23 23:08:39 +02:00
from rollerverbrauch import db
from flask.ext.security import UserMixin, RoleMixin
roles_users = db.Table('roles_users',
2016-04-23 23:24:25 +02:00
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
class Role(db.Model, RoleMixin):
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):
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())
2016-04-23 23:24:25 +02:00
vehicles = db.relationship(
'Vehicle'
)
2016-04-23 23:08:39 +02:00
roles = db.relationship(
'Role',
secondary=roles_users,
backref=db.backref('users', lazy='dynamic')
)
def __repr__(self):
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):
id = db.Column(db.Integer, primary_key=True)
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'))
name = db.Column(db.String(255))
2016-04-23 23:24:25 +02:00
pitstops = db.relationship(
'Pitstop'
)
__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):
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):
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.Date)
odometer = db.Column(db.Integer)
2016-04-23 23:24:25 +02:00
litres = db.Column(db.Numeric(5, 2))
costs = db.Column(db.Numeric(5, 2), default=0)
2016-04-23 23:24:25 +02:00
vehicle_id = db.Column(db.Integer, db.ForeignKey('vehicle.id'))
2016-04-23 23:08:39 +02:00
def __init__(self, odometer, litres, date, costs):
2016-04-23 23:08:39 +02:00
self.odometer = odometer
self.litres = litres
self.date = date
self.costs = costs
2016-04-23 23:08:39 +02:00
def __repr__(self):
return '<Pitstop odometer="%r" litres="%r" date="%r" vehicle_id="%r">' % (self.odometer, self.litres, self.date, self.vehicle_id)