clean actionView of Snapshot section put in other class

This commit is contained in:
Cayo Puigdefabregas 2021-04-22 13:40:45 +02:00
parent 39dc489cb3
commit e2fbe87c6a
1 changed files with 43 additions and 29 deletions

View File

@ -16,8 +16,6 @@ from teal.db import ResourceNotFound
from ereuse_devicehub.db import db from ereuse_devicehub.db import db
from ereuse_devicehub.query import things_response 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, from ereuse_devicehub.resources.action.models import (Action, RateComputer, Snapshot, VisualTest,
InitTransfer, Live, Allocate, Deallocate, InitTransfer, Live, Allocate, Deallocate,
Trade, Confirm) Trade, Confirm)
@ -242,21 +240,20 @@ class ActionView(View):
# defs # defs
resource_def = app.resources[json['type']] resource_def = app.resources[json['type']]
if json['type'] == Snapshot.t: if json['type'] == Snapshot.t:
tmp_snapshots = app.config['TMP_SNAPSHOTS'] snapshot = SnapshotView(json, resource_def, self.schema)
path_snapshot = save_json(json, tmp_snapshots, g.user.email) return snapshot.post()
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
if json['type'] == VisualTest.t: if json['type'] == VisualTest.t:
pass pass
# TODO JN add compute rate with new visual test and old components device # TODO JN add compute rate with new visual test and old components device
if json['type'] == InitTransfer.t: if json['type'] == InitTransfer.t:
return self.transfer_ownership() return self.transfer_ownership()
if json['type'] == Trade.t: if json['type'] == Trade.t:
offer = OfferView(json) offer = OfferView(json, resource_def, self.schema)
return offer.post() return offer.post()
a = resource_def.schema.load(json) a = resource_def.schema.load(json)
Model = db.Model._decl_class_registry.data[json['type']]() Model = db.Model._decl_class_registry.data[json['type']]()
action = Model(**a) action = Model(**a)
@ -272,22 +269,42 @@ class ActionView(View):
action = Action.query.filter_by(id=id).one() action = Action.query.filter_by(id=id).one()
return self.schema.jsonify(action) return self.schema.jsonify(action)
def snapshot(self, snapshot_json: dict, resource_def): def transfer_ownership(self):
"""Performs a Snapshot. """Perform a InitTransfer action to change author_id of device"""
pass
See `Snapshot` section in docs for more info. class SnapshotView():
""" """Performs a Snapshot.
# 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
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 components = None
if snapshot_json['software'] == (SnapshotSoftware.Workbench or SnapshotSoftware.WorkbenchAndroid): if self.snapshot_json['software'] == (SnapshotSoftware.Workbench or SnapshotSoftware.WorkbenchAndroid):
components = snapshot_json.pop('components', None) # type: List[Component] components = self.snapshot_json.pop('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)
snapshot = Snapshot(**snapshot_json) snapshot = Snapshot(**self.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)
@ -299,7 +316,7 @@ class ActionView(View):
assert not device.actions_one assert not device.actions_one
assert all(not c.actions_one for c in components) if components else True 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 del device # Do not use device anymore
snapshot.device = db_device snapshot.device = db_device
@ -341,16 +358,13 @@ class ActionView(View):
db.session.commit() db.session.commit()
return ret return ret
def transfer_ownership(self):
"""Perform a InitTransfer action to change author_id of device"""
pass
class OfferView(): class OfferView():
"""Handler for manager the offer/trade action register from post""" """Handler for manager the offer/trade action register from post"""
def __init__(self, data): def __init__(self, data, resource_def, schema):
a = s_Trade().load(data) self.schema = schema
a = resource_def.schema.load(data)
self.offer = Trade(**a) self.offer = Trade(**a)
self.create_first_confirmation() self.create_first_confirmation()
self.create_phantom_account() self.create_phantom_account()
@ -359,7 +373,7 @@ class OfferView():
def post(self): def post(self):
db.session().final_flush() db.session().final_flush()
ret = s_Action().jsonify(self.offer) ret = self.schema.jsonify(self.offer)
ret.status_code = 201 ret.status_code = 201
db.session.commit() db.session.commit()
return ret return ret