more simple patch in post view

This commit is contained in:
Cayo Puigdefabregas 2020-10-08 14:47:11 +02:00
parent 0e89af1215
commit 258c3b4efd
1 changed files with 62 additions and 61 deletions

View File

@ -1,6 +1,5 @@
import os
import json
import copy
from time import time
from distutils.version import StrictVersion
from typing import List
@ -8,6 +7,7 @@ from uuid import UUID
from flask import current_app as app, request, g
from sqlalchemy.util import OrderedSet
from marshmallow.exceptions import ValidationError as mValidationError
from teal.marshmallow import ValidationError
from teal.resource import View
@ -24,26 +24,28 @@ SUPPORTED_WORKBENCH = StrictVersion('11.0')
TMP_SNAPSHOTS = 'tmp/snapshots'
def save_snapshot_in_file(snapshot_json):
def save_json(req_json):
"""
This function allow save a snapshot in json format un a TMP_SNAPSHOTS directory
The file need to be saved with one name format with the stamptime and uuid joins
"""
name_file = "{uuid}_{time}.json".format(uuid=snapshot_json['uuid'], time=int(time()))
name_file = "{uuid}_{time}.json".format(uuid=req_json.get('uuid', ''), time=int(time()))
path_name = "{tmp}/{file}".format(tmp=TMP_SNAPSHOTS, file=name_file)
if not os.path.isdir(TMP_SNAPSHOTS):
os.system('mkdir -p {}'.format(TMP_SNAPSHOTS))
snapshot_file = open(path_name, 'w')
snapshot_file.write(json.dumps(snapshot_json))
snapshot_file.write(json.dumps(req_json))
snapshot_file.close()
return path_name
class ActionView(View):
def post(self):
"""Posts an action."""
json = request.get_json(validate=False)
path_snapshot = save_json(json)
if not json or 'type' not in json:
raise ValidationError('Resource needs a type.')
# todo there should be a way to better get subclassess resource
@ -51,11 +53,14 @@ class ActionView(View):
resource_def = app.resources[json['type']]
a = resource_def.schema.load(json)
if json['type'] == Snapshot.t:
return self.snapshot(a, resource_def)
response = self.snapshot(a, resource_def)
os.remove(path_snapshot)
return response
if json['type'] == VisualTest.t:
pass
# TODO JN add compute rate with new visual test and old components device
if json['type'] == InitTransfer.t:
os.remove(path_snapshot)
return self.transfer_ownership()
Model = db.Model._decl_class_registry.data[json['type']]()
action = Model(**a)
@ -64,6 +69,7 @@ class ActionView(View):
ret = self.schema.jsonify(action)
ret.status_code = 201
db.session.commit()
os.remove(path_snapshot)
return ret
def one(self, id: UUID):
@ -80,8 +86,6 @@ class ActionView(View):
# model object, when we flush them to the db we will flush
# snapshot, and we want to wait to flush snapshot at the end
snapshot_json_bakup = copy.copy(snapshot_json)
try:
device = snapshot_json.pop('device') # type: Computer
components = None
if snapshot_json['software'] == (SnapshotSoftware.Workbench or SnapshotSoftware.WorkbenchAndroid):
@ -138,9 +142,6 @@ class ActionView(View):
ret.status_code = 201
db.session.commit()
return ret
except Exception as err:
save_snapshot_in_file(snapshot_json_bakup)
raise err
def transfer_ownership(self):
"""Perform a InitTransfer action to change author_id of device"""