From f4418fc72a0267b5feebdc93f0f58089031442c3 Mon Sep 17 00:00:00 2001 From: JNadeu Date: Thu, 11 Oct 2018 17:51:58 +0200 Subject: [PATCH] Add reports files, init report commit --- ereuse_devicehub/resources/device/views.py | 112 +++++++++++++++--- ereuse_devicehub/resources/device/x.py | 8 -- .../resources/reports/__init__.py | 0 ereuse_devicehub/resources/reports/reports.py | 39 ++++++ 4 files changed, 134 insertions(+), 25 deletions(-) delete mode 100644 ereuse_devicehub/resources/device/x.py create mode 100644 ereuse_devicehub/resources/reports/__init__.py create mode 100644 ereuse_devicehub/resources/reports/reports.py diff --git a/ereuse_devicehub/resources/device/views.py b/ereuse_devicehub/resources/device/views.py index 61993ab0..0b88dd8a 100644 --- a/ereuse_devicehub/resources/device/views.py +++ b/ereuse_devicehub/resources/device/views.py @@ -1,4 +1,6 @@ import datetime +import itertools +from collections import OrderedDict import marshmallow from flask import current_app as app, render_template, request @@ -13,9 +15,11 @@ from teal.resource import View from ereuse_devicehub import auth from ereuse_devicehub.db import db from ereuse_devicehub.resources import search -from ereuse_devicehub.resources.device.models import Component, Computer, Device, Manufacturer +from ereuse_devicehub.resources.device.definitions import ComponentDef +from ereuse_devicehub.resources.device.models import Component, Computer, Device, Manufacturer, \ + RamModule, Processor, DataStorage from ereuse_devicehub.resources.device.search import DeviceSearch -from ereuse_devicehub.resources.event.models import Rate +from ereuse_devicehub.resources.event.models import Rate, Event from ereuse_devicehub.resources.lot.models import Lot, LotDevice from ereuse_devicehub.resources.tag.model import Tag @@ -119,21 +123,95 @@ class DeviceView(View): search.Search.rank(properties, search_p) + search.Search.rank(tags, search_p) ) query = query.filter(*args['filter']).order_by(*args['sort']) - devices = query.paginate(page=args['page'], per_page=30) # type: Pagination - ret = { - 'items': self.schema.dump(devices.items, many=True, nested=1), - # todo pagination should be in Header like github - # https://developer.github.com/v3/guides/traversing-with-pagination/ - 'pagination': { - 'page': devices.page, - 'perPage': devices.per_page, - 'total': devices.total, - 'previous': devices.prev_num, - 'next': devices.next_num - }, - 'url': request.path - } - return jsonify(ret) + if args['format']: + ... + return self.spreadsheet(query) + else: + devices = query.paginate(page=args['page'], per_page=30) # type: Pagination + ret = { + 'items': self.schema.dump(devices.items, many=True, nested=1), + # todo pagination should be in Header like github + # https://developer.github.com/v3/guides/traversing-with-pagination/ + 'pagination': { + 'page': devices.page, + 'perPage': devices.per_page, + 'total': devices.total, + 'previous': devices.prev_num, + 'next': devices.next_num + }, + 'url': request.path + } + return jsonify(ret) + + def spreadsheet(self, query): + devices = [] + for device in query: + d = DeviceRow(device) + devices.append(d) + + titles = [name for name in devices[0].keys()] + + rest = [[value for value in row.values()] for row in devices] + + +class DeviceRow(OrderedDict): + NUMS = { + Processor.t: 1 + } + + def __init__(self, device: Device) -> None: + super().__init__() + self.device = device + self['Type'] = device.t + if isinstance(device, Computer): + self['Chassis'] = device.chassis + 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) + self['Serial Number'] = device.serial_number + self['Price'] = device.price + self['Model'] = device.model + self['Manu...'] = device.manufacturer + self['Regsitered in '] = device.created + if isinstance(device, Computer): + self['Processor'] = device.processor_model + self['RAM (GB)'] = device.ram_size + self['Size (MB)'] = device.data_storage_size + rate = device.rate # type: Rate + if rate: + self['Rate'] = rate.rating + self['Range'] = rate.rating_range + self['Processor Rate'] = rate.processor_rate + self['RAM Rate'] = rate.ram_rate + self['Data Storage Rate'] = rate.data_storage_rate + # New Update fields (necessaris?) + # Origin note = Id-DonaciĆ³ + # Target note = Id-Receptor + # Partner = cadena de custodia (cadena de noms dels agents(entitas) implicats) [int] + # Margin = percentatges de com es repeteix els guanys del preu de venta del dispositiu. [int] + # Id invoice = id de la factura + if isinstance(device, Computer): + self.components() + + + def components(self): + assert isinstance(self.device, Computer) + for type in app.resources[Component.t].subresources_types: # type: str + max = self.NUMS.get(type, 4) + 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): + self['{} {} Serial Number'.format(type, i)] = component.serial_number if component else '' + if isinstance(component, DataStorage): + self['{} {} Compliance'.format()] = component.compliance class ManufacturerView(View): diff --git a/ereuse_devicehub/resources/device/x.py b/ereuse_devicehub/resources/device/x.py deleted file mode 100644 index e2d203a3..00000000 --- a/ereuse_devicehub/resources/device/x.py +++ /dev/null @@ -1,8 +0,0 @@ -import csv -import json - -with open('manufacturers.csv', 'w') as o: - writer = csv.writer(o) - with open('manufacturers.json') as i: - for x in json.load(i): - writer.writerow([x['name'], x['url'], x['logo'] if x.get('logo', None) else None]) diff --git a/ereuse_devicehub/resources/reports/__init__.py b/ereuse_devicehub/resources/reports/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ereuse_devicehub/resources/reports/reports.py b/ereuse_devicehub/resources/reports/reports.py new file mode 100644 index 00000000..ae96ecf1 --- /dev/null +++ b/ereuse_devicehub/resources/reports/reports.py @@ -0,0 +1,39 @@ +from typing import Set + +from sqlalchemy import func + +from ereuse_devicehub.db import db +from ereuse_devicehub.resources.device.models import Device +from ereuse_devicehub.resources.event.models import Price, Event, Trade + +ids = {1,2,3} + + +def export(devices_id: Set[str]): + # todo get the last event of device + last_event = Event.end_time. + + devices = Device.id.in_(ids) + + total_value_query = db.session.query(Price, func.sum(Price.price).label('total'))\ + .filter(devices)\ + .join(Price.device)\ + .filter(last_event) + + # todo hacer query para obtener el price + + query(func.max(end_time)).join(Price.devices).filter(Device_id==id).ordey_by(Price.end_time).limit() + + total_price_query = query() + + value = total_value_query.one() + value['total'] + + # + db.session.query(Price, (Price.price / total_value_query).label('asdfas')) + + trade_orgs_q = db.session.query(Trade, func.sum(Trade.org_id)).filter(devices).join(Trade.devices).filter(last_event) + + # execute query + value = trade_orgs_q.scalar() +