From 1515302d9839f96dd1374f4f803f77814512b0cd Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 28 Mar 2023 17:09:47 +0200 Subject: [PATCH] allow remove a document --- ereuse_devicehub/inventory/forms.py | 12 +++++++ ereuse_devicehub/inventory/views.py | 25 +++++++++++++ ereuse_devicehub/resources/models.py | 32 +++++++++++------ .../templates/inventory/device_list.html | 36 +++++++++++++++++++ 4 files changed, 94 insertions(+), 11 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index ffb8d13c..fc5fb5c6 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -1274,8 +1274,14 @@ class TradeDocumentForm(FlaskForm): def __init__(self, *args, **kwargs): lot_id = kwargs.pop('lot') + doc_id = kwargs.pop('document', None) super().__init__(*args, **kwargs) self._lot = Lot.query.filter(Lot.id == lot_id).one() + self.object = None + if doc_id: + self.object = TradeDocument.query.filter_by( + id=doc_id, lot=self._lot, owner=g.user + ).one() if not self._lot.transfer: self.form_errors = ['Error, this lot is not a transfer lot.'] @@ -1307,6 +1313,12 @@ class TradeDocumentForm(FlaskForm): return self._obj + def remove(self): + if self.object: + self.object.delete() + db.session.commit() + return self.object + class TransferForm(FlaskForm): lot_name = StringField( diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 79145f3c..73a55cc1 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -547,6 +547,27 @@ class LotDeleteView(View): return flask.redirect(next_url) +class DocumentDeleteView(View): + methods = ['GET'] + decorators = [login_required] + template_name = 'inventory/device_list.html' + form_class = TradeDocumentForm + + def dispatch_request(self, lot_id, doc_id): + next_url = url_for('inventory.lotdevicelist', lot_id=lot_id) + form = self.form_class(lot=lot_id, document=doc_id) + try: + form.remove() + except Exception as err: + msg = "{}".format(err) + messages.error(msg) + return flask.redirect(next_url) + + msg = "Document removed successfully." + messages.success(msg) + return flask.redirect(next_url) + + class UploadSnapshotView(GenericMixin): methods = ['GET', 'POST'] decorators = [login_required] @@ -1515,6 +1536,10 @@ devices.add_url_rule( '/lot//trade-document/add/', view_func=NewTradeDocumentView.as_view('trade_document_add'), ) +devices.add_url_rule( + '/lot//document/del/', + view_func=DocumentDeleteView.as_view('document_del'), +) devices.add_url_rule('/device/', view_func=DeviceListView.as_view('devicelist')) devices.add_url_rule( '/all/device/', view_func=AllDeviceListView.as_view('alldevicelist') diff --git a/ereuse_devicehub/resources/models.py b/ereuse_devicehub/resources/models.py index e079269f..485fc96e 100644 --- a/ereuse_devicehub/resources/models.py +++ b/ereuse_devicehub/resources/models.py @@ -1,4 +1,5 @@ from datetime import datetime, timezone + from flask_sqlalchemy import event from ereuse_devicehub.db import db @@ -16,18 +17,23 @@ class Thing(db.Model): `schema.org's Thing class `_ using only needed fields. """ + __abstract__ = True - updated = db.Column(db.TIMESTAMP(timezone=True), - nullable=False, - index=True, - server_default=db.text('CURRENT_TIMESTAMP')) - updated.comment = """The last time Devicehub recorded a change for + updated = db.Column( + db.TIMESTAMP(timezone=True), + nullable=False, + index=True, + server_default=db.text('CURRENT_TIMESTAMP'), + ) + updated.comment = """The last time Devicehub recorded a change for this thing. """ - created = db.Column(db.TIMESTAMP(timezone=True), - nullable=False, - index=True, - server_default=db.text('CURRENT_TIMESTAMP')) + created = db.Column( + db.TIMESTAMP(timezone=True), + nullable=False, + index=True, + server_default=db.text('CURRENT_TIMESTAMP'), + ) created.comment = """When Devicehub created this.""" def __init__(self, **kwargs) -> None: @@ -36,11 +42,15 @@ class Thing(db.Model): self.created = kwargs.get('created', datetime.now(timezone.utc)) super().__init__(**kwargs) + def delete(self): + db.session.delete(self) + def update_object_timestamp(mapper, connection, thing_obj): - """ This function update the stamptime of field updated """ + """This function update the stamptime of field updated""" thing_obj.updated = datetime.now(timezone.utc) + def listener_reset_field_updated_in_actual_time(thing_obj): - """ This function launch a event than listen like a signal when some object is saved """ + """This function launch a event than listen like a signal when some object is saved""" event.listen(thing_obj, 'before_update', update_object_timestamp, propagate=True) diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 2e2bc7b6..bcc0f22f 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -530,6 +530,7 @@ File Uploaded on + @@ -545,6 +546,38 @@ {{ doc.created.strftime('%Y-%m-%d %H:%M')}} + + + + + + {% endfor %} {% for doc in lot.trade.documents %} @@ -559,6 +592,9 @@ {{ doc.created.strftime('%Y-%m-%d %H:%M')}} + + + {% endfor %}