This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/resources/device/views.py

376 lines
13 KiB
Python
Raw Normal View History

2018-10-03 12:51:22 +00:00
import datetime
2023-05-19 16:17:22 +00:00
import flask
2018-09-30 17:40:28 +00:00
import marshmallow
from ereuseapi.methods import API
2022-07-18 15:56:54 +00:00
from flask import Response
from flask import current_app as app
from flask import g, render_template, request, session
2018-09-30 17:40:28 +00:00
from flask.json import jsonify
from flask_sqlalchemy import Pagination
2022-07-18 15:56:54 +00:00
from marshmallow import fields
from marshmallow import fields as f
from marshmallow import validate as v
from sqlalchemy.util import OrderedSet
2018-04-10 15:06:39 +00:00
2018-10-03 12:51:22 +00:00
from ereuse_devicehub import auth
from ereuse_devicehub.db import db
from ereuse_devicehub.query import SearchQueryParser, things_response
from ereuse_devicehub.resources import search
from ereuse_devicehub.resources.action import models as actions
2022-07-18 15:56:54 +00:00
from ereuse_devicehub.resources.action.models import Trade
from ereuse_devicehub.resources.device import states
2022-07-18 15:56:54 +00:00
from ereuse_devicehub.resources.device.models import Computer, Device, Manufacturer
from ereuse_devicehub.resources.device.search import DeviceSearch
from ereuse_devicehub.resources.lot.models import LotDeviceDescendants
from ereuse_devicehub.resources.tag.model import Tag
2023-03-21 11:08:13 +00:00
from ereuse_devicehub.teal import query
from ereuse_devicehub.teal.cache import cache
from ereuse_devicehub.teal.db import ResourceNotFound
from ereuse_devicehub.teal.marshmallow import ValidationError
from ereuse_devicehub.teal.resource import View
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(actions.Rate._rating, f.Float())
appearance = query.Between(actions.Rate._appearance, f.Float())
functionality = query.Between(actions.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.Equal(LotDeviceDescendants.ancestor_lot_id, fields.UUID()))
2018-10-06 10:45:56 +00:00
class Filters(query.Query):
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))
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)
2022-07-18 15:56:54 +00:00
rating = query.Join(
(Device.id == actions.ActionWithOneDevice.device_id)
& (actions.ActionWithOneDevice.id == actions.Rate.id),
RateQ,
)
2018-10-06 10:45:56 +00:00
tag = query.Join(Device.id == Tag.device_id, TagQ)
# 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
2022-07-18 15:56:54 +00:00
lot = query.Join((Device.id == LotDeviceDescendants.device_id), LotQ)
class Sorting(query.Sort):
2018-10-06 10:45:56 +00:00
id = query.SortField(Device.id)
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):
QUERY_PARSER = SearchQueryParser()
class FindArgs(marshmallow.Schema):
search = f.Str()
filter = f.Nested(Filters, missing=[])
sort = f.Nested(Sorting, missing=[Device.id.asc()])
page = f.Integer(validate=v.Range(min=1), missing=1)
unassign = f.Integer(validate=v.Range(min=0, max=1), missing=0)
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)
def patch(self, id):
dev = Device.query.filter_by(id=id, owner_id=g.user.id, active=True).one()
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(
2022-07-18 15:56:54 +00:00
only=['transfer_state', 'actions_one'], partial=True
)
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)
db.session.commit()
return Response(status=204)
raise ValueError('Cannot patch a non computer')
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 get_rols(self):
2023-05-19 15:05:02 +00:00
rols = session.get('rols')
if not g.user.is_authenticated and not rols:
return []
2023-05-19 15:05:02 +00:00
if rols:
return [(k, k) for k in rols]
2023-06-02 09:42:38 +00:00
if 'dpp' not in app.blueprints.keys():
return []
if not session.get('token_dlt'):
return []
token_dlt = session.get('token_dlt')
api_dlt = app.config.get('API_DLT')
if not token_dlt or not api_dlt:
return []
api = API(api_dlt, token_dlt, "ethereum")
result = api.check_user_roles()
if result.get('Status') != 200:
return []
if 'Success' not in result.get('Data', {}).get('status'):
return []
rols = result.get('Data', {}).get('data', {})
return [(k, k) for k, v in rols.items() if v]
2018-10-03 12:51:22 +00:00
def one_public(self, id: int):
rols = self.get_rols()
# rols = [("isOperator", "isOperator"), ("Inspector", "Inspector"), ("Recicler", "Recicler")]
rol = len(rols) == 1 and rols[0][0] or None
if 'rol' in request.args:
rol = dict(rols).get(request.args.get('rol'))
devices = Device.query.filter_by(devicehub_id=id, active=True).all()
if not devices:
devices = [Device.query.filter_by(dhid_bk=id, active=True).one()]
device = devices[0]
abstract = None
if device.binding:
return flask.redirect(device.public_link)
if device.is_abstract() == 'Twin':
abstract = device.placeholder.binding
2023-02-01 12:28:31 +00:00
placeholder = device.binding or device.placeholder
device_abstract = placeholder and placeholder.binding or device
device_real = placeholder and placeholder.device or device
2023-05-19 15:05:02 +00:00
oidc = 'oidc' in app.blueprints.keys()
return render_template(
2023-02-01 12:28:31 +00:00
'devices/layout.html',
2023-05-19 15:05:02 +00:00
oidc=oidc,
2023-02-01 12:28:31 +00:00
placeholder=placeholder,
device=device,
device_abstract=device_abstract,
device_real=device_real,
states=states,
2023-02-01 15:35:27 +00:00
abstract=abstract,
rols=rols,
rol=rol,
user=g.user,
)
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):
2022-07-18 15:56:54 +00:00
device = Device.query.filter_by(
2022-07-20 08:03:46 +00:00
devicehub_id=id, owner_id=g.user.id, active=True
2022-07-18 15:56:54 +00:00
).first()
if not device:
return self.one_public(id)
return self.schema.jsonify(device)
2018-10-03 12:51:22 +00:00
@auth.Auth.requires_auth
# @cache(datetime.timedelta(minutes=1))
def find(self, args: dict):
"""Gets many devices."""
# Compute query
query = self.query(args)
devices = query.paginate(page=args['page'], per_page=30) # type: Pagination
return things_response(
self.schema.dump(devices.items, many=True, nested=1),
2022-07-18 15:56:54 +00:00
devices.page,
devices.per_page,
devices.total,
devices.prev_num,
devices.next_num,
)
2018-09-30 17:40:28 +00:00
def query(self, args):
2021-06-06 08:02:24 +00:00
trades = Trade.query.filter(
(Trade.user_from == g.user) | (Trade.user_to == g.user)
).distinct()
trades_dev_ids = {d.id for t in trades for d in t.devices}
2022-07-18 15:56:54 +00:00
query = (
2022-07-20 08:03:46 +00:00
Device.query.filter(Device.active == True)
2022-07-18 15:56:54 +00:00
.filter((Device.owner_id == g.user.id) | (Device.id.in_(trades_dev_ids)))
.distinct()
)
2021-06-06 08:02:24 +00:00
unassign = args.get('unassign', None)
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
2022-07-18 15:56:54 +00:00
query = (
query.join(DeviceSearch)
.filter(
search.Search.match(properties, search_p)
| search.Search.match(tags, search_p)
| search.Search.match(devicehub_ids, search_p)
)
.order_by(
search.Search.rank(properties, search_p)
+ search.Search.rank(tags, search_p)
+ search.Search.rank(devicehub_ids, search_p)
)
)
if unassign:
subquery = LotDeviceDescendants.query.with_entities(
2022-07-18 15:56:54 +00:00
LotDeviceDescendants.device_id
)
query = query.filter(Device.id.notin_(subquery))
return query.filter(*args['filter']).order_by(*args['sort'])
2020-08-17 14:45:18 +00:00
class DeviceMergeView(View):
"""View for merging two devices
2020-10-27 18:13:31 +00:00
Ex. ``device/<dev1_id>/merge/<dev2_id>``.
"""
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)
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
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).
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)
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
2022-07-18 15:56:54 +00:00
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])
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)
# Moving the actions from `with_device` to `base_device`
for action in with_actions_one:
if action.parent:
action.parent = self.base_device
else:
self.base_device.actions_one.add(action)
for action in with_actions_multiple:
if action.parent:
action.parent = self.base_device
else:
self.base_device.actions_multiple.add(action)
# 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
# Properties from with_device
self.merge()
2020-10-27 18:13:31 +00:00
db.session().add(self.base_device)
2020-10-27 18:13:31 +00:00
db.session().final_flush()
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
2022-11-16 10:51:31 +00:00
self.base_device.set_hid()
2018-09-30 17:40:28 +00:00
class ManufacturerView(View):
class FindArgs(marshmallow.Schema):
2022-07-18 15:56:54 +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):
search = args['search']
2022-07-18 15:56:54 +00:00
manufacturers = Manufacturer.query.filter(
Manufacturer.name.ilike(search + '%')
).paginate(
page=1, per_page=6
) # type: Pagination
2018-09-30 17:40:28 +00:00
return jsonify(
items=app.resources[Manufacturer.t].schema.dump(
2022-07-18 15:56:54 +00:00
manufacturers.items, many=True, nested=1
2018-09-30 17:40:28 +00:00
)
)