From fac6380bcf5cd5ac13edd83db66020daa8cd306c Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 12 Dec 2022 14:10:17 +0100 Subject: [PATCH] . --- ereuse_devicehub/resources/device/models.py | 15 +++++++++++++-- ereuse_devicehub/resources/device/schemas.py | 15 --------------- ereuse_devicehub/resources/device/sync.py | 15 +++------------ 3 files changed, 16 insertions(+), 29 deletions(-) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 82adf8e7..df331e70 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -745,7 +745,7 @@ class Device(Thing): return "" - def set_hid(self): + def set_new_hid(self): """ The order is important # product_vendor, is a manufacturer @@ -814,7 +814,18 @@ class Device(Thing): if self.hid: return hashlib.sha3_512(self.hid.encode()).hexdigest() - def set_old_hid(self): + def get_from_db(self): + if not self.hid: + return + + return Device.query.filter_by( + hid=self.hid, + owner_id=g.user.id, + active=True, + placeholder=None, + ).first() + + def set_hid(self): with suppress(TypeError): self.hid = Naming.hid( self.type, self.manufacturer, self.model, self.serial_number diff --git a/ereuse_devicehub/resources/device/schemas.py b/ereuse_devicehub/resources/device/schemas.py index e34502f5..76a1fee2 100644 --- a/ereuse_devicehub/resources/device/schemas.py +++ b/ereuse_devicehub/resources/device/schemas.py @@ -208,21 +208,6 @@ class Computer(Device): receiver_id = UUID(data_key='receiverID') system_uuid = UUID(required=False) - @post_load - def validate_hid(self, data): - """Validates than exist model manufacturer and system_uuid.""" - - minimum = [ - data.get('manufacturer'), - data.get('model'), - data.get('system_uuid'), - ] - - if not all(minimum): - txt = 'You can not register one device with out minimum datas, ' - txt += 'you can to do one placeholder.' - raise ValidationError(txt) - class Desktop(Computer): __doc__ = m.Desktop.__doc__ diff --git a/ereuse_devicehub/resources/device/sync.py b/ereuse_devicehub/resources/device/sync.py index 65731583..f88a62ba 100644 --- a/ereuse_devicehub/resources/device/sync.py +++ b/ereuse_devicehub/resources/device/sync.py @@ -212,19 +212,10 @@ class Sync: placeholder=None, ).one() # if no there are any Computer by uuid search by hid - if not db_device and device.hid: - with suppress(ResourceNotFound): - db_device = Device.query.filter_by( - hid=device.hid, - owner_id=g.user.id, - active=True, - placeholder=None, - ).one() + if not db_device: + db_device = device.get_from_db() elif device.hid: - with suppress(ResourceNotFound): - db_device = Device.query.filter_by( - hid=device.hid, owner_id=g.user.id, active=True, placeholder=None - ).one() + db_device = device.get_from_db() if db_device and db_device.allocated: raise ResourceNotFound('device is actually allocated {}'.format(device))