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 bcc0f22f..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 %}