2020-12-29 14:37:37 +01:00
|
|
|
"""Provide the ORM's `City` model."""
|
2020-08-09 03:45:19 +02:00
|
|
|
|
2021-01-04 20:33:10 +01:00
|
|
|
from typing import Dict
|
2020-08-09 03:45:19 +02:00
|
|
|
|
|
|
|
|
import sqlalchemy as sa
|
|
|
|
|
from sqlalchemy import orm
|
|
|
|
|
from sqlalchemy.dialects import postgresql
|
|
|
|
|
|
|
|
|
|
from urban_meal_delivery.db import meta
|
2021-01-02 16:29:50 +01:00
|
|
|
from urban_meal_delivery.db import utils
|
2020-08-09 03:45:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class City(meta.Base):
|
2020-12-29 14:37:37 +01:00
|
|
|
"""A city where the UDP operates in."""
|
2020-08-09 03:45:19 +02:00
|
|
|
|
|
|
|
|
__tablename__ = 'cities'
|
|
|
|
|
|
|
|
|
|
# Generic columns
|
|
|
|
|
id = sa.Column( # noqa:WPS125
|
|
|
|
|
sa.SmallInteger, primary_key=True, autoincrement=False,
|
|
|
|
|
)
|
|
|
|
|
name = sa.Column(sa.Unicode(length=10), nullable=False)
|
|
|
|
|
kml = sa.Column(sa.UnicodeText, nullable=False)
|
|
|
|
|
|
|
|
|
|
# Google Maps related columns
|
|
|
|
|
_center_latitude = sa.Column(
|
|
|
|
|
'center_latitude', postgresql.DOUBLE_PRECISION, nullable=False,
|
|
|
|
|
)
|
|
|
|
|
_center_longitude = sa.Column(
|
|
|
|
|
'center_longitude', postgresql.DOUBLE_PRECISION, nullable=False,
|
|
|
|
|
)
|
|
|
|
|
_northeast_latitude = sa.Column(
|
|
|
|
|
'northeast_latitude', postgresql.DOUBLE_PRECISION, nullable=False,
|
|
|
|
|
)
|
|
|
|
|
_northeast_longitude = sa.Column(
|
|
|
|
|
'northeast_longitude', postgresql.DOUBLE_PRECISION, nullable=False,
|
|
|
|
|
)
|
|
|
|
|
_southwest_latitude = sa.Column(
|
|
|
|
|
'southwest_latitude', postgresql.DOUBLE_PRECISION, nullable=False,
|
|
|
|
|
)
|
|
|
|
|
_southwest_longitude = sa.Column(
|
|
|
|
|
'southwest_longitude', postgresql.DOUBLE_PRECISION, nullable=False,
|
|
|
|
|
)
|
|
|
|
|
initial_zoom = sa.Column(sa.SmallInteger, nullable=False)
|
|
|
|
|
|
|
|
|
|
# Relationships
|
|
|
|
|
addresses = orm.relationship('Address', back_populates='city')
|
2021-01-03 19:33:36 +01:00
|
|
|
grids = orm.relationship('Grid', back_populates='city')
|
2020-08-09 03:45:19 +02:00
|
|
|
|
2021-01-04 20:33:10 +01:00
|
|
|
# We do not implement a `.__init__()` method and leave that to SQLAlchemy.
|
|
|
|
|
# Instead, we use `hasattr()` to check for uninitialized attributes.
|
|
|
|
|
# grep:d334120e pylint:disable=attribute-defined-outside-init
|
2021-01-02 16:29:50 +01:00
|
|
|
|
2020-08-09 03:45:19 +02:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
|
"""Non-literal text representation."""
|
|
|
|
|
return '<{cls}({name})>'.format(cls=self.__class__.__name__, name=self.name)
|
|
|
|
|
|
|
|
|
|
@property
|
2021-01-04 20:33:10 +01:00
|
|
|
def center(self) -> utils.Location:
|
|
|
|
|
"""Location of the city's center.
|
2020-08-09 03:45:19 +02:00
|
|
|
|
2021-01-04 20:33:10 +01:00
|
|
|
Implementation detail: This property is cached as none of the
|
|
|
|
|
underlying attributes to calculate the value are to be changed.
|
2020-08-09 03:45:19 +02:00
|
|
|
"""
|
2021-01-04 20:33:10 +01:00
|
|
|
if not hasattr(self, '_center'): # noqa:WPS421 note:d334120e
|
|
|
|
|
self._center = utils.Location(
|
|
|
|
|
self._center_latitude, self._center_longitude,
|
|
|
|
|
)
|
|
|
|
|
return self._center
|
2020-08-09 03:45:19 +02:00
|
|
|
|
|
|
|
|
@property
|
2021-01-04 20:33:10 +01:00
|
|
|
def viewport(self) -> Dict[str, utils.Location]:
|
2020-08-09 03:45:19 +02:00
|
|
|
"""Google Maps viewport of the city.
|
|
|
|
|
|
2021-01-04 20:33:10 +01:00
|
|
|
Implementation detail: This property is cached as none of the
|
|
|
|
|
underlying attributes to calculate the value are to be changed.
|
|
|
|
|
"""
|
|
|
|
|
if not hasattr(self, '_viewport'): # noqa:WPS421 note:d334120e
|
|
|
|
|
self._viewport = {
|
|
|
|
|
'northeast': utils.Location(
|
|
|
|
|
self._northeast_latitude, self._northeast_longitude,
|
|
|
|
|
),
|
|
|
|
|
'southwest': utils.Location(
|
|
|
|
|
self._southwest_latitude, self._southwest_longitude,
|
|
|
|
|
),
|
2020-08-09 03:45:19 +02:00
|
|
|
}
|
2021-01-04 20:33:10 +01:00
|
|
|
|
|
|
|
|
return self._viewport
|
2021-01-02 16:29:50 +01:00
|
|
|
|
|
|
|
|
@property
|
2021-01-04 20:33:10 +01:00
|
|
|
def as_xy_origin(self) -> utils.Location:
|
|
|
|
|
"""The southwest corner of the `.viewport`.
|
2021-01-02 16:29:50 +01:00
|
|
|
|
2021-01-04 20:33:10 +01:00
|
|
|
This property serves, for example, as the `other` argument to the
|
|
|
|
|
`Location.relate_to()` method when representing an `Address`
|
|
|
|
|
in the x-y plane.
|
2021-01-02 16:29:50 +01:00
|
|
|
"""
|
2021-01-04 20:33:10 +01:00
|
|
|
return self.viewport['southwest']
|