diff --git a/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py b/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py index 0867d8f9..9212fbcf 100644 --- a/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py +++ b/ereuse_devicehub/migrations/versions/e93aec8fc41f_added_assigned_action.py @@ -27,6 +27,7 @@ def get_inv(): return INV def upgrade(): + # Allocate action op.drop_table('allocate', schema=f'{get_inv()}') 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 '), @@ -39,7 +40,22 @@ def upgrade(): op.drop_table('deallocate', schema=f'{get_inv()}') + # Add allocate as a column in device op.add_column('device', sa.Column('allocate', sa.Boolean(), nullable=True), schema=f'{get_inv()}') + # Receive action + op.drop_table('receive', schema=f'{get_inv()}') + op.create_table('receive', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('agent_from_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('agent_to_id', postgresql.UUID(as_uuid=True), nullable=False), + + sa.ForeignKeyConstraint(['agent_from_id'], [f'{get_inv()}.agent.id'], ), + sa.ForeignKeyConstraint(['agent_to_id'], [f'{get_inv()}.agent.id'], ), + sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ), + sa.PrimaryKeyConstraint('id'), + schema=f'{get_inv()}' + ) + def downgrade(): op.drop_table('allocate', schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 734a5207..638f28be 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1433,21 +1433,25 @@ class MakeAvailable(ActionWithMultipleDevices): class Receive(JoinedTableMixin, ActionWithMultipleDevices): """The act of physically taking delivery of a device. - The receiver confirms that the devices have arrived, and thus, - they are the + The Agent confirm than the device changes hands and have a new possessor :attr:`ereuse_devicehub.resources.device.models.Device.physical_possessor`. - - This differs from :class:`.Trade` in that trading changes the - political possession. As an example, a transporter can *receive* - a device but it is not it's owner. After the delivery, the - transporter performs another *receive* to the final owner. - - The receiver can optionally take a - :class:`ereuse_devicehub.resources.enums.ReceiverRole`. """ - role = Column(DBEnum(ReceiverRole), - nullable=False, - default=ReceiverRole.Intermediary) + agent_from_id = Column(UUID(as_uuid=True), + ForeignKey(Agent.id), + nullable=False, + default=lambda: g.user.individual.id) + agent_from = relationship(Agent, + backref=backref('actions_agent', lazy=True, **_sorted_actions), + primaryjoin=agent_from_id == Agent.id) + agent_from_id.comment = """ This device go from this agent """ + agent_to_id = Column(UUID(as_uuid=True), + ForeignKey(Agent.id), + nullable=False, + default=lambda: g.user.individual.id) + agent_to = relationship(Agent, + backref=backref('actions_agent', lazy=True, **_sorted_actions), + primaryjoin=agent_to_id == Agent.id) + agent_to_id.comment = """ This device go to this agent """ class Migrate(JoinedTableMixin, ActionWithMultipleDevices): diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 682327bb..21688eed 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -221,6 +221,22 @@ class Device(Thing): action = self.last_action_of(*states.Physical.actions()) return states.Physical(action.__class__) + @property + def traking(self): + """The actual traking state, None otherwise.""" + from ereuse_devicehub.resources.device import states + with suppress(LookupError, ValueError): + action = self.last_action_of(*states.Traking.actions()) + return states.Traking(action.__class__) + + @property + def usage(self): + """The actual usage state, None otherwise.""" + from ereuse_devicehub.resources.device import states + with suppress(LookupError, ValueError): + action = self.last_action_of(*states.Usage.actions()) + return states.Usage(action.__class__) + @property def physical_possessor(self): """The actual physical possessor or None. @@ -240,7 +256,7 @@ class Device(Thing): from ereuse_devicehub.resources.action.models import Receive with suppress(LookupError): action = self.last_action_of(Receive) - return action.agent + return action.to @property def working(self): diff --git a/ereuse_devicehub/resources/device/states.py b/ereuse_devicehub/resources/device/states.py index b0d0d439..8a916419 100644 --- a/ereuse_devicehub/resources/device/states.py +++ b/ereuse_devicehub/resources/device/states.py @@ -51,11 +51,27 @@ class Physical(State): :cvar Preparing: The device is going to be or being prepared. :cvar Prepared: The device has been prepared. :cvar Ready: The device is in working conditions. - :cvar InUse: The device is being reported to be in active use. """ ToBeRepaired = e.ToRepair Repaired = e.Repair Preparing = e.ToPrepare Prepared = e.Prepare Ready = e.Ready + + +class Traking(State): + """Traking states. + + :cvar Receive: The device changes hands + """ + Receive = e.Receive + + +class Usage(State): + """Usage states. + + :cvar Allocate: The device is allocate in other Agent (organization, person ...) + :cvar InUse: The device is being reported to be in active use. + """ + Allocate = e.Allocate InUse = e.Live