precommit

This commit is contained in:
Cayo Puigdefabregas 2022-03-08 17:49:56 +01:00
parent 0bef1dee2a
commit be2fda3d8c
2 changed files with 73 additions and 30 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,8 +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_
class LotDeviceForm(FlaskForm): class LotDeviceForm(FlaskForm):
@ -47,9 +65,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(","))
@ -468,11 +491,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]
@ -731,9 +760,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):

View File

@ -41,7 +41,6 @@ logger = logging.getLogger(__name__)
class GenericMixView(View): class GenericMixView(View):
def get_lots(self): def get_lots(self):
return ( return (
Lot.query.outerjoin(Trade) Lot.query.outerjoin(Trade)
@ -246,7 +245,12 @@ class UploadSnapshotView(GenericMixView):
def dispatch_request(self, lot_id=None): def dispatch_request(self, lot_id=None):
lots = self.get_lots() lots = self.get_lots()
form = UploadSnapshotForm() form = UploadSnapshotForm()
context = {'page_title': 'Upload Snapshot', 'lots': lots, 'form': form, 'lot_id': lot_id} context = {
'page_title': 'Upload Snapshot',
'lots': lots,
'form': form,
'lot_id': lot_id,
}
if form.validate_on_submit(): if form.validate_on_submit():
snapshot = form.save(commit=False) snapshot = form.save(commit=False)
# import pdb; pdb.set_trace() # import pdb; pdb.set_trace()
@ -267,7 +271,12 @@ class DeviceCreateView(GenericMixView):
def dispatch_request(self, lot_id=None): def dispatch_request(self, lot_id=None):
lots = self.get_lots() lots = self.get_lots()
form = NewDeviceForm() form = NewDeviceForm()
context = {'page_title': 'New Device', 'lots': lots, 'form': form, 'lot_id': lot_id} context = {
'page_title': 'New Device',
'lots': lots,
'form': form,
'lot_id': lot_id,
}
if form.validate_on_submit(): if form.validate_on_submit():
snapshot = form.save(commit=False) snapshot = form.save(commit=False)
next_url = url_for('inventory.devices.devicelist') next_url = url_for('inventory.devices.devicelist')
@ -278,9 +287,7 @@ class DeviceCreateView(GenericMixView):
db.session.add(lot) db.session.add(lot)
db.session.commit() db.session.commit()
messages.success( messages.success('Device "{}" created successfully!'.format(form.type.data))
'Device "{}" created successfully!'.format(form.type.data)
)
return flask.redirect(next_url) return flask.redirect(next_url)
return flask.render_template(self.template_name, **context) return flask.render_template(self.template_name, **context)
@ -669,11 +676,13 @@ devices.add_url_rule(
'/upload-snapshot/', view_func=UploadSnapshotView.as_view('upload_snapshot') '/upload-snapshot/', view_func=UploadSnapshotView.as_view('upload_snapshot')
) )
devices.add_url_rule( devices.add_url_rule(
'/lot/<string:lot_id>/upload-snapshot/', view_func=UploadSnapshotView.as_view('lot_upload_snapshot') '/lot/<string:lot_id>/upload-snapshot/',
view_func=UploadSnapshotView.as_view('lot_upload_snapshot'),
) )
devices.add_url_rule('/device/add/', view_func=DeviceCreateView.as_view('device_add')) devices.add_url_rule('/device/add/', view_func=DeviceCreateView.as_view('device_add'))
devices.add_url_rule( devices.add_url_rule(
'/lot/<string:lot_id>/device/add/', view_func=DeviceCreateView.as_view('lot_device_add') '/lot/<string:lot_id>/device/add/',
view_func=DeviceCreateView.as_view('lot_device_add'),
) )
devices.add_url_rule('/tag/', view_func=TagListView.as_view('taglist')) devices.add_url_rule('/tag/', view_func=TagListView.as_view('taglist'))
devices.add_url_rule('/tag/add/', view_func=TagAddView.as_view('tag_add')) devices.add_url_rule('/tag/add/', view_func=TagAddView.as_view('tag_add'))