2018-10-03 12:51:22 +00:00
|
|
|
import datetime
|
2020-07-07 12:58:37 +00:00
|
|
|
import uuid
|
|
|
|
from itertools import filterfalse
|
2018-10-03 12:51:22 +00:00
|
|
|
|
2018-09-30 17:40:28 +00:00
|
|
|
import marshmallow
|
2020-04-22 15:53:06 +00:00
|
|
|
from flask import g, current_app as app, render_template, request, Response
|
2018-09-30 17:40:28 +00:00
|
|
|
from flask.json import jsonify
|
|
|
|
from flask_sqlalchemy import Pagination
|
2020-10-28 12:30:22 +00:00
|
|
|
from sqlalchemy.util import OrderedSet
|
2020-08-17 14:45:18 +00:00
|
|
|
from marshmallow import fields, fields as f, validate as v, Schema as MarshmallowSchema
|
2018-10-04 08:59:31 +00:00
|
|
|
from teal import query
|
2020-10-28 12:30:22 +00:00
|
|
|
from teal.db import ResourceNotFound
|
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
|
2020-10-28 12:30:22 +00:00
|
|
|
from teal.marshmallow import ValidationError
|
2018-04-10 15:06:39 +00:00
|
|
|
|
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
|
2019-05-11 14:27:22 +00:00
|
|
|
from ereuse_devicehub.resources.action import models as actions
|
2019-07-07 19:36:09 +00:00
|
|
|
from ereuse_devicehub.resources.device import states
|
2020-03-19 00:55:26 +00:00
|
|
|
from ereuse_devicehub.resources.device.models import Device, Manufacturer, Computer
|
2018-10-04 08:59:31 +00:00
|
|
|
from ereuse_devicehub.resources.device.search import DeviceSearch
|
2020-08-17 14:45:18 +00:00
|
|
|
from ereuse_devicehub.resources.enums import SnapshotSoftware
|
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):
|
2019-05-11 14:27:22 +00:00
|
|
|
rating = query.Between(actions.Rate._rating, f.Float())
|
|
|
|
appearance = query.Between(actions.Rate._appearance, f.Float())
|
|
|
|
functionality = query.Between(actions.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()))
|
2021-04-26 13:22:30 +00:00
|
|
|
devicehub_id = query.Or(query.ILike(Device.devicehub_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)
|
2019-02-05 16:59:15 +00:00
|
|
|
# todo test query for rating (and possibly other filters)
|
2019-05-11 14:27:22 +00:00
|
|
|
rating = query.Join((Device.id == actions.ActionWithOneDevice.device_id)
|
|
|
|
& (actions.ActionWithOneDevice.id == actions.Rate.id),
|
2019-02-05 16:59:15 +00:00
|
|
|
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
|
2020-04-28 19:20:40 +00:00
|
|
|
lot = query.Join((Device.id == LotDeviceDescendants.device_id),
|
2020-04-22 15:53:06 +00:00
|
|
|
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):
|
2018-11-11 21:18:10 +00:00
|
|
|
search = f.Str()
|
2018-10-04 08:59:31 +00:00
|
|
|
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):
|
2019-06-19 11:35:26 +00:00
|
|
|
"""Devices view
|
2018-06-24 14:57:49 +00:00
|
|
|
---
|
|
|
|
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)
|
2020-04-01 17:56:09 +00:00
|
|
|
|
2020-03-19 00:55:26 +00:00
|
|
|
def patch(self, id):
|
2020-11-06 16:13:37 +00:00
|
|
|
dev = Device.query.filter_by(id=id, owner_id=g.user.id).one()
|
2020-03-19 00:55:26 +00:00
|
|
|
if isinstance(dev, Computer):
|
|
|
|
resource_def = app.resources['Computer']
|
|
|
|
# TODO check how to handle the 'actions_one'
|
2020-08-17 14:45:18 +00:00
|
|
|
patch_schema = resource_def.SCHEMA(
|
2021-02-10 12:05:43 +00:00
|
|
|
only=['transfer_state', 'actions_one'], partial=True)
|
2020-03-19 00:55:26 +00:00
|
|
|
json = request.get_json(schema=patch_schema)
|
|
|
|
# TODO check how to handle the 'actions_one'
|
|
|
|
json.pop('actions_one')
|
|
|
|
if not dev:
|
|
|
|
raise ValueError('Device non existent')
|
|
|
|
for key, value in json.items():
|
2020-08-17 14:45:18 +00:00
|
|
|
setattr(dev, key, value)
|
2020-03-19 00:55:26 +00:00
|
|
|
db.session.commit()
|
|
|
|
return Response(status=204)
|
|
|
|
raise ValueError('Cannot patch a non computer')
|
2020-04-01 17:56:09 +00:00
|
|
|
|
2021-03-08 21:43:24 +00:00
|
|
|
def one(self, id: str):
|
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):
|
2021-03-08 21:43:24 +00:00
|
|
|
device = Device.query.filter_by(devicehub_id=id).one()
|
2019-07-07 19:36:09 +00:00
|
|
|
return render_template('devices/layout.html', device=device, states=states)
|
2018-10-03 12:51:22 +00:00
|
|
|
|
|
|
|
@auth.Auth.requires_auth
|
2021-03-08 21:43:24 +00:00
|
|
|
def one_private(self, id: str):
|
|
|
|
device = Device.query.filter_by(devicehub_id=id, owner_id=g.user.id).first()
|
2020-11-06 16:13:37 +00:00
|
|
|
if not device:
|
|
|
|
return self.one_public(id)
|
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
|
2020-04-01 17:56:09 +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):
|
2020-11-04 16:30:12 +00:00
|
|
|
query = Device.query.filter((Device.owner_id == g.user.id)).distinct()
|
2018-11-21 13:26:56 +00:00
|
|
|
search_p = args.get('search', None)
|
|
|
|
if search_p:
|
|
|
|
properties = DeviceSearch.properties
|
|
|
|
tags = DeviceSearch.tags
|
2021-04-26 13:22:30 +00:00
|
|
|
devicehub_ids = DeviceSearch.devicehub_ids
|
2018-11-21 13:26:56 +00:00
|
|
|
query = query.join(DeviceSearch).filter(
|
2021-04-26 13:22:30 +00:00
|
|
|
search.Search.match(properties, search_p) |
|
|
|
|
search.Search.match(tags, search_p) |
|
|
|
|
search.Search.match(devicehub_ids, search_p)
|
2018-11-21 13:26:56 +00:00
|
|
|
).order_by(
|
2021-04-26 13:22:30 +00:00
|
|
|
search.Search.rank(properties, search_p) +
|
|
|
|
search.Search.rank(tags, search_p) +
|
|
|
|
search.Search.rank(devicehub_ids, search_p)
|
2018-11-21 13:26:56 +00:00
|
|
|
)
|
|
|
|
return query.filter(*args['filter']).order_by(*args['sort'])
|
|
|
|
|
2020-07-07 12:58:37 +00:00
|
|
|
|
2020-08-17 14:45:18 +00:00
|
|
|
class DeviceMergeView(View):
|
2020-07-07 12:58:37 +00:00
|
|
|
"""View for merging two devices
|
2020-10-27 18:13:31 +00:00
|
|
|
Ex. ``device/<dev1_id>/merge/<dev2_id>``.
|
2020-07-07 12:58:37 +00:00
|
|
|
"""
|
2020-08-17 14:45:18 +00:00
|
|
|
|
2020-10-27 18:13:31 +00:00
|
|
|
def post(self, dev1_id: int, dev2_id: int):
|
|
|
|
device = self.merge_devices(dev1_id, dev2_id)
|
2020-07-07 12:58:37 +00:00
|
|
|
|
|
|
|
ret = self.schema.jsonify(device)
|
|
|
|
ret.status_code = 201
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
return ret
|
|
|
|
|
2020-10-27 18:13:31 +00:00
|
|
|
@auth.Auth.requires_auth
|
2020-10-28 12:30:22 +00:00
|
|
|
def merge_devices(self, dev1_id: int, dev2_id: int) -> Device:
|
2020-10-27 18:13:31 +00:00
|
|
|
"""Merge the current device with `with_device` (dev2_id) by
|
|
|
|
adding all `with_device` actions under the current device, (dev1_id).
|
2020-07-07 12:58:37 +00:00
|
|
|
|
|
|
|
This operation is highly costly as it forces refreshing
|
|
|
|
many models in session.
|
|
|
|
"""
|
2020-10-27 18:13:31 +00:00
|
|
|
# base_device = Device.query.filter_by(id=dev1_id, owner_id=g.user.id).one()
|
2020-12-09 10:12:47 +00:00
|
|
|
self.base_device = Device.query.filter_by(id=dev1_id, owner_id=g.user.id).one()
|
|
|
|
self.with_device = Device.query.filter_by(id=dev2_id, owner_id=g.user.id).one()
|
|
|
|
|
|
|
|
if self.base_device.allocated or self.with_device.allocated:
|
|
|
|
# Validation than any device is allocated
|
|
|
|
msg = 'The device is allocated, please deallocated before merge.'
|
|
|
|
raise ValidationError(msg)
|
2020-10-28 12:30:22 +00:00
|
|
|
|
|
|
|
if not self.base_device.type == self.with_device.type:
|
|
|
|
# Validation than we are speaking of the same kind of devices
|
|
|
|
raise ValidationError('The devices is not the same type.')
|
|
|
|
|
|
|
|
# Adding actions of self.with_device
|
|
|
|
with_actions_one = [a for a in self.with_device.actions
|
|
|
|
if isinstance(a, actions.ActionWithOneDevice)]
|
|
|
|
with_actions_multiple = [a for a in self.with_device.actions
|
|
|
|
if isinstance(a, actions.ActionWithMultipleDevices)]
|
|
|
|
|
|
|
|
# Moving the tags from `with_device` to `base_device`
|
|
|
|
# Union of tags the device had plus the (potentially) new ones
|
2020-10-28 21:15:28 +00:00
|
|
|
self.base_device.tags.update([x for x in self.with_device.tags])
|
2020-10-28 12:30:22 +00:00
|
|
|
self.with_device.tags.clear() # We don't want to add the transient dummy tags
|
2020-10-28 21:15:28 +00:00
|
|
|
db.session.add(self.with_device)
|
2020-10-28 12:30:22 +00:00
|
|
|
|
|
|
|
# Moving the actions from `with_device` to `base_device`
|
2020-07-07 12:58:37 +00:00
|
|
|
for action in with_actions_one:
|
|
|
|
if action.parent:
|
2020-10-28 12:30:22 +00:00
|
|
|
action.parent = self.base_device
|
2020-07-07 12:58:37 +00:00
|
|
|
else:
|
2020-10-28 12:30:22 +00:00
|
|
|
self.base_device.actions_one.add(action)
|
2020-07-07 12:58:37 +00:00
|
|
|
for action in with_actions_multiple:
|
|
|
|
if action.parent:
|
2020-10-28 12:30:22 +00:00
|
|
|
action.parent = self.base_device
|
2020-07-07 12:58:37 +00:00
|
|
|
else:
|
2020-10-28 12:30:22 +00:00
|
|
|
self.base_device.actions_multiple.add(action)
|
2020-07-07 12:58:37 +00:00
|
|
|
|
2020-10-28 12:30:22 +00:00
|
|
|
# Keeping the components of with_device
|
|
|
|
components = OrderedSet(c for c in self.with_device.components)
|
|
|
|
self.base_device.components = components
|
2020-10-27 18:13:31 +00:00
|
|
|
|
2020-10-28 12:30:22 +00:00
|
|
|
# Properties from with_device
|
|
|
|
self.merge()
|
2020-10-27 18:13:31 +00:00
|
|
|
|
2020-10-28 12:30:22 +00:00
|
|
|
db.session().add(self.base_device)
|
2020-10-27 18:13:31 +00:00
|
|
|
db.session().final_flush()
|
2020-10-28 12:30:22 +00:00
|
|
|
return self.base_device
|
|
|
|
|
|
|
|
def merge(self):
|
|
|
|
"""Copies the physical properties of the base_device to the with_device.
|
|
|
|
This method mutates base_device.
|
|
|
|
"""
|
|
|
|
for field_name, value in self.with_device.physical_properties.items():
|
|
|
|
if value is not None:
|
|
|
|
setattr(self.base_device, field_name, value)
|
|
|
|
|
|
|
|
self.base_device.hid = self.with_device.hid
|
2020-11-25 18:09:24 +00:00
|
|
|
self.base_device.add_mac_to_hid()
|
2020-07-07 12:58:37 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
)
|