add transfer in name of lot

This commit is contained in:
Cayo Puigdefabregas 2022-06-21 16:40:21 +02:00
parent 97edb260b7
commit 7d40f5b537
2 changed files with 25 additions and 5 deletions

View file

@ -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)

View file

@ -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":