precommit

This commit is contained in:
Cayo Puigdefabregas 2022-03-08 11:01:10 +01:00
parent ea66c31a07
commit b16fdd59f9
1 changed files with 60 additions and 26 deletions

View File

@ -1,26 +1,46 @@
import copy import copy
import json import json
from json.decoder import JSONDecodeError from json.decoder import JSONDecodeError
from boltons.urlutils import URL from boltons.urlutils import URL
from flask import g, request from flask import g, request
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from sqlalchemy import or_
from sqlalchemy.util import OrderedSet from sqlalchemy.util import OrderedSet
from wtforms import ( from wtforms import (
BooleanField, DateField, FileField, FloatField, Form, HiddenField, BooleanField,
IntegerField, MultipleFileField, SelectField, StringField, TextAreaField, DateField,
URLField, validators) FileField,
FloatField,
Form,
HiddenField,
IntegerField,
MultipleFileField,
SelectField,
StringField,
TextAreaField,
URLField,
validators,
)
from wtforms.fields import FormField from wtforms.fields import FormField
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.resources.action.models import RateComputer, Snapshot from ereuse_devicehub.resources.action.models import RateComputer, Snapshot, Trade
from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate
from ereuse_devicehub.resources.action.schemas import \ from ereuse_devicehub.resources.action.schemas import Snapshot as SnapshotSchema
Snapshot as SnapshotSchema from ereuse_devicehub.resources.action.views.snapshot import move_json, save_json
from ereuse_devicehub.resources.action.views.snapshot import (
move_json, save_json)
from ereuse_devicehub.resources.device.models import ( from ereuse_devicehub.resources.device.models import (
SAI, Cellphone, Computer, Device, Keyboard, MemoryCardReader, Monitor, SAI,
Mouse, Smartphone, Tablet) Cellphone,
Computer,
Device,
Keyboard,
MemoryCardReader,
Monitor,
Mouse,
Smartphone,
Tablet,
)
from ereuse_devicehub.resources.device.sync import Sync from ereuse_devicehub.resources.device.sync import Sync
from ereuse_devicehub.resources.documents.models import DataWipeDocument from ereuse_devicehub.resources.documents.models import DataWipeDocument
from ereuse_devicehub.resources.enums import Severity, SnapshotSoftware from ereuse_devicehub.resources.enums import Severity, SnapshotSoftware
@ -30,9 +50,6 @@ 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_
DEVICES = [ DEVICES = [
("All", "All"), ("All", "All"),
@ -90,13 +107,14 @@ DEVICES = [
("Recreation", "Recreation"), ("Recreation", "Recreation"),
("Bike", "Bike"), ("Bike", "Bike"),
("Racket", "Racket"), ("Racket", "Racket"),
("Manufacturer", "Manufacturer") ("Manufacturer", "Manufacturer"),
] ]
class FilterForm(FlaskForm): class FilterForm(FlaskForm):
filter = SelectField('', choices=DEVICES, default="Computer", filter = SelectField(
render_kw={'class': "form-select"}) '', choices=DEVICES, default="Computer", render_kw={'class': "form-select"}
)
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
@ -131,9 +149,14 @@ class LotDeviceForm(FlaskForm):
self._lot = ( self._lot = (
Lot.query.outerjoin(Trade) Lot.query.outerjoin(Trade)
.filter(Lot.id == self.lot.data) .filter(Lot.id == self.lot.data)
.filter(or_(Trade.user_from == g.user, .filter(
Trade.user_to == g.user, or_(
Lot.owner_id == g.user.id)).one() 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(","))
@ -550,11 +573,17 @@ class TagDeviceForm(FlaskForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if self.delete: if self.delete:
tags = Tag.query.filter(Tag.owner_id == g.user.id).filter_by( tags = (
device_id=self.device_id Tag.query.filter(Tag.owner_id == g.user.id)
).order_by(Tag.id) .filter_by(device_id=self.device_id)
.order_by(Tag.id)
)
else: else:
tags = Tag.query.filter(Tag.owner_id == g.user.id).filter_by(device_id=None).order_by(Tag.id) tags = (
Tag.query.filter(Tag.owner_id == g.user.id)
.filter_by(device_id=None)
.order_by(Tag.id)
)
self.tag.choices = [(tag.id, tag.id) for tag in tags] self.tag.choices = [(tag.id, tag.id) for tag in tags]
@ -813,9 +842,14 @@ class TradeForm(NewActionForm):
self._lot = ( self._lot = (
Lot.query.outerjoin(Trade) Lot.query.outerjoin(Trade)
.filter(Lot.id == self.lot.data) .filter(Lot.id == self.lot.data)
.filter(or_(Trade.user_from == g.user, .filter(
Trade.user_to == g.user, or_(
Lot.owner_id == g.user.id)).one() 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):