From 16735d0199d243c85b61a581fa8f36cbfbc3b8be Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 18 Nov 2020 18:13:13 +0100 Subject: [PATCH] change assigned for allocate --- ereuse_devicehub/config.py | 4 +- .../e93aec8fc41f_added_assigned_action.py | 17 ++++- ereuse_devicehub/resources/action/models.py | 24 +++--- ereuse_devicehub/resources/action/schemas.py | 33 +++------ .../{assigned => allocate}/__init__.py | 0 .../resources/allocate/definitions.py | 15 ++++ ereuse_devicehub/resources/allocate/views.py | 74 +++++++++++++++++++ .../resources/assigned/definitions.py | 9 --- ereuse_devicehub/resources/assigned/views.py | 42 ----------- 9 files changed, 124 insertions(+), 94 deletions(-) rename ereuse_devicehub/resources/{assigned => allocate}/__init__.py (100%) create mode 100644 ereuse_devicehub/resources/allocate/definitions.py create mode 100644 ereuse_devicehub/resources/allocate/views.py delete mode 100644 ereuse_devicehub/resources/assigned/definitions.py delete mode 100644 ereuse_devicehub/resources/assigned/views.py diff --git a/ereuse_devicehub/config.py b/ereuse_devicehub/config.py index d5226c26..6de4a9e1 100644 --- a/ereuse_devicehub/config.py +++ b/ereuse_devicehub/config.py @@ -14,7 +14,7 @@ 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.assigned import definitions as assigned_def +from ereuse_devicehub.resources.allocate import definitions as allocate_def class DevicehubConfig(Config): @@ -29,7 +29,7 @@ class DevicehubConfig(Config): import_resource(documents), import_resource(inventory), import_resource(versions), - import_resource(assigned_def)), + import_resource(allocate_def)), ) PASSWORD_SCHEMES = {'pbkdf2_sha256'} # type: Set[str] DB_USER = config('DB_USER', 'dhub') diff --git a/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py b/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py index 7caec6c0..273ba1c3 100644 --- a/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py +++ b/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py @@ -27,9 +27,18 @@ def get_inv(): return INV def upgrade(): - op.create_table('assigned', - 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 '), - sa.Column('n_beneficiaries', sa.Numeric(precision=4), nullable=False), + op.drop_table('allocate') + op.create_table('allocate', + 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.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ), sa.PrimaryKeyConstraint('id'), @@ -38,4 +47,4 @@ def upgrade(): def downgrade(): - op.drop_table('assigned') + op.drop_table('allocate') diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index e75964bf..5f442e2d 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -312,15 +312,19 @@ class Remove(ActionWithOneDevice): class Allocate(JoinedTableMixin, ActionWithMultipleDevices): - to_id = Column(UUID, ForeignKey(User.id)) - to = relationship(User, primaryjoin=User.id == to_id) - organization = Column(CIText()) + """The act of assigned one list of devices to one person of the system or not + """ + 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): - from_id = Column(UUID, ForeignKey(User.id)) - from_rel = relationship(User, primaryjoin=User.id == from_id) - organization = Column(CIText()) + @property + def allocate(self): + """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): @@ -1472,14 +1476,6 @@ class MigrateFrom(Migrate): 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 validate values and keep relationships synced diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index ebd4cd97..554317eb 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -64,21 +64,20 @@ class Remove(ActionWithOneDevice): class Allocate(ActionWithMultipleDevices): __doc__ = m.Allocate.__doc__ - to = NestedOn(s_user.User, - description='The user the devices are allocated to.') - organization = SanitizedStr(validate=Length(max=STR_SIZE), - description='The organization where the ' - 'user was when this happened.') + 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='start_time', description=m.Action.start_time.comment) + end_time = DateTime(data_key='end_time', description=m.Action.end_time.comment) + 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): __doc__ = m.Deallocate.__doc__ - from_rel = Nested(s_user.User, - 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.') + end_time = DateTime(data_key='end_time', description=m.Action.end_time.comment) class EraseBasic(ActionWithOneDevice): @@ -457,15 +456,3 @@ class MigrateFrom(Migrate): class Transferred(ActionWithMultipleDevices): __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) diff --git a/ereuse_devicehub/resources/assigned/__init__.py b/ereuse_devicehub/resources/allocate/__init__.py similarity index 100% rename from ereuse_devicehub/resources/assigned/__init__.py rename to ereuse_devicehub/resources/allocate/__init__.py diff --git a/ereuse_devicehub/resources/allocate/definitions.py b/ereuse_devicehub/resources/allocate/definitions.py new file mode 100644 index 00000000..e5c6f62c --- /dev/null +++ b/ereuse_devicehub/resources/allocate/definitions.py @@ -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 + diff --git a/ereuse_devicehub/resources/allocate/views.py b/ereuse_devicehub/resources/allocate/views.py new file mode 100644 index 00000000..b87c592e --- /dev/null +++ b/ereuse_devicehub/resources/allocate/views.py @@ -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) diff --git a/ereuse_devicehub/resources/assigned/definitions.py b/ereuse_devicehub/resources/assigned/definitions.py deleted file mode 100644 index f2ce97ba..00000000 --- a/ereuse_devicehub/resources/assigned/definitions.py +++ /dev/null @@ -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 diff --git a/ereuse_devicehub/resources/assigned/views.py b/ereuse_devicehub/resources/assigned/views.py deleted file mode 100644 index e07471cd..00000000 --- a/ereuse_devicehub/resources/assigned/views.py +++ /dev/null @@ -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)