refactor build in form snapshot

This commit is contained in:
Cayo Puigdefabregas 2022-04-08 11:12:17 +02:00
parent 545a1013e9
commit a8e05d76ae
4 changed files with 48 additions and 49 deletions

View File

@ -50,9 +50,22 @@ class InventoryView(LoginMix, SnapshotMix):
snapshot_json = json.loads(request.data) snapshot_json = json.loads(request.data)
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)
schema = Snapshot_lite() snapshot_json = self.validate(snapshot_json)
self.snapshot_json = ParseSnapshotLsHw(snapshot_json).get_snapshot()
snapshot = self.build()
db.session.add(snapshot)
db.session().final_flush()
db.session.commit()
self.response = self.schema.jsonify(snapshot)
self.response.status_code = 201
move_json(self.tmp_snapshots, self.path_snapshot, g.user.email)
return self.response
def validate(self, snapshot_json):
self.schema = Snapshot_lite()
try: try:
snapshot_json = schema.load(snapshot_json) return self.schema.load(snapshot_json)
except ValidationError as err: except ValidationError as err:
txt = "{}".format(err) txt = "{}".format(err)
uuid = snapshot_json.get('uuid') uuid = snapshot_json.get('uuid')
@ -61,15 +74,6 @@ class InventoryView(LoginMix, SnapshotMix):
) )
error.save(commit=True) error.save(commit=True)
raise err raise err
self.snapshot_json = ParseSnapshotLsHw(snapshot_json).get_snapshot()
snapshot = self.build()
db.session.add(snapshot)
db.session().final_flush()
db.session.commit()
self.response = schema.jsonify(snapshot)
self.response.status_code = 201
move_json(self.tmp_snapshots, self.path_snapshot, g.user.email)
return self.response
api.add_url_rule('/inventory/', view_func=InventoryView.as_view('inventory')) api.add_url_rule('/inventory/', view_func=InventoryView.as_view('inventory'))

View File

@ -32,7 +32,11 @@ from ereuse_devicehub.parser.parser import ParseSnapshotLsHw
from ereuse_devicehub.parser.schemas import Snapshot_lite from ereuse_devicehub.parser.schemas import Snapshot_lite
from ereuse_devicehub.resources.action.models import Snapshot, Trade from ereuse_devicehub.resources.action.models import Snapshot, Trade
from ereuse_devicehub.resources.action.schemas import Snapshot as SnapshotSchema from ereuse_devicehub.resources.action.schemas import Snapshot as SnapshotSchema
from ereuse_devicehub.resources.action.views.snapshot import move_json, save_json from ereuse_devicehub.resources.action.views.snapshot import (
SnapshotMix,
move_json,
save_json,
)
from ereuse_devicehub.resources.device.models import ( from ereuse_devicehub.resources.device.models import (
SAI, SAI,
Cellphone, Cellphone,
@ -195,10 +199,13 @@ class LotForm(FlaskForm):
return self.instance return self.instance
class UploadSnapshotForm(FlaskForm): class UploadSnapshotForm(FlaskForm, SnapshotMix):
snapshot = MultipleFileField('Select a Snapshot File', [validators.DataRequired()]) snapshot = MultipleFileField('Select a Snapshot File', [validators.DataRequired()])
def validate(self, extra_validators=None): def validate(self, extra_validators=None):
import pdb
pdb.set_trace()
is_valid = super().validate(extra_validators) is_valid = super().validate(extra_validators)
if not is_valid: if not is_valid:
@ -245,18 +252,16 @@ class UploadSnapshotForm(FlaskForm):
def save(self, commit=True): def save(self, commit=True):
if any([x == 'Error' for x in self.result.values()]): if any([x == 'Error' for x in self.result.values()]):
return return
self.sync = Sync()
schema = SnapshotSchema() schema = SnapshotSchema()
schema_lite = Snapshot_lite() schema_lite = Snapshot_lite()
self.tmp_snapshots = app.config['TMP_SNAPSHOTS'] self.tmp_snapshots = app.config['TMP_SNAPSHOTS']
for filename, snapshot_json in self.snapshots: for filename, snapshot_json in self.snapshots:
path_snapshot = save_json(snapshot_json, self.tmp_snapshots, g.user.email) path_snapshot = save_json(snapshot_json, self.tmp_snapshots, g.user.email)
snapshot_json.pop('debug', None) snapshot_json.pop('debug', None)
version = snapshot_json.get('version') version = snapshot_json.get('schema_version')
if self.is_wb_lite_snapshot(version): if self.is_wb_lite_snapshot(version):
self.snapshot_json = schema_lite.load(snapshot_json) self.snapshot_json = schema_lite.load(snapshot_json)
snap = ParseSnapshotLsHw(self.snapshot_json) snapshot_json = ParseSnapshotLsHw(self.snapshot_json).snapshot_json
snapshot_json = snap.snapshot_json
try: try:
snapshot_json = schema.load(snapshot_json) snapshot_json = schema.load(snapshot_json)
@ -271,6 +276,7 @@ class UploadSnapshotForm(FlaskForm):
continue continue
response = self.build(snapshot_json) response = self.build(snapshot_json)
db.session.add(response)
if hasattr(response, 'type'): if hasattr(response, 'type'):
self.result[filename] = 'Ok' self.result[filename] = 'Ok'
@ -283,7 +289,7 @@ class UploadSnapshotForm(FlaskForm):
db.session.commit() db.session.commit()
return response return response
def build(self, snapshot_json): # noqa: C901 def build2(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

View File

@ -5,6 +5,7 @@ from enum import Enum, unique
from dmidecode import DMIParse from dmidecode import DMIParse
from marshmallow import ValidationError
from ereuse_devicehub.parser import base2 from ereuse_devicehub.parser import base2
from ereuse_devicehub.parser.computer import Computer from ereuse_devicehub.parser.computer import Computer
from ereuse_devicehub.parser.models import SnapshotErrors from ereuse_devicehub.parser.models import SnapshotErrors
@ -352,7 +353,16 @@ class ParseSnapshotLsHw:
} }
def get_snapshot(self): def get_snapshot(self):
try:
return Snapshot().load(self.snapshot_json) return Snapshot().load(self.snapshot_json)
except ValidationError as err:
txt = "{}".format(err)
uuid = self.snapshot_json.get('uuid')
error = SnapshotErrors(
description=txt, snapshot_uuid=uuid, severity=Severity.Error
)
error.save(commit=True)
raise err
def parse_hwinfo(self): def parse_hwinfo(self):
hw_blocks = self.hwinfo_raw.split("\n\n") hw_blocks = self.hwinfo_raw.split("\n\n")

