From 6e0f627aca2642e4d6c8c83a3ec3339412359847 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 20 Jan 2022 10:33:14 +0100 Subject: [PATCH] add more clear error message, add a check if exist this snapshot and put the commit at the end of the loop --- ereuse_devicehub/inventory/forms.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index f1064ea1..48a42928 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -5,6 +5,7 @@ from wtforms import StringField, validators, MultipleFileField from flask import g, request, app from sqlalchemy.util import OrderedSet from psycopg2.errors import UniqueViolation +from json.decoder import JSONDecodeError from ereuse_devicehub.db import db from ereuse_devicehub.resources.device.models import Device, Computer, Smartphone, Cellphone, \ @@ -107,9 +108,21 @@ class UploadSnapshotForm(FlaskForm): self.result[filename] = 'Not processed' d = d.stream.read() if not d: - self.result[filename] = 'Error' + self.result[filename] = 'Error this snapshot is empty' continue - self.snapshots.append((filename, json.loads(d))) + + try: + d_json = json.loads(d) + except JSONDecodeError: + self.result[filename] = 'Error this snapshot is not a json' + continue + + uuid_snapshot = d_json.get('uuid') + if Snapshot.query.filter(Snapshot.uuid == uuid_snapshot).all(): + self.result[filename] = 'Error this snapshot exist' + continue + + self.snapshots.append((filename, d_json)) if not self.snapshots: return False @@ -142,6 +155,8 @@ class UploadSnapshotForm(FlaskForm): move_json(self.tmp_snapshots, path_snapshot, g.user.email) + db.session.commit() + def build(self, snapshot_json): # this is a copy adaptated from ereuse_devicehub.resources.action.views.snapshot device = snapshot_json.pop('device') # type: Computer @@ -198,7 +213,6 @@ class UploadSnapshotForm(FlaskForm): snapshot.severity = Severity.Warning db.session.add(snapshot) - db.session.commit() return snapshot