create device

This commit is contained in:
Cayo Puigdefabregas 2023-01-11 17:17:50 +01:00
parent 9603ac1f43
commit bc12258db0
1 changed files with 34 additions and 2 deletions

View File

@ -1,6 +1,9 @@
import copy import copy
import hashlib import hashlib
import json
import os
import pathlib import pathlib
import uuid
from contextlib import suppress from contextlib import suppress
from fractions import Fraction from fractions import Fraction
from itertools import chain from itertools import chain
@ -879,25 +882,54 @@ class Device(Thing):
def unreliable(self): def unreliable(self):
self.user_trusts = False self.user_trusts = False
tmp_snapshots = app.config['TMP_SNAPSHOTS']
i = 0 i = 0
snapshot1 = None snapshot1 = None
snapshots = {}
for ac in self.actions: for ac in self.actions:
if ac.type == 'Snapshot': if ac.type == 'Snapshot':
if i == 0: if i == 0:
snapshot1 = ac snapshot1 = ac
if i > 0: if i > 0:
ac.active = False snapshots[ac] = self.get_snapshot_file(ac)
# ac.active = False
i += 1 i += 1
if not snapshot1: if not snapshot1:
return return
self.create_new_device(snapshots.values())
# [self.remove_snapshot(ac) for ac in snapshots.keys()]
self.reset_components(snapshot1) self.reset_components(snapshot1)
return return
def get_snapshot_file(self, action):
uuid = action.uuid
user = g.user.email
name_file = f"*_{user}_{uuid}.json"
tmp_snapshots = app.config['TMP_SNAPSHOTS']
path_dir_base = os.path.join(tmp_snapshots, user)
for _file in pathlib.Path(path_dir_base).glob(name_file):
with open(_file) as file_snapshot:
snapshot = file_snapshot.read()
return json.loads(snapshot)
def create_new_device(self, snapshots):
from ereuse_devicehub.inventory.forms import UploadSnapshotForm
new_snapshots = []
for snapshot in snapshots:
snapshot['uuid'] = str(uuid.uuid4())
filename = "{}.json".format(snapshot['uuid'])
new_snapshots.append((filename, snapshot))
form = UploadSnapshotForm()
form.result = {}
form.snapshots = new_snapshots
form.save(commit=False)
def reliable(self): def reliable(self):
# self.user_trusts = True # self.user_trusts = True
computers = Computer.query.filter_by( computers = Computer.query.filter_by(