refactor build in form snapshot
This commit is contained in:
parent
545a1013e9
commit
a8e05d76ae
|
@ -50,9 +50,22 @@ class InventoryView(LoginMix, SnapshotMix):
|
|||
snapshot_json = json.loads(request.data)
|
||||
self.tmp_snapshots = app.config['TMP_SNAPSHOTS']
|
||||
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:
|
||||
snapshot_json = schema.load(snapshot_json)
|
||||
return self.schema.load(snapshot_json)
|
||||
except ValidationError as err:
|
||||
txt = "{}".format(err)
|
||||
uuid = snapshot_json.get('uuid')
|
||||
|
@ -61,15 +74,6 @@ class InventoryView(LoginMix, SnapshotMix):
|
|||
)
|
||||
error.save(commit=True)
|
||||
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'))
|
||||
|
|
|
@ -32,7 +32,11 @@ from ereuse_devicehub.parser.parser import ParseSnapshotLsHw
|
|||
from ereuse_devicehub.parser.schemas import Snapshot_lite
|
||||
from ereuse_devicehub.resources.action.models import Snapshot, Trade
|
||||
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 (
|
||||
SAI,
|
||||
Cellphone,
|
||||
|
@ -195,10 +199,13 @@ class LotForm(FlaskForm):
|
|||
return self.instance
|
||||
|
||||
|
||||
class UploadSnapshotForm(FlaskForm):
|
||||
class UploadSnapshotForm(FlaskForm, SnapshotMix):
|
||||
snapshot = MultipleFileField('Select a Snapshot File', [validators.DataRequired()])
|
||||
|
||||
def validate(self, extra_validators=None):
|
||||
import pdb
|
||||
|
||||
pdb.set_trace()
|
||||
is_valid = super().validate(extra_validators)
|
||||
|
||||
if not is_valid:
|
||||
|
@ -245,18 +252,16 @@ class UploadSnapshotForm(FlaskForm):
|
|||
def save(self, commit=True):
|
||||
if any([x == 'Error' for x in self.result.values()]):
|
||||
return
|
||||
self.sync = Sync()
|
||||
schema = SnapshotSchema()
|
||||
schema_lite = Snapshot_lite()
|
||||
self.tmp_snapshots = app.config['TMP_SNAPSHOTS']
|
||||
for filename, snapshot_json in self.snapshots:
|
||||
path_snapshot = save_json(snapshot_json, self.tmp_snapshots, g.user.email)
|
||||
snapshot_json.pop('debug', None)
|
||||
version = snapshot_json.get('version')
|
||||
version = snapshot_json.get('schema_version')
|
||||
if self.is_wb_lite_snapshot(version):
|
||||
self.snapshot_json = schema_lite.load(snapshot_json)
|
||||
snap = ParseSnapshotLsHw(self.snapshot_json)
|
||||
snapshot_json = snap.snapshot_json
|
||||
snapshot_json = ParseSnapshotLsHw(self.snapshot_json).snapshot_json
|
||||
|
||||
try:
|
||||
snapshot_json = schema.load(snapshot_json)
|
||||
|
@ -271,6 +276,7 @@ class UploadSnapshotForm(FlaskForm):
|
|||
continue
|
||||
|
||||
response = self.build(snapshot_json)
|
||||
db.session.add(response)
|
||||
|
||||
if hasattr(response, 'type'):
|
||||
self.result[filename] = 'Ok'
|
||||
|
@ -283,7 +289,7 @@ class UploadSnapshotForm(FlaskForm):
|
|||
db.session.commit()
|
||||
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
|
||||
device = snapshot_json.pop('device') # type: Computer
|
||||
components = None
|
||||
|
|
|
@ -5,6 +5,7 @@ from enum import Enum, unique
|
|||
|
||||
from dmidecode import DMIParse
|
||||
|
||||
from marshmallow import ValidationError
|
||||
from ereuse_devicehub.parser import base2
|
||||
from ereuse_devicehub.parser.computer import Computer
|
||||
from ereuse_devicehub.parser.models import SnapshotErrors
|
||||
|
@ -352,7 +353,16 @@ class ParseSnapshotLsHw:
|
|||
}
|
||||
|
||||
def get_snapshot(self):
|
||||
try:
|
||||
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):
|
||||
hw_blocks = self.hwinfo_raw.split("\n\n")
|
||||
|
|
|
@ -13,12 +13,12 @@ from sqlalchemy.util import OrderedSet
|
|||
from ereuse_devicehub.db import db
|
||||
from ereuse_devicehub.parser.models import SnapshotErrors
|
||||
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.device.models import Computer
|
||||
from ereuse_devicehub.resources.device.sync import Sync
|
||||
from ereuse_devicehub.resources.enums import Severity, SnapshotSoftware
|
||||
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):
|
||||
|
@ -67,19 +67,19 @@ def move_json(tmp_snapshots, path_name, user, live=False):
|
|||
class SnapshotMix:
|
||||
sync = Sync()
|
||||
|
||||
def build(self):
|
||||
device = self.snapshot_json.pop('device') # type: Computer
|
||||
def build(self, snapshot_json=None): # noqa: C901
|
||||
if not snapshot_json:
|
||||
snapshot_json = self.snapshot_json
|
||||
device = snapshot_json.pop('device') # type: Computer
|
||||
components = None
|
||||
if self.snapshot_json['software'] == (
|
||||
if snapshot_json['software'] == (
|
||||
SnapshotSoftware.Workbench or SnapshotSoftware.WorkbenchAndroid
|
||||
):
|
||||
components = self.snapshot_json.pop(
|
||||
'components', None
|
||||
) # type: List[Component]
|
||||
components = snapshot_json.pop('components', None) # type: List[Component]
|
||||
if isinstance(device, Computer) and device.hid:
|
||||
device.add_mac_to_hid(components_snap=components)
|
||||
# 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
|
||||
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.path_snapshot = save_json(snapshot_json, self.tmp_snapshots, g.user.email)
|
||||
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:
|
||||
self.snapshot_json = resource_def.schema.load(snapshot_json)
|
||||
except ValidationError as err:
|
||||
|
@ -164,19 +159,3 @@ class SnapshotView(SnapshotMix):
|
|||
|
||||
def post(self):
|
||||
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
|
||||
|
|
Reference in New Issue