2018-06-16 10:41:12 +00:00
|
|
|
from marshmallow import post_load, pre_load
|
2018-05-11 16:58:48 +00:00
|
|
|
from marshmallow.fields import Float, Integer, Str
|
|
|
|
from marshmallow.validate import Length, OneOf, Range
|
2018-06-12 14:50:05 +00:00
|
|
|
from marshmallow_enum import EnumField
|
2018-05-30 10:49:40 +00:00
|
|
|
from sqlalchemy.util import OrderedSet
|
2018-04-10 15:06:39 +00:00
|
|
|
|
2018-06-20 21:18:15 +00:00
|
|
|
from ereuse_devicehub.marshmallow import NestedOn
|
|
|
|
from ereuse_devicehub.resources.device import models as m
|
|
|
|
from ereuse_devicehub.resources.enums import ComputerMonitorTechnologies, RamFormat, RamInterface
|
|
|
|
from ereuse_devicehub.resources.models import STR_BIG_SIZE, STR_SIZE
|
|
|
|
from ereuse_devicehub.resources.schemas import Thing, UnitCodes
|
2018-06-16 10:41:12 +00:00
|
|
|
from teal.marshmallow import ValidationError
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Device(Thing):
|
2018-06-20 21:18:15 +00:00
|
|
|
id = Integer(description=m.Device.id, dump_only=True)
|
|
|
|
hid = Str(dump_only=True, description=m.Device.hid)
|
2018-05-30 10:49:40 +00:00
|
|
|
tags = NestedOn('Tag', many=True, collection_class=OrderedSet)
|
2018-04-10 15:06:39 +00:00
|
|
|
model = Str(validate=Length(max=STR_BIG_SIZE))
|
|
|
|
manufacturer = Str(validate=Length(max=STR_SIZE))
|
2018-04-27 17:16:43 +00:00
|
|
|
serial_number = Str(data_key='serialNumber')
|
|
|
|
product_id = Str(data_key='productId')
|
2018-06-20 21:18:15 +00:00
|
|
|
weight = Float(validate=Range(0.1, 3), unit=UnitCodes.kgm, description=m.Device.weight)
|
|
|
|
width = Float(validate=Range(0.1, 3), unit=UnitCodes.m, description=m.Device.width)
|
|
|
|
height = Float(validate=Range(0.1, 3), unit=UnitCodes.m, description=m.Device.height)
|
2018-05-11 16:58:48 +00:00
|
|
|
events = NestedOn('Event', many=True, dump_only=True)
|
2018-06-16 10:41:12 +00:00
|
|
|
events_one = NestedOn('Event', many=True, load_only=True, collection_class=OrderedSet)
|
|
|
|
|
|
|
|
@pre_load
|
|
|
|
def from_events_to_events_one(self, data: dict):
|
|
|
|
"""
|
|
|
|
Not an elegant way of allowing submitting events to a device
|
|
|
|
(in the context of Snapshots) without creating an ``events``
|
|
|
|
field at the model (which is not possible).
|
|
|
|
:param data:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
# Note that it is secure to allow uploading events_one
|
|
|
|
# as the only time an user can send a device object is
|
|
|
|
# in snapshots.
|
|
|
|
data['events_one'] = data.pop('events', [])
|
|
|
|
return data
|
|
|
|
|
|
|
|
@post_load
|
|
|
|
def validate_snapshot_events(self, data):
|
|
|
|
"""Validates that only snapshot-related events can be uploaded."""
|
2018-06-19 16:38:42 +00:00
|
|
|
from ereuse_devicehub.resources.event.models import EraseBasic, Test, Rate, Install, \
|
|
|
|
Benchmark
|
2018-06-16 10:41:12 +00:00
|
|
|
for event in data['events_one']:
|
2018-06-19 16:38:42 +00:00
|
|
|
if not isinstance(event, (Install, EraseBasic, Rate, Test, Benchmark)):
|
|
|
|
raise ValidationError('You cannot upload {}'.format(event), field_names=['events'])
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Computer(Device):
|
2018-05-30 10:49:40 +00:00
|
|
|
components = NestedOn('Component', many=True, dump_only=True, collection_class=OrderedSet)
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Desktop(Computer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Laptop(Computer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Netbook(Computer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Server(Computer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Microtower(Computer):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-06-20 21:18:15 +00:00
|
|
|
class ComputerMonitor(Device):
|
|
|
|
size = Float(description=m.ComputerMonitor.size.comment, validate=Range(2, 150))
|
|
|
|
technology = EnumField(ComputerMonitorTechnologies,
|
|
|
|
description=m.ComputerMonitor.technology.comment)
|
|
|
|
resolution_width = Integer(data_key='resolutionWidth',
|
|
|
|
validate=Range(10, 20000),
|
|
|
|
description=m.ComputerMonitor.resolution_width.comment)
|
|
|
|
resolution_height = Integer(data_key='resolutionHeight',
|
|
|
|
validate=Range(10, 20000),
|
|
|
|
description=m.ComputerMonitor.resolution_height.comment)
|
|
|
|
|
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
class Component(Device):
|
2018-05-11 16:58:48 +00:00
|
|
|
parent = NestedOn(Device, dump_only=True)
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GraphicCard(Component):
|
2018-04-27 17:16:43 +00:00
|
|
|
memory = Integer(validate=Range(0, 10000),
|
|
|
|
unit=UnitCodes.mbyte,
|
|
|
|
description='The amount of memory of the Graphic Card in MB.')
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
|
2018-06-10 16:47:49 +00:00
|
|
|
class DataStorage(Component):
|
2018-04-27 17:16:43 +00:00
|
|
|
size = Integer(validate=Range(0, 10 ** 8),
|
|
|
|
unit=UnitCodes.mbyte,
|
|
|
|
description='The size of the hard-drive in MB.')
|
2018-05-11 16:58:48 +00:00
|
|
|
erasure = NestedOn('EraseBasic', load_only=True)
|
|
|
|
tests = NestedOn('TestHardDrive', many=True, load_only=True)
|
|
|
|
benchmarks = NestedOn('BenchmarkHardDrive', load_only=True, many=True)
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
|
2018-06-10 16:47:49 +00:00
|
|
|
class HardDrive(DataStorage):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SolidStateDrive(DataStorage):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
class Motherboard(Component):
|
|
|
|
slots = Integer(validate=Range(1, 20), description='PCI slots the motherboard has.')
|
|
|
|
usb = Integer(validate=Range(0, 20))
|
|
|
|
firewire = Integer(validate=Range(0, 20))
|
|
|
|
serial = Integer(validate=Range(0, 20))
|
|
|
|
pcmcia = Integer(validate=Range(0, 20))
|
|
|
|
|
|
|
|
|
|
|
|
class NetworkAdapter(Component):
|
2018-04-27 17:16:43 +00:00
|
|
|
speed = Integer(validate=Range(min=10, max=10000),
|
|
|
|
unit=UnitCodes.mbps,
|
|
|
|
description='The maximum speed this network adapter can handle, in mbps.')
|
|
|
|
|
|
|
|
|
2018-05-11 16:58:48 +00:00
|
|
|
class Processor(Component):
|
|
|
|
speed = Float(validate=Range(min=0.1, max=15), unit=UnitCodes.ghz)
|
|
|
|
cores = Integer(validate=Range(min=1, max=10)) # todo from numberOfCores
|
|
|
|
address = Integer(validate=OneOf({8, 16, 32, 64, 128, 256}))
|
|
|
|
|
|
|
|
|
2018-04-27 17:16:43 +00:00
|
|
|
class RamModule(Component):
|
|
|
|
size = Integer(validate=Range(min=128, max=17000), unit=UnitCodes.mbyte)
|
|
|
|
speed = Float(validate=Range(min=100, max=10000), unit=UnitCodes.mhz)
|
2018-06-12 14:50:05 +00:00
|
|
|
interface = EnumField(RamInterface)
|
|
|
|
format = EnumField(RamFormat)
|