2019-02-28 17:21:24 +00:00
|
|
|
from collections import OrderedDict
|
|
|
|
|
|
|
|
from flask import current_app
|
|
|
|
|
2019-05-11 14:27:22 +00:00
|
|
|
from ereuse_devicehub.resources.action.models import BenchmarkDataStorage, RateComputer, \
|
2019-05-08 17:12:05 +00:00
|
|
|
TestDataStorage
|
2020-03-11 17:47:27 +00:00
|
|
|
from ereuse_devicehub.resources.device import models as d, states
|
2019-02-28 17:21:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeviceRow(OrderedDict):
|
|
|
|
NUMS = {
|
|
|
|
d.Display.t: 1,
|
|
|
|
d.Processor.t: 2,
|
|
|
|
d.GraphicCard.t: 2,
|
|
|
|
d.Motherboard.t: 1,
|
|
|
|
d.NetworkAdapter.t: 2,
|
|
|
|
d.SoundCard.t: 2
|
|
|
|
}
|
|
|
|
|
2019-03-06 17:42:50 +00:00
|
|
|
# TODO Add more fields information
|
2019-02-28 17:21:24 +00:00
|
|
|
def __init__(self, device: d.Device) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.device = device
|
|
|
|
# General information about device
|
|
|
|
self['Type'] = device.t
|
|
|
|
if isinstance(device, d.Computer):
|
|
|
|
self['Chassis'] = device.chassis
|
2019-03-06 17:42:50 +00:00
|
|
|
else:
|
|
|
|
self['Chassis'] = ''
|
2019-02-28 17:21:24 +00:00
|
|
|
self['Tag 1'] = self['Tag 2'] = self['Tag 3'] = ''
|
|
|
|
for i, tag in zip(range(1, 3), device.tags):
|
|
|
|
self['Tag {}'.format(i)] = format(tag)
|
2020-08-17 09:18:52 +00:00
|
|
|
self['Serial Number'] = convert_none_to_empty_str(device.serial_number)
|
|
|
|
self['Model'] = convert_none_to_empty_str(device.model)
|
|
|
|
self['Manufacturer'] = convert_none_to_empty_str(device.manufacturer)
|
2019-02-28 17:21:24 +00:00
|
|
|
self['Registered in'] = format(device.created, '%c')
|
2020-03-11 17:47:27 +00:00
|
|
|
try:
|
|
|
|
self['Physical state'] = device.last_action_of(*states.Physical.actions()).t
|
2020-08-17 09:18:52 +00:00
|
|
|
except LookupError:
|
2020-03-11 17:47:27 +00:00
|
|
|
self['Physical state'] = ''
|
|
|
|
try:
|
|
|
|
self['Trading state'] = device.last_action_of(*states.Trading.actions()).t
|
2020-08-17 09:18:52 +00:00
|
|
|
except LookupError:
|
2020-03-11 17:47:27 +00:00
|
|
|
self['Trading state'] = ''
|
2020-08-17 09:18:52 +00:00
|
|
|
self['Price'] = convert_none_to_empty_str(device.price)
|
2019-02-28 17:21:24 +00:00
|
|
|
if isinstance(device, d.Computer):
|
2020-08-17 09:18:52 +00:00
|
|
|
self['Processor'] = convert_none_to_empty_str(device.processor_model)
|
|
|
|
self['RAM (MB)'] = convert_none_to_empty_str(device.ram_size)
|
|
|
|
self['Data Storage Size (MB)'] = convert_none_to_empty_str(device.data_storage_size)
|
2019-02-28 17:21:24 +00:00
|
|
|
rate = device.rate
|
|
|
|
if rate:
|
|
|
|
self['Rate'] = rate.rating
|
|
|
|
self['Range'] = rate.rating_range
|
2019-05-08 17:12:05 +00:00
|
|
|
assert isinstance(rate, RateComputer)
|
2019-02-28 17:21:24 +00:00
|
|
|
self['Processor Rate'] = rate.processor
|
2019-04-30 00:02:23 +00:00
|
|
|
self['Processor Range'] = rate.processor_range
|
2019-02-28 17:21:24 +00:00
|
|
|
self['RAM Rate'] = rate.ram
|
2019-04-30 00:02:23 +00:00
|
|
|
self['RAM Range'] = rate.ram_range
|
2019-02-28 17:21:24 +00:00
|
|
|
self['Data Storage Rate'] = rate.data_storage
|
2019-04-30 00:02:23 +00:00
|
|
|
self['Data Storage Range'] = rate.data_storage_range
|
2019-02-28 17:21:24 +00:00
|
|
|
# More specific information about components
|
|
|
|
if isinstance(device, d.Computer):
|
|
|
|
self.components()
|
|
|
|
|
|
|
|
def components(self):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Function to get all components information of a device."""
|
2019-02-28 17:21:24 +00:00
|
|
|
assert isinstance(self.device, d.Computer)
|
2019-03-06 17:42:50 +00:00
|
|
|
# todo put an input specific order (non alphabetic) & where are a list of types components
|
2019-02-28 17:21:24 +00:00
|
|
|
for type in sorted(current_app.resources[d.Component.t].subresources_types): # type: str
|
|
|
|
max = self.NUMS.get(type, 4)
|
2020-07-07 15:17:41 +00:00
|
|
|
if type not in ['Component', 'HardDrive', 'SolidStateDrive']:
|
2019-02-28 17:21:24 +00:00
|
|
|
i = 1
|
|
|
|
for component in (r for r in self.device.components if r.type == type):
|
|
|
|
self.fill_component(type, i, component)
|
|
|
|
i += 1
|
|
|
|
if i > max:
|
|
|
|
break
|
|
|
|
while i <= max:
|
|
|
|
self.fill_component(type, i)
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
def fill_component(self, type, i, component=None):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Function to put specific information of components
|
|
|
|
in OrderedDict (csv)
|
2019-02-28 17:21:24 +00:00
|
|
|
:param type: type of component
|
|
|
|
:param component: device.components
|
|
|
|
"""
|
|
|
|
self['{} {}'.format(type, i)] = format(component) if component else ''
|
|
|
|
self['{} {} Manufacturer'.format(type, i)] = component.serial_number if component else ''
|
|
|
|
self['{} {} Model'.format(type, i)] = component.serial_number if component else ''
|
|
|
|
self['{} {} Serial Number'.format(type, i)] = component.serial_number if component else ''
|
|
|
|
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Particular fields for component GraphicCard."""
|
2019-02-28 17:21:24 +00:00
|
|
|
if isinstance(component, d.GraphicCard):
|
|
|
|
self['{} {} Memory (MB)'.format(type, i)] = component.memory
|
|
|
|
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Particular fields for component DataStorage.t ->
|
|
|
|
(HardDrive, SolidStateDrive)
|
|
|
|
"""
|
2019-02-28 17:21:24 +00:00
|
|
|
if isinstance(component, d.DataStorage):
|
|
|
|
self['{} {} Size (MB)'.format(type, i)] = component.size
|
|
|
|
self['{} {} Privacy'.format(type, i)] = component.privacy
|
2019-03-06 17:42:50 +00:00
|
|
|
try:
|
2019-05-11 14:27:22 +00:00
|
|
|
self['{} {} Lifetime'.format(type, i)] = component.last_action_of(
|
2019-05-08 17:12:05 +00:00
|
|
|
TestDataStorage).lifetime
|
2019-03-06 17:42:50 +00:00
|
|
|
except:
|
|
|
|
self['{} {} Lifetime'.format(type, i)] = ''
|
|
|
|
try:
|
2019-05-11 14:27:22 +00:00
|
|
|
self['{} {} Reading speed'.format(type, i)] = component.last_action_of(
|
2019-05-08 17:12:05 +00:00
|
|
|
BenchmarkDataStorage).read_speed
|
2019-03-06 17:42:50 +00:00
|
|
|
except:
|
|
|
|
self['{} {} Reading speed'.format(type, i)] = ''
|
|
|
|
try:
|
2019-05-11 14:27:22 +00:00
|
|
|
self['{} {} Writing speed'.format(type, i)] = component.last_action_of(
|
2019-05-08 17:12:05 +00:00
|
|
|
BenchmarkDataStorage).write_speed
|
2019-03-06 17:42:50 +00:00
|
|
|
except:
|
|
|
|
self['{} {} Writing speed'.format(type, i)] = ''
|
2019-02-28 17:21:24 +00:00
|
|
|
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Particular fields for component Processor."""
|
2019-02-28 17:21:24 +00:00
|
|
|
if isinstance(component, d.Processor):
|
|
|
|
self['{} {} Number of cores'.format(type, i)] = component.cores
|
|
|
|
self['{} {} Speed (GHz)'.format(type, i)] = component.speed
|
|
|
|
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Particular fields for component RamModule."""
|
2019-02-28 17:21:24 +00:00
|
|
|
if isinstance(component, d.RamModule):
|
|
|
|
self['{} {} Size (MB)'.format(type, i)] = component.size
|
|
|
|
self['{} {} Speed (MHz)'.format(type, i)] = component.speed
|
|
|
|
|
2019-06-19 11:35:26 +00:00
|
|
|
# todo add Display, NetworkAdapter, etc...
|
2020-07-27 11:56:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StockRow(OrderedDict):
|
|
|
|
def __init__(self, device: d.Device) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.device = device
|
2020-08-17 09:18:52 +00:00
|
|
|
self['Type'] = convert_none_to_empty_str(device.t)
|
2020-07-27 11:56:56 +00:00
|
|
|
if isinstance(device, d.Computer):
|
|
|
|
self['Chassis'] = device.chassis
|
|
|
|
else:
|
|
|
|
self['Chassis'] = ''
|
2020-08-17 09:18:52 +00:00
|
|
|
self['Serial Number'] = convert_none_to_empty_str(device.serial_number)
|
|
|
|
self['Model'] = convert_none_to_empty_str(device.model)
|
|
|
|
self['Manufacturer'] = convert_none_to_empty_str(device.manufacturer)
|
2020-07-27 11:56:56 +00:00
|
|
|
self['Registered in'] = format(device.created, '%c')
|
|
|
|
try:
|
|
|
|
self['Physical state'] = device.last_action_of(*states.Physical.actions()).t
|
2020-08-17 09:18:52 +00:00
|
|
|
except LookupError:
|
2020-07-27 11:56:56 +00:00
|
|
|
self['Physical state'] = ''
|
|
|
|
try:
|
|
|
|
self['Trading state'] = device.last_action_of(*states.Trading.actions()).t
|
2020-08-17 09:18:52 +00:00
|
|
|
except LookupError:
|
2020-07-27 11:56:56 +00:00
|
|
|
self['Trading state'] = ''
|
2020-08-17 09:18:52 +00:00
|
|
|
self['Price'] = convert_none_to_empty_str(device.price)
|
|
|
|
self['Processor'] = convert_none_to_empty_str(device.processor_model)
|
|
|
|
self['RAM (MB)'] = convert_none_to_empty_str(device.ram_size)
|
|
|
|
self['Data Storage Size (MB)'] = convert_none_to_empty_str(device.data_storage_size)
|
2020-07-27 11:56:56 +00:00
|
|
|
rate = device.rate
|
|
|
|
if rate:
|
|
|
|
self['Rate'] = rate.rating
|
|
|
|
self['Range'] = rate.rating_range
|
|
|
|
assert isinstance(rate, RateComputer)
|
|
|
|
self['Processor Rate'] = rate.processor
|
|
|
|
self['Processor Range'] = rate.processor_range
|
|
|
|
self['RAM Rate'] = rate.ram
|
|
|
|
self['RAM Range'] = rate.ram_range
|
|
|
|
self['Data Storage Rate'] = rate.data_storage
|
|
|
|
self['Data Storage Range'] = rate.data_storage_range
|
2020-08-17 09:18:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def convert_none_to_empty_str(s):
|
|
|
|
if s is None:
|
|
|
|
return ''
|
|
|
|
return s
|