From 9fb2b1c94a2170a3b08bacec71e7f82c3403ff2e Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 8 Feb 2024 18:40:02 +0100 Subject: [PATCH] add snapthot and dpp to action EWaste --- ereuse_devicehub/inventory/forms.py | 3 ++ ereuse_devicehub/resources/action/models.py | 40 +++++++++++++++++++++ ereuse_devicehub/views.py | 6 ++-- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index e2741841..dbcf67f6 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -841,6 +841,9 @@ class ActionFormMixin(FlaskForm): if not self._devices: return False + if len(devices) > 1 and self.type.data == 'EWaste': + return False + return True def generic_validation(self, extra_validators=None): diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index a63ae110..708de0ca 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -902,6 +902,7 @@ class Snapshot(JoinedWithOneDeviceMixin, ActionWithOneDevice): from ereuse_devicehub.modules.dpp.models import Dpp, ALGORITHM dpp = "{chid}:{phid}".format(chid=self.device.chid, phid=self.phid_dpp) + # '54f431688524825f00d6fb786d5211e642e3d4564187f9ad23627ec9d313f17c' if Dpp.query.filter_by(key=dpp).all(): return @@ -1785,11 +1786,50 @@ class EWaste(ActionWithMultipleDevices): proof = Proof(**d) db.session.add(proof) + self.create_snapshot() + def generateDocSig(self): if not self.doc: return return hashlib.sha3_256(self.doc.encode('utf-8')).hexdigest() + def create_snapshot(self): + for device in self.devices: + dev = device.placeholder.binding + hw = self.create_json_hw(dev) + phid = hashlib.sha3_256(hw.encode('utf-8')).hexdigest() + version = self.last_snap.version + software = self.last_snap.software + + snap = Snapshot( + author=dev.owner, + uuid=uuid4(), + device=dev, + json_hw = hw, + phid_dpp = phid, + version = version, + software = software + ) + db.session.add(snap) + snap.register_passport_dlt() + self.snapshot = snap + + def create_json_hw(self, device): + hw = self.get_json_hw(device) + hw['e-waste'] = True + return json.dumps(hw) + + def get_json_hw(self, device): + self.last_snap = None + try: + self.last_snap = device.last_action_of(Snapshot) + except Exception: + return {} + + if self.last_snap.json_hw: + return json.loads(self.last_snap.json_hw) + return {} + class ToPrepare(ActionWithMultipleDevices): """The device has been selected for preparation. diff --git a/ereuse_devicehub/views.py b/ereuse_devicehub/views.py index ef695413..a8922470 100644 --- a/ereuse_devicehub/views.py +++ b/ereuse_devicehub/views.py @@ -28,9 +28,9 @@ class LoginView(View): template_name = 'ereuse_devicehub/user_login.html' def dispatch_request(self): - if session.get('_user_id'): - next_url = flask.request.args.get('next') - return flask.redirect(next_url or flask.url_for('inventory.devicelist')) + # if session.get('_user_id'): + # next_url = flask.request.args.get('next') + # return flask.redirect(next_url or flask.url_for('inventory.devicelist')) form = LoginForm() if form.validate_on_submit():