diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 526dd6cd..fd75858b 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -430,9 +430,12 @@ class Computer(Device): receiver = db.relationship(User, primaryjoin=receiver_id == User.id) deliverynote_address = db.Column(CIText(), nullable=True) - def __init__(self, chassis, **kwargs) -> None: - chassis = ComputerChassis(chassis) - super().__init__(chassis=chassis, **kwargs) + def __init__(self, *args, **kwargs) -> None: + if args: + chassis = ComputerChassis(args[0]) + super().__init__(chassis=chassis, **kwargs) + else: + super().__init__(*args, **kwargs) @property def actions(self) -> list: diff --git a/ereuse_devicehub/resources/device/schemas.py b/ereuse_devicehub/resources/device/schemas.py index d3671c1b..c05199f0 100644 --- a/ereuse_devicehub/resources/device/schemas.py +++ b/ereuse_devicehub/resources/device/schemas.py @@ -101,7 +101,6 @@ class Computer(Device): collection_class=OrderedSet, description='The components that are inside this computer.') chassis = EnumField(enums.ComputerChassis, - required=True, description=m.Computer.chassis.comment) ram_size = Integer(dump_only=True, data_key='ramSize', diff --git a/ereuse_devicehub/resources/device/search.py b/ereuse_devicehub/resources/device/search.py index 381b5406..6cbf8b23 100644 --- a/ereuse_devicehub/resources/device/search.py +++ b/ereuse_devicehub/resources/device/search.py @@ -112,16 +112,27 @@ class DeviceSearch(db.Model): if isinstance(device, Computer): # Aggregate the values of all the components of pc Comp = aliased(Component) - tokens.extend(( - (db.func.string_agg(db.cast(Comp.id, db.TEXT), ' '), search.Weight.D), - (db.func.string_agg(Comp.model, ' '), search.Weight.C), - (db.func.string_agg(Comp.manufacturer, ' '), search.Weight.D), - (db.func.string_agg(Comp.serial_number, ' '), search.Weight.B), - (db.func.string_agg(Comp.type, ' '), search.Weight.B), - ('Computer', search.Weight.C), - ('PC', search.Weight.C), - (inflection.humanize(device.chassis.name), search.Weight.B), - )) + if device.chassis: + tokens.extend(( + (db.func.string_agg(db.cast(Comp.id, db.TEXT), ' '), search.Weight.D), + (db.func.string_agg(Comp.model, ' '), search.Weight.C), + (db.func.string_agg(Comp.manufacturer, ' '), search.Weight.D), + (db.func.string_agg(Comp.serial_number, ' '), search.Weight.B), + (db.func.string_agg(Comp.type, ' '), search.Weight.B), + ('Computer', search.Weight.C), + ('PC', search.Weight.C), + (inflection.humanize(device.chassis.name), search.Weight.B), + )) + else: + tokens.extend(( + (db.func.string_agg(db.cast(Comp.id, db.TEXT), ' '), search.Weight.D), + (db.func.string_agg(Comp.model, ' '), search.Weight.C), + (db.func.string_agg(Comp.manufacturer, ' '), search.Weight.D), + (db.func.string_agg(Comp.serial_number, ' '), search.Weight.B), + (db.func.string_agg(Comp.type, ' '), search.Weight.B), + ('Computer', search.Weight.C), + ('PC', search.Weight.C), + )) properties = session \ .query(search.Search.vectorize(*tokens)) \ diff --git a/ereuse_devicehub/resources/enums.py b/ereuse_devicehub/resources/enums.py index 7648e7d7..b9665f8f 100644 --- a/ereuse_devicehub/resources/enums.py +++ b/ereuse_devicehub/resources/enums.py @@ -1,5 +1,5 @@ from contextlib import suppress -from enum import Enum, IntEnum, unique, EnumMeta +from enum import Enum, IntEnum, unique from typing import Set, Union import inflection @@ -207,20 +207,9 @@ class DisplayTech(Enum): return self.name -class DefaultEnumMeta(EnumMeta): - default = object() - - def __call__(cls, value=default, *args, **kwargs): - # import pdb; pdb.set_trace() - if value is DefaultEnumMeta.default: - # Assume the first enum is default - return next(iter(cls)) - return super().__call__(value, *args, **kwargs) - @unique -class ComputerChassis(Enum, metaclass=DefaultEnumMeta): +class ComputerChassis(Enum): """The chassis of a computer.""" - Nothing = None Tower = 'Tower' Docking = 'Docking' AllInOne = 'All in one' diff --git a/tests/test_snapshot.py b/tests/test_snapshot.py index 7b3f5e20..416b8afe 100644 --- a/tests/test_snapshot.py +++ b/tests/test_snapshot.py @@ -684,7 +684,7 @@ def test_snapshot_not_failed_null_chassis(app: Devicehub, user: UserClient): tmp_snapshots = app.config['TMP_SNAPSHOTS'] path_dir_base = os.path.join(tmp_snapshots, user.user['email'], 'errors') snapshot_error = file('desktop-9644w8n-lenovo-0169622.snapshot') - snapshot_error['device']['chassis'] = 'Nothing' + snapshot_error['device']['chassis'] = None uuid = snapshot_error['uuid'] snapshot, res = user.post(res=Snapshot, data=snapshot_error)