Merge pull request #442 from eReuse/feature/4126-edit-documents

Feature/4126 edit documents
This commit is contained in:
cayop 2023-03-29 14:11:06 +02:00 committed by GitHub
commit 4059dc3a7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 69 additions and 20 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)
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
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,12 @@
{{ 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 }}">
<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>
<td>
<a href="javascript:javascript:void(0)" data-bs-toggle="modal" data-bs-target="#btnRemoveDocument{{ loop.index }}" title="Remove document">
<i class="bi bi-trash-fill"></i>
</a>
<div class="modal fade" id="btnRemoveDocument{{ loop.index }}" tabindex="-1" style="display: none;" aria-hidden="true">

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

@ -66,13 +66,14 @@ def test_api_docs(client: Client):
'/inventory/lot/{lot_id}/device/add/',
'/inventory/lot/{lot_id}/deliverynote/',
'/inventory/lot/{lot_id}/receivernote/',
'/inventory/lot/{lot_id}/trade-document/add/',
'/inventory/lot/{lot_id}/transfer-document/add/',
'/inventory/lot/{lot_id}/transfer/{type_id}/',
'/inventory/lot/{lot_id}/opentransfer/',
'/inventory/lot/{lot_id}/transfer/',
'/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()