View File

@ -13,12 +13,12 @@ from sqlalchemy.util import OrderedSet
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.parser.models import SnapshotErrors from ereuse_devicehub.parser.models import SnapshotErrors
from ereuse_devicehub.parser.parser import ParseSnapshotLsHw from ereuse_devicehub.parser.parser import ParseSnapshotLsHw
from ereuse_devicehub.resources.api.schemas import Snapshot_lite from ereuse_devicehub.parser.schemas import Snapshot_lite
from ereuse_devicehub.resources.action.models import Snapshot from ereuse_devicehub.resources.action.models import Snapshot
from ereuse_devicehub.resources.device.models import Computer from ereuse_devicehub.resources.device.models import Computer
from ereuse_devicehub.resources.device.sync import Sync
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
from ereuse_devicehub.resources.device.sync import Sync
def save_json(req_json, tmp_snapshots, user, live=False): def save_json(req_json, tmp_snapshots, user, live=False):
@ -67,19 +67,19 @@ def move_json(tmp_snapshots, path_name, user, live=False):
class SnapshotMix: class SnapshotMix:
sync = Sync() sync = Sync()
def build(self): def build(self, snapshot_json=None): # noqa: C901
device = self.snapshot_json.pop('device') # type: Computer if not snapshot_json:
snapshot_json = self.snapshot_json
device = snapshot_json.pop('device') # type: Computer
components = None components = None
if self.snapshot_json['software'] == ( if snapshot_json['software'] == (
SnapshotSoftware.Workbench or SnapshotSoftware.WorkbenchAndroid SnapshotSoftware.Workbench or SnapshotSoftware.WorkbenchAndroid
): ):
components = self.snapshot_json.pop( components = snapshot_json.pop('components', None) # type: List[Component]
'components', None
) # type: List[Component]
if isinstance(device, Computer) and device.hid: if isinstance(device, Computer) and device.hid:
device.add_mac_to_hid(components_snap=components) device.add_mac_to_hid(components_snap=components)
# import pdb; pdb.set_trace() # import pdb; pdb.set_trace()
snapshot = Snapshot(**self.snapshot_json) snapshot = Snapshot(**snapshot_json)
# Remove new actions from devices so they don't interfere with sync # Remove new actions from devices so they don't interfere with sync
actions_device = set(e for e in device.actions_one) actions_device = set(e for e in device.actions_one)
@ -138,11 +138,6 @@ class SnapshotView(SnapshotMix):
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)
version = snapshot_json.get('version')
if self.is_wb_lite_snapshot(version):
self.validate_json(snapshot_json)
snapshot_json = self.build_lite()
try: try:
self.snapshot_json = resource_def.schema.load(snapshot_json) self.snapshot_json = resource_def.schema.load(snapshot_json)
except ValidationError as err: except ValidationError as err:
@ -164,19 +159,3 @@ class SnapshotView(SnapshotMix):
def post(self): def post(self):
return self.response return self.response
def validate_json(self, snapshot_json):
self.schema2 = Snapshot_lite()
self.snapshot_json = self.schema2.load(snapshot_json)
def build_lite(self):
# snap = ParseSnapshot(self.snapshot_json)
snap = ParseSnapshotLsHw(self.snapshot_json)
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