2018-07-22 20:42:49 +00:00
|
|
|
from flask import current_app as app, jsonify
|
2018-06-14 13:14:23 +00:00
|
|
|
from flask_sqlalchemy import Pagination
|
2018-06-12 14:50:05 +00:00
|
|
|
from marshmallow import Schema as MarshmallowSchema
|
2018-06-14 13:14:23 +00:00
|
|
|
from marshmallow.fields import Float, Integer, Nested, Str
|
|
|
|
from marshmallow.validate import Range
|
|
|
|
from sqlalchemy import Column
|
2018-09-07 10:38:02 +00:00
|
|
|
from teal.query import Between, FullTextSearch, ILike, Join, Or, Query, Sort, SortField
|
|
|
|
from teal.resource import Resource, View
|
2018-06-12 14:50:05 +00:00
|
|
|
|
|
|
|
from ereuse_devicehub.resources.device.models import Device
|
|
|
|
from ereuse_devicehub.resources.event.models import Rate
|
2018-08-09 22:52:01 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot
|
2018-06-14 13:14:23 +00:00
|
|
|
from ereuse_devicehub.resources.schemas import Thing
|
2018-06-12 14:50:05 +00:00
|
|
|
from ereuse_devicehub.resources.tag import Tag
|
|
|
|
|
|
|
|
|
2018-06-14 13:14:23 +00:00
|
|
|
class Inventory(Thing):
|
2018-06-12 14:50:05 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class RateQ(Query):
|
|
|
|
rating = Between(Rate.rating, Float())
|
|
|
|
appearance = Between(Rate.appearance, Float())
|
|
|
|
functionality = Between(Rate.functionality, Float())
|
|
|
|
|
|
|
|
|
|
|
|
class TagQ(Query):
|
|
|
|
id = Or(ILike(Tag.id), required=True)
|
|
|
|
org = ILike(Tag.org)
|
|
|
|
|
|
|
|
|
2018-06-14 13:14:23 +00:00
|
|
|
class OfType(Str):
|
|
|
|
def __init__(self, column: Column, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.column = column
|
|
|
|
|
|
|
|
def _deserialize(self, value, attr, data):
|
|
|
|
v = super()._deserialize(value, attr, data)
|
2018-07-22 20:42:49 +00:00
|
|
|
return self.column.in_(app.resources[v].subresources_types)
|
2018-06-14 13:14:23 +00:00
|
|
|
|
|
|
|
|
2018-06-12 14:50:05 +00:00
|
|
|
class Filters(Query):
|
2018-06-14 13:14:23 +00:00
|
|
|
type = Or(OfType(Device.type))
|
2018-06-12 14:50:05 +00:00
|
|
|
model = ILike(Device.model)
|
|
|
|
manufacturer = ILike(Device.manufacturer)
|
|
|
|
serialNumber = ILike(Device.serial_number)
|
2018-06-14 13:14:23 +00:00
|
|
|
rating = Join(Device.id == Rate.device_id, RateQ)
|
|
|
|
tag = Join(Device.id == Tag.id, TagQ)
|
|
|
|
|
|
|
|
|
|
|
|
class Sorting(Sort):
|
|
|
|
created = SortField(Device.created)
|
2018-06-12 14:50:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
class InventoryView(View):
|
|
|
|
class FindArgs(MarshmallowSchema):
|
2018-06-14 13:14:23 +00:00
|
|
|
search = FullTextSearch() # todo Develop this. See more at docs/inventory.
|
|
|
|
filter = Nested(Filters, missing=[])
|
|
|
|
sort = Nested(Sorting, missing=[Device.created.desc()])
|
|
|
|
page = Integer(validate=Range(min=1), missing=1)
|
|
|
|
|
2018-06-24 14:57:49 +00:00
|
|
|
def get(self, id):
|
|
|
|
"""Inventory view
|
|
|
|
---
|
|
|
|
description: Supports the inventory view of ``devicehub-client``; returns
|
|
|
|
all the devices, groups and widgets of this Devicehub instance.
|
|
|
|
responses:
|
|
|
|
200:
|
|
|
|
description: The inventory.
|
|
|
|
schema:
|
|
|
|
type: object
|
|
|
|
properties:
|
|
|
|
devices:
|
|
|
|
type: array
|
|
|
|
items:
|
|
|
|
$ref: '#/definitions/Device'
|
|
|
|
pagination:
|
|
|
|
type: object
|
|
|
|
properties:
|
|
|
|
page:
|
|
|
|
type: integer
|
|
|
|
minimum: 0
|
|
|
|
perPage:
|
|
|
|
type: integer
|
|
|
|
minimum: 0
|
|
|
|
total:
|
|
|
|
type: integer
|
|
|
|
minimum: 0
|
2018-06-14 13:14:23 +00:00
|
|
|
"""
|
2018-06-24 14:57:49 +00:00
|
|
|
# todo .format(yaml.load(schema2parameters(self.FindArgs, default_in='path', name='path')))
|
|
|
|
return super().get(id)
|
2018-06-12 14:50:05 +00:00
|
|
|
|
2018-06-24 14:57:49 +00:00
|
|
|
def find(self, args: dict):
|
|
|
|
"""See :meth:`.get` above."""
|
2018-06-14 13:14:23 +00:00
|
|
|
devices = Device.query \
|
|
|
|
.filter(*args['filter']) \
|
|
|
|
.order_by(*args['sort']) \
|
|
|
|
.paginate(page=args['page'], per_page=30) # type: Pagination
|
2018-06-12 14:50:05 +00:00
|
|
|
inventory = {
|
2018-07-17 17:00:07 +00:00
|
|
|
'devices': app.resources[Device.t].schema.dump(devices.items, many=True, nested=1),
|
2018-09-21 09:25:22 +00:00
|
|
|
'lots': app.resources[Lot.t].schema.dump(Lot.roots(), many=True, nested=1),
|
2018-06-14 13:14:23 +00:00
|
|
|
'widgets': {},
|
|
|
|
'pagination': {
|
|
|
|
'page': devices.page,
|
|
|
|
'perPage': devices.per_page,
|
|
|
|
'total': devices.total,
|
|
|
|
}
|
2018-06-12 14:50:05 +00:00
|
|
|
}
|
|
|
|
return jsonify(inventory)
|
|
|
|
|
|
|
|
|
|
|
|
class InventoryDef(Resource):
|
|
|
|
SCHEMA = Inventory
|
|
|
|
VIEW = InventoryView
|
|
|
|
AUTH = True
|