2018-10-03 12:51:22 +00:00
|
|
|
import datetime
|
|
|
|
|
2018-09-30 17:40:28 +00:00
|
|
|
import marshmallow
|
2018-10-03 12:51:22 +00:00
|
|
|
from flask import current_app as app, render_template, request
|
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-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
|
|
|
|
from ereuse_devicehub.resources import search
|
2018-10-06 10:45:56 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Component, Computer, Device, Manufacturer
|
2018-10-04 08:59:31 +00:00
|
|
|
from ereuse_devicehub.resources.device.search import DeviceSearch
|
|
|
|
from ereuse_devicehub.resources.event.models import Rate
|
2018-10-06 10:45:56 +00:00
|
|
|
from ereuse_devicehub.resources.lot.models import Lot, LotDevice
|
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):
|
|
|
|
rating = query.Between(Rate.rating, f.Float())
|
|
|
|
appearance = query.Between(Rate.appearance, f.Float())
|
|
|
|
functionality = query.Between(Rate.functionality, f.Float())
|
|
|
|
|
|
|
|
|
|
|
|
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):
|
|
|
|
id = query.Or(query.QueryField(Lot.descendantsq, fields.UUID()))
|
|
|
|
|
|
|
|
|
2018-10-04 08:59:31 +00:00
|
|
|
class Filters(query.Query):
|
2018-10-06 10:45:56 +00:00
|
|
|
_parent = aliased(Computer)
|
|
|
|
_device_inside_lot = (Device.id == LotDevice.device_id) & (Lot.id == LotDevice.lot_id)
|
|
|
|
_component_inside_lot_through_parent = (Device.id == Component.id) \
|
|
|
|
& (Component.parent_id == _parent.id) \
|
|
|
|
& (_parent.id == LotDevice.device_id)
|
|
|
|
|
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)
|
|
|
|
rating = query.Join(Device.id == Rate.device_id, RateQ)
|
2018-10-06 10:45:56 +00:00
|
|
|
tag = query.Join(Device.id == Tag.device_id, TagQ)
|
|
|
|
lot = query.Join(_device_inside_lot | _component_inside_lot_through_parent, 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-09-07 10:38:02 +00:00
|
|
|
|
2018-04-10 15:06:39 +00:00
|
|
|
|
|
|
|
class DeviceView(View):
|
2018-10-04 08:59:31 +00:00
|
|
|
class FindArgs(marshmallow.Schema):
|
|
|
|
search = f.Str()
|
|
|
|
filter = f.Nested(Filters, missing=[])
|
|
|
|
sort = f.Nested(Sorting, missing=[])
|
|
|
|
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
|
|
|
|
in: path
|
|
|
|
description: The identifier of the device.
|
|
|
|
responses:
|
|
|
|
200:
|
|
|
|
description: The device or devices.
|
|
|
|
"""
|
|
|
|
return super().get(id)
|
|
|
|
|
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
|
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-10-04 08:59:31 +00:00
|
|
|
search_p = args.get('search', None)
|
|
|
|
query = Device.query
|
|
|
|
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'])
|
|
|
|
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,
|
2018-10-05 12:35:51 +00:00
|
|
|
'total': devices.total,
|
2018-10-05 15:13:23 +00:00
|
|
|
'previous': devices.prev_num,
|
|
|
|
'next': devices.next_num
|
|
|
|
},
|
|
|
|
'url': request.path
|
2018-10-04 08:59:31 +00:00
|
|
|
}
|
|
|
|
return jsonify(ret)
|
2018-09-30 17:40:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManufacturerView(View):
|
|
|
|
class FindArgs(marshmallow.Schema):
|
|
|
|
name = marshmallow.fields.Str(required=True,
|
|
|
|
# Disallow like operators
|
|
|
|
validate=lambda x: '%' not in x and '_' not in x)
|
|
|
|
|
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):
|
|
|
|
name = args['name']
|
|
|
|
manufacturers = Manufacturer.query \
|
|
|
|
.filter(Manufacturer.name.ilike(name + '%')) \
|
|
|
|
.paginate(page=1, per_page=6) # type: Pagination
|
|
|
|
return jsonify(
|
|
|
|
items=app.resources[Manufacturer.t].schema.dump(
|
|
|
|
manufacturers.items,
|
|
|
|
many=True,
|
|
|
|
nested=1
|
|
|
|
)
|
|
|
|
)
|