diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 863bc6a2..00fe0240 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -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): diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index e43f764a..c0059183 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -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)