add components as placeholder

This commit is contained in:
Cayo Puigdefabregas 2022-07-12 11:23:55 +02:00
parent a21018a2d2
commit e3ca632177
2 changed files with 22 additions and 17 deletions

View File

@ -858,7 +858,7 @@ class Placeholder(Thing):
binding = db.relationship( binding = db.relationship(
Device, Device,
backref=backref('binding', lazy=True, uselist=False), backref=backref('binding', lazy=True, uselist=False),
primaryjoin=device_id == Device.id, primaryjoin=binding_id == Device.id,
) )
binding_id.comment = "binding placeholder with workbench device" binding_id.comment = "binding placeholder with workbench device"

View File

@ -1,3 +1,4 @@
import copy
import difflib import difflib
from contextlib import suppress from contextlib import suppress
from itertools import groupby from itertools import groupby
@ -234,6 +235,7 @@ class Sync:
else: # Device is new and tags are not linked to a device else: # Device is new and tags are not linked to a device
device.tags.clear() # We don't want to add the transient dummy tags device.tags.clear() # We don't want to add the transient dummy tags
db.session.add(device) db.session.add(device)
self.create_placeholder(device)
db_device = device db_device = device
db_device.tags |= ( db_device.tags |= (
tags # Union of tags the device had plus the (potentially) new ones tags # Union of tags the device had plus the (potentially) new ones
@ -278,22 +280,25 @@ class Sync:
if hasattr(device, 'system_uuid') and device.system_uuid: if hasattr(device, 'system_uuid') and device.system_uuid:
db_device.system_uuid = device.system_uuid db_device.system_uuid = device.system_uuid
if device.placeholder and db_device.placeholder: @staticmethod
db_device.placeholder.pallet = device.placeholder.pallet def create_placeholder(device: Device):
db_device.placeholder.info = device.placeholder.info """If the device is new, we need create automaticaly a new placeholder"""
db_device.placeholder.id_device_supplier = ( # import pdb; pdb.set_trace()
device.placeholder.id_device_supplier dict_device = copy.copy(device.__dict__)
) dict_device.pop('_sa_instance_state')
db_device.sku = device.sku dev_placeholder = device.__class__(**dict_device)
db_device.image = device.image for c in device.components:
db_device.brand = device.brand c_dict = copy.copy(c.__dict__)
db_device.generation = device.generation c_dict.pop('_sa_instance_state')
db_device.variant = device.variant c_placeholder = c.__class__(**c_dict)
db_device.version = device.version c_placeholder.parent = dev_placeholder
db_device.width = device.width component_placeholder = Placeholder(device=c_placeholder, binding=c)
db_device.height = device.height db.session.add(c_placeholder)
db_device.depth = device.depth db.session.add(component_placeholder)
db_device.weight = device.weight
placeholder = Placeholder(device=dev_placeholder, binding=device)
db.session.add(dev_placeholder)
db.session.add(placeholder)
@staticmethod @staticmethod
def add_remove(device: Computer, components: Set[Component]) -> OrderedSet: def add_remove(device: Computer, components: Set[Component]) -> OrderedSet: