change the endpoints for allocate and deallocate

This commit is contained in:
Cayo Puigdefabregas 2020-11-27 18:10:51 +01:00
parent 23e124721c
commit 4172711c93
9 changed files with 53 additions and 65 deletions

View File

@ -14,7 +14,6 @@ from ereuse_devicehub.resources.device import definitions
from ereuse_devicehub.resources.documents import documents
from ereuse_devicehub.resources.enums import PriceSoftware
from ereuse_devicehub.resources.versions import versions
from ereuse_devicehub.resources.allocate import definitions as allocate_def
from ereuse_devicehub.resources.metric import definitions as metric_def
@ -30,7 +29,6 @@ class DevicehubConfig(Config):
import_resource(documents),
import_resource(inventory),
import_resource(versions),
import_resource(allocate_def),
import_resource(metric_def),
),)
PASSWORD_SCHEMES = {'pbkdf2_sha256'} # type: Set[str]

View File

@ -3,7 +3,7 @@ from typing import Callable, Iterable, Tuple
from teal.resource import Converters, Resource
from ereuse_devicehub.resources.action import schemas
from ereuse_devicehub.resources.action.views import ActionView
from ereuse_devicehub.resources.action.views import ActionView, AllocateView, DeallocateView
from ereuse_devicehub.resources.device.sync import Sync
@ -198,6 +198,16 @@ class ToPrepareDef(ActionDef):
SCHEMA = schemas.ToPrepare
class AllocateDef(ActionDef):
VIEW = AllocateView
SCHEMA = schemas.Allocate
class DeallocateDef(ActionDef):
VIEW = DeallocateView
SCHEMA = schemas.Deallocate
class PrepareDef(ActionDef):
VIEW = None
SCHEMA = schemas.Prepare

View File

@ -323,7 +323,6 @@ class Deallocate(JoinedTableMixin, ActionWithMultipleDevices):
"""
code = Column(CIText(), default='', nullable=True)
code.comment = """ This is a internal code for mainteing the secrets of the personal datas of the new holder """
end_users = Column(Numeric(precision=4), check_range('end_users', 0), nullable=True)
class EraseBasic(JoinedWithOneDeviceMixin, ActionWithOneDevice):

View File

@ -67,14 +67,14 @@ class Remove(ActionWithOneDevice):
class Allocate(ActionWithMultipleDevices):
__doc__ = m.Allocate.__doc__
start_time = DateTime(data_key='start_time', required=True,
start_time = DateTime(data_key='startTime', required=True,
description=m.Action.start_time.comment)
end_time = DateTime(data_key='end_time', required=False,
end_time = DateTime(data_key='endTime', required=False,
description=m.Action.end_time.comment)
code = SanitizedStr(data_key='transaction', validate=Length(min=1, max=STR_BIG_SIZE),
required=False,
description='The code of the agent to assigned.')
end_users = Integer(validate=[Range(min=1, error="Value must be greater than 0")])
end_users = Integer(data_key='endUsers', validate=[Range(min=1, error="Value must be greater than 0")])
@validates_schema
def validate_allocate(self, data: dict):
@ -95,7 +95,7 @@ class Allocate(ActionWithMultipleDevices):
class Deallocate(ActionWithMultipleDevices):
__doc__ = m.Deallocate.__doc__
start_time = DateTime(data_key='start_time', required=True,
start_time = DateTime(data_key='startTime', required=True,
description=m.Action.start_time.comment)
code = SanitizedStr(data_key='transaction', validate=Length(min=1, max=STR_BIG_SIZE),
required=False,

View File

@ -8,7 +8,7 @@ from distutils.version import StrictVersion
from uuid import UUID
from flask.json import jsonify
from flask import current_app as app, request, g
from flask import current_app as app, request, g, redirect
from sqlalchemy.util import OrderedSet
from teal.marshmallow import ValidationError
from teal.resource import View
@ -16,7 +16,7 @@ from teal.resource import View
from ereuse_devicehub.db import db
from ereuse_devicehub.query import things_response
from ereuse_devicehub.resources.action.models import (Action, RateComputer, Snapshot, VisualTest,
InitTransfer, Live)
InitTransfer, Live, Allocate, Deallocate)
from ereuse_devicehub.resources.action.rate.v1_0 import CannotRate
from ereuse_devicehub.resources.enums import SnapshotSoftware, Severity
from ereuse_devicehub.resources.user.exceptions import InsufficientPermission
@ -63,6 +63,38 @@ def move_json(tmp_snapshots, path_name, user):
os.remove(path_name)
class AllocateMix():
model = None
def post(self):
""" Create one res_obj """
res_json = request.get_json()
res_obj = self.model(**res_json)
db.session.add(res_obj)
db.session().final_flush()
ret = self.schema.jsonify(res_obj)
ret.status_code = 201
db.session.commit()
return ret
def find(self, args: dict):
res_objs = self.model.query.filter_by(author=g.user) \
.order_by(self.model.created.desc()) \
.paginate(per_page=200)
return things_response(
self.schema.dump(res_objs.items, many=True, nested=0),
res_objs.page, res_objs.per_page, res_objs.total,
res_objs.prev_num, res_objs.next_num
)
class AllocateView(AllocateMix, View):
model = Allocate
class DeallocateView(AllocateMix, View):
model = Deallocate
class ActionView(View):
def post(self):
"""Posts an action."""

View File

@ -1,14 +0,0 @@
from ereuse_devicehub.resources.action import schemas
from teal.resource import Resource
from ereuse_devicehub.resources.allocate.views import AllocateView, DeallocateView
class AllocateDef(Resource):
VIEW = AllocateView
SCHEMA = schemas.Allocate
AUTH = True
class DeallocateDef(Resource):
VIEW = DeallocateView
SCHEMA = schemas.Deallocate
AUTH = True

View File

@ -1,39 +0,0 @@
from flask import g, request
from teal.resource import View
from ereuse_devicehub.db import db
from ereuse_devicehub.query import things_response
from ereuse_devicehub.resources.action.models import Allocate, Deallocate
class AllocateMix():
model = None
def post(self):
""" Create one res_obj """
res_json = request.get_json()
res_obj = self.model(**res_json)
db.session.add(res_obj)
db.session().final_flush()
ret = self.schema.jsonify(res_obj)
ret.status_code = 201
db.session.commit()
return ret
def find(self, args: dict):
res_objs = self.model.query.filter_by(author=g.user) \
.order_by(self.model.created.desc()) \
.paginate(per_page=200)
return things_response(
self.schema.dump(res_objs.items, many=True, nested=0),
res_objs.page, res_objs.per_page, res_objs.total,
res_objs.prev_num, res_objs.next_num
)
class AllocateView(AllocateMix, View):
model = Allocate
class DeallocateView(AllocateMix, View):
model = Deallocate

View File

@ -152,7 +152,7 @@ class Device(Thing):
Actions are returned by descending ``created`` time.
"""
return sorted(chain(self.actions_multiple, self.actions_one))
return sorted(chain(self.actions_multiple, self.actions_one), key=lambda x: x.created)
@property
def problems(self):
@ -298,7 +298,9 @@ class Device(Thing):
"""
try:
# noinspection PyTypeHints
return next(e for e in reversed(self.actions) if isinstance(e, types))
actions = self.actions
actions.sort(key=lambda x: x.created)
return next(e for e in reversed(actions) if isinstance(e, types))
except StopIteration:
raise LookupError('{!r} does not contain actions of types {}.'.format(self, types))