diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index a9d8ac5e..2dc91ea0 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -132,7 +132,10 @@ class SanitizationEntityForm(FlaskForm): logo = URLField( 'Logo', [validators.Optional(), validators.URL()], - render_kw={'class': "form-control"}, + render_kw={ + 'class': "form-control", + "placeholder": "Url where is the logo - acceptd only .png, .jpg, .gif, svg", + }, ) company_name = StringField('Company Name', render_kw={'class': "form-control"}) location = StringField('Location', render_kw={'class': "form-control"}) @@ -154,18 +157,29 @@ class SanitizationEntityForm(FlaskForm): if not is_valid: return False + if not self.logo.data: + return True + + extensions = ["jpg", "jpeg", "png", "gif", "svg"] + if self.logo.data.lower().split(".")[-1] not in extensions: + txt = "Error in Url field - accepted only .PNG, .JPG and .GIF. extensions" + self.logo.errors = [txt] + return False + return True def save(self, commit=True): - sanitation_data = SanitizationEntity( - logo=URL(self.logo.data), - company_name=self.company_name.data, - location=self.location.data, - responsable_person=self.responsable_person.data, - supervisor_person=self.supervisor_person.data, - user=g.user, - ) - db.session.add(sanitation_data) + if isinstance(self.logo.data, str): + self.logo.data = URL(self.logo.data) + + sanitation_data = SanitizationEntity.query.filter_by(user_id=g.user.id).first() + + if not sanitation_data: + sanitation_data = SanitizationEntity(user_id=g.user.id) + self.populate_obj(sanitation_data) + db.session.add(sanitation_data) + else: + self.populate_obj(sanitation_data) if commit: db.session.commit() diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 543ef0ea..4f75c759 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -1540,7 +1540,10 @@ class CustomerDetailsForm(FlaskForm): logo = URLField( 'Logo', [validators.Optional()], - render_kw={'class': "form-control"}, + render_kw={ + 'class': "form-control", + "placeholder": "Url where is the logo - acceptd only .png, .jpg, .gif, svg", + }, description="Url where is the logo", ) @@ -1559,7 +1562,20 @@ class CustomerDetailsForm(FlaskForm): def validate(self, extra_validators=None): is_valid = super().validate(extra_validators) - return is_valid + + if not is_valid: + return is_valid + + if not self.logo.data: + return True + + extensions = ["jpg", "jpeg", "png", "gif", "svg"] + if self.logo.data.lower().split(".")[-1] not in extensions: + txt = "Error in Url field - accepted only .PNG, .JPG and .GIF. extensions" + self.logo.errors = [txt] + return False + + return True def save(self, commit=True): self.populate_obj(self._obj) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 835f95e6..2526a850 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -841,6 +841,21 @@ class NewTransferView(GenericMixin): return flask.render_template(self.template_name, **self.context) +class OpenTransferView(GenericMixin): + methods = ['GET'] + + def dispatch_request(self, lot_id=None): + lot = Lot.query.filter_by(id=lot_id).one() + next_url = url_for('inventory.lotdevicelist', lot_id=str(lot_id)) + + if hasattr(lot, 'transfer'): + lot.transfer.date = None + db.session.commit() + messages.success('Transfer was reopen successfully!') + + return flask.redirect(next_url) + + class EditTransferView(GenericMixin): methods = ['POST'] form_class = EditTransferForm @@ -1058,22 +1073,45 @@ class ExportsView(View): erasures.append(device.privacy) return erasures - def get_costum_details(self): + def get_costum_details(self, erasures): 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] + lot = None + if hasattr(g.user, 'sanitization_entity'): + my_data = g.user.sanitization_entity + + customer_details = self.get_customer_details_from_request() + + if not erasures or customer_details: + return my_data, customer_details + + lots = {erasures[0].device.get_last_incoming_lot()} + for e in erasures[1:]: + lots.add(e.device.get_last_incoming_lot()) + + if len(lots) != 1: + return my_data, customer_details + + lot = lots.pop() try: - if len(request.referrer.split('/lot/')) > 1: - 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 + customer_details = lot.transfer.customer_details except Exception: pass + return my_data, customer_details + def get_customer_details_from_request(self): + try: + if len(request.referrer.split('/lot/')) < 2: + return + + lot_id = request.referrer.split('/lot/')[-1].split('/')[0] + lot = Lot.query.filter_by(owner=g.user).filter_by(id=lot_id).first() + return lot.transfer.customer_details + except Exception: + pass + def get_server_erasure_hosts(self, erasures): erasures_host = [] erasures_on_server = [] @@ -1094,15 +1132,22 @@ class ExportsView(View): erasures[0].snapshot.version, ) - my_data, customer_details = self.get_costum_details() + my_data, customer_details = self.get_costum_details(erasures) a, b = self.get_server_erasure_hosts(erasures) erasures_host, erasures_on_server = a, b + erasures_host = set(erasures_host) result = 'Success' if "Failed" in [e.severity.get_public_name() for e in erasures]: result = 'Failed' + erasures = sorted(erasures, key=lambda x: x.end_time) + erasures_on_server = sorted(erasures_on_server, key=lambda x: x.end_time) + erasures_normal = list(set(erasures) - set(erasures_on_server)) + erasures_normal = sorted(erasures_normal, key=lambda x: x.end_time) + n_computers = len({x.parent for x in erasures} - erasures_host) + params = { 'title': 'Erasure Certificate', 'erasures': tuple(erasures), @@ -1111,11 +1156,11 @@ class ExportsView(View): 'uuid_report': '{}'.format(uuid.uuid4()), 'software': software, 'my_data': my_data, - 'n_computers': len(set([x.parent for x in erasures])), + 'n_computers': n_computers, 'result': result, 'customer_details': customer_details, 'erasure_hosts': erasures_host, - 'erasures_normal': list(set(erasures) - set(erasures_on_server)), + 'erasures_normal': erasures_normal, } return flask.render_template('inventory/erasure.html', **params) @@ -1571,3 +1616,7 @@ devices.add_url_rule( '/device/erasure//', view_func=ErasureListView.as_view('device_erasure_list_orphans'), ) +devices.add_url_rule( + '/lot//opentransfer/', + view_func=OpenTransferView.as_view('open_transfer'), +) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index a06469ea..05c47d7b 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -678,6 +678,12 @@ class Device(Thing): return args def get_lots_for_template(self): + if self.binding: + return self.binding.device.get_lots_for_template() + + if not self.lots and hasattr(self, 'parent') and self.parent: + return self.parent.get_lots_for_template() + lots = [] for lot in self.lots: if lot.is_incoming: @@ -1056,6 +1062,25 @@ class Device(Thing): return + def get_last_incoming_lot(self): + lots = list(self.lots) + if hasattr(self, "orphan") and self.orphan: + lots = list(self.lots) + if self.binding: + lots = list(self.binding.device.lots) + + elif hasattr(self, "parent") and self.parent: + lots = list(self.parent.lots) + if self.parent.binding: + lots = list(self.parent.binding.device.lots) + + lots = sorted(lots, key=lambda x: x.created) + lots.reverse() + for lot in lots: + if lot.is_incoming: + return lot + return None + def __lt__(self, other): return self.id < other.id diff --git a/ereuse_devicehub/resources/user/models.py b/ereuse_devicehub/resources/user/models.py index ff8d0051..2805e42a 100644 --- a/ereuse_devicehub/resources/user/models.py +++ b/ereuse_devicehub/resources/user/models.py @@ -3,12 +3,12 @@ from uuid import uuid4 from citext import CIText from flask import current_app as app -from flask import session +from flask import g, session from flask_login import UserMixin from sqlalchemy import BigInteger, Boolean, Column, Sequence from sqlalchemy.dialects.postgresql import UUID from sqlalchemy_utils import EmailType, PasswordType -from teal.db import URL, IntEnum +from teal.db import CASCADE_OWN, URL, IntEnum from ereuse_devicehub.db import db from ereuse_devicehub.resources.enums import SessionType @@ -181,18 +181,24 @@ class Session(Thing): class SanitizationEntity(Thing): - id = Column(BigInteger, primary_key=True) - company_name = Column(db.String, nullable=True) - location = Column(db.String, nullable=True) - logo = Column(db.String, nullable=True) - logo = Column(URL(), nullable=True) - responsable_person = Column(db.String, nullable=True) - supervisor_person = Column(db.String, nullable=True) - user_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey(User.id)) + id = db.Column(BigInteger, primary_key=True) + company_name = db.Column(db.String, nullable=True) + location = db.Column(db.String, nullable=True) + # logo = db.Column(db.String, nullable=True) + logo = db.Column(URL(), nullable=True) + responsable_person = db.Column(db.String, nullable=True) + supervisor_person = db.Column(db.String, nullable=True) + user_id = db.Column( + db.UUID(as_uuid=True), + db.ForeignKey(User.id), + default=lambda: g.user.id, + ) user = db.relationship( User, - backref=db.backref('sanitization_entity', lazy=True, collection_class=set), - collection_class=set, + backref=db.backref( + 'sanitization_entity', lazy=True, uselist=False, cascade=CASCADE_OWN + ), + primaryjoin=user_id == User.id, ) def __str__(self) -> str: diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index f89c93f6..7f5ac904 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -516,13 +516,13 @@ {% if lot and not lot.is_temporary %}
- +
Documents
@@ -565,6 +565,15 @@
+ {% if form_transfer.date.data %} + + {% endif %}
Transfer
{{ form_transfer.csrf_token }} diff --git a/ereuse_devicehub/templates/inventory/erasure.html b/ereuse_devicehub/templates/inventory/erasure.html index 76df0694..06ad6943 100644 --- a/ereuse_devicehub/templates/inventory/erasure.html +++ b/ereuse_devicehub/templates/inventory/erasure.html @@ -159,19 +159,20 @@ {% if erasure_hosts %} - {% for e in erasure_hosts %} - {% endfor %} - {% else %} + {% endif %} + {% if n_computers %}
- N° of sanitization server ({{ loop.index }}/{{ erasure_hosts|length }}): + SNs of sanitization server: + {% for e in erasure_hosts %} {% if e.serial_number %} - {{ e.serial_number.upper() }} + {{ e.serial_number.upper() }}{% if not loop.last %},{% endif %} {% endif %} + {% endfor %}
N° of computers: @@ -245,7 +246,7 @@ -
+
@@ -266,14 +267,9 @@ {% if erasures %} - {% if erasure_hosts %} - {% for server in erasure_hosts %}
-

