From faa7c1d6058b6348db7c1521d7b4344620595d6b Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 18 Jan 2023 18:56:04 +0100 Subject: [PATCH] . --- ereuse_devicehub/config.py | 2 + ereuse_devicehub/resources/did/__init__.py | 0 ereuse_devicehub/resources/did/did.py | 115 ++++++++++++++++++ ereuse_devicehub/resources/did/models.py | 100 +++++++++++++++ .../resources/did/templates/chid.html | 73 +++++++++++ .../resources/did/templates/dpp.html | 83 +++++++++++++ .../resources/did/templates/layout.html | 26 ++++ .../resources/did/templates/stamp.html | 74 +++++++++++ 8 files changed, 473 insertions(+) create mode 100644 ereuse_devicehub/resources/did/__init__.py create mode 100644 ereuse_devicehub/resources/did/did.py create mode 100644 ereuse_devicehub/resources/did/models.py create mode 100644 ereuse_devicehub/resources/did/templates/chid.html create mode 100644 ereuse_devicehub/resources/did/templates/dpp.html create mode 100644 ereuse_devicehub/resources/did/templates/layout.html create mode 100644 ereuse_devicehub/resources/did/templates/stamp.html diff --git a/ereuse_devicehub/config.py b/ereuse_devicehub/config.py index f2b66492..86e6dbf0 100644 --- a/ereuse_devicehub/config.py +++ b/ereuse_devicehub/config.py @@ -18,6 +18,7 @@ from ereuse_devicehub.resources import ( ) from ereuse_devicehub.resources.device import definitions from ereuse_devicehub.resources.documents import documents +from ereuse_devicehub.resources.did import did from ereuse_devicehub.resources.enums import PriceSoftware from ereuse_devicehub.resources.licences import licences from ereuse_devicehub.resources.metric import definitions as metric_def @@ -36,6 +37,7 @@ class DevicehubConfig(Config): import_resource(lot), import_resource(deliverynote), import_resource(documents), + import_resource(did), import_resource(tradedocument), import_resource(inventory), import_resource(versions), diff --git a/ereuse_devicehub/resources/did/__init__.py b/ereuse_devicehub/resources/did/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/ereuse_devicehub/resources/did/did.py b/ereuse_devicehub/resources/did/did.py new file mode 100644 index 00000000..fd066171 --- /dev/null +++ b/ereuse_devicehub/resources/did/did.py @@ -0,0 +1,115 @@ +import csv +import json +import enum +import uuid +import time +import datetime +from collections import OrderedDict +from io import StringIO +from typing import Callable, Iterable, Tuple + +import boltons +import flask +import flask_weasyprint +import teal.marshmallow +from boltons import urlutils +from flask import make_response, g, request +from flask import current_app as app +from flask.json import jsonify +from teal.cache import cache +from teal.resource import Resource, View + +from ereuse_devicehub import auth +from ereuse_devicehub.db import db +from ereuse_devicehub.resources.device.models import Device +from ereuse_devicehub.resources.did.models import Dpp + + +class DidView(View): + """ + This view render one public ans static page for see the links for to do the check + of one csv file + """ + def get_url_path(self): + url = urlutils.URL(request.url) + url.normalize() + url.path_parts = url.path_parts[:-2] + ['check', ''] + return url.to_text() + + def get(self, dpp: str): + self.dpp = dpp + template = 'dpp.html' + if len(dpp.split(":")) == 2: + result = Dpp.query.filter_by(key=dpp).one() + else: + result = Device.query.filter_by(chid=dpp).one() + template = 'chid.html' + + if 'json' not in request.headers['Accept']: + result = self.get_result(result, template) + return flask.render_template(template, rq_url=self.get_url_path(), + result={"dpp": dpp, "result": result}) + + return jsonify(self.get_result(result, template)) + + def get_result(self, dpp, template): + data = { + 'hardware': {}, + 'dpp': self.dpp, + } + result = {'data': data} + + if template == 'dpp.html': + data['hardware'] = json.loads(dpp.snapshot.json_hw) + last_dpp = self.get_last_dpp(dpp) + url_last = '' + if last_dpp: + url_last = 'http://did.ereuse.org/{did}'.format(did=last_dpp) + data['url_last'] = url_last + return result + + # if dpp is not a dpp then is a device + device = dpp + dpps = [] + for d in device.dpps: + rr = {'dpp': d.key, 'hardware': json.loads(d.snapshot.json_hw)} + dpps.append(rr) + return {'data': dpps} + + def get_last_dpp(self, dpp): + dpps = [act.dpp[0] for act in dpp.device.actions if act.t == 'Snapshot' and act.dpp] + last_dpp = '' + for d in dpps: + if d.key == dpp.key: + return last_dpp + last_dpp = d.key + + return last_dpp + + +class DidDef(Resource): + __type__ = 'Did' + SCHEMA = None + VIEW = None # We do not want to create default / documents endpoint + AUTH = False + + def __init__(self, app, + import_name=__name__, + static_folder='static', + static_url_path=None, + template_folder='templates', + url_prefix=None, + subdomain=None, + url_defaults=None, + root_path=None, + cli_commands: Iterable[Tuple[Callable, str or None]] = tuple()): + super().__init__(app, import_name, static_folder, static_url_path, template_folder, + url_prefix, subdomain, url_defaults, root_path, cli_commands) + + view = DidView.as_view('main', definition=self, auth=app.auth) + + # if self.AUTH: + # view = app.auth.requires_auth(view) + + did_view = DidView.as_view('DidView', definition=self, auth=app.auth) + self.add_url_rule('/', defaults={}, view_func=did_view, methods={'GET'}) diff --git a/ereuse_devicehub/resources/did/models.py b/ereuse_devicehub/resources/did/models.py new file mode 100644 index 00000000..03c84e21 --- /dev/null +++ b/ereuse_devicehub/resources/did/models.py @@ -0,0 +1,100 @@ +from sortedcontainers import SortedSet +from sqlalchemy.util import OrderedSet +from sqlalchemy import Unicode, BigInteger, Column, Sequence, ForeignKey +from sqlalchemy.orm import backref, relationship +from sqlalchemy.dialects.postgresql import UUID +from flask import g +from teal.db import CASCADE_OWN +from ereuse_devicehub.resources.user.models import User +from ereuse_devicehub.resources.device.models import Device +from ereuse_devicehub.resources.action.models import Snapshot, ActionStatus + + +from ereuse_devicehub.resources.models import Thing, STR_SM_SIZE + + +PROOF_ENUM = { + 'Register': 'Register', + 'IssueDPP': 'IssueDPP', + 'proof_of_recycling': 'proof_of_recycling', +} + +_sorted_proofs = { + 'order_by': lambda: Proof.created, + 'collection_class': SortedSet +} + + +class Proof(Thing): + id = Column(BigInteger, Sequence('device_seq'), primary_key=True) + id.comment = """The identifier of the device for this database. Used only + internally for software; users should not use this.""" + documentId = Column(Unicode(STR_SM_SIZE), nullable=True) + documentId.comment = "is the hash of snapshot.json_wb" + documentSignature = Column(Unicode(STR_SM_SIZE), nullable=True) + documentSignature.comment = "is the snapshot.json_wb with the signature of the user" + timestamp = Column(BigInteger, nullable=False) + type = Column(Unicode(STR_SM_SIZE), nullable=False) + + issuer_id = Column(UUID(as_uuid=True), + ForeignKey(User.id), + nullable=False, + default=lambda: g.user.id) + issuer = relationship(User, + backref=backref('issuered_proofs', lazy=True, collection_class=set), + primaryjoin=User.id == issuer_id) + issuer_id.comment = """The user that recorded this proof in the system.""" + device_id = Column(BigInteger, ForeignKey(Device.id), nullable=False) + device = relationship(Device, + backref=backref('proofs', + lazy=True, + cascade=CASCADE_OWN), + primaryjoin=Device.id == device_id) + + snapshot_id = Column(UUID(as_uuid=True), ForeignKey(Snapshot.id), nullable=True) + snapshot = relationship(Snapshot, + backref=backref('proofs', lazy=True), + collection_class=OrderedSet, + primaryjoin=Snapshot.id == snapshot_id) + + action_status_id = Column(UUID(as_uuid=True), ForeignKey(ActionStatus.id), nullable=True) + action_status = relationship(ActionStatus, + backref=backref('proofs', lazy=True), + primaryjoin=ActionStatus.id == action_status_id) + +class Dpp(Thing): + """ + Digital PassPort: + It is a type of proof with some field more. + Is the official Digital Passport + + """ + id = Column(BigInteger, Sequence('device_seq'), primary_key=True) + key = Column(Unicode(STR_SM_SIZE), nullable=False) + key.comment = "chid:phid, (chid it's in device and phid it's in the snapshot)" + documentId = Column(Unicode(STR_SM_SIZE), nullable=True) + documentId.comment = "is the hash of snapshot.json_wb" + documentSignature = Column(Unicode(STR_SM_SIZE), nullable=True) + documentSignature.comment = "is the snapshot.json_wb with the signature of the user" + timestamp = Column(BigInteger, nullable=False) + + issuer_id = Column(UUID(as_uuid=True), + ForeignKey(User.id), + nullable=False, + default=lambda: g.user.id) + issuer = relationship(User, + backref=backref('issuered_dpp', lazy=True, collection_class=set), + primaryjoin=User.id == issuer_id) + issuer_id.comment = """The user that recorded this proof in the system.""" + device_id = Column(BigInteger, ForeignKey(Device.id), nullable=False) + device = relationship(Device, + backref=backref('dpps', + lazy=True, + cascade=CASCADE_OWN), + primaryjoin=Device.id == device_id) + + snapshot_id = Column(UUID(as_uuid=True), ForeignKey(Snapshot.id), nullable=False) + snapshot = relationship(Snapshot, + backref=backref('dpp', lazy=True), + collection_class=OrderedSet, + primaryjoin=Snapshot.id == snapshot_id) diff --git a/ereuse_devicehub/resources/did/templates/chid.html b/ereuse_devicehub/resources/did/templates/chid.html new file mode 100644 index 00000000..75f15af3 --- /dev/null +++ b/ereuse_devicehub/resources/did/templates/chid.html @@ -0,0 +1,73 @@ + + + + + + + + Devicehub | Stamp create and Stamp verify + + + + +
+
+ +
+ {% for dpp in result.result.data %} +
Digital Passport
+ {{ dpp.dpp }} +
Hardware
+
    +
  • Device
  • +
      +
    • Chassis: {{ dpp.hardware.device.chassis }}
    • +
    • Manufacturer: {{ dpp.hardware.device.manufacturer }}
    • +
    • Model: {{ dpp.hardware.device.model }}
    • +
    • SerialNumber: {{ dpp.hardware.device.serialNumber }}
    • +
    • Sku: {{ dpp.hardware.device.sku }}
    • +
    • Type: {{ dpp.hardware.device.type }}
    • +
    • Version: {{ dpp.hardware.device.version }}
    • +
    +
  • Components
  • +
      + {% for component in dpp.hardware.components %} +
    • {{ component }}
    • + {% endfor %} +
    +
