diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 1f973841..26a4d6e1 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -1,7 +1,9 @@ import copy import csv +import datetime import logging import os +import uuid from io import StringIO from pathlib import Path @@ -1043,7 +1045,7 @@ class ExportsView(View): return self.response_csv(data, "Erasures.csv") - def build_erasure_certificate(self): + def get_datastorages(self): erasures = [] for device in self.find_devices(): if device.placeholder and device.placeholder.binding: @@ -1054,11 +1056,66 @@ class ExportsView(View): elif isinstance(device, DataStorage): if device.privacy: erasures.append(device.privacy) + return erasures + + def get_custum_details(self): + my_data = None + customer_details = None + if hasattr(g.user, 'sanitization_entity'): + if g.user.sanitization_entity: + my_data = list(g.user.sanitization_entity)[0] + + if len(request.referrer.split('/lot/')) > 1: + try: + lot_id = request.referrer.split('/lot/')[-1].split('/')[0] + lot = Lot.query.filter_by(owner=g.user).filter_by(id=lot_id).first() + customer_details = lot.transfer.customer_details + except Exception: + pass + return my_data, customer_details + + def get_server_erasure_hosts(self, erasures): + erasures_host = [] + erasures_on_server = [] + for erase in erasures: + try: + if erase.parent.binding.kangaroo: + erasures_host.append(erase.parent) + erasures_on_server.append(erase) + except Exception: + pass + return erasures_host, erasures_on_server + + def build_erasure_certificate(self): + erasures = self.get_datastorages() + software = 'USODY DRIVE ERASURE' + if erasures and erasures[0].snapshot: + software += ' {}'.format( + erasures[0].snapshot.version, + ) + + my_data, customer_details = self.get_custum_details() + + a, b = self.get_server_erasure_hosts(erasures) + erasures_host, erasures_on_server = a, b + + result = 'Success' + if "Failed" in [e.severity.get_public_name() for e in erasures]: + result = 'Failed' params = { 'title': 'Erasure Certificate', 'erasures': tuple(erasures), 'url_pdf': '', + 'date_report': '{:%c}'.format(datetime.datetime.now()), + 'uuid_report': '{}'.format(uuid.uuid4()), + 'software': software, + 'my_data': my_data, + 'n_computers': len(set([x.parent for x in erasures])), + 'result': result, + 'customer_details': customer_details, + 'erasure_hosts': erasures_host, + 'erasures_normal': list(set(erasures) - set(erasures_on_server)), } return flask.render_template('inventory/erasure.html', **params) diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 1f373962..f1bbb5dd 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -481,6 +481,9 @@ class EraseBasic(JoinedWithOneDeviceMixin, ActionWithOneDevice): return self.snapshot.device.phid() return '' + def get_public_name(self): + return "Basic" + def __str__(self) -> str: return '{} on {}.'.format(self.severity, self.date_str) @@ -510,12 +513,33 @@ class EraseSectors(EraseBasic): method = 'Badblocks' + def get_public_name(self): + # import pdb; pdb.set_trace() + steps_random = 0 + steps_zeros = 0 + for s in self.steps: + if s.type == 'StepRandom': + steps_random += 1 + if s.type == 'StepZero': + steps_zeros += 1 + if steps_zeros < 1: + return "Basic" + if 0 < steps_random < 3: + return "Baseline" + if steps_random > 2: + return "Enhanced" + + return "Basic" + class ErasePhysical(EraseBasic): """The act of physically destroying a data storage unit.""" method = Column(DBEnum(PhysicalErasureMethod)) + def get_public_name(self): + return "Physical" + class Step(db.Model): erasure_id = Column( diff --git a/ereuse_devicehub/resources/enums.py b/ereuse_devicehub/resources/enums.py index bbbcdc10..2d527802 100644 --- a/ereuse_devicehub/resources/enums.py +++ b/ereuse_devicehub/resources/enums.py @@ -334,6 +334,12 @@ class Severity(IntEnum): def __format__(self, format_spec): return str(self) + def get_public_name(self): + if self.value == 3: + return "Failed" + + return "Success" + class PhysicalErasureMethod(Enum): """Methods of physically erasing the data-storage, usually diff --git a/ereuse_devicehub/templates/inventory/erasure.html b/ereuse_devicehub/templates/inventory/erasure.html index 67bfa1ad..2ccd2ad1 100644 --- a/ereuse_devicehub/templates/inventory/erasure.html +++ b/ereuse_devicehub/templates/inventory/erasure.html @@ -68,16 +68,16 @@
2022-12-26 16:51:32 (+0100), USODY DRIVE ERASURE 2022
+{{ date_report }}, {{ software }}
+ N° of sanitization server ({{ loop.index }}/{{ erasure_hosts|length }}): + | ++ {% if e.serial_number %} + {{ e.serial_number.upper() }} + {% endif %} + | +
N° of computers: | - 16 + {{ n_computers }} |
N° of data storage unit(s): | - 16 + {{ erasures | length }} |
- Failed + {{ result }} |
SN Storage | +Method | +Result | +Date | +
---|---|---|---|
+ {{ erasure.device.serial_number.upper() }} + | ++ {{ erasure.get_public_name() }} + | ++ {{ erasure.severity.get_public_name() }} + | ++ {{ erasure.date_str }} + | +
SN Storage | -Method | -Result | -Date | +SN Storage | +Method | +Result | +Date |
---|---|---|---|---|---|---|---|
{{ erasure.device.serial_number.upper() }} | - {{ erasure.type }} + {{ erasure.get_public_name() }} | - {{ erasure.severity }} + {{ erasure.severity.get_public_name() }} | {{ erasure.date_str }} @@ -283,10 +350,11 @@ |