From 6389956adcab95537c68eb62c5ff3086a15b846a Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 1 Mar 2022 14:29:25 +0100 Subject: [PATCH 1/9] fix bug #2841. Show lot and devices for all users involved in a trade --- ereuse_devicehub/inventory/forms.py | 13 ++++++++++--- ereuse_devicehub/inventory/views.py | 7 ++++++- 2 files changed, 16 insertions(+), 4 deletions(-) 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) From 15d913efead21747a948170a588177fa197bb3c5 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 2 Mar 2022 10:36:26 +0100 Subject: [PATCH 2/9] add message with result when a devices is add or remove of a lot --- ereuse_devicehub/inventory/forms.py | 22 +++++++++++++--------- ereuse_devicehub/inventory/views.py | 18 ++++++++++++++---- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 00fe0240..d8677dd2 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -46,9 +46,11 @@ class LotDeviceForm(FlaskForm): return False self._lot = ( - 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() ) devices = set(self.devices.data.split(",")) @@ -68,14 +70,16 @@ class LotDeviceForm(FlaskForm): if trade not in dev.actions: trade.devices.add(dev) - self._lot.devices.update(self._devices) - db.session.add(self._lot) - db.session.commit() + if self._devices: + self._lot.devices.update(self._devices) + db.session.add(self._lot) + db.session.commit() def remove(self): - self._lot.devices.difference_update(self._devices) - db.session.add(self._lot) - db.session.commit() + if self._devices: + self._lot.devices.difference_update(self._devices) + db.session.add(self._lot) + db.session.commit() class LotForm(FlaskForm): diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index c0059183..62cd97cb 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -135,9 +135,14 @@ class LotDeviceAddView(View): form = LotDeviceForm() if form.validate_on_submit(): form.save() + messages.success( + 'Add devices to lot "{}" successfully!'.format(form._lot.name) + ) + else: + messages.error('Error adding devices to lot!') - next_url = request.referrer or url_for('inventory.devices.devicelist') - return flask.redirect(next_url) + next_url = request.referrer or url_for('inventory.devices.devicelist') + return flask.redirect(next_url) class LotDeviceDeleteView(View): @@ -149,9 +154,14 @@ class LotDeviceDeleteView(View): form = LotDeviceForm() if form.validate_on_submit(): form.remove() + messages.success( + 'Remove devices from lot "{}" successfully!'.format(form._lot.name) + ) + else: + messages.error('Error removing devices from lot!') - next_url = request.referrer or url_for('inventory.devices.devicelist') - return flask.redirect(next_url) + next_url = request.referrer or url_for('inventory.devices.devicelist') + return flask.redirect(next_url) class LotCreateView(View): From 806e9ad8d05b9719bc7efa1b22b9c46ae984038a Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 2 Mar 2022 10:56:31 +0100 Subject: [PATCH 3/9] fix bug #2840 about trade without confirm and with code --- ereuse_devicehub/inventory/forms.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index d8677dd2..c216a4b6 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -822,7 +822,6 @@ class TradeForm(NewActionForm): self.user_from = g.user self.user_to = self.get_or_create_user(code) - return # Create supplier (from) phantom account if not user_from and user_to: @@ -831,6 +830,9 @@ class TradeForm(NewActionForm): self.user_from = self.get_or_create_user(code) self.user_to = g.user + self.db_user_to = self.user_to + self.db_user_from = self.user_from + def get_or_create_user(self, code): email = "{}_{}@dhub.com".format(str(g.user.id), code) user = User.query.filter_by(email=email).first() From 56b8e0886cfddea41d324685613515b8ff3b616f Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 2 Mar 2022 13:36:06 +0100 Subject: [PATCH 4/9] ignoring warnings of too complex functions --- ereuse_devicehub/inventory/forms.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index c216a4b6..e5775d4b 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -185,7 +185,7 @@ class UploadSnapshotForm(FlaskForm): db.session.commit() return response - def build(self, snapshot_json): + def build(self, snapshot_json): # noqa: C901 # this is a copy adaptated from ereuse_devicehub.resources.action.views.snapshot device = snapshot_json.pop('device') # type: Computer components = None @@ -299,7 +299,7 @@ class NewDeviceForm(FlaskForm): if not self.depth.data: self.depth.data = 0.1 - def validate(self, extra_validators=None): + def validate(self, extra_validators=None): # noqa: C901 error = ["Not a correct value"] is_valid = super().validate(extra_validators) @@ -490,7 +490,7 @@ class TagDeviceForm(FlaskForm): if self.device.data: try: self.device.data = int(self.device.data.split(',')[-1]) - except: + except: # noqa: E722 self.device.data = None if self.device_id or self.device.data: From eb84884db43698834cc860c5944b7294e0dbe05b Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 2 Mar 2022 13:38:22 +0100 Subject: [PATCH 5/9] remove imported but unused --- ereuse_devicehub/cli.py | 2 +- ereuse_devicehub/devicehub.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/ereuse_devicehub/cli.py b/ereuse_devicehub/cli.py index 4c253235..47cece32 100644 --- a/ereuse_devicehub/cli.py +++ b/ereuse_devicehub/cli.py @@ -11,7 +11,7 @@ import sys sys.ps1 = '\001\033[92m\002>>> \001\033[0m\002' sys.ps2= '\001\033[94m\002... \001\033[0m\002' -import os, readline, rlcompleter, atexit +import os, readline, atexit history_file = os.path.join(os.environ['HOME'], '.python_history') try: readline.read_history_file(history_file) diff --git a/ereuse_devicehub/devicehub.py b/ereuse_devicehub/devicehub.py index 1051c951..7163173d 100644 --- a/ereuse_devicehub/devicehub.py +++ b/ereuse_devicehub/devicehub.py @@ -10,7 +10,6 @@ from ereuse_utils.session import DevicehubClient from flask import _app_ctx_stack, g from flask_login import LoginManager, current_user from flask_sqlalchemy import SQLAlchemy -from flask_wtf.csrf import CSRFProtect from teal.db import SchemaSQLAlchemy from teal.teal import Teal From 473a7a02de134a7a80abc73700dd1585416380dd Mon Sep 17 00:00:00 2001 From: Santiago L Date: Wed, 2 Mar 2022 18:51:04 +0100 Subject: [PATCH 6/9] Update ereuse_devicehub/inventory/forms.py --- ereuse_devicehub/inventory/forms.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index e5775d4b..7702be79 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -724,9 +724,6 @@ 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.outerjoin(Trade) .filter(Lot.id == self.lot.data) .filter(or_(Trade.user_from == g.user, From c2e5fef0bda4e47f916a6ddc26ed0c0e68f935e4 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 3 Mar 2022 09:35:36 +0100 Subject: [PATCH 7/9] Drop references to venv (tests are run on global env) --- .github/workflows/flask.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/flask.yml b/.github/workflows/flask.yml index 328533a8..61ec9316 100644 --- a/.github/workflows/flask.yml +++ b/.github/workflows/flask.yml @@ -76,8 +76,7 @@ jobs: - name: Run Tests run: | export SECRET_KEY=`python3 -c 'import secrets; print(secrets.token_hex())'` - source env/bin/activate - coverage run --source='ereuse_devicehub' env/bin/pytest -m mvp --maxfail=5 tests/ + coverage run --source='ereuse_devicehub' pytest -m mvp --maxfail=5 tests/ coverage report --include='ereuse_devicehub/*' coverage xml From b8a5ddd8d73ab58fb03a71574691fb7593ff2a4f Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 3 Mar 2022 09:35:52 +0100 Subject: [PATCH 8/9] Optimize apt avoiding install recommends --- .github/workflows/flask.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/flask.yml b/.github/workflows/flask.yml index 61ec9316..8a089ec7 100644 --- a/.github/workflows/flask.yml +++ b/.github/workflows/flask.yml @@ -45,7 +45,7 @@ jobs: - name: Install dependencies run: | sudo apt-get update -qy - sudo apt-get -y install postgresql-client + sudo apt-get -y install postgresql-client --no-install-recommends python -m pip install --upgrade pip pip install flake8 pytest coverage pip install -r requirements.txt From ceb63b4eb3f90f4abf36b6b6e91c01edc0a548be Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 3 Mar 2022 09:59:47 +0100 Subject: [PATCH 9/9] Fix broken coverage command --- .github/workflows/flask.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/flask.yml b/.github/workflows/flask.yml index 8a089ec7..16086b5b 100644 --- a/.github/workflows/flask.yml +++ b/.github/workflows/flask.yml @@ -76,7 +76,7 @@ jobs: - name: Run Tests run: | export SECRET_KEY=`python3 -c 'import secrets; print(secrets.token_hex())'` - coverage run --source='ereuse_devicehub' pytest -m mvp --maxfail=5 tests/ + coverage run --source='ereuse_devicehub' -m pytest -m mvp --maxfail=5 tests/ coverage report --include='ereuse_devicehub/*' coverage xml