2018-08-08 19:25:53 +00:00
|
|
|
import uuid
|
2018-10-05 16:57:35 +00:00
|
|
|
from collections import deque
|
2018-10-08 15:32:45 +00:00
|
|
|
from enum import Enum
|
2018-10-05 16:57:35 +00:00
|
|
|
from typing import List, Set
|
2018-08-08 19:25:53 +00:00
|
|
|
|
2018-09-11 19:50:40 +00:00
|
|
|
import marshmallow as ma
|
2018-10-05 16:57:35 +00:00
|
|
|
from flask import jsonify, request
|
2018-10-08 15:32:45 +00:00
|
|
|
from marshmallow import Schema as MarshmallowSchema, fields as f
|
|
|
|
from teal import query
|
|
|
|
from teal.marshmallow import EnumField
|
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
|
2018-09-20 16:25:47 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Device
|
2018-10-05 16:57:35 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot, Path
|
2018-08-08 19:25:53 +00:00
|
|
|
|
|
|
|
|
2018-10-08 15:32:45 +00:00
|
|
|
class Filters(query.Query):
|
|
|
|
name = query.ILike(Lot.name)
|
|
|
|
|
|
|
|
|
|
|
|
class LotFormat(Enum):
|
|
|
|
UiTree = 'UiTree'
|
|
|
|
|
|
|
|
|
2018-08-08 19:25:53 +00:00
|
|
|
class LotView(View):
|
2018-10-08 15:32:45 +00:00
|
|
|
class FindArgs(MarshmallowSchema):
|
|
|
|
"""
|
|
|
|
Allowed arguments for the ``find``
|
|
|
|
method (GET collection) endpoint
|
|
|
|
"""
|
|
|
|
format = EnumField(LotFormat, missing=None)
|
|
|
|
filter = f.Nested(Filters, missing=[])
|
|
|
|
|
2018-08-08 19:25:53 +00:00
|
|
|
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)
|
|
|
|
|
2018-10-05 16:57:35 +00:00
|
|
|
def find(self, args: dict):
|
2018-10-08 15:32:45 +00:00
|
|
|
"""
|
|
|
|
Gets lots.
|
|
|
|
|
|
|
|
By passing the value `UiTree` in the parameter `format`
|
|
|
|
of the query you get a recursive nested suited for ui-tree::
|
2018-10-05 16:57:35 +00:00
|
|
|
|
2018-10-08 15:32:45 +00:00
|
|
|
[
|
2018-10-05 16:57:35 +00:00
|
|
|
{title: 'lot1',
|
|
|
|
nodes: [{title: 'child1', nodes:[]}]
|
|
|
|
]
|
2018-10-08 15:32:45 +00:00
|
|
|
|
|
|
|
Note that in this format filters are ignored.
|
|
|
|
|
|
|
|
Otherwise it just returns the standard flat view of lots that
|
|
|
|
you can filter.
|
2018-10-05 16:57:35 +00:00
|
|
|
"""
|
2018-10-08 15:32:45 +00:00
|
|
|
if args['format'] == LotFormat.UiTree:
|
|
|
|
nodes = []
|
|
|
|
for model in Path.query: # type: Path
|
|
|
|
path = deque(model.path.path.split('.'))
|
|
|
|
self._p(nodes, path)
|
|
|
|
return jsonify({
|
|
|
|
'items': nodes,
|
|
|
|
'url': request.path
|
|
|
|
})
|
|
|
|
else:
|
|
|
|
query = Lot.query.filter(*args['filter'])
|
|
|
|
lots = query.paginate(per_page=6)
|
|
|
|
ret = {
|
|
|
|
'items': self.schema.dump(lots.items, many=True, nested=0),
|
|
|
|
'pagination': {
|
|
|
|
'page': lots.page,
|
|
|
|
'perPage': lots.per_page,
|
|
|
|
'total': lots.total,
|
|
|
|
'previous': lots.prev_num,
|
|
|
|
'next': lots.next_num
|
|
|
|
},
|
|
|
|
'url': request.path
|
|
|
|
}
|
|
|
|
return jsonify(ret)
|
2018-10-05 16:57:35 +00:00
|
|
|
|
|
|
|
def _p(self, nodes: List[dict], path: deque):
|
|
|
|
"""Recursively creates the nested lot structure.
|
|
|
|
|
|
|
|
Every recursive step consumes path (a deque of lot_id),
|
|
|
|
trying to find it as the value of id in nodes, otherwise
|
|
|
|
it adds itself. Then moves to the node's children.
|
|
|
|
"""
|
|
|
|
lot_id = uuid.UUID(path.popleft().replace('_', '-'))
|
|
|
|
try:
|
|
|
|
# does lot_id exist already in node?
|
|
|
|
node = next(part for part in nodes if lot_id == part['id'])
|
|
|
|
except StopIteration:
|
|
|
|
lot = Lot.query.filter_by(id=lot_id).one()
|
|
|
|
node = {
|
|
|
|
'id': lot_id,
|
2018-10-08 10:25:28 +00:00
|
|
|
'name': lot.name,
|
2018-10-05 16:57:35 +00:00
|
|
|
'url': lot.url.to_text(),
|
|
|
|
'closed': lot.closed,
|
|
|
|
'updated': lot.updated,
|
|
|
|
'created': lot.created,
|
|
|
|
'nodes': []
|
|
|
|
}
|
|
|
|
nodes.append(node)
|
|
|
|
if path:
|
|
|
|
self._p(node['nodes'], path)
|
|
|
|
|
2018-09-11 19:50:40 +00:00
|
|
|
|
|
|
|
class LotBaseChildrenView(View):
|
|
|
|
"""Base class for adding / removing children devices and
|
|
|
|
lots from a lot.
|
|
|
|
"""
|
|
|
|
|
|
|
|
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()
|
2018-09-12 12:53:14 +00:00
|
|
|
|
2018-09-11 19:50:40 +00:00
|
|
|
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``.
|
|
|
|
"""
|
|
|
|
|
2018-09-20 16:25:47 +00:00
|
|
|
class ListArgs(ma.Schema):
|
|
|
|
id = ma.fields.List(ma.fields.UUID())
|
|
|
|
|
2018-09-11 19:50:40 +00:00
|
|
|
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``.
|
|
|
|
"""
|
|
|
|
|
2018-09-20 16:25:47 +00:00
|
|
|
class ListArgs(ma.Schema):
|
|
|
|
id = ma.fields.List(ma.fields.Integer())
|
2018-09-11 19:50:40 +00:00
|
|
|
|
2018-09-20 16:25:47 +00:00
|
|
|
def _post(self, lot: Lot, ids: Set[int]):
|
|
|
|
lot.devices.update(Device.query.filter(Device.id.in_(ids)))
|
|
|
|
|
|
|
|
def _delete(self, lot: Lot, ids: Set[int]):
|
|
|
|
lot.devices.difference_update(Device.query.filter(Device.id.in_(ids)))
|