Merge pull request #206 from eReuse/bugfix/server-side-render-#2841

Bugfix/server side render #2841
This commit is contained in:
Santiago L 2022-03-02 18:51:18 +01:00 committed by GitHub
commit 4ec63762d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 23 deletions

View File

@ -11,7 +11,7 @@ import sys
sys.ps1 = '\001\033[92m\002>>> \001\033[0m\002' sys.ps1 = '\001\033[92m\002>>> \001\033[0m\002'
sys.ps2= '\001\033[94m\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') history_file = os.path.join(os.environ['HOME'], '.python_history')
try: try:
readline.read_history_file(history_file) readline.read_history_file(history_file)

View File

@ -10,7 +10,6 @@ from ereuse_utils.session import DevicehubClient
from flask import _app_ctx_stack, g from flask import _app_ctx_stack, g
from flask_login import LoginManager, current_user from flask_login import LoginManager, current_user
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from flask_wtf.csrf import CSRFProtect
from teal.db import SchemaSQLAlchemy from teal.db import SchemaSQLAlchemy
from teal.teal import Teal from teal.teal import Teal

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.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):
@ -44,9 +46,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(","))
@ -66,14 +70,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):
@ -179,7 +185,7 @@ class UploadSnapshotForm(FlaskForm):
db.session.commit() db.session.commit()
return response 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 # this is a copy adaptated from ereuse_devicehub.resources.action.views.snapshot
device = snapshot_json.pop('device') # type: Computer device = snapshot_json.pop('device') # type: Computer
components = None components = None
@ -293,7 +299,7 @@ class NewDeviceForm(FlaskForm):
if not self.depth.data: if not self.depth.data:
self.depth.data = 0.1 self.depth.data = 0.1
def validate(self, extra_validators=None): def validate(self, extra_validators=None): # noqa: C901
error = ["Not a correct value"] error = ["Not a correct value"]
is_valid = super().validate(extra_validators) is_valid = super().validate(extra_validators)
@ -484,7 +490,7 @@ class TagDeviceForm(FlaskForm):
if self.device.data: if self.device.data:
try: try:
self.device.data = int(self.device.data.split(',')[-1]) self.device.data = int(self.device.data.split(',')[-1])
except: except: # noqa: E722
self.device.data = None self.device.data = None
if self.device_id or self.device.data: if self.device_id or self.device.data:
@ -718,9 +724,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):
@ -811,7 +819,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:
@ -820,6 +827,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):