change assigned for allocate

This commit is contained in:
Cayo Puigdefabregas 2020-11-18 18:13:13 +01:00
parent 64fe93491c
commit 16735d0199
9 changed files with 124 additions and 94 deletions

View File

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

View File

@ -27,9 +27,18 @@ def get_inv():
return INV return INV
def upgrade(): def upgrade():
op.create_table('assigned', op.drop_table('allocate')
sa.Column('assigned', citext.CIText(), nullable=True, comment=' This is a internal code for mainteing the secrets of the personal datas of the new holder '), op.create_table('allocate',
sa.Column('n_beneficiaries', sa.Numeric(precision=4), nullable=False), sa.Column('code', citext.CIText(), nullable=True, comment=' This is a internal code for mainteing the secrets of the personal datas of the new holder '),
sa.Column('end_users', sa.Numeric(precision=4), nullable=False),
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()}'
)
op.drop_table('deallocate')
op.create_table('deallocate',
sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False),
sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ), sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ),
sa.PrimaryKeyConstraint('id'), sa.PrimaryKeyConstraint('id'),
@ -38,4 +47,4 @@ def upgrade():
def downgrade(): def downgrade():
op.drop_table('assigned') op.drop_table('allocate')

View File

@ -312,15 +312,19 @@ class Remove(ActionWithOneDevice):
class Allocate(JoinedTableMixin, ActionWithMultipleDevices): class Allocate(JoinedTableMixin, ActionWithMultipleDevices):
to_id = Column(UUID, ForeignKey(User.id)) """The act of assigned one list of devices to one person of the system or not
to = relationship(User, primaryjoin=User.id == to_id) """
organization = Column(CIText()) 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): class Deallocate(JoinedTableMixin, ActionWithMultipleDevices):
from_id = Column(UUID, ForeignKey(User.id)) @property
from_rel = relationship(User, primaryjoin=User.id == from_id) def allocate(self):
organization = Column(CIText()) """The URL of the allocate than closes one allocate. """
allocate = [a for a in self.devices[0].actions if is_instance(action, 'Allocate')][-1]
return urlutils.URL(url_for_resource('Allocate', item=allocate))
class EraseBasic(JoinedWithOneDeviceMixin, ActionWithOneDevice): class EraseBasic(JoinedWithOneDeviceMixin, ActionWithOneDevice):
@ -1472,14 +1476,6 @@ class MigrateFrom(Migrate):
pass pass
class Assigned(JoinedTableMixin, ActionWithMultipleDevices):
"""The act of assigned one list of devices to one person of the system or not
"""
assigned = Column(CIText(), default='', nullable=True)
assigned.comment = """ This is a internal code for mainteing the secrets of the personal datas of the new holder """
n_beneficiaries = Column(Numeric(precision=4), check_range('n_beneficiaries', 0), nullable=False)
# Listeners # Listeners
# Listeners validate values and keep relationships synced # Listeners validate values and keep relationships synced

View File

@ -64,21 +64,20 @@ class Remove(ActionWithOneDevice):
class Allocate(ActionWithMultipleDevices): class Allocate(ActionWithMultipleDevices):
__doc__ = m.Allocate.__doc__ __doc__ = m.Allocate.__doc__
to = NestedOn(s_user.User, agent = NestedOn(s_agent.Agent, only_query='id', required=False, comment=m.Trade.to_comment)
description='The user the devices are allocated to.') description = SanitizedStr(default='', description=m.Action.description.comment)
organization = SanitizedStr(validate=Length(max=STR_SIZE), start_time = DateTime(data_key='start_time', description=m.Action.start_time.comment)
description='The organization where the ' end_time = DateTime(data_key='end_time', description=m.Action.end_time.comment)
'user was when this happened.') code = SanitizedStr(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")],
required=True)
class Deallocate(ActionWithMultipleDevices): class Deallocate(ActionWithMultipleDevices):
__doc__ = m.Deallocate.__doc__ __doc__ = m.Deallocate.__doc__
from_rel = Nested(s_user.User, end_time = DateTime(data_key='end_time', description=m.Action.end_time.comment)
data_key='from',
description='The user where the devices are not allocated to anymore.')
organization = SanitizedStr(validate=Length(max=STR_SIZE),
description='The organization where the '
'user was when this happened.')
class EraseBasic(ActionWithOneDevice): class EraseBasic(ActionWithOneDevice):
@ -457,15 +456,3 @@ class MigrateFrom(Migrate):
class Transferred(ActionWithMultipleDevices): class Transferred(ActionWithMultipleDevices):
__doc__ = m.Transferred.__doc__ __doc__ = m.Transferred.__doc__
class Assigned(ActionWithMultipleDevices):
__doc__ = m.Assigned.__doc__
agent = NestedOn(s_agent.Agent, only_query='id', required=False, comment=m.Trade.to_comment)
description = SanitizedStr(default='', description=m.Action.description.comment)
start_time = DateTime(data_key='startTime', description=m.Action.start_time.comment)
end_time = DateTime(data_key='endTime', description=m.Action.end_time.comment)
assigned = SanitizedStr(validate=Length(min=1, max=STR_BIG_SIZE),
required=False,
description='The code of the agent to assigned.')
n_beneficiaries = Integer(validate=[Range(min=1, error="Value must be greater than 0")],
required=True)

