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.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)

View file

@ -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

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):
@ -44,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(","))
@ -66,11 +70,13 @@ class LotDeviceForm(FlaskForm):
if trade not in dev.actions:
trade.devices.add(dev)
if self._devices:
self._lot.devices.update(self._devices)
db.session.add(self._lot)
db.session.commit()
def remove(self):
if self._devices:
self._lot.devices.difference_update(self._devices)
db.session.add(self._lot)
db.session.commit()
@ -179,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
@ -293,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)
@ -484,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:
@ -718,9 +724,11 @@ 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,
Trade.user_to == g.user,
Lot.owner_id == g.user.id)).one()
)
def validate(self, extra_validators=None):
@ -811,7 +819,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:
@ -820,6 +827,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()

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)
@ -130,6 +135,11 @@ 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)
@ -144,6 +154,11 @@ 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)