2018-11-13 16:58:26 +00:00
|
|
|
import csv
|
2018-10-03 12:51:22 +00:00
|
|
|
import datetime
|
2018-11-13 16:58:26 +00:00
|
|
|
from io import StringIO
|
2018-10-18 16:16:49 +00:00
|
|
|
|
2018-09-30 17:40:28 +00:00
|
|
|
import marshmallow
|
2018-11-13 16:58:26 +00:00
|
|
|
from flask import current_app as app, render_template, request, make_response
|
2018-09-30 17:40:28 +00:00
|
|
|
from flask.json import jsonify
|
|
|
|
from flask_sqlalchemy import Pagination
|
2018-10-06 10:45:56 +00:00
|
|
|
from marshmallow import fields, fields as f, validate as v
|
|
|
|
from sqlalchemy.orm import aliased
|
2018-11-13 16:58:26 +00:00
|
|
|
from sqlalchemy.util import OrderedDict
|
2018-10-04 08:59:31 +00:00
|
|
|
from teal import query
|
2018-10-03 12:51:22 +00:00
|
|
|
from teal.cache import cache
|
2018-04-10 15:06:39 +00:00
|
|
|
from teal.resource import View
|
|
|
|
|
2018-10-03 12:51:22 +00:00
|
|
|
from ereuse_devicehub import auth
|
2018-10-04 08:59:31 +00:00
|
|
|
from ereuse_devicehub.db import db
|
2019-01-26 11:49:31 +00:00
|
|
|
from ereuse_devicehub.query import SearchQueryParser, things_response
|
2018-10-04 08:59:31 +00:00
|
|
|
from ereuse_devicehub.resources import search
|
2018-11-13 16:58:26 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Component, Computer, Device, Manufacturer, \
|
|
|
|
Display, Processor, GraphicCard, Motherboard, NetworkAdapter, DataStorage, RamModule, \
|
|
|
|
SoundCard
|
2018-10-04 08:59:31 +00:00
|
|
|
from ereuse_devicehub.resources.device.search import DeviceSearch
|
2018-11-21 13:26:56 +00:00
|
|
|
from ereuse_devicehub.resources.event import models as events
|
2018-11-11 20:52:55 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import LotDeviceDescendants
|
2018-10-04 08:59:31 +00:00
|
|
|
from ereuse_devicehub.resources.tag.model import Tag
|
|
|
|
|
|
|
|
|
|
|
|
class OfType(f.Str):
|
|
|
|
def __init__(self, column: db.Column, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.column = column
|
|
|
|
|
|
|
|
def _deserialize(self, value, attr, data):
|
|
|
|
v = super()._deserialize(value, attr, data)
|
|
|
|
return self.column.in_(app.resources[v].subresources_types)
|
|
|
|
|
|
|
|
|
|
|
|
class RateQ(query.Query):
|
2018-11-21 13:26:56 +00:00
|
|
|
rating = query.Between(events.Rate.rating, f.Float())
|
|
|
|
appearance = query.Between(events.Rate.appearance, f.Float())
|
|
|
|
functionality = query.Between(events.Rate.functionality, f.Float())
|
2018-10-04 08:59:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TagQ(query.Query):
|
|
|
|
id = query.Or(query.ILike(Tag.id), required=True)
|
|
|
|
org = query.ILike(Tag.org)
|
|
|
|
|
|
|
|
|
2018-10-06 10:45:56 +00:00
|
|
|
class LotQ(query.Query):
|
2018-11-11 20:52:55 +00:00
|
|
|
id = query.Or(query.Equal(LotDeviceDescendants.ancestor_lot_id, fields.UUID()))
|
2018-10-06 10:45:56 +00:00
|
|
|
|
|
|
|
|
2018-10-04 08:59:31 +00:00
|
|
|
class Filters(query.Query):
|
2018-11-21 13:26:56 +00:00
|
|
|
id = query.Or(query.Equal(Device.id, fields.Integer()))
|
2018-10-04 08:59:31 +00:00
|
|
|
type = query.Or(OfType(Device.type))
|
|
|
|
model = query.ILike(Device.model)
|
|
|
|
manufacturer = query.ILike(Device.manufacturer)
|
|
|
|
serialNumber = query.ILike(Device.serial_number)
|
2019-02-05 16:59:15 +00:00
|
|
|
# todo test query for rating (and possibly other filters)
|
|
|
|
rating = query.Join((Device.id == events.EventWithOneDevice.device_id)
|
|
|
|
& (events.EventWithOneDevice.id == events.Rate.id),
|
|
|
|
RateQ)
|
2018-10-06 10:45:56 +00:00
|
|
|
tag = query.Join(Device.id == Tag.device_id, TagQ)
|
2018-11-04 21:40:14 +00:00
|
|
|
# todo This part of the query is really slow
|
|
|
|
# And forces usage of distinct, as it returns many rows
|
|
|
|
# due to having multiple paths to the same
|
2018-11-11 20:52:55 +00:00
|
|
|
lot = query.Join(Device.id == LotDeviceDescendants.device_id, LotQ)
|
2018-10-04 08:59:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Sorting(query.Sort):
|
2018-10-06 10:45:56 +00:00
|
|
|
id = query.SortField(Device.id)
|
2018-10-04 08:59:31 +00:00
|
|
|
created = query.SortField(Device.created)
|
2018-12-30 11:43:29 +00:00
|
|
|
updated = query.SortField(Device.updated)
|
2018-09-07 10:38:02 +00:00
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
class DeviceView(View):
|
2018-11-11 21:18:10 +00:00
|
|
|
QUERY_PARSER = SearchQueryParser()
|
|
|
|
|
2018-10-04 08:59:31 +00:00
|
|
|
class FindArgs(marshmallow.Schema):
|
|
|
|
search = f.Str()
|
|
|
|
filter = f.Nested(Filters, missing=[])
|
2018-11-04 21:40:14 +00:00
|
|
|
sort = f.Nested(Sorting, missing=[Device.id.asc()])
|
2018-10-04 08:59:31 +00:00
|
|
|
page = f.Integer(validate=v.Range(min=1), missing=1)
|
2018-06-24 14:57:49 +00:00
|
|
|
|
|
|
|
def get(self, id):
|
|
|
|
"""
|
|
|
|
Devices view
|
|
|
|
---
|
|
|
|
description: Gets a device or multiple devices.
|
|
|
|
parameters:
|
|
|
|
- name: id
|
|
|
|
type: integer
|
2018-11-21 13:26:56 +00:00
|
|
|
in: path}
|
2018-06-24 14:57:49 +00:00
|
|
|
description: The identifier of the device.
|
|
|
|
responses:
|
|
|
|
200:
|
|
|
|
description: The device or devices.
|
|
|
|
"""
|
2018-11-21 13:26:56 +00:00
|
|
|
return super().get(id)
|
2018-06-24 14:57:49 +00:00
|
|
|
|
2018-04-27 17:16:43 +00:00
|
|
|
def one(self, id: int):
|
2018-04-10 15:06:39 +00:00
|
|
|
"""Gets one device."""
|
2018-10-03 12:51:22 +00:00
|
|
|
if not request.authorization:
|
|
|
|
return self.one_public(id)
|
|
|
|
else:
|
|
|
|
return self.one_private(id)
|
|
|
|
|
|
|
|
def one_public(self, id: int):
|
|
|
|
device = Device.query.filter_by(id=id).one()
|
|
|
|
return render_template('devices/layout.html', device=device)
|
|
|
|
|
|
|
|
@auth.Auth.requires_auth
|
|
|
|
def one_private(self, id: int):
|
2018-05-11 16:58:48 +00:00
|
|
|
device = Device.query.filter_by(id=id).one()
|
2018-05-13 13:13:12 +00:00
|
|
|
return self.schema.jsonify(device)
|
2018-05-11 16:58:48 +00:00
|
|
|
|
2018-10-03 12:51:22 +00:00
|
|
|
@auth.Auth.requires_auth
|
2019-01-29 15:29:08 +00:00
|
|
|
@cache(datetime.timedelta(minutes=1))
|
2018-05-11 16:58:48 +00:00
|
|
|
def find(self, args: dict):
|
2018-06-24 14:57:49 +00:00
|
|
|
"""Gets many devices."""
|
2018-11-21 13:26:56 +00:00
|
|
|
# Compute query
|
|
|
|
query = self.query(args)
|
2018-10-04 08:59:31 +00:00
|
|
|
devices = query.paginate(page=args['page'], per_page=30) # type: Pagination
|
2019-01-26 11:49:31 +00:00
|
|
|
return things_response(
|
|
|
|
self.schema.dump(devices.items, many=True, nested=1),
|
|
|
|
devices.page, devices.per_page, devices.total, devices.prev_num, devices.next_num
|
|
|
|
)
|
2018-09-30 17:40:28 +00:00
|
|
|
|
2018-11-21 13:26:56 +00:00
|
|
|
def query(self, args):
|
2018-11-04 21:40:14 +00:00
|
|
|
query = Device.query.distinct() # todo we should not force to do this if the query is ok
|
2018-11-21 13:26:56 +00:00
|
|
|
search_p = args.get('search', None)
|
2018-10-04 08:59:31 +00:00
|
|
|
if search_p:
|
|
|
|
properties = DeviceSearch.properties
|
|
|
|
tags = DeviceSearch.tags
|
|
|
|
query = query.join(DeviceSearch).filter(
|
|
|
|
search.Search.match(properties, search_p) | search.Search.match(tags, search_p)
|
|
|
|
).order_by(
|
|
|
|
search.Search.rank(properties, search_p) + search.Search.rank(tags, search_p)
|
|
|
|
)
|
|
|
|
query = query.filter(*args['filter']).order_by(*args['sort'])
|
2018-10-18 16:16:49 +00:00
|
|
|
if 'text/csv' in request.accept_mimetypes:
|
|
|
|
return self.generate_post_csv(query)
|
2018-10-11 15:51:58 +00:00
|
|
|
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)
|
|
|
|
|
2018-10-18 16:16:49 +00:00
|
|
|
def generate_post_csv(self, query):
|
|
|
|
"""
|
|
|
|
Get device query and put information in csv format
|
|
|
|
:param query:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
data = StringIO()
|
|
|
|
cw = csv.writer(data)
|
2018-10-23 16:59:58 +00:00
|
|
|
first = True
|
|
|
|
for device in query:
|
|
|
|
d = DeviceRow(device)
|
|
|
|
if first:
|
|
|
|
cw.writerow(name for name in d.keys())
|
|
|
|
first = False
|
|
|
|
cw.writerow(v for v in d.values())
|
2018-10-18 16:16:49 +00:00
|
|
|
output = make_response(data.getvalue())
|
2018-10-23 16:59:58 +00:00
|
|
|
output.headers['Content-Disposition'] = 'attachment; filename=export.csv'
|
|
|
|
output.headers['Content-type'] = 'text/csv'
|
2018-10-18 16:16:49 +00:00
|
|
|
return output
|
2018-10-11 15:51:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DeviceRow(OrderedDict):
|
|
|
|
NUMS = {
|
2018-10-25 10:44:24 +00:00
|
|
|
Display.t: 1,
|
2018-10-24 19:11:32 +00:00
|
|
|
Processor.t: 2,
|
|
|
|
GraphicCard.t: 2,
|
|
|
|
Motherboard.t: 1,
|
|
|
|
NetworkAdapter.t: 2,
|
|
|
|
SoundCard.t: 2
|
2018-10-11 15:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self, device: Device) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.device = device
|
2018-10-24 19:11:32 +00:00
|
|
|
# General information about device
|
2018-10-11 15:51:58 +00:00
|
|
|
self['Type'] = device.t
|
|
|
|
if isinstance(device, Computer):
|
2018-10-24 19:11:32 +00:00
|
|
|
self['Chassis'] = device.chassis
|
2018-10-11 15:51:58 +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)
|
|
|
|
self['Serial Number'] = device.serial_number
|
|
|
|
self['Model'] = device.model
|
2018-10-18 16:16:49 +00:00
|
|
|
self['Manufacturer'] = device.manufacturer
|
2018-10-24 19:11:32 +00:00
|
|
|
# self['State'] = device.last_event_of()
|
|
|
|
self['Price'] = device.price
|
|
|
|
self['Registered in'] = format(device.created, '%c')
|
2018-10-11 15:51:58 +00:00
|
|
|
if isinstance(device, Computer):
|
|
|
|
self['Processor'] = device.processor_model
|
|
|
|
self['RAM (GB)'] = device.ram_size
|
2018-10-24 19:11:32 +00:00
|
|
|
self['Storage Size (MB)'] = device.data_storage_size
|
2018-10-18 16:16:49 +00:00
|
|
|
rate = device.rate
|
2018-10-11 15:51:58 +00:00
|
|
|
if rate:
|
|
|
|
self['Rate'] = rate.rating
|
|
|
|
self['Range'] = rate.rating_range
|
2018-10-18 16:16:49 +00:00
|
|
|
self['Processor Rate'] = rate.processor
|
|
|
|
self['Processor Range'] = rate.workbench.processor_range
|
|
|
|
self['RAM Rate'] = rate.ram
|
|
|
|
self['RAM Range'] = rate.workbench.ram_range
|
|
|
|
self['Data Storage Rate'] = rate.data_storage
|
|
|
|
self['Data Storage Range'] = rate.workbench.data_storage_range
|
2018-10-24 19:11:32 +00:00
|
|
|
# More specific information about components
|
2018-10-11 15:51:58 +00:00
|
|
|
if isinstance(device, Computer):
|
|
|
|
self.components()
|
|
|
|
|
2018-10-24 19:11:32 +00:00
|
|
|
|
2018-10-11 15:51:58 +00:00
|
|
|
def components(self):
|
2018-10-24 19:11:32 +00:00
|
|
|
"""
|
|
|
|
Function to get all components information of a device
|
|
|
|
"""
|
2018-10-11 15:51:58 +00:00
|
|
|
assert isinstance(self.device, Computer)
|
2018-10-24 19:11:32 +00:00
|
|
|
# todo put an input specific order (non alphabetic)
|
|
|
|
for type in sorted(app.resources[Component.t].subresources_types): # type: str
|
2018-10-11 15:51:58 +00:00
|
|
|
max = self.NUMS.get(type, 4)
|
2018-10-24 19:11:32 +00:00
|
|
|
if type not in ['Component', 'HardDrive', 'SolidStateDrive']:
|
|
|
|
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):
|
|
|
|
"""
|
|
|
|
Function to put specific information of components in OrderedDict (csv)
|
|
|
|
: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 ''
|
2018-10-11 15:51:58 +00:00
|
|
|
self['{} {} Serial Number'.format(type, i)] = component.serial_number if component else ''
|
2018-10-24 19:11:32 +00:00
|
|
|
|
|
|
|
""" Particular fields for component GraphicCard """
|
|
|
|
if isinstance(component, GraphicCard):
|
|
|
|
self['{} {} Memory (MB)'.format(type, i)] = component.memory
|
|
|
|
|
|
|
|
""" Particular fields for component DataStorage.t -> (HardDrive, SolidStateDrive) """
|
2018-10-11 15:51:58 +00:00
|
|
|
if isinstance(component, DataStorage):
|
2018-10-24 19:11:32 +00:00
|
|
|
self['{} {} Size (MB)'.format(type, i)] = component.size
|
|
|
|
self['{} {} Privacy'.format(type, i)] = component.privacy
|
|
|
|
|
|
|
|
# todo decide if is relevant more info about Motherboard
|
|
|
|
""" Particular fields for component Motherboard """
|
|
|
|
if isinstance(component, Motherboard):
|
|
|
|
self['{} {} Slots'.format(type, i)] = component.slots
|
|
|
|
|
|
|
|
""" Particular fields for component Processor """
|
|
|
|
if isinstance(component, Processor):
|
|
|
|
self['{} {} Number of cores'.format(type, i)] = component.cores
|
|
|
|
self['{} {} Speed (GHz)'.format(type, i)] = component.speed
|
|
|
|
|
|
|
|
""" Particular fields for component RamModule """
|
|
|
|
if isinstance(component, RamModule):
|
|
|
|
self['{} {} Size (MB)'.format(type, i)] = component.size
|
|
|
|
self['{} {} Speed (MHz)'.format(type, i)] = component.speed
|
|
|
|
self['{} {} Size'.format(type, i)] = component.size
|
|
|
|
|
|
|
|
# todo add Display size, ...
|
|
|
|
# todo add NetworkAdapter speedLink?
|
|
|
|
# todo add some ComputerAccessories
|
2018-09-30 17:40:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManufacturerView(View):
|
|
|
|
class FindArgs(marshmallow.Schema):
|
2018-10-18 11:08:42 +00:00
|
|
|
search = marshmallow.fields.Str(required=True,
|
|
|
|
# Disallow like operators
|
|
|
|
validate=lambda x: '%' not in x and '_' not in x)
|
2018-09-30 17:40:28 +00:00
|
|
|
|
2018-10-03 12:51:22 +00:00
|
|
|
@cache(datetime.timedelta(days=1))
|
2018-09-30 17:40:28 +00:00
|
|
|
def find(self, args: dict):
|
2018-10-18 11:08:42 +00:00
|
|
|
search = args['search']
|
2018-09-30 17:40:28 +00:00
|
|
|
manufacturers = Manufacturer.query \
|
2018-10-18 11:08:42 +00:00
|
|
|
.filter(Manufacturer.name.ilike(search + '%')) \
|
2018-09-30 17:40:28 +00:00
|
|
|
.paginate(page=1, per_page=6) # type: Pagination
|
|
|
|
return jsonify(
|
|
|
|
items=app.resources[Manufacturer.t].schema.dump(
|
|
|
|
manufacturers.items,
|
|
|
|
many=True,
|
|
|
|
nested=1
|
|
|
|
)
|
|
|
|
)
|