From 9d7869929c578c5aeb7282da2fe0ce34fbab3e75 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 25 Oct 2022 11:10:39 +0200 Subject: [PATCH 1/2] fix disks without placeholder --- ereuse_devicehub/inventory/views.py | 6 +++++- ereuse_devicehub/templates/inventory/erasure_list.html | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 9682e0c1..567196eb 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -166,10 +166,14 @@ class DeviceDetailView(GenericMixin): ) form_tags = TagDeviceForm(dhid=id) + placeholder = device.binding or device.placeholder + if not placeholder: + return NotFound() + self.context.update( { 'device': device, - 'placeholder': device.binding or device.placeholder, + 'placeholder': placeholder, 'page_title': 'Device {}'.format(device.devicehub_id), 'form_tag_device': form_tags, } diff --git a/ereuse_devicehub/templates/inventory/erasure_list.html b/ereuse_devicehub/templates/inventory/erasure_list.html index 2d93d4bd..57f91b75 100644 --- a/ereuse_devicehub/templates/inventory/erasure_list.html +++ b/ereuse_devicehub/templates/inventory/erasure_list.html @@ -71,9 +71,13 @@ /> + {% if ac.device.phid() %} {{ ac.device.serial_number.upper() }} + {% else %} + {{ ac.device.serial_number.upper() }} + {% endif %} From 694663c4126934ca7aa10200523ffd2af0644a85 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 25 Oct 2022 15:09:03 +0200 Subject: [PATCH 2/2] add script for rebuild disks without placeholder --- scripts/datastorage_orphans.py | 63 ++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 scripts/datastorage_orphans.py diff --git a/scripts/datastorage_orphans.py b/scripts/datastorage_orphans.py new file mode 100644 index 00000000..72148e94 --- /dev/null +++ b/scripts/datastorage_orphans.py @@ -0,0 +1,63 @@ +import copy +import sys + +from flask import g + +from ereuse_devicehub.db import db +from ereuse_devicehub.devicehub import Devicehub +from ereuse_devicehub.resources.device.models import DataStorage, Placeholder +from ereuse_devicehub.resources.lot.models import LotDevice + + +def clone_device(device): + if device.phid(): + return + + g.user = device.owner + + old_devicehub_id = device.devicehub_id + + dict_device = copy.copy(device.__dict__) + dict_device.pop('_sa_instance_state') + dict_device.pop('id', None) + dict_device.pop('devicehub_id', None) + dict_device.pop('actions_multiple', None) + dict_device.pop('actions_one', None) + dict_device.pop('components', None) + dict_device.pop('tags', None) + dict_device.pop('system_uuid', None) + new_device = device.__class__(**dict_device) + new_device.devicehub_id = old_devicehub_id + device.devicehub_id = None + new_device.owner = device.owner + db.session.add(new_device) + + placeholder = Placeholder( + device=new_device, binding=device, is_abstract=True, owner_id=device.owner_id + ) + db.session.add(placeholder) + + tags = [x for x in device.tags] + for tag in tags: + tag.device = new_device + + lots = [x for x in device.lots] + for lot in lots: + for rel_lot in LotDevice.query.filter_by(lot_id=lot.id, device=device): + rel_lot.device = new_device + return new_device + + +def main(): + schema = sys.argv[1] + app = Devicehub(inventory=schema) + app.app_context().push() + for device in DataStorage.query.all(): + if not device.phid(): + clone_device(device) + + db.session.commit() + + +if __name__ == '__main__': + main()