Update MPN barcode scanner (#13319)

This commit is contained in:
Oliver authored and GitHub committed 2026-11-18 11:51:23 +11:00
1 parent c79acc32d9
commit d0cc310431
2 files changed
+93 -12

No files matched your search

@@ -7,8 +7,11 @@ a manufacturer part number (MPN).
The following barcode formats are supported:
- ISO/IEC 15434 / ECIA 2D barcodes (e.g. DataMatrix) with GS / RS field separators,
containing a '1P' (manufacturer part number) or 'P' data identifier field.
- A single field with a '1P' or 'P' prefix, followed by the MPN (e.g. '1PLM358' or 'PLM358').
containing a '1P' (manufacturer part number), '50P' or 'P' data identifier field.
- A single field with a '1P', '50P' or 'P' prefix, followed by the MPN (e.g. '1PLM358' or 'PLM358').
Support for the '50P' data identifier is controlled by the ENABLE_50P plugin setting,
and is treated identically to a '1P' field.
- The raw MPN string (e.g. 'LM358').
A match is only returned if exactly one ManufacturerPart matches the MPN exactly.
@@ -25,7 +28,7 @@ import structlog
from company.models import ManufacturerPart
from plugin import InvenTreePlugin
from plugin.mixins import BarcodeMixin
from plugin.mixins import BarcodeMixin, SettingsMixin
from users.permissions import check_user_permission
logger = structlog.get_logger('inventree')
@@ -43,10 +46,14 @@ DATA_IDENTIFIER_REGEX = re.compile(r'^(\d{0,3}[A-Z])')
CONTROL_CHAR_REGEX = re.compile(r'[\x00-\x1f\x7f]')
# Data identifiers which may contain a manufacturer part number, in order of preference
MPN_IDENTIFIERS = ['1P', 'P']
MPN_IDENTIFIERS = ['1P', '50P', 'P']
# Optional data identifiers, which can be disabled via plugin settings
# Maps identifier -> setting key
OPTIONAL_MPN_IDENTIFIERS = {'50P': 'ENABLE_50P'}
class ManufacturerBarcodePlugin(BarcodeMixin, InvenTreePlugin):
class ManufacturerBarcodePlugin(BarcodeMixin, SettingsMixin, InvenTreePlugin):
"""Fallback barcode plugin for matching manufacturer part numbers."""
NAME = 'ManufacturerBarcodePlugin'
@@ -55,12 +62,32 @@ class ManufacturerBarcodePlugin(BarcodeMixin, InvenTreePlugin):
DESCRIPTION = _(
'Fallback barcode support for matching manufacturer part numbers (MPN)'
)
VERSION = '1.0.0'
VERSION = '1.1.0'
AUTHOR = _('InvenTree contributors')
# Fallback plugin - query after all other barcode plugins
BARCODE_PRIORITY = -100
SETTINGS = {
'ENABLE_50P': {
'name': _('Enable 50P Codes'),
'description': _(
"Treat '50P' data identifier fields as manufacturer part numbers (same as '1P')"
),
'default': True,
'validator': bool,
}
}
def mpn_identifiers(self) -> list[str]:
"""Return the list of enabled MPN data identifiers, in order of preference."""
return [
identifier
for identifier in MPN_IDENTIFIERS
if identifier not in OPTIONAL_MPN_IDENTIFIERS
or self.get_setting(OPTIONAL_MPN_IDENTIFIERS[identifier], backup_value=True)
]
@staticmethod
def max_mpn_length() -> int:
"""Return the maximum length of a ManufacturerPart MPN."""
@@ -84,11 +111,14 @@ class ManufacturerBarcodePlugin(BarcodeMixin, InvenTreePlugin):
return fields
@classmethod
def extract_mpn_candidates(cls, barcode_data: str) -> list[str]:
def extract_mpn_candidates(
cls, barcode_data: str, identifiers: list[str] | None = None
) -> list[str]:
"""Extract a list of candidate MPN values from the provided barcode data.
Arguments:
barcode_data: The raw barcode data
identifiers: Data identifiers to match, in order of preference (default = all supported)
Returns:
A list of candidate MPN strings (may be empty)
@@ -96,6 +126,9 @@ class ManufacturerBarcodePlugin(BarcodeMixin, InvenTreePlugin):
if not isinstance(barcode_data, str) or not barcode_data:
return []
if identifiers is None:
identifiers = MPN_IDENTIFIERS
candidates = []
if GS in barcode_data or RS in barcode_data:
@@ -103,9 +136,9 @@ class ManufacturerBarcodePlugin(BarcodeMixin, InvenTreePlugin):
# Only the MPN fields are considered - never the raw barcode data
fields = cls.parse_data_identifier_fields(barcode_data)
for identifier in MPN_IDENTIFIERS:
for identifier in identifiers:
if value := fields.get(identifier, '').strip():
# Prefer '1P' over 'P' - a 'P' field is typically a
# Prefer '1P' / '50P' over 'P' - a 'P' field is typically a
# customer or supplier part number when '1P' is present
candidates.append(value)
break
@@ -116,8 +149,8 @@ class ManufacturerBarcodePlugin(BarcodeMixin, InvenTreePlugin):
if not data or CONTROL_CHAR_REGEX.search(data):
return []
# Single field with a '1P' or 'P' prefix
for identifier in MPN_IDENTIFIERS:
# Single field with a '1P', '50P' or 'P' prefix
for identifier in identifiers:
if data.startswith(identifier):
candidates.append(data[len(identifier) :].strip())
break
@@ -146,7 +179,9 @@ class ManufacturerBarcodePlugin(BarcodeMixin, InvenTreePlugin):
Returns:
A dict containing the matched ManufacturerPart, or None if there is not exactly one match
"""
candidates = self.extract_mpn_candidates(barcode_data)
candidates = self.extract_mpn_candidates(
barcode_data, identifiers=self.mpn_identifiers()
)
if not candidates:
return None
@@ -49,12 +49,29 @@ class ManufacturerBarcodeParseTests(InvenTreeAPITestCase):
# Empty MPN field
self.assertEqual(extract(ecia('1P', 'Q10')), [])
# 50P field (treated the same as 1P)
self.assertEqual(extract(ecia('K123', '50PLM358', 'Q10')), ['LM358'])
self.assertEqual(extract(ecia('PSKU-123', '50PLM358')), ['LM358'])
# 1P is preferred over 50P
self.assertEqual(extract(ecia('50PXYZ', '1PLM358')), ['LM358'])
# 50P support disabled
self.assertEqual(
extract(ecia('50PLM358', 'PSKU-123'), identifiers=['1P', 'P']), ['SKU-123']
)
self.assertEqual(extract(ecia('50PLM358'), identifiers=['1P', 'P']), [])
def test_simple(self):
"""Test extraction from simple (single field) barcodes."""
extract = ManufacturerBarcodePlugin.extract_mpn_candidates
self.assertEqual(extract('1PLM358'), ['LM358', '1PLM358'])
self.assertEqual(extract('PLM358'), ['LM358', 'PLM358'])
self.assertEqual(extract('50PLM358'), ['LM358', '50PLM358'])
# 50P support disabled - only the raw data is a candidate
self.assertEqual(extract('50PLM358', identifiers=['1P', 'P']), ['50PLM358'])
self.assertEqual(extract(' LM358 \n'), ['LM358'])
self.assertEqual(extract('LM358'), ['LM358'])
@@ -182,6 +199,35 @@ class ManufacturerBarcodeTests(InvenTreeAPITestCase):
# Raw MPN which happens to start with 'P'
self.assertMatch('PIC16F84A', self.pic)
def test_50p(self):
"""Test scanning of '50P' barcodes, which can be disabled via plugin setting."""
plugin = registry.get_plugin(PLUGIN_SLUG)
# Enabled by default
self.assertTrue(plugin.get_setting('ENABLE_50P'))
self.assertMatch(ecia('K123', '50PLM358', 'Q10'), self.lm358)
self.assertMatch('50PLM358', self.lm358)
# 50P takes precedence over P
self.assertMatch(ecia('PPIC16F84A', '50PLM358'), self.lm358)
plugin.set_setting('ENABLE_50P', False)
try:
self.assertEqual(plugin.mpn_identifiers(), ['1P', 'P'])
self.assertNoMatch(ecia('K123', '50PLM358', 'Q10'))
self.assertNoMatch('50PLM358')
# Falls back to the P field
self.assertMatch(ecia('PPIC16F84A', '50PLM358'), self.pic)
# 1P is unaffected
self.assertMatch(ecia('1PLM358'), self.lm358)
finally:
plugin.set_setting('ENABLE_50P', True)
self.assertEqual(plugin.mpn_identifiers(), ['1P', '50P', 'P'])
def test_exact_match(self):
"""Partial matches are not accepted."""
for barcode in ['LM35', 'LM3588', 'XLM358', ecia('1PLM35')]: