review pr 212 about refactor and schemas

This commit is contained in:
Cayo Puigdefabregas 2022-03-30 18:27:57 +02:00
parent de23503f42
commit 3be389c23f
3 changed files with 17 additions and 47 deletions

View file

@ -53,6 +53,7 @@ class DevicehubConfig(Config):
"""The minimum version of ereuse.org workbench that this devicehub """The minimum version of ereuse.org workbench that this devicehub
accepts. we recommend not changing this value. accepts. we recommend not changing this value.
""" """
WORKBENCH_LITE = ["2022.03"]
TMP_SNAPSHOTS = config('TMP_SNAPSHOTS', '/tmp/snapshots') TMP_SNAPSHOTS = config('TMP_SNAPSHOTS', '/tmp/snapshots')
TMP_LIVES = config('TMP_LIVES', '/tmp/lives') TMP_LIVES = config('TMP_LIVES', '/tmp/lives')

View file

@ -416,34 +416,6 @@ class Install(ActionWithOneDevice):
address = Integer(validate=OneOf({8, 16, 32, 64, 128, 256})) address = Integer(validate=OneOf({8, 16, 32, 64, 128, 256}))
class Snapshot_lite_data(MarshmallowSchema):
dmidecode = String(required=False)
hwinfo = String(required=False)
smart = String(required=False)
lshw = String(required=False)
class Snapshot_lite(MarshmallowSchema):
uuid = String(required=True)
version = String(required=True)
software = String(required=True)
wbid = String(required=True)
type = String(required=True)
timestamp = String(required=True)
data = Nested(Snapshot_lite_data)
@validates_schema
def validate_workbench_version(self, data: dict):
if data['version'] < app.config['MIN_WORKBENCH']:
raise ValidationError(
'Min. supported Workbench version is '
'{} but yours is {}.'.format(
app.config['MIN_WORKBENCH'], data['version']
),
field_names=['version'],
)
class Snapshot(ActionWithOneDevice): class Snapshot(ActionWithOneDevice):
__doc__ = m.Snapshot.__doc__ __doc__ = m.Snapshot.__doc__
""" """

View file

@ -7,13 +7,12 @@ from datetime import datetime
from flask import current_app as app from flask import current_app as app
from flask import g from flask import g
from flask.json import jsonify
from sqlalchemy.util import OrderedSet from sqlalchemy.util import OrderedSet
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.parser.parser import ParseSnapshotLsHw from ereuse_devicehub.parser.parser import ParseSnapshotLsHw
from ereuse_devicehub.resources.action.models import Snapshot from ereuse_devicehub.resources.action.models import Snapshot
from ereuse_devicehub.resources.action.schemas import Snapshot_lite from ereuse_devicehub.parser.schemas import Snapshot_lite
from ereuse_devicehub.resources.device.models import Computer from ereuse_devicehub.resources.device.models import Computer
from ereuse_devicehub.resources.enums import Severity, SnapshotSoftware from ereuse_devicehub.resources.enums import Severity, SnapshotSoftware
from ereuse_devicehub.resources.user.exceptions import InsufficientPermission from ereuse_devicehub.resources.user.exceptions import InsufficientPermission
@ -78,12 +77,13 @@ class SnapshotView:
self.tmp_snapshots = app.config['TMP_SNAPSHOTS'] self.tmp_snapshots = app.config['TMP_SNAPSHOTS']
self.path_snapshot = save_json(snapshot_json, self.tmp_snapshots, g.user.email) self.path_snapshot = save_json(snapshot_json, self.tmp_snapshots, g.user.email)
snapshot_json.pop('debug', None) snapshot_json.pop('debug', None)
if snapshot_json.get('version') in ["2022.03"]: version = snapshot_json.get('version')
if self.is_wb_lite_snapshot(version):
self.validate_json(snapshot_json) self.validate_json(snapshot_json)
self.response = self.build_lite() snapshot_json = self.build_lite()
else:
self.snapshot_json = resource_def.schema.load(snapshot_json) self.snapshot_json = resource_def.schema.load(snapshot_json)
self.response = self.build() self.response = self.build()
move_json(self.tmp_snapshots, self.path_snapshot, g.user.email) move_json(self.tmp_snapshots, self.path_snapshot, g.user.email)
def post(self): def post(self):
@ -134,15 +134,6 @@ class SnapshotView:
# Check ownership of (non-component) device to from current.user # Check ownership of (non-component) device to from current.user
if db_device.owner_id != g.user.id: if db_device.owner_id != g.user.id:
raise InsufficientPermission() raise InsufficientPermission()
# Compute ratings
# try:
# rate_computer, price = RateComputer.compute(db_device)
# except CannotRate:
# pass
# else:
# snapshot.actions.add(rate_computer)
# if price:
# snapshot.actions.add(price)
elif snapshot.software == SnapshotSoftware.WorkbenchAndroid: elif snapshot.software == SnapshotSoftware.WorkbenchAndroid:
pass # TODO try except to compute RateMobile pass # TODO try except to compute RateMobile
# Check if HID is null and add Severity:Warning to Snapshot # Check if HID is null and add Severity:Warning to Snapshot
@ -161,7 +152,13 @@ class SnapshotView:
self.snapshot_json = self.schema2.load(snapshot_json) self.snapshot_json = self.schema2.load(snapshot_json)
def build_lite(self): def build_lite(self):
snap = ParseSnapshotLsHw(self.snapshot_json)
# snap = ParseSnapshot(self.snapshot_json) # snap = ParseSnapshot(self.snapshot_json)
self.snapshot_json = self.resource_def.schema.load(snap.snapshot_json) snap = ParseSnapshotLsHw(self.snapshot_json)
return self.build() return snap.snapshot_json
def is_wb_lite_snapshot(self, version: str) -> bool:
is_lite = False
if version in app.config['WORKBENCH_LITE']:
is_lite = True
return is_lite