From 0ff2bcae92745f567da98d6cc85cba7f3da3472e Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 7 Mar 2023 16:39:52 +0100 Subject: [PATCH 1/7] use populate_obj --- ereuse_devicehub/forms.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index 895160c5..495446b8 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -141,15 +141,17 @@ class SanitizationEntityForm(FlaskForm): 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() From 7534df083cf1e8eab498dc674eaf0ba5ca1256e8 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 7 Mar 2023 16:40:45 +0100 Subject: [PATCH 2/7] fix definition of model SanitizationEntity --- ereuse_devicehub/resources/user/models.py | 29 ++++++++++++++--------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/ereuse_devicehub/resources/user/models.py b/ereuse_devicehub/resources/user/models.py index 14e1c7ad..351907a8 100644 --- a/ereuse_devicehub/resources/user/models.py +++ b/ereuse_devicehub/resources/user/models.py @@ -1,11 +1,12 @@ from uuid import uuid4 from flask import current_app as app +from flask import g 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 @@ -122,18 +123,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: From eed1075771dee95a673cda90328ad93318154d9f Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 7 Mar 2023 16:41:24 +0100 Subject: [PATCH 3/7] save with commit for default --- ereuse_devicehub/views.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ereuse_devicehub/views.py b/ereuse_devicehub/views.py index f2cd4bc1..5d3861d9 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,13 @@ class SanitizationEntityView(View): def dispatch_request(self): form = SanitizationEntityForm() - db.session.commit() if form.validate_on_submit(): - form.save(commit=False) + form.save() messages.success('Sanitization datas updated successfully!') else: messages.error('Error modifying Sanitization datas!') - db.session.commit() + # db.session.commit() return flask.redirect(flask.url_for('core.user-profile')) From ab6f89c3d2e7231c389954613fac4563d277f902 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 8 Mar 2023 10:58:28 +0100 Subject: [PATCH 4/7] fix sanitization_entity in view --- ereuse_devicehub/inventory/views.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 835f95e6..4522c5b5 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -1062,8 +1062,7 @@ class ExportsView(View): 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] + my_data = g.user.sanitization_entity try: if len(request.referrer.split('/lot/')) > 1: From eb945ae348264eaf44f07a1e47c971b0e676c17b Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 8 Mar 2023 12:34:52 +0100 Subject: [PATCH 5/7] fix ortigraphy --- ereuse_devicehub/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/views.py b/ereuse_devicehub/views.py index 5d3861d9..915a4dc4 100644 --- a/ereuse_devicehub/views.py +++ b/ereuse_devicehub/views.py @@ -140,9 +140,9 @@ class SanitizationEntityView(View): form = SanitizationEntityForm() if form.validate_on_submit(): form.save() - messages.success('Sanitization datas updated successfully!') + messages.success('Sanitization data updated successfully!') else: - messages.error('Error modifying Sanitization datas!') + messages.error('Error modifying Sanitization data!') # db.session.commit() return flask.redirect(flask.url_for('core.user-profile')) From 54372ad2f90ee674d66b7749cd10525644e4b9ba Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 8 Mar 2023 12:43:37 +0100 Subject: [PATCH 6/7] put comment as placeholder in field of logo --- ereuse_devicehub/forms.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ereuse_devicehub/forms.py b/ereuse_devicehub/forms.py index 495446b8..68485f33 100644 --- a/ereuse_devicehub/forms.py +++ b/ereuse_devicehub/forms.py @@ -116,7 +116,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"}) From 40a151df5a434eb3a409ead174792c593035508a Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 8 Mar 2023 17:22:26 +0100 Subject: [PATCH 7/7] fix order of devices --- ereuse_devicehub/inventory/views.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 4522c5b5..675409b4 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -1102,6 +1102,12 @@ class ExportsView(View): 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_host = sorted(erasures_host, 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) + params = { 'title': 'Erasure Certificate', 'erasures': tuple(erasures), @@ -1114,7 +1120,7 @@ class ExportsView(View): '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)