adding the directory structure for save all jsons

This commit is contained in:
Cayo Puigdefabregas 2020-10-29 16:09:02 +01:00
parent 785d8091ea
commit 5f68803d9f
1 changed files with 22 additions and 6 deletions

View File

@ -2,6 +2,7 @@
import os import os
import json import json
import shutil
from datetime import datetime from datetime import datetime
from distutils.version import StrictVersion from distutils.version import StrictVersion
from uuid import UUID from uuid import UUID
@ -35,10 +36,14 @@ def save_json(req_json, tmp_snapshots, user):
minutes = now.min minutes = now.min
name_file = f"{year}-{month}-{day}-{hour}-{minutes}_{user}_{uuid}.json" name_file = f"{year}-{month}-{day}-{hour}-{minutes}_{user}_{uuid}.json"
path_name = os.path.join(tmp_snapshots, name_file) path_dir_base = os.path.join(tmp_snapshots, user)
path_errors = os.path.join(path_dir_base, 'errors')
path_fixeds = os.path.join(path_dir_base, 'fixeds')
path_name = os.path.join(path_errors, name_file)
if not os.path.isdir(tmp_snapshots): if not os.path.isdir(path_dir_base):
os.system('mkdir -p {}'.format(tmp_snapshots)) os.system(f'mkdir -p {path_errors}')
os.system(f'mkdir -p {path_fixeds}')
with open(path_name, 'w') as snapshot_file: with open(path_name, 'w') as snapshot_file:
snapshot_file.write(json.dumps(req_json)) snapshot_file.write(json.dumps(req_json))
@ -46,12 +51,23 @@ def save_json(req_json, tmp_snapshots, user):
return path_name return path_name
def move_json(tmp_snapshots, path_name, user):
"""
This function move the json than it's correct
"""
path_dir_base = os.path.join(tmp_snapshots, user)
if os.path.isfile(path_name):
shutil.copy(path_name, path_dir_base)
os.remove(path_name)
class ActionView(View): class ActionView(View):
def post(self): def post(self):
"""Posts an action.""" """Posts an action."""
json = request.get_json(validate=False) json = request.get_json(validate=False)
tmp_snapshots = app.config['TMP_SNAPSHOTS'] tmp_snapshots = app.config['TMP_SNAPSHOTS']
path_snapshot = save_json(json, tmp_snapshots, g.user.email) path_snapshot = save_json(json, tmp_snapshots, g.user.email)
json.pop('debug', None)
if not json or 'type' not in json: if not json or 'type' not in json:
raise ValidationError('Resource needs a type.') raise ValidationError('Resource needs a type.')
# todo there should be a way to better get subclassess resource # todo there should be a way to better get subclassess resource
@ -60,13 +76,13 @@ class ActionView(View):
a = resource_def.schema.load(json) a = resource_def.schema.load(json)
if json['type'] == Snapshot.t: if json['type'] == Snapshot.t:
response = self.snapshot(a, resource_def) response = self.snapshot(a, resource_def)
os.remove(path_snapshot) move_json(tmp_snapshots, path_snapshot, g.user.email)
return response 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:
os.remove(path_snapshot) move_json(tmp_snapshots, path_snapshot, g.user.email)
return self.transfer_ownership() return self.transfer_ownership()
Model = db.Model._decl_class_registry.data[json['type']]() Model = db.Model._decl_class_registry.data[json['type']]()
action = Model(**a) action = Model(**a)
@ -75,7 +91,7 @@ class ActionView(View):
ret = self.schema.jsonify(action) ret = self.schema.jsonify(action)
ret.status_code = 201 ret.status_code = 201
db.session.commit() db.session.commit()
os.remove(path_snapshot) move_json(tmp_snapshots, path_snapshot, g.user.email)
return ret return ret
def one(self, id: UUID): def one(self, id: UUID):