Adds ethereum address to device, and adds PATCH

This commit is contained in:
emmdim 2020-03-19 01:55:26 +01:00
parent f6af58ef51
commit dfb518068d
3 changed files with 23 additions and 4 deletions

View file

@ -382,6 +382,7 @@ class Computer(Device):
It is a subset of the Linux definition of DMI / DMI decode. 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) deposit = Column(Integer, check_range('deposit',min=0,max=100), default=0)
owner_address = db.Column(CIText(), owner_address = db.Column(CIText(),
db.ForeignKey(User.ethereum_address), db.ForeignKey(User.ethereum_address),

View file

@ -122,6 +122,7 @@ class Computer(Device):
dump_only=True, dump_only=True,
collection_class=set, collection_class=set,
description=m.Computer.privacy.__doc__) description=m.Computer.privacy.__doc__)
ethereum_address = SanitizedStr(validate=f.validate.Length(max=42))
deposit = Integer(validate=f.validate.Range(min=0, max=100), deposit = Integer(validate=f.validate.Range(min=0, max=100),
description=m.Computer.deposit.__doc__) description=m.Computer.deposit.__doc__)
# author_id = NestedOn(s_user.User,only_query='author_id') # author_id = NestedOn(s_user.User,only_query='author_id')

View file

@ -1,10 +1,10 @@
import datetime import datetime
import marshmallow 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.json import jsonify
from flask_sqlalchemy import Pagination 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 import query
from teal.cache import cache from teal.cache import cache
from teal.resource import View 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 import search
from ereuse_devicehub.resources.action import models as actions from ereuse_devicehub.resources.action import models as actions
from ereuse_devicehub.resources.device import states 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.device.search import DeviceSearch
from ereuse_devicehub.resources.lot.models import LotDeviceDescendants from ereuse_devicehub.resources.lot.models import LotDeviceDescendants
from ereuse_devicehub.resources.tag.model import Tag from ereuse_devicehub.resources.tag.model import Tag
@ -92,7 +92,24 @@ class DeviceView(View):
description: The device or devices. description: The device or devices.
""" """
return super().get(id) 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): def one(self, id: int):
"""Gets one device.""" """Gets one device."""
if not request.authorization: if not request.authorization: