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-11-13 14:52:27 +00:00
|
|
|
from typing import Dict, List, Set, Union
|
2018-08-08 19:25:53 +00:00
|
|
|
|
2018-09-11 19:50:40 +00:00
|
|
|
import marshmallow as ma
|
2020-04-28 20:33:33 +00:00
|
|
|
from flask import Response, jsonify, request, g
|
2018-10-08 15:32:45 +00:00
|
|
|
from marshmallow import Schema as MarshmallowSchema, fields as f
|
2020-08-17 14:45:18 +00:00
|
|
|
from sqlalchemy import or_
|
2018-10-08 15:32:45 +00:00
|
|
|
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
|
2019-01-26 11:49:31 +00:00
|
|
|
from ereuse_devicehub.query import things_response
|
2020-08-17 14:45:18 +00:00
|
|
|
from ereuse_devicehub.resources.deliverynote.models import Deliverynote
|
2019-12-18 13:29:25 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Device, Computer
|
2018-10-05 16:57:35 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot, Path
|
2021-04-20 14:21:02 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import TradeNote
|
2018-08-08 19:25:53 +00:00
|
|
|
|
|
|
|
|
2018-10-08 15:32:45 +00:00
|
|
|
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):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Allowed arguments for the ``find``
|
2018-10-08 15:32:45 +00:00
|
|
|
method (GET collection) endpoint
|
|
|
|
"""
|
|
|
|
format = EnumField(LotFormat, missing=None)
|
2018-10-08 15:51:51 +00:00
|
|
|
search = f.Str(missing=None)
|
2018-10-08 15:32:45 +00:00
|
|
|
|
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)
|
2019-02-04 17:20:50 +00:00
|
|
|
db.session().final_flush()
|
2018-08-08 19:25:53 +00:00
|
|
|
ret = self.schema.jsonify(lot)
|
|
|
|
ret.status_code = 201
|
2019-02-04 17:20:50 +00:00
|
|
|
db.session.commit()
|
2018-08-08 19:25:53 +00:00
|
|
|
return ret
|
|
|
|
|
2018-10-19 08:35:23 +00:00
|
|
|
def patch(self, id):
|
2020-08-17 14:45:18 +00:00
|
|
|
patch_schema = self.resource_def.SCHEMA(only=(
|
2021-02-05 12:21:20 +00:00
|
|
|
'name', 'description', 'transfer_state', 'receiver_address', 'amount', 'devices',
|
2020-08-17 14:45:18 +00:00
|
|
|
'owner_address'), partial=True)
|
2019-02-07 12:47:42 +00:00
|
|
|
l = request.get_json(schema=patch_schema)
|
2018-10-19 08:35:23 +00:00
|
|
|
lot = Lot.query.filter_by(id=id).one()
|
2021-02-05 12:21:20 +00:00
|
|
|
device_fields = ['transfer_state', 'receiver_address', 'amount', 'owner_address']
|
2019-12-18 13:29:25 +00:00
|
|
|
computers = [x for x in lot.all_devices if isinstance(x, Computer)]
|
2018-10-19 08:35:23 +00:00
|
|
|
for key, value in l.items():
|
|
|
|
setattr(lot, key, value)
|
2019-12-18 13:29:25 +00:00
|
|
|
if key in device_fields:
|
|
|
|
for dev in computers:
|
|
|
|
setattr(dev, key, value)
|
2018-10-19 08:35:23 +00:00
|
|
|
db.session.commit()
|
|
|
|
return Response(status=204)
|
|
|
|
|
2018-08-08 19:25:53 +00:00
|
|
|
def one(self, id: uuid.UUID):
|
2019-05-11 14:27:22 +00:00
|
|
|
"""Gets one action."""
|
2018-09-11 19:50:40 +00:00
|
|
|
lot = Lot.query.filter_by(id=id).one() # type: Lot
|
2020-03-03 19:33:37 +00:00
|
|
|
return self.schema.jsonify(lot, nested=2)
|
2018-09-11 19:50:40 +00:00
|
|
|
|
2020-04-01 17:56:09 +00:00
|
|
|
# @teal.cache.cache(datetime.timedelta(minutes=5))
|
2018-10-05 16:57:35 +00:00
|
|
|
def find(self, args: dict):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Gets lots.
|
2018-10-08 15:32:45 +00:00
|
|
|
|
|
|
|
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:
|
2020-03-03 19:33:37 +00:00
|
|
|
lots = self.schema.dump(Lot.query, many=True, nested=2)
|
2018-11-13 14:52:27 +00:00
|
|
|
ret = {
|
|
|
|
'items': {l['id']: l for l in lots},
|
|
|
|
'tree': self.ui_tree(),
|
2018-10-08 15:32:45 +00:00
|
|
|
'url': request.path
|
2018-11-13 14:52:27 +00:00
|
|
|
}
|
2018-10-08 15:32:45 +00:00
|
|
|
else:
|
2018-10-08 15:51:51 +00:00
|
|
|
query = Lot.query
|
2020-04-28 20:33:33 +00:00
|
|
|
query = self.visibility_filter(query)
|
2018-10-08 15:51:51 +00:00
|
|
|
if args['search']:
|
|
|
|
query = query.filter(Lot.name.ilike(args['search'] + '%'))
|
|
|
|
lots = query.paginate(per_page=6 if args['search'] else 30)
|
2019-01-26 11:49:31 +00:00
|
|
|
return things_response(
|
2020-04-29 18:41:32 +00:00
|
|
|
self.schema.dump(lots.items, many=True, nested=2),
|
2019-01-26 11:49:31 +00:00
|
|
|
lots.page, lots.per_page, lots.total, lots.prev_num, lots.next_num
|
|
|
|
)
|
2018-11-13 14:52:27 +00:00
|
|
|
return jsonify(ret)
|
2018-10-11 09:22:59 +00:00
|
|
|
|
2020-04-28 20:33:33 +00:00
|
|
|
def visibility_filter(self, query):
|
|
|
|
query = query.outerjoin(Deliverynote) \
|
|
|
|
.filter(or_(Deliverynote.receiver_address == g.user.email,
|
|
|
|
Deliverynote.supplier_email == g.user.email,
|
|
|
|
Lot.owner_id == g.user.id))
|
|
|
|
return query
|
|
|
|
|
2020-08-03 16:25:55 +00:00
|
|
|
def query(self, args):
|
|
|
|
query = Lot.query.distinct()
|
|
|
|
return query
|
|
|
|
|
2018-11-11 20:52:55 +00:00
|
|
|
def delete(self, id):
|
|
|
|
lot = Lot.query.filter_by(id=id).one()
|
|
|
|
lot.delete()
|
|
|
|
db.session.commit()
|
|
|
|
return Response(status=204)
|
|
|
|
|
2018-10-11 09:22:59 +00:00
|
|
|
@classmethod
|
2018-11-13 14:52:27 +00:00
|
|
|
def ui_tree(cls) -> List[Dict]:
|
|
|
|
tree = []
|
|
|
|
for model in Path.query: # type: Path
|
|
|
|
path = deque(model.path.path.split('.'))
|
|
|
|
cls._p(tree, path)
|
|
|
|
return tree
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _p(cls, nodes: List[Dict[str, Union[uuid.UUID, List]]], path: deque):
|
2018-10-05 16:57:35 +00:00
|
|
|
"""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:
|
|
|
|
node = {
|
|
|
|
'id': lot_id,
|
|
|
|
'nodes': []
|
|
|
|
}
|
|
|
|
nodes.append(node)
|
|
|
|
if path:
|
2018-10-11 09:22:59 +00:00
|
|
|
cls._p(node['nodes'], path)
|
2018-10-05 16:57:35 +00:00
|
|
|
|
2021-02-05 12:21:20 +00:00
|
|
|
def get_lot_amount(self, l: Lot):
|
|
|
|
"""Return lot amount value"""
|
|
|
|
return l.amount
|
2019-12-12 20:17:35 +00:00
|
|
|
|
|
|
|
def change_state(self):
|
|
|
|
"""Change state of Lot"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
def transfer_ownership_lot(self):
|
|
|
|
"""Perform a InitTransfer action to change author_id of lot"""
|
|
|
|
pass
|
|
|
|
|
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())
|
2018-09-12 12:53:14 +00:00
|
|
|
|
2019-02-04 17:20:50 +00:00
|
|
|
db.session().final_flush()
|
2018-09-11 19:50:40 +00:00
|
|
|
ret = self.schema.jsonify(lot)
|
|
|
|
ret.status_code = 201
|
2019-02-04 17:20:50 +00:00
|
|
|
|
|
|
|
db.session.commit()
|
2018-09-11 19:50:40 +00:00
|
|
|
return ret
|
|
|
|
|
|
|
|
def delete(self, id: uuid.UUID):
|
|
|
|
lot = self.get_lot(id)
|
|
|
|
self._delete(lot, self.get_ids())
|
2019-02-04 17:20:50 +00:00
|
|
|
db.session().final_flush()
|
|
|
|
response = self.schema.jsonify(lot)
|
2018-09-11 19:50:40 +00:00
|
|
|
db.session.commit()
|
2019-02-04 17:20:50 +00:00
|
|
|
return response
|
2018-09-11 19:50:40 +00:00
|
|
|
|
|
|
|
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]):
|
2018-11-13 14:52:27 +00:00
|
|
|
lot.add_children(*ids)
|
2018-09-11 19:50:40 +00:00
|
|
|
|
|
|
|
def _delete(self, lot: Lot, ids: Set[uuid.UUID]):
|
2018-11-13 14:52:27 +00:00
|
|
|
lot.remove_children(*ids)
|
2018-09-11 19:50:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
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]):
|
2021-04-20 14:21:02 +00:00
|
|
|
if lot.trade:
|
|
|
|
lot_device_ids = [d.id for d in lot.devices]
|
|
|
|
new_device_ids = {d for d in ids if not d in lot_device_ids}
|
|
|
|
devices = set(Device.query.filter(Device.id.in_(new_device_ids)).all())
|
|
|
|
txt = 'Adding new device in lot of trade {}'.format(lot.trade.id)
|
|
|
|
note = TradeNote(description=txt,
|
|
|
|
devices=devices,
|
|
|
|
trade=lot.trade)
|
|
|
|
db.session.add(note)
|
|
|
|
|
2018-09-20 16:25:47 +00:00
|
|
|
lot.devices.update(Device.query.filter(Device.id.in_(ids)))
|
|
|
|
|
2021-04-20 14:21:02 +00:00
|
|
|
if lot.trade:
|
|
|
|
lot.trade.devices = lot.devices
|
|
|
|
|
|
|
|
|
2018-09-20 16:25:47 +00:00
|
|
|
def _delete(self, lot: Lot, ids: Set[int]):
|
2021-04-20 14:21:02 +00:00
|
|
|
if lot.trade:
|
|
|
|
devices = set(Device.query.filter(Device.id.in_(ids)).all())
|
|
|
|
txt = 'Removing device from lot of trade {}'.format(lot.trade.id)
|
|
|
|
note = TradeNote(description=txt,
|
|
|
|
devices=devices,
|
|
|
|
trade=lot.trade)
|
|
|
|
db.session.add(note)
|
|
|
|
|
2018-09-20 16:25:47 +00:00
|
|
|
lot.devices.difference_update(Device.query.filter(Device.id.in_(ids)))
|
2021-04-20 14:21:02 +00:00
|
|
|
|
|
|
|
if lot.trade:
|
|
|
|
lot.trade.devices = lot.devices
|