From d30ff8a291d945c13984d6998dce67b4091839c4 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sun, 19 Nov 2023 23:35:11 +0100 Subject: [PATCH] Bump weasyprint (#5885) * bumped weasyprint * factored reused section out (reduce DB access) * added pdf-context testing Co-authored-by: miggland * switched to pdfminer.six * make test more resilient --------- Co-authored-by: miggland --- .../plugin/base/label/test_label_mixin.py | 21 +++++++++++-------- requirements-dev.in | 1 + requirements-dev.txt | 15 +++++++++++++ requirements.in | 2 +- requirements.txt | 4 ++-- 5 files changed, 31 insertions(+), 12 deletions(-) diff --git a/InvenTree/plugin/base/label/test_label_mixin.py b/InvenTree/plugin/base/label/test_label_mixin.py index 6824ca55f7..2eedbcc156 100644 --- a/InvenTree/plugin/base/label/test_label_mixin.py +++ b/InvenTree/plugin/base/label/test_label_mixin.py @@ -7,6 +7,7 @@ from unittest import mock from django.apps import apps from django.urls import reverse +from pdfminer.high_level import extract_text from PIL import Image from InvenTree.unit_test import InvenTreeAPITestCase @@ -138,6 +139,7 @@ class LabelMixinTests(InvenTreeAPITestCase): # Lookup references part = Part.objects.first() + parts = Part.objects.all()[:2] plugin_ref = 'samplelabelprinter' label = PartLabel.objects.first() @@ -158,13 +160,13 @@ class LabelMixinTests(InvenTreeAPITestCase): self.get(url, expected_code=200) # Print multiple parts - self.get(self.do_url(Part.objects.all()[:2], plugin_ref, label), expected_code=200) + self.get(self.do_url(parts, plugin_ref, label), expected_code=200) # Print multiple parts without a plugin - self.get(self.do_url(Part.objects.all()[:2], None, label), expected_code=200) + self.get(self.do_url(parts, None, label), expected_code=200) # Print multiple parts without a plugin in debug mode - response = self.get(self.do_url(Part.objects.all()[:2], None, label), expected_code=200) + response = self.get(self.do_url(parts, None, label), expected_code=200) data = json.loads(response.content) self.assertIn('file', data) @@ -177,9 +179,9 @@ class LabelMixinTests(InvenTreeAPITestCase): self.assertTrue(os.path.exists('label.pdf')) # Read the raw .pdf data - ensure it contains some sensible information - with open('label.pdf', 'rb') as f: - pdf_data = str(f.read()) - self.assertIn('WeasyPrint', pdf_data) + filetext = extract_text('label.pdf') + matched = [part.name in filetext for part in parts] + self.assertIn(True, matched) # Check that the .png file has already been created self.assertTrue(os.path.exists('label.png')) @@ -193,24 +195,25 @@ class LabelMixinTests(InvenTreeAPITestCase): apps.get_app_config('label').create_labels() # Lookup references + parts = Part.objects.all()[:2] plugin_ref = 'samplelabelprinter' label = PartLabel.objects.first() self.do_activate_plugin() # test options response - options = self.options(self.do_url(Part.objects.all()[:2], plugin_ref, label), expected_code=200).json() + options = self.options(self.do_url(parts, plugin_ref, label), expected_code=200).json() self.assertTrue("amount" in options["actions"]["POST"]) plg = registry.get_plugin(plugin_ref) with mock.patch.object(plg, "print_label") as print_label: # wrong value type - res = self.post(self.do_url(Part.objects.all()[:2], plugin_ref, label), data={"amount": "-no-valid-int-"}, expected_code=400).json() + res = self.post(self.do_url(parts, plugin_ref, label), data={"amount": "-no-valid-int-"}, expected_code=400).json() self.assertTrue("amount" in res) print_label.assert_not_called() # correct value type - self.post(self.do_url(Part.objects.all()[:2], plugin_ref, label), data={"amount": 13}, expected_code=200).json() + self.post(self.do_url(parts, plugin_ref, label), data={"amount": 13}, expected_code=200).json() self.assertEqual(print_label.call_args.kwargs["printing_options"], {"amount": 13}) def test_printing_endpoints(self): diff --git a/requirements-dev.in b/requirements-dev.in index ad87a439e6..50f25f136c 100644 --- a/requirements-dev.in +++ b/requirements-dev.in @@ -12,3 +12,4 @@ pep8-naming # PEP naming convention extension pip-tools # Compile pip requirements pre-commit # Git pre-commit setuptools # Standard dependency +pdfminer.six # PDF validation diff --git a/requirements-dev.txt b/requirements-dev.txt index 24e6fde3f5..e123f11fc5 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -14,11 +14,16 @@ certifi==2023.7.22 # via # -c requirements.txt # requests +cffi==1.16.0 + # via + # -c requirements.txt + # cryptography cfgv==3.4.0 # via pre-commit charset-normalizer==3.3.2 # via # -c requirements.txt + # pdfminer-six # requests click==8.1.7 # via pip-tools @@ -28,6 +33,10 @@ coverage==5.5 # coveralls coveralls==2.1.2 # via -r requirements-dev.in +cryptography==41.0.5 + # via + # -c requirements.txt + # pdfminer-six distlib==0.3.7 # via virtualenv django==3.2.23 @@ -72,6 +81,8 @@ packaging==23.2 # via # -c requirements.txt # build +pdfminer-six==20221105 + # via -r requirements-dev.in pep8-naming==0.13.3 # via -r requirements-dev.in pip-tools==7.3.0 @@ -82,6 +93,10 @@ pre-commit==3.5.0 # via -r requirements-dev.in pycodestyle==2.11.1 # via flake8 +pycparser==2.21 + # via + # -c requirements.txt + # cffi pydocstyle==6.3.0 # via flake8-docstrings pyflakes==3.1.0 diff --git a/requirements.in b/requirements.in index f9a5d91dc7..9a7c7e339d 100644 --- a/requirements.in +++ b/requirements.in @@ -48,4 +48,4 @@ regex # Advanced regular expressions sentry-sdk # Error reporting (optional) setuptools # Standard dependency tablib[xls,xlsx,yaml] # Support for XLS and XLSX formats -weasyprint==54.3 # PDF generation +weasyprint # PDF generation diff --git a/requirements.txt b/requirements.txt index 8145622024..9657d00178 100644 --- a/requirements.txt +++ b/requirements.txt @@ -269,7 +269,7 @@ requests==2.31.0 # requests-oauthlib requests-oauthlib==1.3.1 # via django-allauth -rpds-py==0.10.6 +rpds-py==0.12.0 # via # jsonschema # referencing @@ -311,7 +311,7 @@ urllib3==2.0.7 # dulwich # requests # sentry-sdk -weasyprint==54.3 +weasyprint==60.1 # via # -r requirements.in # django-weasyprint