add edit document form

This commit is contained in:
Cayo Puigdefabregas 2023-03-29 13:40:27 +02:00
parent ca0b31059f
commit 509a445480
7 changed files with 63 additions and 19 deletions

View file

@ -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):

View file

@ -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/<string:lot_id>/trade-document/add/',
view_func=NewTradeDocumentView.as_view('trade_document_add'),
'/lot/<string:lot_id>/transfer-document/add/',
view_func=NewTradeDocumentView.as_view('transfer_document_add'),
)
devices.add_url_rule(
'/lot/<string:lot_id>/document/edit/<string:doc_id>',
view_func=EditTransferDocumentView.as_view('transfer_document_edit'),
)
devices.add_url_rule(
'/lot/<string:lot_id>/document/del/<string:doc_id>',

View file

@ -517,7 +517,7 @@
{% if lot and not lot.is_temporary %}
<div id="trade-documents-list" class="tab-pane fade trade-documents-list">
<div class="btn-group dropdown ml-1 mt-1" uib-dropdown="">
<a href="{{ url_for('inventory.trade_document_add', lot_id=lot.id)}}" class="btn btn-primary">
<a href="{{ url_for('inventory.transfer_document_add', lot_id=lot.id)}}" class="btn btn-primary">
<i class="bi bi-plus"></i>
Add new document
<span class="caret"></span>
@ -547,7 +547,7 @@
{{ doc.created.strftime('%Y-%m-%d %H:%M')}}
</td>
<td>
<a href="javascript:javascript:void(0)" data-bs-toggle="modal" data-bs-target="#btnRemoveDocument{{ loop.index }}" title="Edit document">
<a href="{{ url_for('inventory.transfer_document_edit', lot_id=lot.id, doc_id=doc.id)}}" title="Edit document">
<i class="bi bi-pencil-square"></i>
</a>
</td>

View file

@ -254,7 +254,7 @@
</button>
<ul class="dropdown-menu" aria-labelledby="btnSnapshot">
<li>
<a href="{{ url_for('inventory.trade_document_add', lot_id=lot.id)}}" class="dropdown-item">
<a href="{{ url_for('inventory.transfer_document_add', lot_id=lot.id)}}" class="dropdown-item">
<i class="bi bi-plus"></i>
Add new document
<span class="caret"></span>

View file

@ -30,8 +30,13 @@
{% endif %}
</div>
<form action="{{ url_for('inventory.trade_document_add', lot_id=form._lot.id) }}" method="post"
{% if form._obj %}
<form action="{{ url_for('inventory.transfer_document_edit', lot_id=form._lot.id, doc_id=form._obj.id) }}" method="post"
class="row g-3 needs-validation" enctype="multipart/form-data">
{% else %}
<form action="{{ url_for('inventory.transfer_document_add', lot_id=form._lot.id) }}" method="post"
class="row g-3 needs-validation" enctype="multipart/form-data">
{% endif %}
{{ form.csrf_token }}
{% for field in form %}
{% if field != form.csrf_token %}

View file

@ -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/',

View file

@ -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()