From e2fbe87c6ae5f2f2a56ce6e2611cf551a9912c13 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 22 Apr 2021 13:40:45 +0200 Subject: [PATCH] clean actionView of Snapshot section put in other class --- ereuse_devicehub/resources/action/views.py | 72 +++++++++++++--------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/ereuse_devicehub/resources/action/views.py b/ereuse_devicehub/resources/action/views.py index 286b13f2..56d5882f 100644 --- a/ereuse_devicehub/resources/action/views.py +++ b/ereuse_devicehub/resources/action/views.py @@ -16,8 +16,6 @@ from teal.db import ResourceNotFound from ereuse_devicehub.db import db from ereuse_devicehub.query import things_response -from ereuse_devicehub.resources.action.schemas import Action as s_Action -from ereuse_devicehub.resources.action.schemas import Trade as s_Trade from ereuse_devicehub.resources.action.models import (Action, RateComputer, Snapshot, VisualTest, InitTransfer, Live, Allocate, Deallocate, Trade, Confirm) @@ -242,21 +240,20 @@ class ActionView(View): # defs resource_def = app.resources[json['type']] if json['type'] == Snapshot.t: - tmp_snapshots = app.config['TMP_SNAPSHOTS'] - path_snapshot = save_json(json, tmp_snapshots, g.user.email) - json.pop('debug', None) - a = resource_def.schema.load(json) - response = self.snapshot(a, resource_def) - move_json(tmp_snapshots, path_snapshot, g.user.email) - return response + snapshot = SnapshotView(json, resource_def, self.schema) + return snapshot.post() + if json['type'] == VisualTest.t: pass # TODO JN add compute rate with new visual test and old components device + if json['type'] == InitTransfer.t: return self.transfer_ownership() + if json['type'] == Trade.t: - offer = OfferView(json) + offer = OfferView(json, resource_def, self.schema) return offer.post() + a = resource_def.schema.load(json) Model = db.Model._decl_class_registry.data[json['type']]() action = Model(**a) @@ -272,22 +269,42 @@ class ActionView(View): action = Action.query.filter_by(id=id).one() return self.schema.jsonify(action) - def snapshot(self, snapshot_json: dict, resource_def): - """Performs a Snapshot. + def transfer_ownership(self): + """Perform a InitTransfer action to change author_id of device""" + pass + - See `Snapshot` section in docs for more info. - """ - # Note that if we set the device / components into the snapshot - # model object, when we flush them to the db we will flush - # snapshot, and we want to wait to flush snapshot at the end +class SnapshotView(): + """Performs a Snapshot. - device = snapshot_json.pop('device') # type: Computer + See `Snapshot` section in docs for more info. + """ + # Note that if we set the device / components into the snapshot + # model object, when we flush them to the db we will flush + # snapshot, and we want to wait to flush snapshot at the end + + def __init__(self, snapshot_json: dict, resource_def, schema): + self.schema = schema + self.snapshot_json = snapshot_json + self.resource_def = resource_def + 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) + self.snapshot_json = resource_def.schema.load(snapshot_json) + self.response = self.build() + move_json(self.tmp_snapshots, self.path_snapshot, g.user.email) + + def post(self): + return self.response + + def build(self): + device = self.snapshot_json.pop('device') # type: Computer components = None - if snapshot_json['software'] == (SnapshotSoftware.Workbench or SnapshotSoftware.WorkbenchAndroid): - components = snapshot_json.pop('components', None) # type: List[Component] + if self.snapshot_json['software'] == (SnapshotSoftware.Workbench or SnapshotSoftware.WorkbenchAndroid): + components = self.snapshot_json.pop('components', None) # type: List[Component] if isinstance(device, Computer) and device.hid: device.add_mac_to_hid(components_snap=components) - snapshot = Snapshot(**snapshot_json) + snapshot = Snapshot(**self.snapshot_json) # Remove new actions from devices so they don't interfere with sync actions_device = set(e for e in device.actions_one) @@ -299,7 +316,7 @@ class ActionView(View): assert not device.actions_one assert all(not c.actions_one for c in components) if components else True - db_device, remove_actions = resource_def.sync.run(device, components) + db_device, remove_actions = self.resource_def.sync.run(device, components) del device # Do not use device anymore snapshot.device = db_device @@ -341,16 +358,13 @@ class ActionView(View): db.session.commit() return ret - def transfer_ownership(self): - """Perform a InitTransfer action to change author_id of device""" - pass - class OfferView(): """Handler for manager the offer/trade action register from post""" - def __init__(self, data): - a = s_Trade().load(data) + def __init__(self, data, resource_def, schema): + self.schema = schema + a = resource_def.schema.load(data) self.offer = Trade(**a) self.create_first_confirmation() self.create_phantom_account() @@ -359,7 +373,7 @@ class OfferView(): def post(self): db.session().final_flush() - ret = s_Action().jsonify(self.offer) + ret = self.schema.jsonify(self.offer) ret.status_code = 201 db.session.commit() return ret