2018-08-08 19:25:53 +00:00
|
|
|
import uuid
|
2018-09-11 19:50:40 +00:00
|
|
|
from typing import Set
|
2018-08-08 19:25:53 +00:00
|
|
|
|
2018-09-11 19:50:40 +00:00
|
|
|
import marshmallow as ma
|
|
|
|
from flask import request
|
2018-09-07 10:38:02 +00:00
|
|
|
from teal.resource import View
|
2018-08-08 19:25:53 +00:00
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
|
|
|
from ereuse_devicehub.resources.lot.models import Lot
|
|
|
|
|
|
|
|
|
|
|
|
class LotView(View):
|
|
|
|
def post(self):
|
2018-09-11 19:50:40 +00:00
|
|
|
l = request.get_json()
|
|
|
|
lot = Lot(**l)
|
2018-08-08 19:25:53 +00:00
|
|
|
db.session.add(lot)
|
|
|
|
db.session.commit()
|
|
|
|
ret = self.schema.jsonify(lot)
|
|
|
|
ret.status_code = 201
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def one(self, id: uuid.UUID):
|
|
|
|
"""Gets one event."""
|
2018-09-11 19:50:40 +00:00
|
|
|
lot = Lot.query.filter_by(id=id).one() # type: Lot
|
|
|
|
return self.schema.jsonify(lot)
|
|
|
|
|
|
|
|
|
|
|
|
class LotBaseChildrenView(View):
|
|
|
|
"""Base class for adding / removing children devices and
|
|
|
|
lots from a lot.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class ListArgs(ma.Schema):
|
|
|
|
id = ma.fields.List(ma.fields.UUID())
|
|
|
|
|
|
|
|
def __init__(self, definition: 'Resource', **kw) -> None:
|
|
|
|
super().__init__(definition, **kw)
|
|
|
|
self.list_args = self.ListArgs()
|
|
|
|
|
|
|
|
def get_ids(self) -> Set[uuid.UUID]:
|
|
|
|
args = self.QUERY_PARSER.parse(self.list_args, request, locations=('querystring',))
|
|
|
|
return set(args['id'])
|
|
|
|
|
|
|
|
def get_lot(self, id: uuid.UUID) -> Lot:
|
|
|
|
return Lot.query.filter_by(id=id).one()
|
|
|
|
|
|
|
|
# noinspection PyMethodOverriding
|
|
|
|
def post(self, id: uuid.UUID):
|
|
|
|
lot = self.get_lot(id)
|
|
|
|
self._post(lot, self.get_ids())
|
|
|
|
db.session.commit()
|
|
|
|
ret = self.schema.jsonify(lot)
|
|
|
|
ret.status_code = 201
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def delete(self, id: uuid.UUID):
|
|
|
|
lot = self.get_lot(id)
|
|
|
|
self._delete(lot, self.get_ids())
|
|
|
|
db.session.commit()
|
|
|
|
return self.schema.jsonify(lot)
|
|
|
|
|
|
|
|
def _post(self, lot: Lot, ids: Set[uuid.UUID]):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def _delete(self, lot: Lot, ids: Set[uuid.UUID]):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class LotChildrenView(LotBaseChildrenView):
|
|
|
|
"""View for adding and removing child lots from a lot.
|
|
|
|
|
|
|
|
Ex. ``lot/<id>/children/id=X&id=Y``.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _post(self, lot: Lot, ids: Set[uuid.UUID]):
|
|
|
|
for id in ids:
|
|
|
|
lot.add_child(id) # todo what to do if child exists already?
|
|
|
|
|
|
|
|
def _delete(self, lot: Lot, ids: Set[uuid.UUID]):
|
|
|
|
for id in ids:
|
|
|
|
lot.remove_child(id)
|
|
|
|
|
|
|
|
|
|
|
|
class LotDeviceView(LotBaseChildrenView):
|
|
|
|
"""View for adding and removing child devices from a lot.
|
|
|
|
|
|
|
|
Ex. ``lot/<id>/devices/id=X&id=Y``.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def _post(self, lot: Lot, ids: Set[uuid.UUID]):
|
|
|
|
lot.devices |= self.get_ids()
|
|
|
|
|
|
|
|
def _delete(self, lot: Lot, ids: Set[uuid.UUID]):
|
|
|
|
lot.devices -= self.get_ids()
|