2018-04-27 17:16:43 +00:00
|
|
|
from distutils.version import StrictVersion
|
2018-06-16 10:41:12 +00:00
|
|
|
from typing import List
|
2018-06-15 13:31:03 +00:00
|
|
|
from uuid import UUID
|
2018-04-27 17:16:43 +00:00
|
|
|
|
2018-08-03 16:15:08 +00:00
|
|
|
from flask import current_app as app, request
|
2018-05-30 10:49:40 +00:00
|
|
|
from sqlalchemy.util import OrderedSet
|
2019-01-16 19:40:27 +00:00
|
|
|
from teal.marshmallow import ValidationError
|
2018-09-07 10:35:32 +00:00
|
|
|
from teal.resource import View
|
2018-04-27 17:16:43 +00:00
|
|
|
|
|
|
|
from ereuse_devicehub.db import db
|
2019-12-12 20:17:35 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import Action, RateComputer, Snapshot, VisualTest, \
|
|
|
|
InitTransfer
|
2019-06-12 14:49:55 +00:00
|
|
|
from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate
|
2018-06-16 10:41:12 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Component, Computer
|
2018-07-14 14:41:22 +00:00
|
|
|
from ereuse_devicehub.resources.enums import SnapshotSoftware
|
2018-04-10 15:06:39 +00:00
|
|
|
|
2019-02-18 11:43:50 +00:00
|
|
|
SUPPORTED_WORKBENCH = StrictVersion('11.0')
|
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
class ActionView(View):
|
2018-08-03 16:15:08 +00:00
|
|
|
def post(self):
|
2019-05-11 14:27:22 +00:00
|
|
|
"""Posts an action."""
|
2018-08-03 16:15:08 +00:00
|
|
|
json = request.get_json(validate=False)
|
2019-02-18 11:43:50 +00:00
|
|
|
if not json or 'type' not in json:
|
2019-01-16 19:40:27 +00:00
|
|
|
raise ValidationError('Resource needs a type.')
|
2019-02-18 11:43:50 +00:00
|
|
|
# todo there should be a way to better get subclassess resource
|
|
|
|
# defs
|
|
|
|
resource_def = app.resources[json['type']]
|
2019-05-11 14:27:22 +00:00
|
|
|
a = resource_def.schema.load(json)
|
2019-02-18 11:43:50 +00:00
|
|
|
if json['type'] == Snapshot.t:
|
2019-05-11 14:27:22 +00:00
|
|
|
return self.snapshot(a, resource_def)
|
2019-05-14 18:31:43 +00:00
|
|
|
if json['type'] == VisualTest.t:
|
|
|
|
pass
|
|
|
|
# TODO JN add compute rate with new visual test and old components device
|
2019-12-12 20:17:35 +00:00
|
|
|
if json['type'] == InitTransfer.t:
|
|
|
|
return self.transfer_ownership()
|
2018-08-03 16:15:08 +00:00
|
|
|
Model = db.Model._decl_class_registry.data[json['type']]()
|
2019-05-11 14:27:22 +00:00
|
|
|
action = Model(**a)
|
|
|
|
db.session.add(action)
|
2019-02-04 17:20:50 +00:00
|
|
|
db.session().final_flush()
|
2019-05-11 14:27:22 +00:00
|
|
|
ret = self.schema.jsonify(action)
|
2018-08-03 16:15:08 +00:00
|
|
|
ret.status_code = 201
|
2019-02-04 17:20:50 +00:00
|
|
|
db.session.commit()
|
2018-08-03 16:15:08 +00:00
|
|
|
return ret
|
|
|
|
|
2018-06-15 13:31:03 +00:00
|
|
|
def one(self, id: UUID):
|
2019-05-11 14:27:22 +00:00
|
|
|
"""Gets one action."""
|
|
|
|
action = Action.query.filter_by(id=id).one()
|
|
|
|
return self.schema.jsonify(action)
|
2018-04-27 17:16:43 +00:00
|
|
|
|
2019-02-18 11:43:50 +00:00
|
|
|
def snapshot(self, snapshot_json: dict, resource_def):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Performs a Snapshot.
|
2018-05-13 13:13:12 +00:00
|
|
|
|
|
|
|
See `Snapshot` section in docs for more info.
|
|
|
|
"""
|
2018-04-30 17:58:19 +00:00
|
|
|
# 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
|
2019-02-18 11:43:50 +00:00
|
|
|
device = snapshot_json.pop('device') # type: Computer
|
|
|
|
components = None
|
|
|
|
if snapshot_json['software'] == SnapshotSoftware.Workbench:
|
|
|
|
components = snapshot_json.pop('components') # type: List[Component]
|
|
|
|
snapshot = Snapshot(**snapshot_json)
|
2018-06-16 10:41:12 +00:00
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
# Remove new actions from devices so they don't interfere with sync
|
|
|
|
actions_device = set(e for e in device.actions_one)
|
|
|
|
device.actions_one.clear()
|
2018-06-20 21:18:15 +00:00
|
|
|
if components:
|
2019-05-11 14:27:22 +00:00
|
|
|
actions_components = tuple(set(e for e in c.actions_one) for c in components)
|
2018-06-20 21:18:15 +00:00
|
|
|
for component in components:
|
2019-05-11 14:27:22 +00:00
|
|
|
component.actions_one.clear()
|
2018-06-16 10:41:12 +00:00
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
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)
|
2019-05-10 09:58:38 +00:00
|
|
|
del device # Do not use device anymore
|
2018-06-16 10:41:12 +00:00
|
|
|
snapshot.device = db_device
|
2019-05-11 14:27:22 +00:00
|
|
|
snapshot.actions |= remove_actions | actions_device # Set actions to snapshot
|
2018-05-13 13:13:12 +00:00
|
|
|
# commit will change the order of the components by what
|
2018-05-30 10:49:40 +00:00
|
|
|
# the DB wants. Let's get a copy of the list so we preserve order
|
|
|
|
ordered_components = OrderedSet(x for x in snapshot.components)
|
2018-06-16 10:41:12 +00:00
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
# Add the new actions to the db-existing devices and components
|
|
|
|
db_device.actions_one |= actions_device
|
2018-06-20 21:18:15 +00:00
|
|
|
if components:
|
2019-05-11 14:27:22 +00:00
|
|
|
for component, actions in zip(ordered_components, actions_components):
|
|
|
|
component.actions_one |= actions
|
|
|
|
snapshot.actions |= actions
|
2018-06-16 10:41:12 +00:00
|
|
|
|
2018-07-14 14:41:22 +00:00
|
|
|
# Compute ratings
|
2019-04-30 00:02:23 +00:00
|
|
|
if snapshot.software == SnapshotSoftware.Workbench:
|
|
|
|
try:
|
2019-05-10 09:58:38 +00:00
|
|
|
rate_computer, price = RateComputer.compute(db_device)
|
2019-04-30 00:02:23 +00:00
|
|
|
except CannotRate:
|
|
|
|
pass
|
|
|
|
else:
|
2019-05-11 14:27:22 +00:00
|
|
|
snapshot.actions.add(rate_computer)
|
2019-04-30 00:02:23 +00:00
|
|
|
if price:
|
2019-05-11 14:27:22 +00:00
|
|
|
snapshot.actions.add(price)
|
2019-04-30 00:02:23 +00:00
|
|
|
|
2018-04-27 17:16:43 +00:00
|
|
|
db.session.add(snapshot)
|
2019-02-04 17:20:50 +00:00
|
|
|
db.session().final_flush()
|
2018-05-11 16:58:48 +00:00
|
|
|
ret = self.schema.jsonify(snapshot) # transform it back
|
|
|
|
ret.status_code = 201
|
2019-02-04 17:20:50 +00:00
|
|
|
db.session.commit()
|
2018-05-11 16:58:48 +00:00
|
|
|
return ret
|
2019-12-12 20:17:35 +00:00
|
|
|
|
|
|
|
def transfer_ownership(self):
|
|
|
|
"""Perform a InitTransfer action to change author_id of device"""
|
|
|
|
pass
|