2018-08-08 19:25:53 +00:00
|
|
|
import uuid
|
2021-11-11 16:06:26 +00:00
|
|
|
from sqlalchemy.util import OrderedSet
|
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
|
2019-12-18 13:29:25 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Device, Computer
|
2021-11-02 13:25:49 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import Trade, Confirm, Revoke
|
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 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'] + '%'))
|
2021-06-16 15:07:00 +00:00
|
|
|
lots = query.paginate(per_page=6 if args['search'] else query.count())
|
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):
|
2021-06-02 08:10:36 +00:00
|
|
|
query = query.outerjoin(Trade) \
|
|
|
|
.filter(or_(Trade.user_from == g.user,
|
|
|
|
Trade.user_to == g.user,
|
2020-04-28 20:33:33 +00:00
|
|
|
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):
|
2021-06-02 08:11:20 +00:00
|
|
|
lot = Lot.query.filter_by(id=id, owner=g.user).one()
|
2018-11-11 20:52:55 +00:00
|
|
|
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-05-10 09:48:43 +00:00
|
|
|
# get only new devices
|
|
|
|
ids -= {x.id for x in lot.devices}
|
|
|
|
if not ids:
|
|
|
|
return
|
|
|
|
|
|
|
|
devices = set(Device.query.filter(Device.id.in_(ids)).filter(
|
2021-06-11 13:49:31 +00:00
|
|
|
Device.owner==g.user))
|
2021-05-10 09:48:43 +00:00
|
|
|
|
|
|
|
lot.devices.update(devices)
|
|
|
|
|
2021-04-20 14:21:02 +00:00
|
|
|
if lot.trade:
|
|
|
|
lot.trade.devices = lot.devices
|
2021-05-10 09:48:43 +00:00
|
|
|
if g.user in [lot.trade.user_from, lot.trade.user_to]:
|
|
|
|
confirm = Confirm(action=lot.trade, user=g.user, devices=devices)
|
|
|
|
db.session.add(confirm)
|
2021-04-20 14:21:02 +00:00
|
|
|
|
2018-09-20 16:25:47 +00:00
|
|
|
def _delete(self, lot: Lot, ids: Set[int]):
|
2021-05-10 09:48:43 +00:00
|
|
|
# if there are some devices in ids than not exist now in the lot, then exit
|
|
|
|
if not ids.issubset({x.id for x in lot.devices}):
|
|
|
|
return
|
|
|
|
|
|
|
|
if lot.trade:
|
2021-11-11 16:06:26 +00:00
|
|
|
devices = Device.query.filter(Device.id.in_(ids)).all()
|
|
|
|
return delete_from_trade(lot, devices)
|
2021-05-10 09:48:43 +00:00
|
|
|
|
2021-06-09 09:36:11 +00:00
|
|
|
if not g.user == lot.owner:
|
|
|
|
txt = 'This is not your lot'
|
2021-06-07 08:50:08 +00:00
|
|
|
raise ma.ValidationError(txt)
|
2021-06-09 09:36:11 +00:00
|
|
|
|
2021-05-10 09:48:43 +00:00
|
|
|
devices = set(Device.query.filter(Device.id.in_(ids)).filter(
|
2021-06-09 09:36:11 +00:00
|
|
|
Device.owner_id == g.user.id))
|
2021-06-07 08:50:08 +00:00
|
|
|
|
|
|
|
lot.devices.difference_update(devices)
|
|
|
|
|
|
|
|
|
2021-11-11 16:06:26 +00:00
|
|
|
def delete_from_trade(lot: Lot, devices: List):
|
|
|
|
users = [lot.trade.user_from.id, lot.trade.user_to.id]
|
|
|
|
if g.user.id not in users:
|
|
|
|
# theoretically this case is impossible
|
|
|
|
txt = 'This is not your trade'
|
|
|
|
raise ma.ValidationError(txt)
|
|
|
|
|
2021-11-11 21:21:58 +00:00
|
|
|
# we need lock the action revoke for devices than travel for futures trades
|
|
|
|
for dev in devices:
|
|
|
|
if dev.owner not in users:
|
|
|
|
txt = 'This is not your device'
|
|
|
|
raise ma.ValidationError(txt)
|
|
|
|
|
2021-11-11 16:06:26 +00:00
|
|
|
drop_of_lot = []
|
|
|
|
without_confirms = []
|
|
|
|
for dev in devices:
|
|
|
|
if dev.trading_for_web(lot) in ['NeedConfirmation', 'Confirm', 'NeedConfirmRevoke']:
|
|
|
|
drop_of_lot.append(dev)
|
|
|
|
dev.reset_owner()
|
|
|
|
|
2021-11-11 16:23:45 +00:00
|
|
|
if not lot.trade.confirm:
|
2021-11-11 16:06:26 +00:00
|
|
|
drop_of_lot.append(dev)
|
|
|
|
without_confirms.append(dev)
|
|
|
|
dev.reset_owner()
|
|
|
|
|
|
|
|
|
|
|
|
revoke = Revoke(action=lot.trade, user=g.user, devices=set(devices))
|
|
|
|
db.session.add(revoke)
|
|
|
|
|
|
|
|
if without_confirms:
|
|
|
|
phantom = lot.trade.user_to
|
|
|
|
if lot.trade.user_to == g.user:
|
|
|
|
phantom = lot.trade.user_from
|
|
|
|
|
|
|
|
phantom_revoke = Revoke(
|
|
|
|
action=lot.trade,
|
|
|
|
user=phantom,
|
|
|
|
devices=set(without_confirms)
|
|
|
|
)
|
|
|
|
db.session.add(phantom_revoke)
|
|
|
|
|
|
|
|
lot.devices.difference_update(OrderedSet(drop_of_lot))
|
|
|
|
return revoke
|
|
|
|
|
|
|
|
|
|
|
|
def delete_from_trade2(lot: Lot, ids: Set[int]):
|
2021-06-07 08:50:08 +00:00
|
|
|
users = [lot.trade.user_from.id, lot.trade.user_to.id]
|
|
|
|
if not g.user.id in users:
|
|
|
|
# theoretically this case is impossible
|
|
|
|
txt = 'This is not your trade'
|
|
|
|
raise ma.ValidationError(txt)
|
|
|
|
|
|
|
|
devices = set(Device.query.filter(Device.id.in_(ids)).filter(
|
|
|
|
Device.owner_id.in_(users)))
|
|
|
|
|
|
|
|
# Now we need to know which devices we need extract of the lot
|
|
|
|
without_confirms = set() # set of devs without confirms of user2
|
|
|
|
|
|
|
|
# if the trade need confirmation, then extract all devs than
|
|
|
|
# have only one confirmation and is from the same user than try to do
|
|
|
|
# now the revoke action
|
|
|
|
if lot.trade.confirm:
|
|
|
|
for dev in devices:
|
|
|
|
# if have only one confirmation
|
|
|
|
# then can be revoked and deleted of the lot
|
2021-06-07 09:48:50 +00:00
|
|
|
# Confirm of dev.trading mean that there are only one confirmation
|
|
|
|
# and the first user than put this device in trade is the actual g.user
|
2021-11-11 16:06:26 +00:00
|
|
|
if dev.trading(lot) == 'Confirm':
|
2021-06-07 08:50:08 +00:00
|
|
|
without_confirms.add(dev)
|
|
|
|
dev.reset_owner()
|
|
|
|
|
|
|
|
# we need to mark one revoke for every devs
|
|
|
|
revoke = Revoke(action=lot.trade, user=g.user, devices=devices)
|
|
|
|
db.session.add(revoke)
|
|
|
|
|
|
|
|
if not lot.trade.confirm:
|
|
|
|
# if the trade is with phantom account
|
|
|
|
without_confirms = devices
|
|
|
|
|
|
|
|
if without_confirms:
|
2021-10-28 10:19:49 +00:00
|
|
|
phantom = lot.trade.user_to
|
|
|
|
if lot.trade.user_to == g.user:
|
|
|
|
phantom = lot.trade.user_from
|
|
|
|
|
|
|
|
phantom_revoke = Revoke(
|
|
|
|
action=lot.trade,
|
|
|
|
user=phantom,
|
2021-06-07 08:50:08 +00:00
|
|
|
devices=without_confirms
|
|
|
|
)
|
2021-10-28 10:19:49 +00:00
|
|
|
db.session.add(phantom_revoke)
|
2021-06-07 08:50:08 +00:00
|
|
|
|
|
|
|
lot.devices.difference_update(without_confirms)
|
2021-11-10 17:59:44 +00:00
|
|
|
# TODO @cayop ?? we dont need this line
|
2021-06-07 08:50:08 +00:00
|
|
|
lot.trade.devices = lot.devices
|
|
|
|
|
|
|
|
return revoke
|