Merge pull request #363 from eReuse/bugfix/3831-bug-documents

fix bug and test
This commit is contained in:
cayop 2022-09-23 12:20:05 +02:00 committed by GitHub
commit 2ecec6590a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 73 additions and 5 deletions

View File

@ -1247,10 +1247,13 @@ class TradeDocumentForm(FlaskForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._lot = Lot.query.filter(Lot.id == lot_id).one() self._lot = Lot.query.filter(Lot.id == lot_id).one()
if not self._lot.transfer:
self.form_errors = ['Error, this lot is not a transfer lot.']
def validate(self, extra_validators=None): def validate(self, extra_validators=None):
is_valid = super().validate(extra_validators) is_valid = super().validate(extra_validators)
if g.user not in [self._lot.trade.user_from, self._lot.trade.user_to]: if g.user not in [self._lot.transfer.user_from, self._lot.transfer.user_to]:
is_valid = False is_valid = False
return is_valid return is_valid
@ -1268,7 +1271,7 @@ class TradeDocumentForm(FlaskForm):
self._obj.file_name = file_name self._obj.file_name = file_name
self._obj.file_hash = file_hash self._obj.file_hash = file_hash
db.session.add(self._obj) db.session.add(self._obj)
self._lot.trade.documents.add(self._obj) self._lot.documents.add(self._obj)
if commit: if commit:
db.session.commit() db.session.commit()

View File

@ -724,7 +724,7 @@ class NewTradeView(DeviceListMixin, NewActionView):
return flask.redirect(next_url) return flask.redirect(next_url)
class NewTradeDocumentView(View): class NewTradeDocumentView(GenericMixin):
methods = ['POST', 'GET'] methods = ['POST', 'GET']
decorators = [login_required] decorators = [login_required]
template_name = 'inventory/trade_document.html' template_name = 'inventory/trade_document.html'

View File

@ -462,10 +462,24 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% for doc in lot.documents %}
<tr>
<td>
{% if doc.url.to_text() %}
<a href="{{ doc.url.to_text() }}" target="_blank">{{ doc.file_name}}</a>
{% else %}
{{ doc.file_name}}
{% endif %}
</td>
<td>
{{ doc.created.strftime('%H:%M %d-%m-%Y')}}
</td>
</tr>
{% endfor %}
{% for doc in lot.trade.documents %} {% for doc in lot.trade.documents %}
<tr> <tr>
<td> <td>
{% if doc.url %} {% if doc.url.to_text() %}
<a href="{{ doc.url.to_text() }}" target="_blank">{{ doc.file_name}}</a> <a href="{{ doc.url.to_text() }}" target="_blank">{{ doc.file_name}}</a>
{% else %} {% else %}
{{ doc.file_name}} {{ doc.file_name}}

View File

@ -184,7 +184,6 @@ def test_upload_snapshot(user3: UserClientFlask):
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_upload_snapshot_to_lot(user3: UserClientFlask): def test_upload_snapshot_to_lot(user3: UserClientFlask):
# TODO
user3.get('/inventory/lot/add/') user3.get('/inventory/lot/add/')
lot_name = 'lot1' lot_name = 'lot1'
data = { data = {
@ -2406,3 +2405,55 @@ def test_bug_3821_binding(user3: UserClientFlask):
body, status = user3.get(uri) body, status = user3.get(uri)
assert status == '200 OK' assert status == '200 OK'
assert 'is not a Snapshot device!' in body assert 'is not a Snapshot device!' in body
@pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__)
def test_bug_3831_documents(user3: UserClientFlask):
uri = '/inventory/lot/add/'
user3.get(uri)
lot_name = 'lot1'
data = {
'name': lot_name,
'csrf_token': generate_csrf(),
}
user3.post(uri, data=data)
lot = Lot.query.filter_by(name=lot_name).one()
lot_id = lot.id
uri = f'/inventory/lot/{lot_id}/trade-document/add/'
body, status = user3.get(uri)
txt = 'Error, this lot is not a transfer lot.'
assert status == '200 OK'
assert txt in body
uri = f'/inventory/lot/{lot_id}/transfer/incoming/'
user3.get(uri)
data = {'csrf_token': generate_csrf(), 'code': 'AAA'}
body, status = user3.post(uri, data=data)
assert status == '200 OK'
assert 'Transfer created successfully!' in body
assert 'Delete Lot' in body
assert 'Incoming Lot' in body
uri = f'/inventory/lot/{lot_id}/trade-document/add/'
body, status = user3.get(uri)
b_file = b'1234567890'
file_name = "my_file.doc"
file_upload = (BytesIO(b_file), file_name)
data = {
'csrf_token': generate_csrf(),
'url': "",
'description': "",
'document_id': "",
'date': "",
'file': file_upload,
}
uri = f'/inventory/lot/{lot_id}/trade-document/add/'
# body, status = user3.post(uri, data=data, content_type="multipart/form-data")
# assert status == '200 OK'