+
+ {% endfor %} +
+
+
+ + diff --git a/ereuse_devicehub/resources/did/templates/dpp.html b/ereuse_devicehub/resources/did/templates/dpp.html new file mode 100644 index 00000000..3a447355 --- /dev/null +++ b/ereuse_devicehub/resources/did/templates/dpp.html @@ -0,0 +1,83 @@ + + + + + + + + Devicehub | Stamp create and Stamp verify + + + + +
+
+ +
+
Hardware
+
    +
  • Device
  • +
      +
    • Chassis: {{ result.result.data.hardware.device.chassis }}
    • +
    • Manufacturer: {{ result.result.data.hardware.device.manufacturer }}
    • +
    • Model: {{ result.result.data.hardware.device.model }}
    • +
    • SerialNumber: {{ result.result.data.hardware.device.serialNumber }}
    • +
    • Sku: {{ result.result.data.hardware.device.sku }}
    • +
    • Type: {{ result.result.data.hardware.device.type }}
    • +
    • Version: {{ result.result.data.hardware.device.version }}
    • +
    +
  • Components
  • +
      + {% for component in result.result.data.hardware.components %} +
    • {{ component }}
    • + {% endfor %} +
    +
+ + {% if result.result.data.url_last %} +
Last Digital passport
+ {{ result.result.data.url_last }} + {% endif %} + + {% if result.result.data.digitalPassports %} +
Digital Passports
+
    + {% for dpp in result.result.data.digitalPassports %} +
  • {{ dpp }}
  • + {% endfor %} +
+ {% endif %} +
+
+
+
+ + diff --git a/ereuse_devicehub/resources/did/templates/layout.html b/ereuse_devicehub/resources/did/templates/layout.html new file mode 100644 index 00000000..3794f343 --- /dev/null +++ b/ereuse_devicehub/resources/did/templates/layout.html @@ -0,0 +1,26 @@ +{% import 'devices/macros.html' as macros %} + + + + + + + + USOdy | {{ title }} + + +
+
+ +
+ {% block body %}{% endblock %} +
+ + diff --git a/ereuse_devicehub/resources/did/templates/stamp.html b/ereuse_devicehub/resources/did/templates/stamp.html new file mode 100644 index 00000000..d07efc49 --- /dev/null +++ b/ereuse_devicehub/resources/did/templates/stamp.html @@ -0,0 +1,74 @@ + + + + + + + + Devicehub | Stamp create and Stamp verify + + + + +
+ + +
+ +
+
+ +
+ +
+
+
+
+ +