From 0f6551d70f4dccd970d409b2d0008d488ad75896 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Mon, 22 Jul 2024 01:53:16 +0200 Subject: [PATCH 1/5] cI: Test color theme again (#7700) * Adjust caching key to be numeric for user * Add strong usermodel to colortheme * switch to using user model everywhere for colortheme * re-enable ColorTheme tests * fix call * remove old migratin * fix directory discovery --- src/backend/InvenTree/common/models.py | 14 +++++++++++--- src/backend/InvenTree/common/tests.py | 10 ---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/backend/InvenTree/common/models.py b/src/backend/InvenTree/common/models.py index a55e829cc8..4dd125d5a5 100644 --- a/src/backend/InvenTree/common/models.py +++ b/src/backend/InvenTree/common/models.py @@ -2589,14 +2589,22 @@ class ColorTheme(models.Model): @classmethod def get_color_themes_choices(cls): """Get all color themes from static folder.""" - if not django_settings.STATIC_COLOR_THEMES_DIR.exists(): - logger.error('Theme directory does not exist') + color_theme_dir = ( + django_settings.STATIC_COLOR_THEMES_DIR + if django_settings.STATIC_COLOR_THEMES_DIR.exists() + else django_settings.BASE_DIR.joinpath( + 'InvenTree', 'static', 'css', 'color-themes' + ) + ) + + if not color_theme_dir.exists(): + logger.error(f'Theme directory "{color_theme_dir}" does not exist') return [] # Get files list from css/color-themes/ folder files_list = [] - for file in django_settings.STATIC_COLOR_THEMES_DIR.iterdir(): + for file in color_theme_dir.iterdir(): files_list.append([file.stem, file.suffix]) # Get color themes choices (CSS sheets) diff --git a/src/backend/InvenTree/common/tests.py b/src/backend/InvenTree/common/tests.py index 9c3dea87d7..b08ab0b5f2 100644 --- a/src/backend/InvenTree/common/tests.py +++ b/src/backend/InvenTree/common/tests.py @@ -1199,20 +1199,10 @@ class ColorThemeTest(TestCase): def test_choices(self): """Test that default choices are returned.""" result = ColorTheme.get_color_themes_choices() - - # skip due to directories not being set up - if not result: - return # pragma: no cover self.assertIn(('default', 'Default'), result) def test_valid_choice(self): """Check that is_valid_choice works correctly.""" - result = ColorTheme.get_color_themes_choices() - - # skip due to directories not being set up - if not result: - return # pragma: no cover - # check wrong reference self.assertFalse(ColorTheme.is_valid_choice('abcdd')) From 16e535f45ffadcbc54941ad9b82f02b3b616ecbd Mon Sep 17 00:00:00 2001 From: Lukas <76838159+wolflu05@users.noreply.github.com> Date: Mon, 22 Jul 2024 03:52:45 +0200 Subject: [PATCH 2/5] Add barcode generation capabilities to plugins (#7648) * initial implementation of barcode generation using plugins * implement short QR code scanning * add PUI qrcode preview * use barcode generation for CUI show barcode modal * remove short qr prefix validators and fix short qr detection regex * catch errors if model with pk is not found for scanning and generating * improve qrcode templatetag * fix comments * fix for python 3.9 * add tests * fix: tests * add docs * fix: tests * bump api version * add docs to BarcodeMixin * fix: test * added suggestions from code review * fix: tests * Add MinLengthValidator to short barcode prefix setting * fix: tests? * trigger: ci * try custom cache * try custom cache ignore all falsy * remove debugging * Revert "Add MinLengthValidator to short barcode prefix setting" This reverts commit 76043ed96b1700ae50f9cfda7997629efa8d97e2. * Revert "fix: tests" This reverts commit 3a2d46ff720c35b51e21aedaa22357d71bd120e1. --- docs/docs/barcodes/internal.md | 45 +++++- docs/docs/extend/plugins/barcode.md | 49 +++++-- .../InvenTree/InvenTree/api_version.py | 5 +- src/backend/InvenTree/InvenTree/helpers.py | 32 ----- .../InvenTree/InvenTree/helpers_model.py | 7 +- src/backend/InvenTree/InvenTree/models.py | 26 +++- src/backend/InvenTree/InvenTree/tests.py | 27 ---- src/backend/InvenTree/build/models.py | 5 + .../build/templates/build/build_base.html | 2 +- src/backend/InvenTree/common/models.py | 23 +++- src/backend/InvenTree/company/models.py | 10 ++ .../templates/company/supplier_part.html | 2 +- src/backend/InvenTree/order/models.py | 15 ++ .../order/templates/order/order_base.html | 2 +- .../templates/order/return_order_base.html | 2 +- .../templates/order/sales_order_base.html | 2 +- src/backend/InvenTree/part/models.py | 9 +- .../part/templates/part/part_base.html | 2 +- src/backend/InvenTree/part/test_part.py | 7 +- .../InvenTree/plugin/base/barcodes/api.py | 53 ++++++- .../InvenTree/plugin/base/barcodes/helper.py | 79 +++++++++++ .../InvenTree/plugin/base/barcodes/mixins.py | 25 ++++ .../plugin/base/barcodes/serializers.py | 30 +++- .../plugin/base/barcodes/test_barcode.py | 23 +++- .../builtin/barcodes/inventree_barcode.py | 79 +++++++++-- .../barcodes/test_inventree_barcode.py | 89 +++++++++++- .../InvenTree/report/templatetags/barcode.py | 54 +++++--- src/backend/InvenTree/stock/models.py | 14 +- .../stock/templates/stock/item_base.html | 2 +- .../stock/templates/stock/location.html | 2 +- src/backend/InvenTree/stock/tests.py | 8 +- .../templates/InvenTree/settings/barcode.html | 1 + src/frontend/package.json | 3 +- .../src/components/buttons/ButtonMenu.tsx | 2 +- .../src/components/buttons/CopyButton.tsx | 44 +++--- .../src/components/details/Details.tsx | 27 +--- .../TemplateEditor/CodeEditor/CodeEditor.tsx | 18 ++- .../src/components/items/ActionDropdown.tsx | 16 ++- src/frontend/src/components/items/QRCode.tsx | 130 ++++++++++++++++++ src/frontend/src/enums/ApiEndpoints.tsx | 1 + .../pages/Index/Settings/SystemSettings.tsx | 3 +- src/frontend/src/pages/build/BuildDetail.tsx | 5 +- src/frontend/src/pages/part/PartDetail.tsx | 5 +- .../pages/purchasing/PurchaseOrderDetail.tsx | 5 +- .../src/pages/stock/LocationDetail.tsx | 39 +++--- src/frontend/src/pages/stock/StockDetail.tsx | 5 +- src/frontend/src/tables/InvenTreeTable.tsx | 2 +- src/frontend/yarn.lock | 50 ++++--- 48 files changed, 845 insertions(+), 241 deletions(-) create mode 100644 src/backend/InvenTree/plugin/base/barcodes/helper.py create mode 100644 src/frontend/src/components/items/QRCode.tsx diff --git a/docs/docs/barcodes/internal.md b/docs/docs/barcodes/internal.md index 04dcd6e6c3..0eb7d35724 100644 --- a/docs/docs/barcodes/internal.md +++ b/docs/docs/barcodes/internal.md @@ -4,7 +4,11 @@ title: Internal Barcodes ## Internal Barcodes -InvenTree defines an internal format for generating barcodes for various items. This format uses a simple JSON-style string to uniquely identify an item in the database. +InvenTree ships with two integrated internal formats for generating barcodes for various items which are available through the built-in InvenTree Barcode plugin. The used format can be selected through the plugin settings of the InvenTree Barcode plugin. + +### 1. JSON-based QR Codes + +This format uses a simple JSON-style string to uniquely identify an item in the database. Some simple examples of this format are shown below: @@ -12,10 +16,49 @@ Some simple examples of this format are shown below: | --- | --- | | Part | `{% raw %}{"part": 10}{% endraw %}` | | Stock Item | `{% raw %}{"stockitem": 123}{% endraw %}` | +| Stock Location | `{% raw %}{"stocklocation": 1}{% endraw %}` | | Supplier Part | `{% raw %}{"supplierpart": 99}{% endraw %}` | The numerical ID value used is the *Primary Key* (PK) of the particular object in the database. +#### Downsides + +1. The JSON format includes binary only characters (`{% raw %}{{% endraw %}` and `{% raw %}"{% endraw %}`) which requires unnecessary use of the binary QR code encoding which means fewer amount of chars can be encoded with the same version of QR code. +2. The model name key has not a fixed length. Some model names are longer than others. E.g. a part QR code with the shortest possible id requires 11 chars, while a stock location QR code with the same id would already require 20 chars, which already requires QR code version 2 and quickly version 3. + +!!! info "QR code versions" + There are 40 different qr code versions from 1-40. They all can encode more data than the previous version, but require more "squares". E.g. a V1 QR codes has 21x21 "squares" while a V2 already has 25x25. For more information see [QR code comparison](https://www.qrcode.com/en/about/version.html). + +For a more detailed size analysis of the JSON-based QR codes refer to [this issue](https://github.com/inventree/InvenTree/issues/6612). + +### 2. Short alphanumeric QR Codes + +While JSON-based QR Codes encode all necessary information, they come with the described downsides. This new, short, alphanumeric only format is build to improve those downsides. The basic format uses an alphanumeric string: `INV-??x` + +- `INV-` is a constant prefix. This is configurable in the InvenTree Barcode plugins settings per instance to support environments that use multiple instances. +- `??` is a two character alphanumeric (`0-9A-Z $%*+-./:` (45 chars)) code, individual to each model. +- `x` the actual pk of the model. + +Now with an overhead of 6 chars for every model, this format supports the following amount of model instances using the described QR code modes: + +| QR code mode | Alphanumeric mode | Mixed mode | +| --- | --- | --- | +| v1 M ECL (15%) | `10**14` items (~3.170 items per sec for 1000 years) | `10**20` items (~3.170.979.198 items per sec for 1000 years) | +| v1 Q ECL (25%) | `10**10` items (~0.317 items per sec for 1000 years) | `10**13` items (~317 items per sec for 1000 years) | +| v1 H ECL (30%) | `10**4` items (~100 items per day for 100 days) | `10**3` items (~100 items per day for 10 days (*even worse*)) | + +!!! info "QR code mixed mode" + Normally the QR code data is encoded only in one format (binary, alphanumeric, numeric). But the data can also be split into multiple chunks using different formats. This is especially useful with long model ids, because the first 6 chars can be encoded using the alphanumeric mode and the id using the more efficient numeric mode. Mixed mode is used by default, because the `qrcode` template tag uses a default value for optimize of 1. + +Some simple examples of this format are shown below: + +| Model Type | Example Barcode | +| --- | --- | +| Part | `INV-PA10` | +| Stock Item | `INV-SI123` | +| Stock Location | `INV-SL1` | +| Supplier Part | `INV-SP99` | + ## Report Integration This barcode format can be used to generate 1D or 2D barcodes (e.g. for [labels and reports](../report/barcodes.md)) diff --git a/docs/docs/extend/plugins/barcode.md b/docs/docs/extend/plugins/barcode.md index 51f953b0cd..76a6356e36 100644 --- a/docs/docs/extend/plugins/barcode.md +++ b/docs/docs/extend/plugins/barcode.md @@ -4,7 +4,7 @@ title: Barcode Mixin ## Barcode Plugins -InvenTree supports decoding of arbitrary barcode data via a **Barcode Plugin** interface. Barcode data POSTed to the `/api/barcode/` endpoint will be supplied to all loaded barcode plugins, and the first plugin to successfully interpret the barcode data will return a response to the client. +InvenTree supports decoding of arbitrary barcode data and generation of internal barcode formats via a **Barcode Plugin** interface. Barcode data POSTed to the `/api/barcode/` endpoint will be supplied to all loaded barcode plugins, and the first plugin to successfully interpret the barcode data will return a response to the client. InvenTree can generate native QR codes to represent database objects (e.g. a single StockItem). This barcode can then be used to perform quick lookup of a stock item or location in the database. A client application (for example the InvenTree mobile app) scans a barcode, and sends the barcode data to the InvenTree server. The server then uses the **InvenTreeBarcodePlugin** (found at `src/backend/InvenTree/plugin/builtin/barcodes/inventree_barcode.py`) to decode the supplied barcode data. @@ -26,7 +26,7 @@ POST { ### Builtin Plugin -The InvenTree server includes a builtin barcode plugin which can decode QR codes generated by the server. This plugin is enabled by default. +The InvenTree server includes a builtin barcode plugin which can generate and decode the QR codes. This plugin is enabled by default. ::: plugin.builtin.barcodes.inventree_barcode.InvenTreeInternalBarcodePlugin options: @@ -39,14 +39,12 @@ The InvenTree server includes a builtin barcode plugin which can decode QR codes ### Example Plugin -Please find below a very simple example that is executed each time a barcode is scanned. +Please find below a very simple example that is used to return a part if the barcode starts with `PART-` ```python -from django.utils.translation import gettext_lazy as _ - -from InvenTree.models import InvenTreeBarcodeMixin from plugin import InvenTreePlugin from plugin.mixins import BarcodeMixin +from part.models import Part class InvenTreeBarcodePlugin(BarcodeMixin, InvenTreePlugin): @@ -56,16 +54,39 @@ class InvenTreeBarcodePlugin(BarcodeMixin, InvenTreePlugin): VERSION = "0.0.1" AUTHOR = "Michael" - status = 0 - def scan(self, barcode_data): + if barcode_data.startswith("PART-"): + try: + pk = int(barcode_data.split("PART-")[1]) + instance = Part.objects.get(pk=pk) + label = Part.barcode_model_type() - self.status = self.status+1 - print('Started barcode plugin', self.status) - print(barcode_data) - response = {} - return response - + return {label: instance.format_matched_response()} + except Part.DoesNotExist: + pass ``` To try it just copy the file to src/InvenTree/plugins and restart the server. Open the scan barcode window and start to scan codes or type in text manually. Each time the timeout is hit the plugin will execute and printout the result. The timeout can be changed in `Settings->Barcode Support->Barcode Input Delay`. + +### Custom Internal Format + +To implement a custom internal barcode format, the `generate(...)` method from the Barcode Mixin needs to be overridden. Then the plugin can be selected at `System Settings > Barcodes > Barcode Generation Plugin`. + +```python +from InvenTree.models import InvenTreeBarcodeMixin +from plugin import InvenTreePlugin +from plugin.mixins import BarcodeMixin + +class InvenTreeBarcodePlugin(BarcodeMixin, InvenTreePlugin): + NAME = "MyInternalBarcode" + TITLE = "My Internal Barcodes" + DESCRIPTION = "support for custom internal barcodes" + VERSION = "0.0.1" + AUTHOR = "InvenTree contributors" + + def generate(self, model_instance: InvenTreeBarcodeMixin): + return f'{model_instance.barcode_model_type()}: {model_instance.pk}' +``` + +!!! info "Scanning implementation required" + The parsing of the custom format needs to be implemented too, so that the scanning of the generated QR codes resolves to the correct part. diff --git a/src/backend/InvenTree/InvenTree/api_version.py b/src/backend/InvenTree/InvenTree/api_version.py index 0079fb39be..95b0caa66a 100644 --- a/src/backend/InvenTree/InvenTree/api_version.py +++ b/src/backend/InvenTree/InvenTree/api_version.py @@ -1,12 +1,15 @@ """InvenTree API version information.""" # InvenTree API version -INVENTREE_API_VERSION = 225 +INVENTREE_API_VERSION = 226 """Increment this API version number whenever there is a significant change to the API that any clients need to know about.""" INVENTREE_API_TEXT = """ +v226 - 2024-07-15 : https://github.com/inventree/InvenTree/pull/7648 + - Adds barcode generation API endpoint + v225 - 2024-07-17 : https://github.com/inventree/InvenTree/pull/7671 - Adds "filters" field to DataImportSession API diff --git a/src/backend/InvenTree/InvenTree/helpers.py b/src/backend/InvenTree/InvenTree/helpers.py index ae72f59578..4558594436 100644 --- a/src/backend/InvenTree/InvenTree/helpers.py +++ b/src/backend/InvenTree/InvenTree/helpers.py @@ -396,38 +396,6 @@ def WrapWithQuotes(text, quote='"'): return text -def MakeBarcode(cls_name, object_pk: int, object_data=None, **kwargs): - """Generate a string for a barcode. Adds some global InvenTree parameters. - - Args: - cls_name: string describing the object type e.g. 'StockItem' - object_pk (int): ID (Primary Key) of the object in the database - object_data: Python dict object containing extra data which will be rendered to string (must only contain stringable values) - - Returns: - json string of the supplied data plus some other data - """ - if object_data is None: - object_data = {} - - brief = kwargs.get('brief', True) - - data = {} - - if brief: - data[cls_name] = object_pk - else: - data['tool'] = 'InvenTree' - data['version'] = InvenTree.version.inventreeVersion() - data['instance'] = InvenTree.version.inventreeInstanceName() - - # Ensure PK is included - object_data['id'] = object_pk - data[cls_name] = object_data - - return str(json.dumps(data, sort_keys=True)) - - def GetExportFormats(): """Return a list of allowable file formats for importing or exporting tabular data.""" return ['csv', 'xlsx', 'tsv', 'json'] diff --git a/src/backend/InvenTree/InvenTree/helpers_model.py b/src/backend/InvenTree/InvenTree/helpers_model.py index e9bbdc0b06..bf814dc12a 100644 --- a/src/backend/InvenTree/InvenTree/helpers_model.py +++ b/src/backend/InvenTree/InvenTree/helpers_model.py @@ -15,9 +15,6 @@ from djmoney.contrib.exchange.models import convert_money from djmoney.money import Money from PIL import Image -import InvenTree -import InvenTree.helpers_model -import InvenTree.version from common.notifications import ( InvenTreeNotificationBodies, NotificationBody, @@ -331,9 +328,7 @@ def notify_users( 'instance': instance, 'name': content.name.format(**content_context), 'message': content.message.format(**content_context), - 'link': InvenTree.helpers_model.construct_absolute_url( - instance.get_absolute_url() - ), + 'link': construct_absolute_url(instance.get_absolute_url()), 'template': {'subject': content.name.format(**content_context)}, } diff --git a/src/backend/InvenTree/InvenTree/models.py b/src/backend/InvenTree/InvenTree/models.py index 33cd1e9097..81a4fd002a 100644 --- a/src/backend/InvenTree/InvenTree/models.py +++ b/src/backend/InvenTree/InvenTree/models.py @@ -3,9 +3,7 @@ import logging from datetime import datetime -from django.conf import settings from django.contrib.auth import get_user_model -from django.contrib.auth.models import User from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ValidationError from django.db import models @@ -934,6 +932,8 @@ class InvenTreeBarcodeMixin(models.Model): - barcode_data : Raw data associated with an assigned barcode - barcode_hash : A 'hash' of the assigned barcode data used to improve matching + + The barcode_model_type_code() classmethod must be implemented in the model class. """ class Meta: @@ -964,11 +964,25 @@ class InvenTreeBarcodeMixin(models.Model): # By default, use the name of the class return cls.__name__.lower() + @classmethod + def barcode_model_type_code(cls): + r"""Return a 'short' code for the model type. + + This is used to generate a efficient QR code for the model type. + It is expected to match this pattern: [0-9A-Z $%*+-.\/:]{2} + + Note: Due to the shape constrains (45**2=2025 different allowed codes) + this needs to be explicitly implemented in the model class to avoid collisions. + """ + raise NotImplementedError( + 'barcode_model_type_code() must be implemented in the model class' + ) + def format_barcode(self, **kwargs): """Return a JSON string for formatting a QR code for this model instance.""" - return InvenTree.helpers.MakeBarcode( - self.__class__.barcode_model_type(), self.pk, **kwargs - ) + from plugin.base.barcodes.helper import generate_barcode + + return generate_barcode(self) def format_matched_response(self): """Format a standard response for a matched barcode.""" @@ -986,7 +1000,7 @@ class InvenTreeBarcodeMixin(models.Model): @property def barcode(self): """Format a minimal barcode string (e.g. for label printing).""" - return self.format_barcode(brief=True) + return self.format_barcode() @classmethod def lookup_barcode(cls, barcode_hash): diff --git a/src/backend/InvenTree/InvenTree/tests.py b/src/backend/InvenTree/InvenTree/tests.py index 165c981edd..b4ff159c90 100644 --- a/src/backend/InvenTree/InvenTree/tests.py +++ b/src/backend/InvenTree/InvenTree/tests.py @@ -789,33 +789,6 @@ class TestIncrement(TestCase): self.assertEqual(result, b) -class TestMakeBarcode(TestCase): - """Tests for barcode string creation.""" - - def test_barcode_extended(self): - """Test creation of barcode with extended data.""" - bc = helpers.MakeBarcode( - 'part', 3, {'id': 3, 'url': 'www.google.com'}, brief=False - ) - - self.assertIn('part', bc) - self.assertIn('tool', bc) - self.assertIn('"tool": "InvenTree"', bc) - - data = json.loads(bc) - - self.assertEqual(data['part']['id'], 3) - self.assertEqual(data['part']['url'], 'www.google.com') - - def test_barcode_brief(self): - """Test creation of simple barcode.""" - bc = helpers.MakeBarcode('stockitem', 7) - - data = json.loads(bc) - self.assertEqual(len(data), 1) - self.assertEqual(data['stockitem'], 7) - - class TestDownloadFile(TestCase): """Tests for DownloadFile.""" diff --git a/src/backend/InvenTree/build/models.py b/src/backend/InvenTree/build/models.py index 2ad57b9f62..85a97fb625 100644 --- a/src/backend/InvenTree/build/models.py +++ b/src/backend/InvenTree/build/models.py @@ -115,6 +115,11 @@ class Build( return defaults + @classmethod + def barcode_model_type_code(cls): + """Return the associated barcode model type code for this model.""" + return "BO" + def save(self, *args, **kwargs): """Custom save method for the BuildOrder model""" self.validate_reference_field(self.reference) diff --git a/src/backend/InvenTree/build/templates/build/build_base.html b/src/backend/InvenTree/build/templates/build/build_base.html index 8254673fc7..5a40b32e7b 100644 --- a/src/backend/InvenTree/build/templates/build/build_base.html +++ b/src/backend/InvenTree/build/templates/build/build_base.html @@ -277,7 +277,7 @@ src="{% static 'img/blank_image.png' %}" $('#show-qr-code').click(function() { showQRDialog( '{% trans "Build Order QR Code" escape %}', - '{"build": {{ build.pk }} }' + '{{ build.barcode }}' ); }); diff --git a/src/backend/InvenTree/common/models.py b/src/backend/InvenTree/common/models.py index 4dd125d5a5..93da434ad5 100644 --- a/src/backend/InvenTree/common/models.py +++ b/src/backend/InvenTree/common/models.py @@ -9,12 +9,13 @@ import hmac import json import logging import os +import sys import uuid from datetime import timedelta, timezone from enum import Enum from io import BytesIO from secrets import compare_digest -from typing import Any, Callable, Collection, TypedDict, Union +from typing import Any, Callable, TypedDict, Union from django.apps import apps from django.conf import settings as django_settings @@ -49,6 +50,7 @@ import InvenTree.ready import InvenTree.tasks import InvenTree.validators import order.validators +import plugin.base.barcodes.helper import report.helpers import users.models from InvenTree.sanitizer import sanitize_svg @@ -56,6 +58,17 @@ from plugin import registry logger = logging.getLogger('inventree') +if sys.version_info >= (3, 11): + from typing import NotRequired +else: + + class NotRequired: # pragma: no cover + """NotRequired type helper is only supported with Python 3.11+.""" + + def __class_getitem__(cls, item): + """Return the item.""" + return item + class MetaMixin(models.Model): """A base class for InvenTree models to include shared meta fields. @@ -1167,7 +1180,7 @@ class InvenTreeSettingsKeyType(SettingsKeyType): requires_restart: If True, a server restart is required after changing the setting """ - requires_restart: bool + requires_restart: NotRequired[bool] class InvenTreeSetting(BaseInvenTreeSetting): @@ -1402,6 +1415,12 @@ class InvenTreeSetting(BaseInvenTreeSetting): 'default': False, 'validator': bool, }, + 'BARCODE_GENERATION_PLUGIN': { + 'name': _('Barcode Generation Plugin'), + 'description': _('Plugin to use for internal barcode data generation'), + 'choices': plugin.base.barcodes.helper.barcode_plugins, + 'default': 'inventreebarcode', + }, 'PART_ENABLE_REVISION': { 'name': _('Part Revisions'), 'description': _('Enable revision field for Part'), diff --git a/src/backend/InvenTree/company/models.py b/src/backend/InvenTree/company/models.py index 52eeb80cdc..9e1c878c57 100644 --- a/src/backend/InvenTree/company/models.py +++ b/src/backend/InvenTree/company/models.py @@ -475,6 +475,11 @@ class ManufacturerPart( """Return the API URL associated with the ManufacturerPart instance.""" return reverse('api-manufacturer-part-list') + @classmethod + def barcode_model_type_code(cls): + """Return the associated barcode model type code for this model.""" + return 'MP' + part = models.ForeignKey( 'part.Part', on_delete=models.CASCADE, @@ -678,6 +683,11 @@ class SupplierPart( """Return custom API filters for this particular instance.""" return {'manufacturer_part': {'part': self.part.pk}} + @classmethod + def barcode_model_type_code(cls): + """Return the associated barcode model type code for this model.""" + return 'SP' + def clean(self): """Custom clean action for the SupplierPart model. diff --git a/src/backend/InvenTree/company/templates/company/supplier_part.html b/src/backend/InvenTree/company/templates/company/supplier_part.html index 16841c5570..b723f1ef8b 100644 --- a/src/backend/InvenTree/company/templates/company/supplier_part.html +++ b/src/backend/InvenTree/company/templates/company/supplier_part.html @@ -303,7 +303,7 @@ onPanelLoad('supplier-part-notes', function() { $("#show-qr-code").click(function() { showQRDialog( '{% trans "Supplier Part QR Code" escape %}', - '{"supplierpart": {{ part.pk }} }' + '{{ part.barcode }}' ); }); diff --git a/src/backend/InvenTree/order/models.py b/src/backend/InvenTree/order/models.py index 6020456971..cfa17f4d72 100644 --- a/src/backend/InvenTree/order/models.py +++ b/src/backend/InvenTree/order/models.py @@ -408,6 +408,11 @@ class PurchaseOrder(TotalPriceMixin, Order): return defaults + @classmethod + def barcode_model_type_code(cls): + """Return the associated barcode model type code for this model.""" + return 'PO' + @staticmethod def filterByDate(queryset, min_date, max_date): """Filter by 'minimum and maximum date range'. @@ -880,6 +885,11 @@ class SalesOrder(TotalPriceMixin, Order): return defaults + @classmethod + def barcode_model_type_code(cls): + """Return the associated barcode model type code for this model.""" + return 'SO' + @staticmethod def filterByDate(queryset, min_date, max_date): """Filter by "minimum and maximum date range". @@ -2044,6 +2054,11 @@ class ReturnOrder(TotalPriceMixin, Order): return defaults + @classmethod + def barcode_model_type_code(cls): + """Return the associated barcode model type code for this model.""" + return 'RO' + def __str__(self): """Render a string representation of this ReturnOrder.""" return f"{self.reference} - {self.customer.name if self.customer else _('no customer')}" diff --git a/src/backend/InvenTree/order/templates/order/order_base.html b/src/backend/InvenTree/order/templates/order/order_base.html index af422e6794..ce9bc02fad 100644 --- a/src/backend/InvenTree/order/templates/order/order_base.html +++ b/src/backend/InvenTree/order/templates/order/order_base.html @@ -312,7 +312,7 @@ $("#export-order").click(function() { $('#show-qr-code').click(function() { showQRDialog( '{% trans "Purchase Order QR Code" escape %}', - '{"purchaseorder": {{ order.pk }} }' + '{{ order.barcode }}' ); }); diff --git a/src/backend/InvenTree/order/templates/order/return_order_base.html b/src/backend/InvenTree/order/templates/order/return_order_base.html index 32ccd23f85..f4590c71eb 100644 --- a/src/backend/InvenTree/order/templates/order/return_order_base.html +++ b/src/backend/InvenTree/order/templates/order/return_order_base.html @@ -257,7 +257,7 @@ $('#print-order-report').click(function() { $('#show-qr-code').click(function() { showQRDialog( '{% trans "Return Order QR Code" escape %}', - '{"returnorder": {{ order.pk }} }' + '{{ order.barcode }}' ); }); diff --git a/src/backend/InvenTree/order/templates/order/sales_order_base.html b/src/backend/InvenTree/order/templates/order/sales_order_base.html index bc306502a7..2a3e676e95 100644 --- a/src/backend/InvenTree/order/templates/order/sales_order_base.html +++ b/src/backend/InvenTree/order/templates/order/sales_order_base.html @@ -319,7 +319,7 @@ $('#print-order-report').click(function() { $('#show-qr-code').click(function() { showQRDialog( '{% trans "Sales Order QR Code" escape %}', - '{"salesorder": {{ order.pk }} }' + '{{ order.barcode }}' ); }); diff --git a/src/backend/InvenTree/part/models.py b/src/backend/InvenTree/part/models.py index 730cf5a270..f759241aa7 100644 --- a/src/backend/InvenTree/part/models.py +++ b/src/backend/InvenTree/part/models.py @@ -416,6 +416,11 @@ class Part( """Return API query filters for limiting field results against this instance.""" return {'variant_of': {'exclude_tree': self.pk}} + @classmethod + def barcode_model_type_code(cls): + """Return the associated barcode model type code for this model.""" + return 'PA' + def report_context(self): """Return custom report context information.""" return { @@ -426,11 +431,11 @@ class Part( 'name': self.name, 'parameters': self.parameters_map(), 'part': self, - 'qr_data': self.format_barcode(brief=True), + 'qr_data': self.barcode, 'qr_url': self.get_absolute_url(), 'revision': self.revision, 'test_template_list': self.getTestTemplates(), - 'test_templates': self.getTestTemplates(), + 'test_templates': self.getTestTemplateMap(), } def get_context_data(self, request, **kwargs): diff --git a/src/backend/InvenTree/part/templates/part/part_base.html b/src/backend/InvenTree/part/templates/part/part_base.html index 835258c8ea..b29f40f751 100644 --- a/src/backend/InvenTree/part/templates/part/part_base.html +++ b/src/backend/InvenTree/part/templates/part/part_base.html @@ -451,7 +451,7 @@ $("#show-qr-code").click(function() { showQRDialog( '{% trans "Part QR Code" escape %}', - '{"part": {{ part.pk }} }', + '{{ part.barcode }}', ); }); diff --git a/src/backend/InvenTree/part/test_part.py b/src/backend/InvenTree/part/test_part.py index 6f0eb5beba..04e89f4520 100644 --- a/src/backend/InvenTree/part/test_part.py +++ b/src/backend/InvenTree/part/test_part.py @@ -169,7 +169,7 @@ class PartTest(TestCase): self.assertEqual(Part.barcode_model_type(), 'part') p = Part.objects.get(pk=1) - barcode = p.format_barcode(brief=True) + barcode = p.format_barcode() self.assertEqual(barcode, '{"part": 1}') def test_tree(self): @@ -270,9 +270,8 @@ class PartTest(TestCase): def test_barcode(self): """Test barcode format functionality.""" - barcode = self.r1.format_barcode(brief=False) - self.assertIn('InvenTree', barcode) - self.assertIn('"part": {"id": 3}', barcode) + barcode = self.r1.format_barcode() + self.assertEqual('{"part": 3}', barcode) def test_sell_pricing(self): """Check that the sell pricebreaks were loaded.""" diff --git a/src/backend/InvenTree/plugin/base/barcodes/api.py b/src/backend/InvenTree/plugin/base/barcodes/api.py index 8cd75cf8b6..01b2f6ae9b 100644 --- a/src/backend/InvenTree/plugin/base/barcodes/api.py +++ b/src/backend/InvenTree/plugin/base/barcodes/api.py @@ -6,16 +6,17 @@ from django.db.models import F from django.urls import path from django.utils.translation import gettext_lazy as _ -from rest_framework import permissions +from drf_spectacular.utils import extend_schema, extend_schema_view +from rest_framework import permissions, status from rest_framework.exceptions import PermissionDenied, ValidationError from rest_framework.generics import CreateAPIView from rest_framework.response import Response import order.models +import plugin.base.barcodes.helper import stock.models from InvenTree.helpers import hash_barcode from plugin import registry -from plugin.builtin.barcodes.inventree_barcode import InvenTreeInternalBarcodePlugin from users.models import RuleSet from . import serializers as barcode_serializers @@ -129,6 +130,48 @@ class BarcodeScan(BarcodeView): return Response(result) +@extend_schema_view( + post=extend_schema(responses={200: barcode_serializers.BarcodeSerializer}) +) +class BarcodeGenerate(CreateAPIView): + """Endpoint for generating a barcode for a database object. + + The barcode is generated by the selected barcode plugin. + """ + + serializer_class = barcode_serializers.BarcodeGenerateSerializer + + def queryset(self): + """This API view does not have a queryset.""" + return None + + # Default permission classes (can be overridden) + permission_classes = [permissions.IsAuthenticated] + + def create(self, request, *args, **kwargs): + """Perform the barcode generation action.""" + serializer = self.get_serializer(data=request.data) + serializer.is_valid(raise_exception=True) + + model = serializer.validated_data.get('model') + pk = serializer.validated_data.get('pk') + model_cls = plugin.base.barcodes.helper.get_supported_barcode_models_map().get( + model, None + ) + + if model_cls is None: + raise ValidationError({'error': _('Model is not supported')}) + + try: + model_instance = model_cls.objects.get(pk=pk) + except model_cls.DoesNotExist: + raise ValidationError({'error': _('Model instance not found')}) + + barcode_data = plugin.base.barcodes.helper.generate_barcode(model_instance) + + return Response({'barcode': barcode_data}, status=status.HTTP_200_OK) + + class BarcodeAssign(BarcodeView): """Endpoint for assigning a barcode to a stock item. @@ -161,7 +204,7 @@ class BarcodeAssign(BarcodeView): valid_labels = [] - for model in InvenTreeInternalBarcodePlugin.get_supported_barcode_models(): + for model in plugin.base.barcodes.helper.get_supported_barcode_models(): label = model.barcode_model_type() valid_labels.append(label) @@ -203,7 +246,7 @@ class BarcodeUnassign(BarcodeView): serializer.is_valid(raise_exception=True) data = serializer.validated_data - supported_models = InvenTreeInternalBarcodePlugin.get_supported_barcode_models() + supported_models = plugin.base.barcodes.helper.get_supported_barcode_models() supported_labels = [model.barcode_model_type() for model in supported_models] model_names = ', '.join(supported_labels) @@ -567,6 +610,8 @@ class BarcodeSOAllocate(BarcodeView): barcode_api_urls = [ + # Generate a barcode for a database object + path('generate/', BarcodeGenerate.as_view(), name='api-barcode-generate'), # Link a third-party barcode to an item (e.g. Part / StockItem / etc) path('link/', BarcodeAssign.as_view(), name='api-barcode-link'), # Unlink a third-party barcode from an item diff --git a/src/backend/InvenTree/plugin/base/barcodes/helper.py b/src/backend/InvenTree/plugin/base/barcodes/helper.py new file mode 100644 index 0000000000..f1815ba505 --- /dev/null +++ b/src/backend/InvenTree/plugin/base/barcodes/helper.py @@ -0,0 +1,79 @@ +"""Helper functions for barcode generation.""" + +import logging +from typing import Type, cast + +import InvenTree.helpers_model +from InvenTree.models import InvenTreeBarcodeMixin + +logger = logging.getLogger('inventree') + + +def cache(func): + """Cache the result of a function, but do not cache falsy results.""" + cache = {} + + def wrapper(): + """Wrapper function for caching.""" + if 'default' not in cache: + res = func() + + if res: + cache['default'] = res + + return res + + return cache['default'] + + return wrapper + + +def barcode_plugins() -> list: + """Return a list of plugin choices which can be used for barcode generation.""" + try: + from plugin import registry + + plugins = registry.with_mixin('barcode', active=True) + except Exception: + plugins = [] + + return [ + (plug.slug, plug.human_name) for plug in plugins if plug.has_barcode_generation + ] + + +def generate_barcode(model_instance: InvenTreeBarcodeMixin): + """Generate a barcode for a given model instance.""" + from common.settings import get_global_setting + from plugin import registry + from plugin.mixins import BarcodeMixin + + # Find the selected barcode generation plugin + slug = get_global_setting('BARCODE_GENERATION_PLUGIN', create=False) + + plugin = cast(BarcodeMixin, registry.get_plugin(slug)) + + return plugin.generate(model_instance) + + +@cache +def get_supported_barcode_models() -> list[Type[InvenTreeBarcodeMixin]]: + """Returns a list of database models which support barcode functionality.""" + return InvenTree.helpers_model.getModelsWithMixin(InvenTreeBarcodeMixin) + + +@cache +def get_supported_barcode_models_map(): + """Return a mapping of barcode model types to the model class.""" + return { + model.barcode_model_type(): model for model in get_supported_barcode_models() + } + + +@cache +def get_supported_barcode_model_codes_map(): + """Return a mapping of barcode model type codes to the model class.""" + return { + model.barcode_model_type_code(): model + for model in get_supported_barcode_models() + } diff --git a/src/backend/InvenTree/plugin/base/barcodes/mixins.py b/src/backend/InvenTree/plugin/base/barcodes/mixins.py index 929a037115..28fb1c49a8 100644 --- a/src/backend/InvenTree/plugin/base/barcodes/mixins.py +++ b/src/backend/InvenTree/plugin/base/barcodes/mixins.py @@ -10,6 +10,7 @@ from django.db.models import F, Q from django.utils.translation import gettext_lazy as _ from company.models import Company, SupplierPart +from InvenTree.models import InvenTreeBarcodeMixin from order.models import PurchaseOrder, PurchaseOrderStatus from plugin.base.integration.SettingsMixin import SettingsMixin from stock.models import StockLocation @@ -53,6 +54,30 @@ class BarcodeMixin: """ return None + @property + def has_barcode_generation(self): + """Does this plugin support barcode generation.""" + try: + # Attempt to call the generate method + self.generate(None) # type: ignore + except NotImplementedError: + # If a NotImplementedError is raised, then barcode generation is not supported + return False + except: + pass + + return True + + def generate(self, model_instance: InvenTreeBarcodeMixin): + """Generate barcode data for the given model instance. + + Arguments: + model_instance: The model instance to generate barcode data for. It is extending the InvenTreeBarcodeMixin. + + Returns: The generated barcode data. + """ + raise NotImplementedError('Generate must be implemented by a plugin') + class SupplierBarcodeMixin(BarcodeMixin): """Mixin that provides default implementations for scan functions for supplier barcodes. diff --git a/src/backend/InvenTree/plugin/base/barcodes/serializers.py b/src/backend/InvenTree/plugin/base/barcodes/serializers.py index 6ad15713b7..b31ab1818a 100644 --- a/src/backend/InvenTree/plugin/base/barcodes/serializers.py +++ b/src/backend/InvenTree/plugin/base/barcodes/serializers.py @@ -6,9 +6,9 @@ from django.utils.translation import gettext_lazy as _ from rest_framework import serializers import order.models +import plugin.base.barcodes.helper import stock.models from order.status_codes import PurchaseOrderStatus, SalesOrderStatus -from plugin.builtin.barcodes.inventree_barcode import InvenTreeInternalBarcodePlugin class BarcodeSerializer(serializers.Serializer): @@ -23,6 +23,30 @@ class BarcodeSerializer(serializers.Serializer): ) +class BarcodeGenerateSerializer(serializers.Serializer): + """Serializer for generating a barcode.""" + + model = serializers.CharField( + required=True, help_text=_('Model name to generate barcode for') + ) + + pk = serializers.IntegerField( + required=True, + help_text=_('Primary key of model object to generate barcode for'), + ) + + def validate_model(self, model: str): + """Validate the provided model.""" + supported_models = ( + plugin.base.barcodes.helper.get_supported_barcode_models_map() + ) + + if model not in supported_models.keys(): + raise ValidationError(_('Model is not supported')) + + return model + + class BarcodeAssignMixin(serializers.Serializer): """Serializer for linking and unlinking barcode to an internal class.""" @@ -30,7 +54,7 @@ class BarcodeAssignMixin(serializers.Serializer): """Generate serializer fields for each supported model type.""" super().__init__(*args, **kwargs) - for model in InvenTreeInternalBarcodePlugin.get_supported_barcode_models(): + for model in plugin.base.barcodes.helper.get_supported_barcode_models(): self.fields[model.barcode_model_type()] = ( serializers.PrimaryKeyRelatedField( queryset=model.objects.all(), @@ -45,7 +69,7 @@ class BarcodeAssignMixin(serializers.Serializer): """Return a list of model fields.""" fields = [ model.barcode_model_type() - for model in InvenTreeInternalBarcodePlugin.get_supported_barcode_models() + for model in plugin.base.barcodes.helper.get_supported_barcode_models() ] return fields diff --git a/src/backend/InvenTree/plugin/base/barcodes/test_barcode.py b/src/backend/InvenTree/plugin/base/barcodes/test_barcode.py index 408c28d35f..9512a31fa5 100644 --- a/src/backend/InvenTree/plugin/base/barcodes/test_barcode.py +++ b/src/backend/InvenTree/plugin/base/barcodes/test_barcode.py @@ -21,6 +21,7 @@ class BarcodeAPITest(InvenTreeAPITestCase): super().setUp() self.scan_url = reverse('api-barcode-scan') + self.generate_url = reverse('api-barcode-generate') self.assign_url = reverse('api-barcode-link') self.unassign_url = reverse('api-barcode-unlink') @@ -30,6 +31,14 @@ class BarcodeAPITest(InvenTreeAPITestCase): url, data={'barcode': str(barcode)}, expected_code=expected_code ) + def generateBarcode(self, model: str, pk: int, expected_code: int): + """Post barcode generation and return barcode contents.""" + return self.post( + self.generate_url, + data={'model': model, 'pk': pk}, + expected_code=expected_code, + ) + def test_invalid(self): """Test that invalid requests fail.""" # test scan url @@ -130,7 +139,7 @@ class BarcodeAPITest(InvenTreeAPITestCase): data = response.data self.assertIn('error', data) - def test_barcode_generation(self): + def test_barcode_scan(self): """Test that a barcode is generated with a scan.""" item = StockItem.objects.get(pk=522) @@ -145,6 +154,18 @@ class BarcodeAPITest(InvenTreeAPITestCase): self.assertEqual(pk, item.pk) + def test_barcode_generation(self): + """Test that a barcode can be generated for a StockItem.""" + item = StockItem.objects.get(pk=522) + + data = self.generateBarcode('stockitem', item.pk, expected_code=200).data + self.assertEqual(data['barcode'], '{"stockitem": 522}') + + def test_barcode_generation_invalid(self): + """Test barcode generation for invalid model/pk.""" + self.generateBarcode('invalidmodel', 1, expected_code=400) + self.generateBarcode('stockitem', 99999999, expected_code=400) + def test_association(self): """Test that a barcode can be associated with a StockItem.""" item = StockItem.objects.get(pk=522) diff --git a/src/backend/InvenTree/plugin/builtin/barcodes/inventree_barcode.py b/src/backend/InvenTree/plugin/builtin/barcodes/inventree_barcode.py index 78f694424d..c3c0f75e2a 100644 --- a/src/backend/InvenTree/plugin/builtin/barcodes/inventree_barcode.py +++ b/src/backend/InvenTree/plugin/builtin/barcodes/inventree_barcode.py @@ -8,29 +8,45 @@ references model objects actually exist in the database. """ import json +import re +from typing import cast from django.utils.translation import gettext_lazy as _ +import plugin.base.barcodes.helper from InvenTree.helpers import hash_barcode -from InvenTree.helpers_model import getModelsWithMixin from InvenTree.models import InvenTreeBarcodeMixin from plugin import InvenTreePlugin -from plugin.mixins import BarcodeMixin +from plugin.mixins import BarcodeMixin, SettingsMixin -class InvenTreeInternalBarcodePlugin(BarcodeMixin, InvenTreePlugin): +class InvenTreeInternalBarcodePlugin(SettingsMixin, BarcodeMixin, InvenTreePlugin): """Builtin BarcodePlugin for matching and generating internal barcodes.""" NAME = 'InvenTreeBarcode' TITLE = _('InvenTree Barcodes') DESCRIPTION = _('Provides native support for barcodes') - VERSION = '2.0.0' + VERSION = '2.1.0' AUTHOR = _('InvenTree contributors') - @staticmethod - def get_supported_barcode_models(): - """Returns a list of database models which support barcode functionality.""" - return getModelsWithMixin(InvenTreeBarcodeMixin) + SETTINGS = { + 'INTERNAL_BARCODE_FORMAT': { + 'name': _('Internal Barcode Format'), + 'description': _('Select an internal barcode format'), + 'choices': [ + ('json', _('JSON barcodes (human readable)')), + ('short', _('Short barcodes (space optimized)')), + ], + 'default': 'json', + }, + 'SHORT_BARCODE_PREFIX': { + 'name': _('Short Barcode Prefix'), + 'description': _( + 'Customize the prefix used for short barcodes, may be useful for environments with multiple InvenTree instances' + ), + 'default': 'INV-', + }, + } def format_matched_response(self, label, model, instance): """Format a response for the scanned data.""" @@ -41,8 +57,35 @@ class InvenTreeInternalBarcodePlugin(BarcodeMixin, InvenTreePlugin): Here we are looking for a dict object which contains a reference to a particular InvenTree database object """ + # Internal Barcodes - Short Format + # Attempt to match the barcode data against the short barcode format + prefix = cast(str, self.get_setting('SHORT_BARCODE_PREFIX')) + if type(barcode_data) is str and ( + m := re.match( + f'^{re.escape(prefix)}([0-9A-Z $%*+-.\\/:]{"{2}"})(\\d+)$', barcode_data + ) + ): + model_type_code, pk = m.groups() + + supported_models_map = ( + plugin.base.barcodes.helper.get_supported_barcode_model_codes_map() + ) + model = supported_models_map.get(model_type_code, None) + + if model is None: + return None + + label = model.barcode_model_type() + + try: + instance = model.objects.get(pk=int(pk)) + return self.format_matched_response(label, model, instance) + except (ValueError, model.DoesNotExist): + pass + + # Internal Barcodes - JSON Format # Attempt to coerce the barcode data into a dict object - # This is the internal barcode representation that InvenTree uses + # This is the internal JSON barcode representation that InvenTree uses barcode_dict = None if type(barcode_data) is dict: @@ -53,7 +96,7 @@ class InvenTreeInternalBarcodePlugin(BarcodeMixin, InvenTreePlugin): except json.JSONDecodeError: pass - supported_models = self.get_supported_barcode_models() + supported_models = plugin.base.barcodes.helper.get_supported_barcode_models() if barcode_dict is not None and type(barcode_dict) is dict: # Look for various matches. First good match will be returned @@ -68,6 +111,7 @@ class InvenTreeInternalBarcodePlugin(BarcodeMixin, InvenTreePlugin): except (ValueError, model.DoesNotExist): pass + # External Barcodes (Linked barcodes) # Create hash from raw barcode data barcode_hash = hash_barcode(barcode_data) @@ -79,3 +123,18 @@ class InvenTreeInternalBarcodePlugin(BarcodeMixin, InvenTreePlugin): if instance is not None: return self.format_matched_response(label, model, instance) + + def generate(self, model_instance: InvenTreeBarcodeMixin): + """Generate a barcode for a given model instance.""" + barcode_format = self.get_setting('INTERNAL_BARCODE_FORMAT') + + if barcode_format == 'json': + return json.dumps({model_instance.barcode_model_type(): model_instance.pk}) + + if barcode_format == 'short': + prefix = self.get_setting('SHORT_BARCODE_PREFIX') + model_type_code = model_instance.barcode_model_type_code() + + return f'{prefix}{model_type_code}{model_instance.pk}' + + return None diff --git a/src/backend/InvenTree/plugin/builtin/barcodes/test_inventree_barcode.py b/src/backend/InvenTree/plugin/builtin/barcodes/test_inventree_barcode.py index 233d174d64..aab979d2db 100644 --- a/src/backend/InvenTree/plugin/builtin/barcodes/test_inventree_barcode.py +++ b/src/backend/InvenTree/plugin/builtin/barcodes/test_inventree_barcode.py @@ -52,6 +52,21 @@ class TestInvenTreeBarcode(InvenTreeAPITestCase): reverse('api-barcode-scan'), data=data, expected_code=expected_code ) + def generate(self, model: str, pk: int, expected_code: int): + """Generate a barcode for a given model instance.""" + return self.post( + reverse('api-barcode-generate'), + data={'model': model, 'pk': pk}, + expected_code=expected_code, + ) + + def set_plugin_setting(self, key: str, value: str): + """Set the internal barcode format for the plugin.""" + from plugin import registry + + plugin = registry.get_plugin('inventreebarcode') + plugin.set_setting(key, value) + def test_unassign_errors(self): """Test various error conditions for the barcode unassign endpoint.""" # Fail without any fields provided @@ -248,8 +263,8 @@ class TestInvenTreeBarcode(InvenTreeAPITestCase): self.assertIn('success', response.data) self.assertEqual(response.data['stockitem']['pk'], 1) - def test_scan_inventree(self): - """Test scanning of first-party barcodes.""" + def test_scan_inventree_json(self): + """Test scanning of first-party json barcodes.""" # Scan a StockItem object (which does not exist) response = self.scan({'barcode': '{"stockitem": 5}'}, expected_code=400) @@ -290,3 +305,73 @@ class TestInvenTreeBarcode(InvenTreeAPITestCase): self.assertIn('success', response.data) self.assertIn('barcode_data', response.data) self.assertIn('barcode_hash', response.data) + + def test_scan_inventree_short(self): + """Test scanning of first-party short barcodes.""" + # Scan a StockItem object (which does not exist) + response = self.scan({'barcode': 'INV-SI5'}, expected_code=400) + + self.assertIn('No match found for barcode data', str(response.data)) + + # Scan a StockItem object (which does exist) + response = self.scan({'barcode': 'INV-SI1'}, expected_code=200) + + self.assertIn('success', response.data) + self.assertIn('stockitem', response.data) + self.assertEqual(response.data['stockitem']['pk'], 1) + + # Scan a StockLocation object + response = self.scan({'barcode': 'INV-SL5'}, expected_code=200) + + self.assertIn('success', response.data) + self.assertEqual(response.data['stocklocation']['pk'], 5) + self.assertEqual( + response.data['stocklocation']['api_url'], '/api/stock/location/5/' + ) + if settings.ENABLE_CLASSIC_FRONTEND: + self.assertEqual( + response.data['stocklocation']['web_url'], '/stock/location/5/' + ) + self.assertEqual(response.data['plugin'], 'InvenTreeBarcode') + + # Scan a Part object + response = self.scan({'barcode': 'INV-PA5'}, expected_code=200) + + self.assertEqual(response.data['part']['pk'], 5) + + # Scan a SupplierPart instance with custom prefix + for prefix in ['TEST', '']: + self.set_plugin_setting('SHORT_BARCODE_PREFIX', prefix) + response = self.scan({'barcode': f'{prefix}SP1'}, expected_code=200) + self.assertEqual(response.data['supplierpart']['pk'], 1) + self.assertEqual(response.data['plugin'], 'InvenTreeBarcode') + self.assertIn('success', response.data) + self.assertIn('barcode_data', response.data) + self.assertIn('barcode_hash', response.data) + + self.set_plugin_setting('SHORT_BARCODE_PREFIX', 'INV-') + + def test_generation_inventree_json(self): + """Test JSON barcode generation.""" + item = stock.models.StockLocation.objects.get(pk=5) + data = self.generate('stocklocation', item.pk, expected_code=200).data + self.assertEqual(data['barcode'], '{"stocklocation": 5}') + + def test_generation_inventree_short(self): + """Test short barcode generation.""" + self.set_plugin_setting('INTERNAL_BARCODE_FORMAT', 'short') + + item = stock.models.StockLocation.objects.get(pk=5) + + # test with default prefix + data = self.generate('stocklocation', item.pk, expected_code=200).data + self.assertEqual(data['barcode'], 'INV-SL5') + + # test generation with custom prefix + for prefix in ['TEST', '']: + self.set_plugin_setting('SHORT_BARCODE_PREFIX', prefix) + data = self.generate('stocklocation', item.pk, expected_code=200).data + self.assertEqual(data['barcode'], f'{prefix}SL5') + + self.set_plugin_setting('SHORT_BARCODE_PREFIX', 'INV-') + self.set_plugin_setting('INTERNAL_BARCODE_FORMAT', 'json') diff --git a/src/backend/InvenTree/report/templatetags/barcode.py b/src/backend/InvenTree/report/templatetags/barcode.py index 8e9f2af993..85aeed953f 100644 --- a/src/backend/InvenTree/report/templatetags/barcode.py +++ b/src/backend/InvenTree/report/templatetags/barcode.py @@ -3,12 +3,20 @@ from django import template import barcode as python_barcode -import qrcode as python_qrcode +import qrcode.constants as ECL +from qrcode.main import QRCode import report.helpers register = template.Library() +QR_ECL_LEVEL_MAP = { + 'L': ECL.ERROR_CORRECT_L, + 'M': ECL.ERROR_CORRECT_M, + 'Q': ECL.ERROR_CORRECT_Q, + 'H': ECL.ERROR_CORRECT_H, +} + def image_data(img, fmt='PNG'): """Convert an image into HTML renderable data. @@ -22,36 +30,44 @@ def image_data(img, fmt='PNG'): def qrcode(data, **kwargs): """Return a byte-encoded QR code image. - kwargs: - fill_color: Fill color (default = black) - back_color: Background color (default = white) - version: Default = 1 - box_size: Default = 20 - border: Default = 1 + Arguments: + data: Data to encode + + Keyword Arguments: + version: QR code version, (None to auto detect) (default = None) + error_correction: Error correction level (L: 7%, M: 15%, Q: 25%, H: 30%) (default = 'M') + box_size: pixel dimensions for one black square pixel in the QR code (default = 20) + border: count white QR square pixels around the qr code, needed as padding (default = 1) + optimize: data will be split into multiple chunks of at least this length using different modes (text, alphanumeric, binary) to optimize the QR code size. Set to `0` to disable. (default = 1) + format: Image format (default = 'PNG') + fill_color: Fill color (default = "black") + back_color: Background color (default = "white") Returns: base64 encoded image data """ - # Construct "default" values - params = {'box_size': 20, 'border': 1, 'version': 1} - + # Extract other arguments from kwargs fill_color = kwargs.pop('fill_color', 'black') back_color = kwargs.pop('back_color', 'white') + image_format = kwargs.pop('format', 'PNG') + optimize = kwargs.pop('optimize', 1) - img_format = kwargs.pop('format', 'PNG') - - params.update(**kwargs) - - qr = python_qrcode.QRCode(**params) - - qr.add_data(data, optimize=20) - qr.make(fit=True) + # Construct QR code object + qr = QRCode(**{ + 'box_size': 20, + 'border': 1, + 'version': None, + **kwargs, + 'error_correction': QR_ECL_LEVEL_MAP[kwargs.get('error_correction', 'M')], + }) + qr.add_data(data, optimize=optimize) + qr.make(fit=False) # if version is None, it will automatically use fit=True qri = qr.make_image(fill_color=fill_color, back_color=back_color) # Render to byte-encoded image - return image_data(qri, fmt=img_format) + return image_data(qri, fmt=image_format) @register.simple_tag() diff --git a/src/backend/InvenTree/stock/models.py b/src/backend/InvenTree/stock/models.py index 3a65f2c2f6..e0fa3d3c6f 100644 --- a/src/backend/InvenTree/stock/models.py +++ b/src/backend/InvenTree/stock/models.py @@ -142,11 +142,16 @@ class StockLocation( """Return API url.""" return reverse('api-location-list') + @classmethod + def barcode_model_type_code(cls): + """Return the associated barcode model type code for this model.""" + return 'SL' + def report_context(self): """Return report context data for this StockLocation.""" return { 'location': self, - 'qr_data': self.format_barcode(brief=True), + 'qr_data': self.barcode, 'parent': self.parent, 'stock_location': self, 'stock_items': self.get_stock_items(), @@ -367,6 +372,11 @@ class StockItem( """Custom API instance filters.""" return {'parent': {'exclude_tree': self.pk}} + @classmethod + def barcode_model_type_code(cls): + """Return the associated barcode model type code for this model.""" + return 'SI' + def get_test_keys(self, include_installed=True): """Construct a flattened list of test 'keys' for this StockItem.""" keys = [] @@ -397,7 +407,7 @@ class StockItem( 'item': self, 'name': self.part.full_name, 'part': self.part, - 'qr_data': self.format_barcode(brief=True), + 'qr_data': self.barcode, 'qr_url': self.get_absolute_url(), 'parameters': self.part.parameters_map(), 'quantity': InvenTree.helpers.normalize(self.quantity), diff --git a/src/backend/InvenTree/stock/templates/stock/item_base.html b/src/backend/InvenTree/stock/templates/stock/item_base.html index 05107cac28..1a7f0fe5cf 100644 --- a/src/backend/InvenTree/stock/templates/stock/item_base.html +++ b/src/backend/InvenTree/stock/templates/stock/item_base.html @@ -534,7 +534,7 @@ $('#stock-edit-status').click(function () { $("#show-qr-code").click(function() { showQRDialog( '{% trans "Stock Item QR Code" escape %}', - '{"stockitem": {{ item.pk }} }', + '{{ item.barcode }}', ); }); diff --git a/src/backend/InvenTree/stock/templates/stock/location.html b/src/backend/InvenTree/stock/templates/stock/location.html index 6c052e517b..37039dd017 100644 --- a/src/backend/InvenTree/stock/templates/stock/location.html +++ b/src/backend/InvenTree/stock/templates/stock/location.html @@ -392,7 +392,7 @@ $('#show-qr-code').click(function() { showQRDialog( '{% trans "Stock Location QR Code" escape %}', - '{"stocklocation": {{ location.pk }} }' + '{{ location.barcode }}' ); }); diff --git a/src/backend/InvenTree/stock/tests.py b/src/backend/InvenTree/stock/tests.py index 99f6f51a48..7eb0302876 100644 --- a/src/backend/InvenTree/stock/tests.py +++ b/src/backend/InvenTree/stock/tests.py @@ -952,12 +952,6 @@ class StockBarcodeTest(StockTestBase): self.assertEqual(StockItem.barcode_model_type(), 'stockitem') - # Call format_barcode method - barcode = item.format_barcode(brief=False) - - for key in ['tool', 'version', 'instance', 'stockitem']: - self.assertIn(key, barcode) - # Render simple barcode data for the StockItem barcode = item.barcode self.assertEqual(barcode, '{"stockitem": 1}') @@ -968,7 +962,7 @@ class StockBarcodeTest(StockTestBase): loc = StockLocation.objects.get(pk=1) - barcode = loc.format_barcode(brief=True) + barcode = loc.format_barcode() self.assertEqual('{"stocklocation": 1}', barcode) diff --git a/src/backend/InvenTree/templates/InvenTree/settings/barcode.html b/src/backend/InvenTree/templates/InvenTree/settings/barcode.html index 982548a58c..8da99c0a9a 100644 --- a/src/backend/InvenTree/templates/InvenTree/settings/barcode.html +++ b/src/backend/InvenTree/templates/InvenTree/settings/barcode.html @@ -16,6 +16,7 @@ {% include "InvenTree/settings/setting.html" with key="BARCODE_INPUT_DELAY" icon="fa-hourglass-half" %} {% include "InvenTree/settings/setting.html" with key="BARCODE_WEBCAM_SUPPORT" icon="fa-video" %} {% include "InvenTree/settings/setting.html" with key="BARCODE_SHOW_TEXT" icon="fa-closed-captioning" %} + {% include "InvenTree/settings/setting.html" with key="BARCODE_GENERATION_PLUGIN" icon="fa-qrcode" %} diff --git a/src/frontend/package.json b/src/frontend/package.json index 54dad53cc2..48592cc1fe 100644 --- a/src/frontend/package.json +++ b/src/frontend/package.json @@ -38,7 +38,6 @@ "@mantine/spotlight": "^7.11.0", "@mantine/vanilla-extract": "^7.11.0", "@mdxeditor/editor": "^3.6.1", - "@naisutech/react-tree": "^3.1.0", "@sentry/react": "^8.13.0", "@tabler/icons-react": "^3.7.0", "@tanstack/react-query": "^5.49.2", @@ -52,6 +51,7 @@ "dayjs": "^1.11.10", "embla-carousel-react": "^8.1.6", "html5-qrcode": "^2.3.8", + "qrcode": "^1.5.3", "mantine-datatable": "^7.11.2", "react": "^18.3.1", "react-dom": "^18.3.1", @@ -72,6 +72,7 @@ "@lingui/macro": "^4.11.1", "@playwright/test": "^1.45.0", "@types/node": "^20.14.9", + "@types/qrcode": "^1.5.5", "@types/react": "^18.3.3", "@types/react-dom": "^18.3.0", "@types/react-grid-layout": "^1.3.5", diff --git a/src/frontend/src/components/buttons/ButtonMenu.tsx b/src/frontend/src/components/buttons/ButtonMenu.tsx index 42feeb0718..0bf2a36df7 100644 --- a/src/frontend/src/components/buttons/ButtonMenu.tsx +++ b/src/frontend/src/components/buttons/ButtonMenu.tsx @@ -11,7 +11,7 @@ export function ButtonMenu({ label = '' }: { icon: any; - actions: any[]; + actions: React.ReactNode[]; label?: string; tooltip?: string; }) { diff --git a/src/frontend/src/components/buttons/CopyButton.tsx b/src/frontend/src/components/buttons/CopyButton.tsx index 3ac02e5da2..c63ec29630 100644 --- a/src/frontend/src/components/buttons/CopyButton.tsx +++ b/src/frontend/src/components/buttons/CopyButton.tsx @@ -1,6 +1,13 @@ import { t } from '@lingui/macro'; -import { Button, CopyButton as MantineCopyButton } from '@mantine/core'; -import { IconCopy } from '@tabler/icons-react'; +import { + ActionIcon, + Button, + CopyButton as MantineCopyButton, + Text, + Tooltip +} from '@mantine/core'; + +import { InvenTreeIcon } from '../../functions/icons'; export function CopyButton({ value, @@ -9,24 +16,27 @@ export function CopyButton({ value: any; label?: JSX.Element; }) { + const ButtonComponent = label ? Button : ActionIcon; + return ( {({ copied, copy }) => ( - + + + {copied ? ( + + ) : ( + + )} + + {label && {label}} + + )} ); diff --git a/src/frontend/src/components/details/Details.tsx b/src/frontend/src/components/details/Details.tsx index d3675bceaa..7c2346eb38 100644 --- a/src/frontend/src/components/details/Details.tsx +++ b/src/frontend/src/components/details/Details.tsx @@ -1,15 +1,12 @@ import { t } from '@lingui/macro'; import { - ActionIcon, Anchor, Badge, - CopyButton, Paper, Skeleton, Stack, Table, - Text, - Tooltip + Text } from '@mantine/core'; import { useSuspenseQuery } from '@tanstack/react-query'; import { getValueAtPath } from 'mantine-datatable'; @@ -24,6 +21,7 @@ import { navigateToLink } from '../../functions/navigation'; import { getDetailUrl } from '../../functions/urls'; import { apiUrl } from '../../states/ApiState'; import { useGlobalSettingsState } from '../../states/SettingsState'; +import { CopyButton } from '../buttons/CopyButton'; import { YesNoButton } from '../buttons/YesNoButton'; import { ProgressBar } from '../items/ProgressBar'; import { StylishText } from '../items/StylishText'; @@ -325,26 +323,7 @@ function StatusValue(props: Readonly) { } function CopyField({ value }: { value: string }) { - return ( - - {({ copied, copy }) => ( - - - {copied ? ( - - ) : ( - - )} - - - )} - - ); + return ; } export function DetailsTableField({ diff --git a/src/frontend/src/components/editors/TemplateEditor/CodeEditor/CodeEditor.tsx b/src/frontend/src/components/editors/TemplateEditor/CodeEditor/CodeEditor.tsx index e69d8136e7..789fbad4c1 100644 --- a/src/frontend/src/components/editors/TemplateEditor/CodeEditor/CodeEditor.tsx +++ b/src/frontend/src/components/editors/TemplateEditor/CodeEditor/CodeEditor.tsx @@ -25,12 +25,18 @@ const tags: Tag[] = [ description: 'Generate a QR code image', args: ['data'], kwargs: { - fill_color: 'Fill color (default = black)', - back_color: 'Background color (default = white)', - version: 'Version (default = 1)', - box_size: 'Box size (default = 20)', - border: 'Border width (default = 1)', - format: 'Format (default = PNG)' + version: 'QR code version, (None to auto detect) (default = None)', + error_correction: + "Error correction level (L: 7%, M: 15%, Q: 25%, H: 30%) (default = 'Q')", + box_size: + 'pixel dimensions for one black square pixel in the QR code (default = 20)', + border: + 'count white QR square pixels around the qr code, needed as padding (default = 1)', + optimize: + 'data will be split into multiple chunks of at least this length using different modes (text, alphanumeric, binary) to optimize the QR code size. Set to `0` to disable. (default = 1)', + format: "Image format (default = 'PNG')", + fill_color: 'Fill color (default = "black")', + back_color: 'Background color (default = "white")' }, returns: 'base64 encoded qr code image data' }, diff --git a/src/frontend/src/components/items/ActionDropdown.tsx b/src/frontend/src/components/items/ActionDropdown.tsx index 7d04fb0d56..d29c21c93f 100644 --- a/src/frontend/src/components/items/ActionDropdown.tsx +++ b/src/frontend/src/components/items/ActionDropdown.tsx @@ -6,6 +6,7 @@ import { Menu, Tooltip } from '@mantine/core'; +import { modals } from '@mantine/modals'; import { IconCopy, IconEdit, @@ -16,9 +17,11 @@ import { } from '@tabler/icons-react'; import { ReactNode, useMemo } from 'react'; +import { ModelType } from '../../enums/ModelType'; import { identifierString } from '../../functions/conversion'; import { InvenTreeIcon } from '../../functions/icons'; import { notYetImplemented } from '../../functions/notifications'; +import { InvenTreeQRCode } from './QRCode'; export type ActionDropdownItem = { icon: ReactNode; @@ -128,11 +131,20 @@ export function BarcodeActionDropdown({ // Common action button for viewing a barcode export function ViewBarcodeAction({ hidden = false, - onClick + model, + pk }: { hidden?: boolean; - onClick?: () => void; + model: ModelType; + pk: number; }): ActionDropdownItem { + const onClick = () => { + modals.open({ + title: t`View Barcode`, + children: + }); + }; + return { icon: , name: t`View`, diff --git a/src/frontend/src/components/items/QRCode.tsx b/src/frontend/src/components/items/QRCode.tsx new file mode 100644 index 0000000000..1077692314 --- /dev/null +++ b/src/frontend/src/components/items/QRCode.tsx @@ -0,0 +1,130 @@ +import { Trans, t } from '@lingui/macro'; +import { + Box, + Code, + Group, + Image, + Select, + Skeleton, + Stack, + Text +} from '@mantine/core'; +import { useQuery } from '@tanstack/react-query'; +import QR from 'qrcode'; +import { useEffect, useMemo, useState } from 'react'; + +import { api } from '../../App'; +import { ApiEndpoints } from '../../enums/ApiEndpoints'; +import { ModelType } from '../../enums/ModelType'; +import { apiUrl } from '../../states/ApiState'; +import { useGlobalSettingsState } from '../../states/SettingsState'; +import { CopyButton } from '../buttons/CopyButton'; + +type QRCodeProps = { + ecl?: 'L' | 'M' | 'Q' | 'H'; + margin?: number; + data?: string; +}; + +export const QRCode = ({ data, ecl = 'Q', margin = 1 }: QRCodeProps) => { + const [qrCode, setQRCode] = useState(); + + useEffect(() => { + if (!data) return setQRCode(undefined); + + QR.toString(data, { errorCorrectionLevel: ecl, type: 'svg', margin }).then( + (svg) => { + setQRCode(`data:image/svg+xml;utf8,${encodeURIComponent(svg)}`); + } + ); + }, [data, ecl]); + + return ( + + {qrCode ? ( + QR Code + ) : ( + + )} + + ); +}; + +type InvenTreeQRCodeProps = { + model: ModelType; + pk: number; + showEclSelector?: boolean; +} & Omit; + +export const InvenTreeQRCode = ({ + showEclSelector = true, + model, + pk, + ecl: eclProp = 'Q', + ...props +}: InvenTreeQRCodeProps) => { + const settings = useGlobalSettingsState(); + const [ecl, setEcl] = useState(eclProp); + + useEffect(() => { + if (eclProp) setEcl(eclProp); + }, [eclProp]); + + const { data } = useQuery({ + queryKey: ['qr-code', model, pk], + queryFn: async () => { + const res = await api.post(apiUrl(ApiEndpoints.generate_barcode), { + model, + pk + }); + + return res.data?.barcode as string; + } + }); + + const eclOptions = useMemo( + () => [ + { value: 'L', label: t`Low (7%)` }, + { value: 'M', label: t`Medium (15%)` }, + { value: 'Q', label: t`Quartile (25%)` }, + { value: 'H', label: t`High (30%)` } + ], + [] + ); + + return ( + + + + {data && settings.getSetting('BARCODE_SHOW_TEXT', 'false') && ( + + + + Barcode Data: + + + {data} + + + + + {showEclSelector && ( +