Merge branch 'feature/server-side-render' into feature/server-side-render-UX-improvements

This commit is contained in:
Cayo Puigdefabregas 2022-03-03 11:18:57 +01:00
commit 075135c191
3 changed files with 45 additions and 21 deletions

View File

@ -45,7 +45,7 @@ jobs:
- name: Install dependencies - name: Install dependencies
run: | run: |
sudo apt-get update -qy 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 python -m pip install --upgrade pip
pip install flake8 pytest coverage pip install flake8 pytest coverage
pip install -r requirements.txt pip install -r requirements.txt
@ -76,8 +76,7 @@ jobs:
- name: Run Tests - name: Run Tests
run: | run: |
export SECRET_KEY=`python3 -c 'import secrets; print(secrets.token_hex())'` export SECRET_KEY=`python3 -c 'import secrets; print(secrets.token_hex())'`
source env/bin/activate coverage run --source='ereuse_devicehub' -m pytest -m mvp --maxfail=5 tests/
coverage run --source='ereuse_devicehub' env/bin/pytest -m mvp --maxfail=5 tests/
coverage report --include='ereuse_devicehub/*' coverage report --include='ereuse_devicehub/*'
coverage xml coverage xml

View File

@ -32,6 +32,8 @@ from ereuse_devicehub.resources.tag.model import Tag
from ereuse_devicehub.resources.tradedocument.models import TradeDocument from ereuse_devicehub.resources.tradedocument.models import TradeDocument
from ereuse_devicehub.resources.user.exceptions import InsufficientPermission from ereuse_devicehub.resources.user.exceptions import InsufficientPermission
from ereuse_devicehub.resources.user.models import User from ereuse_devicehub.resources.user.models import User
from ereuse_devicehub.resources.action.models import Trade
from sqlalchemy import or_
class LotDeviceForm(FlaskForm): class LotDeviceForm(FlaskForm):
@ -45,9 +47,11 @@ class LotDeviceForm(FlaskForm):
return False return False
self._lot = ( self._lot = (
Lot.query.filter(Lot.id == self.lot.data) Lot.query.outerjoin(Trade)
.filter(Lot.owner_id == g.user.id) .filter(Lot.id == self.lot.data)
.one() .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(",")) devices = set(self.devices.data.split(","))
@ -67,14 +71,16 @@ class LotDeviceForm(FlaskForm):
if trade not in dev.actions: if trade not in dev.actions:
trade.devices.add(dev) trade.devices.add(dev)
self._lot.devices.update(self._devices) if self._devices:
db.session.add(self._lot) self._lot.devices.update(self._devices)
db.session.commit() db.session.add(self._lot)
db.session.commit()
def remove(self): def remove(self):
self._lot.devices.difference_update(self._devices) if self._devices:
db.session.add(self._lot) self._lot.devices.difference_update(self._devices)
db.session.commit() db.session.add(self._lot)
db.session.commit()
class LotForm(FlaskForm): class LotForm(FlaskForm):
@ -723,9 +729,11 @@ class TradeForm(NewActionForm):
self.user_from.render_kw['data-email'] = g.user.email self.user_from.render_kw['data-email'] = g.user.email
self.user_to.render_kw['data-email'] = g.user.email self.user_to.render_kw['data-email'] = g.user.email
self._lot = ( self._lot = (
Lot.query.filter(Lot.id == self.lot.data) Lot.query.outerjoin(Trade)
.filter(Lot.owner_id == g.user.id) .filter(Lot.id == self.lot.data)
.one() .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): def validate(self, extra_validators=None):
@ -816,7 +824,6 @@ class TradeForm(NewActionForm):
self.user_from = g.user self.user_from = g.user
self.user_to = self.get_or_create_user(code) self.user_to = self.get_or_create_user(code)
return
# Create supplier (from) phantom account # Create supplier (from) phantom account
if not user_from and user_to: if not user_from and user_to:
@ -825,6 +832,9 @@ class TradeForm(NewActionForm):
self.user_from = self.get_or_create_user(code) self.user_from = self.get_or_create_user(code)
self.user_to = g.user 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): def get_or_create_user(self, code):
email = "{}_{}@dhub.com".format(str(g.user.id), code) email = "{}_{}@dhub.com".format(str(g.user.id), code)
user = User.query.filter_by(email=email).first() user = User.query.filter_by(email=email).first()

View File

@ -7,6 +7,7 @@ from flask import Blueprint, g, make_response, request, url_for
from flask.views import View from flask.views import View
from flask_login import current_user, login_required from flask_login import current_user, login_required
from werkzeug.exceptions import NotFound from werkzeug.exceptions import NotFound
from sqlalchemy import or_
from ereuse_devicehub import messages from ereuse_devicehub import messages
from ereuse_devicehub.inventory.forms import ( from ereuse_devicehub.inventory.forms import (
@ -42,7 +43,10 @@ class DeviceListMix(View):
# TODO @cayop adding filter # TODO @cayop adding filter
# https://github.com/eReuse/devicehub-teal/blob/testing/ereuse_devicehub/resources/device/views.py#L56 # https://github.com/eReuse/devicehub-teal/blob/testing/ereuse_devicehub/resources/device/views.py#L56
filter_types = ['Desktop', 'Laptop', 'Server'] 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 lot = None
tags = ( tags = (
Tag.query.filter(Tag.owner_id == current_user.id) Tag.query.filter(Tag.owner_id == current_user.id)
@ -51,6 +55,7 @@ class DeviceListMix(View):
) )
if lot_id: if lot_id:
# import pdb; pdb.set_trace()
lot = lots.filter(Lot.id == lot_id).one() lot = lots.filter(Lot.id == lot_id).one()
devices = [dev for dev in lot.devices if dev.type in filter_types] devices = [dev for dev in lot.devices if dev.type in filter_types]
devices = sorted(devices, key=lambda x: x.updated, reverse=True) devices = sorted(devices, key=lambda x: x.updated, reverse=True)
@ -130,9 +135,14 @@ class LotDeviceAddView(View):
form = LotDeviceForm() form = LotDeviceForm()
if form.validate_on_submit(): if form.validate_on_submit():
form.save() 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') next_url = request.referrer or url_for('inventory.devices.devicelist')
return flask.redirect(next_url) return flask.redirect(next_url)
class LotDeviceDeleteView(View): class LotDeviceDeleteView(View):
@ -144,9 +154,14 @@ class LotDeviceDeleteView(View):
form = LotDeviceForm() form = LotDeviceForm()
if form.validate_on_submit(): if form.validate_on_submit():
form.remove() 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') next_url = request.referrer or url_for('inventory.devices.devicelist')
return flask.redirect(next_url) return flask.redirect(next_url)
class LotCreateView(View): class LotCreateView(View):