mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Fix for barcode URL generation (#3924)
* Fix for barcode URL generation * Refactor the fix entirely - move the URL generation out of MakeBarcode function - Improved unit testing - actually render a label template * Updates for CI * CI fix
This commit is contained in:
parent
44270e064e
commit
60e44700a9
@ -456,7 +456,7 @@ def WrapWithQuotes(text, quote='"'):
|
||||
return text
|
||||
|
||||
|
||||
def MakeBarcode(object_name, object_pk, object_data=None, **kwargs):
|
||||
def MakeBarcode(cls_name, object_pk: int, object_data=None, **kwargs):
|
||||
"""Generate a string for a barcode. Adds some global InvenTree parameters.
|
||||
|
||||
Args:
|
||||
@ -468,29 +468,16 @@ def MakeBarcode(object_name, object_pk, object_data=None, **kwargs):
|
||||
Returns:
|
||||
json string of the supplied data plus some other data
|
||||
"""
|
||||
|
||||
if object_data is None:
|
||||
object_data = {}
|
||||
|
||||
url = kwargs.get('url', False)
|
||||
brief = kwargs.get('brief', True)
|
||||
|
||||
data = {}
|
||||
|
||||
if url:
|
||||
request = object_data.get('request', None)
|
||||
item_url = object_data.get('item_url', None)
|
||||
absolute_url = None
|
||||
|
||||
if request and item_url:
|
||||
absolute_url = request.build_absolute_uri(item_url)
|
||||
# Return URL (No JSON)
|
||||
return absolute_url
|
||||
|
||||
if item_url:
|
||||
# Return URL (No JSON)
|
||||
return item_url
|
||||
elif brief:
|
||||
data[object_name] = object_pk
|
||||
if brief:
|
||||
data[cls_name] = object_pk
|
||||
else:
|
||||
data['tool'] = 'InvenTree'
|
||||
data['version'] = InvenTree.version.inventreeVersion()
|
||||
@ -498,7 +485,7 @@ def MakeBarcode(object_name, object_pk, object_data=None, **kwargs):
|
||||
|
||||
# Ensure PK is included
|
||||
object_data['id'] = object_pk
|
||||
data[object_name] = object_data
|
||||
data[cls_name] = object_data
|
||||
|
||||
return json.dumps(data, sort_keys=True)
|
||||
|
||||
|
@ -252,7 +252,7 @@ class StockItemLabel(LabelTemplate):
|
||||
'barcode_data': stock_item.barcode_data,
|
||||
'barcode_hash': stock_item.barcode_hash,
|
||||
'qr_data': stock_item.format_barcode(brief=True),
|
||||
'qr_url': stock_item.format_barcode(url=True, request=request),
|
||||
'qr_url': request.build_absolute_uri(stock_item.get_absolute_url()),
|
||||
'tests': stock_item.testResultMap(),
|
||||
'parameters': stock_item.part.parameters_map(),
|
||||
|
||||
@ -318,6 +318,6 @@ class PartLabel(LabelTemplate):
|
||||
'IPN': part.IPN,
|
||||
'revision': part.revision,
|
||||
'qr_data': part.format_barcode(brief=True),
|
||||
'qr_url': part.format_barcode(url=True, request=request),
|
||||
'qr_url': request.build_absolute_uri(part.get_absolute_url()),
|
||||
'parameters': part.parameters_map(),
|
||||
}
|
||||
|
@ -1,10 +1,14 @@
|
||||
"""Tests for labels"""
|
||||
|
||||
import io
|
||||
|
||||
from django.apps import apps
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.core.files.base import ContentFile
|
||||
from django.urls import reverse
|
||||
|
||||
from common.models import InvenTreeSetting
|
||||
from InvenTree.api_tester import InvenTreeAPITestCase
|
||||
from InvenTree.helpers import validateFilterString
|
||||
from part.models import Part
|
||||
@ -73,3 +77,55 @@ class LabelTest(InvenTreeAPITestCase):
|
||||
for label in labels:
|
||||
url = reverse('api-part-label-print', kwargs={'pk': label.pk})
|
||||
self.get(f'{url}?parts={part.pk}', expected_code=200)
|
||||
|
||||
def test_print_part_label(self):
|
||||
"""Actually 'print' a label, and ensure that the correct information is contained."""
|
||||
|
||||
label_data = """
|
||||
{% load barcode %}
|
||||
{% load report %}
|
||||
|
||||
<html>
|
||||
<!-- Test that the part instance is supplied -->
|
||||
part: {{ part.pk }} - {{ part.name }}
|
||||
<!-- Test qr data -->
|
||||
data: {{ qr_data|safe }}
|
||||
<!-- Test InvenTree URL -->
|
||||
url: {{ qr_url|safe }}
|
||||
<!-- Test image URL generation -->
|
||||
image: {% part_image part %}
|
||||
<!-- Test InvenTree logo -->
|
||||
logo: {% logo_image %}
|
||||
</html>
|
||||
"""
|
||||
|
||||
buffer = io.StringIO()
|
||||
buffer.write(label_data)
|
||||
|
||||
template = ContentFile(buffer.getvalue(), "label.html")
|
||||
|
||||
# Construct a label template
|
||||
label = PartLabel.objects.create(
|
||||
name='test',
|
||||
description='Test label',
|
||||
enabled=True,
|
||||
label=template,
|
||||
)
|
||||
|
||||
# Ensure we are in "debug" mode (so the report is generated as HTML)
|
||||
InvenTreeSetting.set_setting('REPORT_ENABLE', True, None)
|
||||
InvenTreeSetting.set_setting('REPORT_DEBUG_MODE', True, None)
|
||||
|
||||
# Print via the API
|
||||
url = reverse('api-part-label-print', kwargs={'pk': label.pk})
|
||||
|
||||
response = self.get(f'{url}?parts=1', expected_code=200)
|
||||
|
||||
content = str(response.content)
|
||||
|
||||
# Test that each element has been rendered correctly
|
||||
self.assertIn("part: 1 - M2x4 LPHS", content)
|
||||
self.assertIn('data: {"part": 1}', content)
|
||||
self.assertIn("http://testserver/part/1/", content)
|
||||
self.assertIn("image: /static/img/blank_image.png", content)
|
||||
self.assertIn("logo: /static/img/inventree.png", content)
|
||||
|
Loading…
Reference in New Issue
Block a user