deallocate

This commit is contained in:
Cayo Puigdefabregas 2020-11-21 13:17:23 +01:00
parent 32a17decd7
commit 0415b7efb9
4 changed files with 39 additions and 27 deletions

View file

@ -38,7 +38,14 @@ def upgrade():
schema=f'{get_inv()}'
)
# Deallocate action
op.drop_table('deallocate', schema=f'{get_inv()}')
op.create_table('deallocate',
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ),
sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}'
)
# Add allocate as a column in device
op.add_column('device', sa.Column('allocated', sa.Boolean(), nullable=True), schema=f'{get_inv()}')

View file

@ -312,13 +312,18 @@ class Remove(ActionWithOneDevice):
class Allocate(JoinedTableMixin, ActionWithMultipleDevices):
"""The act of assigned one list of devices to one person of the system or not
"""The act of allocate one list of devices to one person
"""
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=False)
class Deallocate(JoinedTableMixin, ActionWithMultipleDevices):
"""The act of deallocate one list of devices to one person of the system or not
"""
pass
class EraseBasic(JoinedWithOneDeviceMixin, ActionWithOneDevice):
"""An erasure attempt to a ``DataStorage``. The action contains
information about success and nature of the erasure.

View file

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

View file

@ -1,43 +1,39 @@
import uuid
# from typing import Callable, Iterable, Tuple
from flask import g, request
# from flask.json import jsonify
from teal.resource import View
from ereuse_devicehub import auth
from ereuse_devicehub.db import db
from ereuse_devicehub.query import things_response
from ereuse_devicehub.resources.action.models import Allocate
from ereuse_devicehub.resources.action.models import Allocate, Deallocate
class AllocateView(View):
@auth.Auth.requires_auth
def get(self, id: uuid.UUID) -> Allocate:
return super().get(id)
class AllocateMix():
model = None
@auth.Auth.requires_auth
def post(self):
""" Create one allocate """
""" Create one res_obj """
res_json = request.get_json()
allocate = Allocate(**res_json)
db.session.add(allocate)
res_obj = model(**res_json)
db.session.add(res_obj)
db.session().final_flush()
ret = self.schema.jsonify(allocate)
ret = self.schema.jsonify(res_obj)
ret.status_code = 201
db.session.commit()
return ret
def find(self, args: dict):
allocates = Allocate.query.filter_by(author=g.user) \
.order_by(Allocate.created.desc()) \
res_objs = model.query.filter_by(author=g.user) \
.order_by(model.created.desc()) \
.paginate(per_page=200)
return things_response(
self.schema.dump(allocates.items, many=True, nested=0),
allocates.page, allocates.per_page, allocates.total,
allocates.prev_num, allocates.next_num
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
)
def one(self, id: uuid.UUID):
"""Gets one action."""
allocate = Allocate.query.filter_by(id=id, author=g.user).one()
return self.schema.jsonify(allocate, nested=2)
class AllocateView(AllocateMix):
model = Allocate
class DeallocateView(AllocateMix):
model = Deallocate