Server Summary

-
-
-

SN Server {{ server.serial_number and server.serial_number.upper() }}

+

Summary

@@ -282,6 +278,7 @@
+ @@ -289,51 +286,12 @@ {% for erasure in erasures %} - {% if erasure.parent == server %} - - - - {% endif %} - {% endfor %} - -
SN StorageSN Host Method Result Date
{{ erasure.device.serial_number.upper() }} - {{ erasure.get_public_name() }} - - {{ erasure.severity.get_public_name() }} - - {{ erasure.date_str }} -
-
-
- {% endfor %} - {% endif %} - {% if erasures_normal %} -
-
-

Devices Summary

-
-
-
-
- - - - - - - - - - - {% for erasure in erasures_normal %} - -
SN StorageMethodResultDate
- {{ erasure.device.serial_number.upper() }} + {{ erasure.parent.serial_number.upper() }} {{ erasure.get_public_name() }} @@ -350,7 +308,6 @@
- {% endif %}
{% for erasure in erasures %}
@@ -366,12 +323,14 @@
Hid: {{ erasure.parent.hid }}
Tags: {{ erasure.parent.tags }}
+ {% if erasure.device.parent %}
Computer where it resides:
Title: {{ erasure.device.parent.__format__('ts') }}
DevicehubID: {{ erasure.device.parent.dhid }}
Hid: {{ erasure.device.parent.hid }}
Tags: {{ erasure.device.parent.tags }}
- {% endif %} + {% endif %} + {% endif %}
Erasure:
{{ erasure.__format__('ts') }}
diff --git a/ereuse_devicehub/templates/inventory/erasure_list.html b/ereuse_devicehub/templates/inventory/erasure_list.html index 2f36de71..e2154c3e 100644 --- a/ereuse_devicehub/templates/inventory/erasure_list.html +++ b/ereuse_devicehub/templates/inventory/erasure_list.html @@ -187,9 +187,9 @@ {% endif %} {{ ac.device.serial_number.upper() }} {% endif %} - {% if ac.device.my_partner.lots | length > 0 %} + {% if ac.device.get_lots_for_template() | length > 0 %}
- {% for lot in ac.device.my_partner.get_lots_for_template() %} + {% for lot in ac.device.get_lots_for_template() %} {{ lot }} {% endfor %}
diff --git a/ereuse_devicehub/views.py b/ereuse_devicehub/views.py index f2cd4bc1..8f9a5e1d 100644 --- a/ereuse_devicehub/views.py +++ b/ereuse_devicehub/views.py @@ -102,7 +102,7 @@ class UserProfileView(GenericMixin): self.get_context() sanitization_form = SanitizationEntityForm() if g.user.sanitization_entity: - sanitization = list(g.user.sanitization_entity)[0] + sanitization = g.user.sanitization_entity sanitization_form = SanitizationEntityForm(obj=sanitization) self.context.update( { @@ -138,14 +138,17 @@ class SanitizationEntityView(View): def dispatch_request(self): form = SanitizationEntityForm() - db.session.commit() if form.validate_on_submit(): - form.save(commit=False) - messages.success('Sanitization datas updated successfully!') + form.save() + messages.success('Sanitization data updated successfully!') else: - messages.error('Error modifying Sanitization datas!') + messages.error('Error modifying Sanitization data!') + if form.errors: + for k in form.errors.keys(): + errors = ", ".join(form.errors[k]) + txt = "{}: {}".format(k, errors) + messages.error(txt) - db.session.commit() return flask.redirect(flask.url_for('core.user-profile')) diff --git a/tests/test_basic.py b/tests/test_basic.py index e9927a0e..4b734079 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -68,6 +68,7 @@ def test_api_docs(client: Client): '/inventory/lot/{lot_id}/receivernote/', '/inventory/lot/{lot_id}/trade-document/add/', '/inventory/lot/{lot_id}/transfer/{type_id}/', + '/inventory/lot/{lot_id}/opentransfer/', '/inventory/lot/{lot_id}/transfer/', '/inventory/lot/transfer/{type_id}/', '/inventory/lot/{lot_id}/upload-snapshot/',