Adding stock row class for new document endpoint

This commit is contained in:
nad 2020-07-27 13:56:56 +02:00
parent 936c042c62
commit c90fa4e84e
2 changed files with 50 additions and 7 deletions

View File

@ -51,10 +51,6 @@ class DeviceRow(OrderedDict):
self['Processor'] = device.processor_model
self['RAM (MB)'] = device.ram_size
self['Data Storage Size (MB)'] = device.data_storage_size
if isinstance(device, d.Mobile):
self['Display Size'] = device.display_size
self['RAM (MB)'] = device.ram_size
self['Data Storage Size (MB)'] = device.data_storage_size
rate = device.rate
if rate:
self['Rate'] = rate.rating
@ -135,3 +131,49 @@ class DeviceRow(OrderedDict):
self['{} {} Speed (MHz)'.format(type, i)] = component.speed
# todo add Display, NetworkAdapter, etc...
class StockRow(OrderedDict):
def __init__(self, device: d.Device) -> None:
super().__init__()
self.device = device
self['Type'] = device.t
if isinstance(device, d.Computer):
self['Chassis'] = device.chassis
else:
self['Chassis'] = ''
self['Serial Number'] = device.serial_number
self['Model'] = device.model
self['Manufacturer'] = device.manufacturer
self['Registered in'] = format(device.created, '%c')
try:
self['Physical state'] = device.last_action_of(*states.Physical.actions()).t
except:
self['Physical state'] = ''
try:
self['Trading state'] = device.last_action_of(*states.Trading.actions()).t
except:
self['Trading state'] = ''
try:
self['Price'] = device.price
except:
self['Price'] = ''
try:
self['Processor'] = device.processor_model
self['RAM (MB)'] = device.ram_size
self['Data Storage Size (MB)'] = device.data_storage_size
except:
self['Processor'] = ''
self['RAM (MB)'] = ''
self['Data Storage Size (MB)'] = ''
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

View File

@ -18,7 +18,8 @@ from ereuse_devicehub.db import db
from ereuse_devicehub.resources.action import models as evs
from ereuse_devicehub.resources.device import models as devs
from ereuse_devicehub.resources.device.views import DeviceView
from ereuse_devicehub.resources.documents.device_row import DeviceRow
from ereuse_devicehub.resources.documents.device_row import DeviceRow, StockRow
class Format(enum.Enum):
@ -138,13 +139,13 @@ class StockDocumentView(DeviceView):
cw = csv.writer(data)
first = True
for device in query:
d = DeviceRow(device)
d = StockRow(device)
if first:
cw.writerow(d.keys())
first = False
cw.writerow(d.values())
output = make_response(data.getvalue())
output.headers['Content-Disposition'] = 'attachment; filename=export.csv'
output.headers['Content-Disposition'] = 'attachment; filename=stock.csv'
output.headers['Content-type'] = 'text/csv'
return output