diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 61a1b3df..9e14ca13 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -2,7 +2,6 @@ import copy import csv import datetime import json -import uuid from json.decoder import JSONDecodeError import pandas as pd @@ -204,7 +203,7 @@ class FilterForm(FlaskForm): if filter_type: self.devices = self.devices.filter(Device.type.in_(filter_type)) - return self.devices.filter(Device.active == True).order_by( + return self.devices.filter(Device.active.is_(False)).order_by( Device.updated.desc() ) @@ -1735,22 +1734,21 @@ class UserTrustsForm(FlaskForm): try: return self._unic except Exception: - self._unic = ( + self._devices = ( Device.query.filter_by( hid=self.device.hid, owner=g.user, placeholder=None, active=True - ).count() - < 2 + ) + .order_by(Device.updated.asc()) + .all() ) + self._unic = len(self._devices) < 2 return self._unic def show(self): if not self.snapshot or not self.device: return False - if not self.snapshot.active: - return False - if not hasattr(self.device, 'system_uuid'): return False @@ -1762,6 +1760,8 @@ class UserTrustsForm(FlaskForm): return True if not self.unic(): + if self.device == self._devices[0]: + return False # To do merge return True diff --git a/ereuse_devicehub/migrations/versions/93daff872771_add_hash_hid_to_device.py b/ereuse_devicehub/migrations/versions/93daff872771_add_hash_hid_to_device.py index e274496e..9b33ca95 100644 --- a/ereuse_devicehub/migrations/versions/93daff872771_add_hash_hid_to_device.py +++ b/ereuse_devicehub/migrations/versions/93daff872771_add_hash_hid_to_device.py @@ -39,7 +39,6 @@ def upgrade_data(): sql = f"update {get_inv()}.device set chid='{chid}' where id={dev_id};" con.execute(sql) - sql = f"update {get_inv()}.snapshot set active=true;" con.execute(sql) @@ -56,19 +55,11 @@ def upgrade(): schema=f'{get_inv()}', ) - op.add_column( - 'snapshot', - sa.Column('active', sa.Boolean(), default=True, nullable=True), - schema=f'{get_inv()}', - ) - upgrade_data() op.alter_column('computer', 'user_trusts', nullable=False, schema=f'{get_inv()}') - op.alter_column('snapshot', 'active', nullable=False, schema=f'{get_inv()}') def downgrade(): op.drop_column('computer', 'user_trusts', schema=f'{get_inv()}') op.drop_column('device', 'chid', schema=f'{get_inv()}') - op.drop_column('snapshot', 'active', schema=f'{get_inv()}') diff --git a/ereuse_devicehub/parser/models.py b/ereuse_devicehub/parser/models.py index 85eb8251..79414270 100644 --- a/ereuse_devicehub/parser/models.py +++ b/ereuse_devicehub/parser/models.py @@ -39,8 +39,6 @@ class SnapshotsLog(Thing): def get_status(self): if self.snapshot: - if not self.snapshot.active: - return Severity(2) return Severity(self.severity) return '' @@ -72,9 +70,6 @@ class SnapshotsLog(Thing): if not self.snapshot: return '' - if not self.snapshot.active: - return '' - if not self.snapshot.device: return '' diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index b0ec76c6..1f373962 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -48,17 +48,14 @@ from sqlalchemy.util import OrderedSet from teal.db import ( CASCADE_OWN, INHERIT_COND, - IP, POLYMORPHIC_ID, POLYMORPHIC_ON, URL, - ResourceNotFound, StrictVersionType, check_lower, check_range, ) -from teal.enums import Country, Currency, Subdivision -from teal.marshmallow import ValidationError +from teal.enums import Currency from teal.resource import url_for_resource from ereuse_devicehub.db import db @@ -678,7 +675,6 @@ class Snapshot(JoinedWithOneDeviceMixin, ActionWithOneDevice): sid = Column(CIText(), nullable=True) settings_version = Column(CIText(), nullable=True) is_server_erase = Column(Boolean(), nullable=True) - active = Column(Boolean(), default=True, nullable=False) def get_last_lifetimes(self): """We get the lifetime and serial_number of the first disk""" diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 78bae1d9..378613df 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -275,8 +275,7 @@ class Device(Thing): for ac in actions_one: ac.real_created = ac.created - if ac.type != 'Snapshot' or ac.active: - actions.append(ac) + actions.append(ac) return sorted(actions, key=lambda x: x.real_created) @@ -996,15 +995,8 @@ class Device(Thing): if not snapshot1: return - self.reset_components(snapshot1) - return - def reset_components(self, snapshot): - for c in snapshot.components: - if c.parent is None: - c.parent = snapshot.device - def __lt__(self, other): return self.id < other.id diff --git a/tests/test_render_2_0.py b/tests/test_render_2_0.py index f6d21225..ac8c6887 100644 --- a/tests/test_render_2_0.py +++ b/tests/test_render_2_0.py @@ -2677,8 +2677,10 @@ def test_system_uuid_motherboard(user3: UserClientFlask): @pytest.mark.mvp @pytest.mark.usefixtures(conftest.app_context.__name__) def test_unreliable_device(user3: UserClientFlask): + # Create device snapshot = create_device(user3, 'real-eee-1001pxd.snapshot.12.json') + # Update device uri = '/inventory/upload-snapshot/' file_name = 'real-eee-1001pxd.snapshot.12' snapshot_json = conftest.yaml2json(file_name) @@ -2694,7 +2696,13 @@ def test_unreliable_device(user3: UserClientFlask): user3.post(uri, data=data, content_type="multipart/form-data") snapshot2 = Snapshot.query.filter_by(uuid=snapshot_json['uuid']).first() assert snapshot2.device == snapshot.device + assert Snapshot.query.count() == 2 + snapshots = Snapshot.query.all() + assert snapshots[0].device == snapshots[1].device + assert len(snapshots[0].device.components) + assert snapshot2 in snapshots + # Change update for new device uuid2 = snapshot2.uuid uri = f"/inventory/snapshots/{uuid2}/" user3.get(uri) @@ -2706,8 +2714,63 @@ def test_unreliable_device(user3: UserClientFlask): assert Device.query.filter_by(hid=snapshot.device.hid).count() == 2 user3.post(uri, data=data) assert Device.query.filter_by(hid=snapshot.device.hid).count() == 4 - assert Snapshot.query.count() == 3 + assert Snapshot.query.count() == 2 + snapshots = Snapshot.query.all() + assert snapshot2 not in snapshots + assert snapshots[0].device != snapshots[1].device + assert len(snapshots[0].device.components) == 4 + assert len(snapshots[1].device.components) == 9 + assert len(snapshots[0].device.actions) == 11 + assert len(snapshots[1].device.actions) == 10 - import pdb - pdb.set_trace() +@pytest.mark.mvp +@pytest.mark.usefixtures(conftest.app_context.__name__) +def test_reliable_device(user3: UserClientFlask): + # Create device + snapshot = create_device(user3, 'real-eee-1001pxd.snapshot.12.json') + + # Update device + uri = '/inventory/upload-snapshot/' + file_name = 'real-eee-1001pxd.snapshot.12' + snapshot_json = conftest.yaml2json(file_name) + snapshot_json['uuid'] = 'c058e8d2-fb92-47cb-a4b7-522b75561136' + b_snapshot = bytes(json.dumps(snapshot_json), 'utf-8') + file_snap = (BytesIO(b_snapshot), file_name) + user3.get(uri) + + data = { + 'snapshot': file_snap, + 'csrf_token': generate_csrf(), + } + user3.post(uri, data=data, content_type="multipart/form-data") + snapshot2 = Snapshot.query.filter_by(uuid=snapshot_json['uuid']).first() + + # Change update for new device + uuid2 = snapshot2.uuid + uri = f"/inventory/snapshots/{uuid2}/" + user3.get(uri) + + data = { + 'snapshot_type': "new_device", + 'csrf_token': generate_csrf(), + } + user3.post(uri, data=data) + + # Change update for update + snapshot3 = Snapshot.query.all()[-1] + uuid3 = snapshot3.uuid + uri = f"/inventory/snapshots/{uuid3}/" + user3.get(uri) + + data = { + 'snapshot_type': "update", + 'csrf_token': generate_csrf(), + } + assert Device.query.filter_by(hid=snapshot.device.hid).count() == 4 + user3.post(uri, data=data) + assert Device.query.filter_by(hid=snapshot.device.hid).count() == 2 + assert Snapshot.query.count() == 1 + assert Snapshot.query.first() == snapshot + assert len(snapshot.device.components) == 4 + assert len(snapshot.device.actions) == 4