From 509a4454804419fdc8eaba80ace2d706b6a195a4 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 29 Mar 2023 13:40:27 +0200 Subject: [PATCH] add edit document form --- ereuse_devicehub/inventory/forms.py | 30 +++++++++++++------ ereuse_devicehub/inventory/views.py | 30 +++++++++++++++++-- .../templates/inventory/device_list.html | 4 +-- .../templates/inventory/search.html | 2 +- .../templates/inventory/trade_document.html | 7 ++++- tests/test_basic.py | 1 + tests/test_render_2_0.py | 8 ++--- 7 files changed, 63 insertions(+), 19 deletions(-) diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index fc5fb5c6..bd5c5498 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -1275,13 +1275,19 @@ 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 + self._obj = None if doc_id: - self.object = TradeDocument.query.filter_by( + self._obj = TradeDocument.query.filter_by( id=doc_id, lot=self._lot, owner=g.user ).one() + kwargs['obj'] = self._obj + + super().__init__(*args, **kwargs) + + if self._obj: + if isinstance(self.url.data, URL): + self.url.data = self.url.data.to_text() if not self._lot.transfer: self.form_errors = ['Error, this lot is not a transfer lot.'] @@ -1302,22 +1308,28 @@ class TradeDocumentForm(FlaskForm): file_hash = insert_hash(self.file_name.data.read(), commit=False) self.url.data = URL(self.url.data) - self._obj = TradeDocument(lot_id=self._lot.id) + if not self._obj: + self._obj = TradeDocument(lot_id=self._lot.id) + self.populate_obj(self._obj) + self._obj.file_name = file_name self._obj.file_hash = file_hash - db.session.add(self._obj) - self._lot.documents.add(self._obj) + + if not self._obj.id: + db.session.add(self._obj) + self._lot.documents.add(self._obj) + if commit: db.session.commit() return self._obj def remove(self): - if self.object: - self.object.delete() + if self._obj: + self._obj.delete() db.session.commit() - return self.object + return self._obj class TransferForm(FlaskForm): diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 73a55cc1..a1481399 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -831,6 +831,28 @@ class NewTradeDocumentView(GenericMixin): return flask.render_template(self.template_name, **self.context) +class EditTransferDocumentView(GenericMixin): + + decorators = [login_required] + methods = ['POST', 'GET'] + template_name = 'inventory/trade_document.html' + form_class = TradeDocumentForm + title = "Edit document" + + def dispatch_request(self, lot_id, doc_id): + self.form = self.form_class(lot=lot_id, document=doc_id) + self.get_context() + + if self.form.validate_on_submit(): + self.form.save() + messages.success('Edit document successfully!') + next_url = url_for('inventory.lotdevicelist', lot_id=lot_id) + return flask.redirect(next_url) + + self.context.update({'form': self.form, 'title': self.title}) + return flask.render_template(self.template_name, **self.context) + + class NewTransferView(GenericMixin): methods = ['POST', 'GET'] template_name = 'inventory/new_transfer.html' @@ -1533,8 +1555,12 @@ devices.add_url_rule( '/action/datawipe/add/', view_func=NewDataWipeView.as_view('datawipe_add') ) devices.add_url_rule( - '/lot//trade-document/add/', - view_func=NewTradeDocumentView.as_view('trade_document_add'), + '/lot//transfer-document/add/', + view_func=NewTradeDocumentView.as_view('transfer_document_add'), +) +devices.add_url_rule( + '/lot//document/edit/', + view_func=EditTransferDocumentView.as_view('transfer_document_edit'), ) devices.add_url_rule( '/lot//document/del/', diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 58a7fc4f..cc304687 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -517,7 +517,7 @@ {% if lot and not lot.is_temporary %}
-
+ {% else %} + + {% endif %} {{ form.csrf_token }} {% for field in form %} {% if field != form.csrf_token %} diff --git a/tests/test_basic.py b/tests/test_basic.py index e93aacb1..e834cba2 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -73,6 +73,7 @@ def test_api_docs(client: Client): '/inventory/lot/transfer/{type_id}/', '/inventory/lot/{lot_id}/upload-snapshot/', '/inventory/lot/{lot_id}/customerdetails/', + '/inventory/lot/{lot_id}/document/edit/{doc_id}', '/inventory/lot/{lot_id}/document/del/{doc_id}', '/inventory/snapshots/{snapshot_uuid}/', '/inventory/snapshots/', diff --git a/tests/test_render_2_0.py b/tests/test_render_2_0.py index 6cb43f31..bf127c14 100644 --- a/tests/test_render_2_0.py +++ b/tests/test_render_2_0.py @@ -2468,7 +2468,7 @@ def test_bug_3831_documents(user3: UserClientFlask): lot = Lot.query.filter_by(name=lot_name).one() lot_id = lot.id - uri = f'/inventory/lot/{lot_id}/trade-document/add/' + uri = f'/inventory/lot/{lot_id}/transfer-document/add/' body, status = user3.get(uri) txt = 'Error, this lot is not a transfer lot.' @@ -2486,7 +2486,7 @@ def test_bug_3831_documents(user3: UserClientFlask): assert 'Incoming Lot' in body lot_id = Lot.query.all()[1].id - uri = f'/inventory/lot/{lot_id}/trade-document/add/' + uri = f'/inventory/lot/{lot_id}/transfer-document/add/' body, status = user3.get(uri) b_file = b'1234567890' @@ -2502,12 +2502,12 @@ def test_bug_3831_documents(user3: UserClientFlask): 'file': file_upload, } - uri = f'/inventory/lot/{lot_id}/trade-document/add/' + uri = f'/inventory/lot/{lot_id}/transfer-document/add/' body, status = user3.post(uri, data=data, content_type="multipart/form-data") assert status == '200 OK' # Second document - uri = f'/inventory/lot/{lot_id}/trade-document/add/' + uri = f'/inventory/lot/{lot_id}/transfer-document/add/' file_upload = (BytesIO(b_file), file_name) data['file'] = file_upload data['csrf_token'] = generate_csrf()