diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index dbcf67f6..c870861c 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -43,6 +43,7 @@ from ereuse_devicehub.parser.parser import ParseSnapshotLsHw from ereuse_devicehub.parser.schemas import Snapshot_lite from ereuse_devicehub.resources.action.models import Snapshot, Trade from ereuse_devicehub.resources.action.schemas import EWaste as EWasteSchema +from ereuse_devicehub.resources.action.schemas import Recycled as RecycledSchema from ereuse_devicehub.resources.action.schemas import Snapshot as SnapshotSchema from ereuse_devicehub.resources.action.views.snapshot import ( SnapshotMixin, @@ -866,6 +867,11 @@ class ActionFormMixin(FlaskForm): doc = "{}".format(ewaste) self.instance.register_proof(doc) + if self.instance.type == 'Recycled': + recycled = RecycledSchema().dump(self.instance) + doc = "{}".format(recycled) + self.instance.register_proof(doc) + db.session.commit() self.devices.data = devices diff --git a/ereuse_devicehub/modules/dpp/models.py b/ereuse_devicehub/modules/dpp/models.py index 2b359acd..387a8c66 100644 --- a/ereuse_devicehub/modules/dpp/models.py +++ b/ereuse_devicehub/modules/dpp/models.py @@ -22,6 +22,7 @@ PROOF_ENUM = { 'proof_of_recycling': 'proof_of_recycling', 'Erase': 'Erase', 'EWaste': 'EWaste', + 'Recycled': 'Device_recycled', } diff --git a/ereuse_devicehub/resources/action/__init__.py b/ereuse_devicehub/resources/action/__init__.py index 4deb17c6..bfd92b36 100644 --- a/ereuse_devicehub/resources/action/__init__.py +++ b/ereuse_devicehub/resources/action/__init__.py @@ -221,6 +221,11 @@ class EWasteDef(ActionDef): SCHEMA = schemas.EWaste +class RecycledDef(ActionDef): + VIEW = None + SCHEMA = schemas.Recycled + + class RecyclingDef(ActionDef): VIEW = None SCHEMA = schemas.Recycling diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 708de0ca..89ac097c 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1720,6 +1720,74 @@ class Ready(ActionWithMultipleDevices): """ +class Recycled(ActionWithMultipleDevices): + + def register_proof(self, doc): + """This method is used for register a proof of erasure en dlt.""" + + if 'dpp' not in app.blueprints.keys(): + return + + if not session.get('token_dlt'): + return + + if not doc: + return + + self.doc = doc + token_dlt = session.get('token_dlt') + api_dlt = app.config.get('API_DLT') + dh_instance = app.config.get('ID_FEDERATED', 'dh1') + if not token_dlt or not api_dlt: + return + + api = API(api_dlt, token_dlt, "ethereum") + + from ereuse_devicehub.modules.dpp.models import ( + PROOF_ENUM, + Proof, + ALGORITHM + ) + from ereuse_devicehub.resources.enums import StatusCode + + for device in self.devices: + deviceCHID = device.chid + docHash = self.generateDocSig() + docHashAlgorithm = ALGORITHM + proof_type = PROOF_ENUM['Recycled'] + result = api.generate_proof( + deviceCHID, + docHashAlgorithm, + docHash, + proof_type, + dh_instance, + ) + + if result['Status'] == StatusCode.Success.value: + timestamp = result.get('Data', {}).get('data', {}).get('timestamp') + + if not timestamp: + return + + d = { + "type": PROOF_ENUM['Recycled'], + "device": device, + "action": self, + "documentId": self.id, + "timestamp": timestamp, + "issuer_id": g.user.id, + "documentSignature": docHash, + "normalizeDoc": self.doc, + } + proof = Proof(**d) + db.session.add(proof) + + def generateDocSig(self): + if not self.doc: + return + return hashlib.sha3_256(self.doc.encode('utf-8')).hexdigest() + + class EWaste(ActionWithMultipleDevices): """The device is declared as e-waste, this device is not allow use more. diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index 12063755..a534731e 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -527,6 +527,10 @@ class EWaste(ActionWithMultipleDevicesCheckingOwner): __doc__ = m.EWaste.__doc__ +class Recycled(ActionWithMultipleDevicesCheckingOwner): + __doc__ = m.Recycled.__doc__ + + class ActionStatus(Action): rol_user = NestedOn(s_user.User, dump_only=True, exclude=('token',)) devices = NestedOn( diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index f5452f1e..111cecf8 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -232,6 +232,12 @@ E-Waste +