2020-12-29 14:37:37 +01:00
|
|
|
"""Provide the ORM's `Restaurant` model."""
|
2020-08-09 03:45:19 +02:00
|
|
|
|
|
|
|
|
import sqlalchemy as sa
|
|
|
|
|
from sqlalchemy import orm
|
|
|
|
|
|
|
|
|
|
from urban_meal_delivery.db import meta
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Restaurant(meta.Base):
|
2020-12-29 14:37:37 +01:00
|
|
|
"""A restaurant selling meals on the UDP.
|
|
|
|
|
|
|
|
|
|
In the historic dataset, a `Restaurant` may have changed its `Address`
|
|
|
|
|
throughout its life time. The ORM model only stores the current one,
|
|
|
|
|
which in most cases is also the only one.
|
|
|
|
|
"""
|
2020-08-09 03:45:19 +02:00
|
|
|
|
|
|
|
|
__tablename__ = 'restaurants'
|
|
|
|
|
|
|
|
|
|
# Columns
|
|
|
|
|
id = sa.Column( # noqa:WPS125
|
|
|
|
|
sa.SmallInteger, primary_key=True, autoincrement=False,
|
|
|
|
|
)
|
|
|
|
|
created_at = sa.Column(sa.DateTime, nullable=False)
|
2021-01-24 18:31:02 +01:00
|
|
|
name = sa.Column(sa.Unicode(length=45), nullable=False)
|
2021-01-05 22:37:12 +01:00
|
|
|
address_id = sa.Column(sa.Integer, nullable=False, index=True)
|
2020-08-09 03:45:19 +02:00
|
|
|
estimated_prep_duration = sa.Column(sa.SmallInteger, nullable=False)
|
|
|
|
|
|
|
|
|
|
# Constraints
|
|
|
|
|
__table_args__ = (
|
|
|
|
|
sa.ForeignKeyConstraint(
|
|
|
|
|
['address_id'], ['addresses.id'], onupdate='RESTRICT', ondelete='RESTRICT',
|
|
|
|
|
),
|
|
|
|
|
sa.CheckConstraint(
|
|
|
|
|
'0 <= estimated_prep_duration AND estimated_prep_duration <= 2400',
|
|
|
|
|
name='realistic_estimated_prep_duration',
|
|
|
|
|
),
|
2021-01-24 18:57:44 +01:00
|
|
|
# Needed by a `ForeignKeyConstraint` in `Order`.
|
|
|
|
|
sa.UniqueConstraint('id', 'address_id'),
|
2020-08-09 03:45:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Relationships
|
2021-01-24 18:57:44 +01:00
|
|
|
address = orm.relationship('Address', back_populates='restaurants')
|
2020-08-09 03:45:19 +02:00
|
|
|
orders = orm.relationship('Order', back_populates='restaurant')
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
"""Non-literal text representation."""
|
|
|
|
|
return '<{cls}({name})>'.format(cls=self.__class__.__name__, name=self.name)
|