diff --git a/ereuse_devicehub/inventory/forms.py b/ereuse_devicehub/inventory/forms.py index 641d4b84..1dc9cff2 100644 --- a/ereuse_devicehub/inventory/forms.py +++ b/ereuse_devicehub/inventory/forms.py @@ -1247,10 +1247,13 @@ class TradeDocumentForm(FlaskForm): super().__init__(*args, **kwargs) 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): 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 return is_valid @@ -1268,7 +1271,7 @@ class TradeDocumentForm(FlaskForm): self._obj.file_name = file_name self._obj.file_hash = file_hash db.session.add(self._obj) - self._lot.trade.documents.add(self._obj) + self._lot.documents.add(self._obj) if commit: db.session.commit() diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 1e0896de..3c084088 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -724,7 +724,7 @@ class NewTradeView(DeviceListMixin, NewActionView): return flask.redirect(next_url) -class NewTradeDocumentView(View): +class NewTradeDocumentView(GenericMixin): methods = ['POST', 'GET'] decorators = [login_required] template_name = 'inventory/trade_document.html' diff --git a/ereuse_devicehub/templates/inventory/device_list.html b/ereuse_devicehub/templates/inventory/device_list.html index 57f42a55..21694dfc 100644 --- a/ereuse_devicehub/templates/inventory/device_list.html +++ b/ereuse_devicehub/templates/inventory/device_list.html @@ -462,10 +462,24 @@ + {% for doc in lot.documents %} + + + {% if doc.url.to_text() %} + {{ doc.file_name}} + {% else %} + {{ doc.file_name}} + {% endif %} + + + {{ doc.created.strftime('%H:%M %d-%m-%Y')}} + + + {% endfor %} {% for doc in lot.trade.documents %} - {% if doc.url %} + {% if doc.url.to_text() %} {{ doc.file_name}} {% else %} {{ doc.file_name}} diff --git a/tests/test_render_2_0.py b/tests/test_render_2_0.py index a088df5a..a7672f35 100644 --- a/tests/test_render_2_0.py +++ b/tests/test_render_2_0.py @@ -184,7 +184,6 @@ def test_upload_snapshot(user3: UserClientFlask): @pytest.mark.mvp @pytest.mark.usefixtures(conftest.app_context.__name__) def test_upload_snapshot_to_lot(user3: UserClientFlask): - # TODO user3.get('/inventory/lot/add/') lot_name = 'lot1' data = { @@ -2407,3 +2406,55 @@ def test_bug_3821_binding(user3: UserClientFlask): body, status = user3.get(uri) assert status == '200 OK' 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'