not required chassis in all system

This commit is contained in:
Cayo Puigdefabregas 2020-12-17 10:45:01 +01:00
parent aef220a5dc
commit 5241c73a98
5 changed files with 30 additions and 28 deletions

View File

@ -430,9 +430,12 @@ class Computer(Device):
receiver = db.relationship(User, primaryjoin=receiver_id == User.id) receiver = db.relationship(User, primaryjoin=receiver_id == User.id)
deliverynote_address = db.Column(CIText(), nullable=True) deliverynote_address = db.Column(CIText(), nullable=True)
def __init__(self, chassis, **kwargs) -> None: def __init__(self, *args, **kwargs) -> None:
chassis = ComputerChassis(chassis) if args:
chassis = ComputerChassis(args[0])
super().__init__(chassis=chassis, **kwargs) super().__init__(chassis=chassis, **kwargs)
else:
super().__init__(*args, **kwargs)
@property @property
def actions(self) -> list: def actions(self) -> list:

View File

@ -101,7 +101,6 @@ class Computer(Device):
collection_class=OrderedSet, collection_class=OrderedSet,
description='The components that are inside this computer.') description='The components that are inside this computer.')
chassis = EnumField(enums.ComputerChassis, chassis = EnumField(enums.ComputerChassis,
required=True,
description=m.Computer.chassis.comment) description=m.Computer.chassis.comment)
ram_size = Integer(dump_only=True, ram_size = Integer(dump_only=True,
data_key='ramSize', data_key='ramSize',

View File

@ -112,6 +112,7 @@ class DeviceSearch(db.Model):
if isinstance(device, Computer): if isinstance(device, Computer):
# Aggregate the values of all the components of pc # Aggregate the values of all the components of pc
Comp = aliased(Component) Comp = aliased(Component)
if device.chassis:
tokens.extend(( tokens.extend((
(db.func.string_agg(db.cast(Comp.id, db.TEXT), ' '), search.Weight.D), (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.model, ' '), search.Weight.C),
@ -122,6 +123,16 @@ class DeviceSearch(db.Model):
('PC', search.Weight.C), ('PC', search.Weight.C),
(inflection.humanize(device.chassis.name), search.Weight.B), (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 \ properties = session \
.query(search.Search.vectorize(*tokens)) \ .query(search.Search.vectorize(*tokens)) \

View File

@ -1,5 +1,5 @@
from contextlib import suppress from contextlib import suppress
from enum import Enum, IntEnum, unique, EnumMeta from enum import Enum, IntEnum, unique
from typing import Set, Union from typing import Set, Union
import inflection import inflection
@ -207,20 +207,9 @@ class DisplayTech(Enum):
return self.name 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 @unique
class ComputerChassis(Enum, metaclass=DefaultEnumMeta): class ComputerChassis(Enum):
"""The chassis of a computer.""" """The chassis of a computer."""
Nothing = None
Tower = 'Tower' Tower = 'Tower'
Docking = 'Docking' Docking = 'Docking'
AllInOne = 'All in one' AllInOne = 'All in one'

View File

@ -684,7 +684,7 @@ def test_snapshot_not_failed_null_chassis(app: Devicehub, user: UserClient):
tmp_snapshots = app.config['TMP_SNAPSHOTS'] tmp_snapshots = app.config['TMP_SNAPSHOTS']
path_dir_base = os.path.join(tmp_snapshots, user.user['email'], 'errors') path_dir_base = os.path.join(tmp_snapshots, user.user['email'], 'errors')
snapshot_error = file('desktop-9644w8n-lenovo-0169622.snapshot') snapshot_error = file('desktop-9644w8n-lenovo-0169622.snapshot')
snapshot_error['device']['chassis'] = 'Nothing' snapshot_error['device']['chassis'] = None
uuid = snapshot_error['uuid'] uuid = snapshot_error['uuid']
snapshot, res = user.post(res=Snapshot, data=snapshot_error) snapshot, res = user.post(res=Snapshot, data=snapshot_error)