From dfb518068ddf6223c5d3245925fdeaa81ad49045 Mon Sep 17 00:00:00 2001 From: emmdim Date: Thu, 19 Mar 2020 01:55:26 +0100 Subject: [PATCH] Adds ethereum address to device, and adds PATCH --- ereuse_devicehub/resources/device/models.py | 1 + ereuse_devicehub/resources/device/schemas.py | 1 + ereuse_devicehub/resources/device/views.py | 25 ++++++++++++++++---- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 1ecbe8f3..68134b8e 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -382,6 +382,7 @@ class Computer(Device): It is a subset of the Linux definition of DMI / DMI decode. """ + ethereum_address = Column(CIText(), unique=True, default=None) deposit = Column(Integer, check_range('deposit',min=0,max=100), default=0) owner_address = db.Column(CIText(), db.ForeignKey(User.ethereum_address), diff --git a/ereuse_devicehub/resources/device/schemas.py b/ereuse_devicehub/resources/device/schemas.py index 093cec75..61c399ad 100644 --- a/ereuse_devicehub/resources/device/schemas.py +++ b/ereuse_devicehub/resources/device/schemas.py @@ -122,6 +122,7 @@ class Computer(Device): dump_only=True, collection_class=set, description=m.Computer.privacy.__doc__) + ethereum_address = SanitizedStr(validate=f.validate.Length(max=42)) deposit = Integer(validate=f.validate.Range(min=0, max=100), description=m.Computer.deposit.__doc__) # author_id = NestedOn(s_user.User,only_query='author_id') diff --git a/ereuse_devicehub/resources/device/views.py b/ereuse_devicehub/resources/device/views.py index 665acd68..71e77cfc 100644 --- a/ereuse_devicehub/resources/device/views.py +++ b/ereuse_devicehub/resources/device/views.py @@ -1,10 +1,10 @@ import datetime import marshmallow -from flask import current_app as app, render_template, request +from flask import current_app as app, render_template, request, Response from flask.json import jsonify from flask_sqlalchemy import Pagination -from marshmallow import fields, fields as f, validate as v +from marshmallow import fields, fields as f, validate as v, ValidationError from teal import query from teal.cache import cache from teal.resource import View @@ -15,7 +15,7 @@ from ereuse_devicehub.query import SearchQueryParser, things_response from ereuse_devicehub.resources import search from ereuse_devicehub.resources.action import models as actions from ereuse_devicehub.resources.device import states -from ereuse_devicehub.resources.device.models import Device, Manufacturer +from ereuse_devicehub.resources.device.models import Device, Manufacturer, Computer from ereuse_devicehub.resources.device.search import DeviceSearch from ereuse_devicehub.resources.lot.models import LotDeviceDescendants from ereuse_devicehub.resources.tag.model import Tag @@ -92,7 +92,24 @@ class DeviceView(View): description: The device or devices. """ return super().get(id) - + + def patch(self, id): + dev = Device.query.filter_by(id=id).one() + if isinstance(dev, Computer): + resource_def = app.resources['Computer'] + # TODO check how to handle the 'actions_one' + patch_schema = resource_def.SCHEMA(only=['ethereum_address', '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(): + setattr(dev,key,value) + db.session.commit() + return Response(status=204) + raise ValueError('Cannot patch a non computer') + def one(self, id: int): """Gets one device.""" if not request.authorization: