From 7d40f5b537174de9119de7f236bc60d9c79c1b73 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 21 Jun 2022 16:40:21 +0200 Subject: [PATCH] add transfer in name of lot --- ereuse_devicehub/inventory/models.py | 10 ++++++++++ ereuse_devicehub/resources/lot/views.py | 20 +++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/ereuse_devicehub/inventory/models.py b/ereuse_devicehub/inventory/models.py index 69414c10..45d25157 100644 --- a/ereuse_devicehub/inventory/models.py +++ b/ereuse_devicehub/inventory/models.py @@ -1,6 +1,7 @@ from uuid import uuid4 from citext import CIText +from flask import g from sqlalchemy import Column, Integer from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.orm import backref, relationship @@ -43,6 +44,15 @@ class Transfer(Thing): return False + def type_transfer(self): + if self.user_from == g.user: + return 'Outgoing' + + if self.user_to == g.user: + return 'Incoming' + + return 'Temporary' + class DeliveryNote(Thing): id = Column(UUID(as_uuid=True), primary_key=True, default=uuid4) diff --git a/ereuse_devicehub/resources/lot/views.py b/ereuse_devicehub/resources/lot/views.py index cb160317..e992dc90 100644 --- a/ereuse_devicehub/resources/lot/views.py +++ b/ereuse_devicehub/resources/lot/views.py @@ -111,7 +111,7 @@ class LotView(View): query = query.filter(Lot.name.ilike(args['search'] + '%')) lots = query.paginate(per_page=6 if args['search'] else query.count()) return things_response( - self.schema.dump(lots.items, many=True, nested=2), + self.get_lots_dump(lots), lots.page, lots.per_page, lots.total, @@ -120,6 +120,19 @@ class LotView(View): ) return jsonify(ret) + def get_lots_dump(self, lots): + lots_dump = self.schema.dump(lots.items, many=True, nested=2) + for lot in lots.items: + if not lot.transfer: + continue + for _lot in lots_dump: + if _lot['id'] == str(lot.id): + name = _lot['name'] + type_transfer = lot.type_transfer() + _lot['name'] = f'{name} - ({type_transfer})' + break + return lots_dump + def visibility_filter(self, query): query = ( query.outerjoin(Trade) @@ -142,10 +155,7 @@ class LotView(View): # temporary if lot_type == "temporary": return query.filter(Lot.trade == None).filter( - or_( - Lot.transfer == None, - Transfer.date == None - ) + or_(Lot.transfer == None, Transfer.date == None) ) if lot_type == "incoming":