View File

@ -0,0 +1,15 @@
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, DeAllocateView
class AssignedDef(Resource):
VIEW = AllocateView
SCHEMA = schemas.Allocate
# class EndAssignedDef(Resource):
# VIEW = DeAllocateView
# SCHEMA = schemas.DeAllocate

View File

@ -0,0 +1,74 @@
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
class AllocateView(View):
@auth.Auth.requires_auth
def get(self, id: uuid.UUID) -> Allocate:
return super().get(id)
@auth.Auth.requires_auth
def post(self):
""" Create one rent """
res_json = request.get_json()
assigned = Allocate(**res_json)
db.session.add(assigned)
db.session().final_flush()
ret = self.schema.jsonify(assigned)
ret.status_code = 201
db.session.commit()
return ret
def find(self, args: dict):
rents = Allocate.query.filter_by(author=g.user) \
.order_by(Allocate.created.desc()) \
.paginate(per_page=200)
return things_response(
self.schema.dump(rents.items, many=True, nested=0),
rents.page, rents.per_page, rents.total, rents.prev_num, rents.next_num
)
def one(self, id: uuid.UUID):
"""Gets one action."""
assigned = Allocate.query.filter_by(id=id, author=g.user).one()
return self.schema.jsonify(assigned, nested=2)
class DeAllocateView(View):
@auth.Auth.requires_auth
def get(self, id: uuid.UUID) -> Allocate:
return super().get(id)
@auth.Auth.requires_auth
def post(self):
""" Create one rent """
res_json = request.get_json()
assigned = Allocate(**res_json)
db.session.add(assigned)
db.session().final_flush()
ret = self.schema.jsonify(assigned)
ret.status_code = 201
db.session.commit()
return ret
def find(self, args: dict):
rents = Allocate.query.filter_by(author=g.user) \
.order_by(Allocate.created.desc()) \
.paginate(per_page=200)
return things_response(
self.schema.dump(rents.items, many=True, nested=0),
rents.page, rents.per_page, rents.total, rents.prev_num, rents.next_num
)
def one(self, id: uuid.UUID):
"""Gets one action."""
assigned = Allocate.query.filter_by(id=id, author=g.user).one()
return self.schema.jsonify(assigned, nested=2)

View File

@ -1,9 +0,0 @@
from typing import Callable, Iterable, Tuple
from ereuse_devicehub.resources.action import schemas
from teal.resource import Resource
from ereuse_devicehub.resources.assigned.views import AssignedView
class AssignedDef(Resource):
VIEW = AssignedView
SCHEMA = schemas.Assigned

View File

@ -1,42 +0,0 @@
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 Assigned
class AssignedView(View):
@auth.Auth.requires_auth
def get(self, id: uuid.UUID) -> Assigned:
return super().get(id)
@auth.Auth.requires_auth
def post(self):
""" Create one rent """
res_json = request.get_json()
assigned = Assigned(**res_json)
db.session.add(assigned)
db.session().final_flush()
ret = self.schema.jsonify(assigned)
ret.status_code = 201
db.session.commit()
return ret
def find(self, args: dict):
rents = Assigned.query.filter_by(author=g.user) \
.order_by(Assigned.created.desc()) \
.paginate(per_page=200)
return things_response(
self.schema.dump(rents.items, many=True, nested=0),
rents.page, rents.per_page, rents.total, rents.prev_num, rents.next_num
)
def one(self, id: uuid.UUID):
"""Gets one action."""
assigned = Assigned.query.filter_by(id=id, author=g.user).one()
return self.schema.jsonify(assigned, nested=2)