fix bug #2841. Show lot and devices for all users involved in a trade

This commit is contained in:
Cayo Puigdefabregas 2022-03-01 14:29:25 +01:00
parent 1e9b65e0b5
commit 6389956adc
2 changed files with 16 additions and 4 deletions

View File

@ -31,6 +31,8 @@ from ereuse_devicehub.resources.tag.model import Tag
from ereuse_devicehub.resources.tradedocument.models import TradeDocument
from ereuse_devicehub.resources.user.exceptions import InsufficientPermission
from ereuse_devicehub.resources.user.models import User
from ereuse_devicehub.resources.action.models import Trade
from sqlalchemy import or_
class LotDeviceForm(FlaskForm):
@ -718,9 +720,14 @@ class TradeForm(NewActionForm):
self.user_from.render_kw['data-email'] = g.user.email
self.user_to.render_kw['data-email'] = g.user.email
self._lot = (
Lot.query.filter(Lot.id == self.lot.data)
.filter(Lot.owner_id == g.user.id)
.one()
# Lot.query.filter(Lot.id == self.lot.data)
# .filter(Lot.owner_id == g.user.id)
# .one()
Lot.query.outerjoin(Trade)
.filter(Lot.id == self.lot.data)
.filter(or_(Trade.user_from == g.user,
Trade.user_to == g.user,
Lot.owner_id == g.user.id)).one()
)
def validate(self, extra_validators=None):

View File

@ -7,6 +7,7 @@ from flask import Blueprint, g, make_response, request, url_for
from flask.views import View
from flask_login import current_user, login_required
from werkzeug.exceptions import NotFound
from sqlalchemy import or_
from ereuse_devicehub import messages
from ereuse_devicehub.inventory.forms import (
@ -42,7 +43,10 @@ class DeviceListMix(View):
# TODO @cayop adding filter
# https://github.com/eReuse/devicehub-teal/blob/testing/ereuse_devicehub/resources/device/views.py#L56
filter_types = ['Desktop', 'Laptop', 'Server']
lots = Lot.query.filter(Lot.owner_id == current_user.id)
lots = Lot.query.outerjoin(Trade) \
.filter(or_(Trade.user_from == g.user,
Trade.user_to == g.user,
Lot.owner_id == g.user.id)).distinct()
lot = None
tags = (
Tag.query.filter(Tag.owner_id == current_user.id)
@ -51,6 +55,7 @@ class DeviceListMix(View):
)
if lot_id:
# import pdb; pdb.set_trace()
lot = lots.filter(Lot.id == lot_id).one()
devices = [dev for dev in lot.devices if dev.type in filter_types]
devices = sorted(devices, key=lambda x: x.updated, reverse=True)