2018-08-08 19:25:53 +00:00
|
|
|
import pathlib
|
2018-09-11 19:50:40 +00:00
|
|
|
from typing import Callable, Iterable, Tuple
|
2018-08-08 19:25:53 +00:00
|
|
|
|
2018-09-07 10:38:02 +00:00
|
|
|
from teal.resource import Converters, Resource
|
|
|
|
|
2018-08-08 19:25:53 +00:00
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.resources.lot import schemas
|
2018-09-11 19:50:40 +00:00
|
|
|
from ereuse_devicehub.resources.lot.views import LotBaseChildrenView, LotChildrenView, \
|
|
|
|
LotDeviceView, LotView
|
2018-08-08 19:25:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LotDef(Resource):
|
|
|
|
SCHEMA = schemas.Lot
|
|
|
|
VIEW = LotView
|
|
|
|
AUTH = True
|
|
|
|
ID_CONVERTER = Converters.uuid
|
|
|
|
|
2018-09-29 13:55:29 +00:00
|
|
|
def __init__(self, app, import_name=__name__.split('.')[0], static_folder=None, static_url_path=None,
|
2018-09-11 19:50:40 +00:00
|
|
|
template_folder=None, url_prefix=None, subdomain=None, url_defaults=None,
|
|
|
|
root_path=None, cli_commands: Iterable[Tuple[Callable, str or None]] = tuple()):
|
|
|
|
super().__init__(app, import_name, static_folder, static_url_path, template_folder,
|
|
|
|
url_prefix, subdomain, url_defaults, root_path, cli_commands)
|
2018-09-20 16:25:47 +00:00
|
|
|
lot_children = LotChildrenView.as_view('lot-children', definition=self, auth=app.auth)
|
|
|
|
if self.AUTH:
|
|
|
|
lot_children = app.auth.requires_auth(lot_children)
|
2018-09-11 19:50:40 +00:00
|
|
|
self.add_url_rule('/<{}:{}>/children'.format(self.ID_CONVERTER.value, self.ID_NAME),
|
2018-09-20 16:25:47 +00:00
|
|
|
view_func=lot_children,
|
2018-09-11 19:50:40 +00:00
|
|
|
methods={'POST', 'DELETE'})
|
2018-09-20 16:25:47 +00:00
|
|
|
lot_device = LotDeviceView.as_view('lot-device', definition=self, auth=app.auth)
|
|
|
|
if self.AUTH:
|
|
|
|
lot_device = app.auth.requires_auth(lot_device)
|
2018-09-11 19:50:40 +00:00
|
|
|
self.add_url_rule('/<{}:{}>/devices'.format(self.ID_CONVERTER.value, self.ID_NAME),
|
2018-09-20 16:25:47 +00:00
|
|
|
view_func=lot_device,
|
2018-09-11 19:50:40 +00:00
|
|
|
methods={'POST', 'DELETE'})
|
|
|
|
|
2018-08-08 19:25:53 +00:00
|
|
|
def init_db(self, db: 'db.SQLAlchemy'):
|
|
|
|
# Create functions
|
|
|
|
with pathlib.Path(__file__).parent.joinpath('dag.sql').open() as f:
|
|
|
|
sql = f.read()
|
|
|
|
db.session.execute(sql)
|