mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-15 11:35:41 +00:00
added suggestions from code review
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
"""Helper functions for barcode generation."""
|
||||
|
||||
import logging
|
||||
from typing import Type
|
||||
from functools import lru_cache
|
||||
from typing import Type, cast
|
||||
|
||||
import InvenTree.helpers_model
|
||||
from InvenTree.models import InvenTreeBarcodeMixin
|
||||
@ -27,20 +28,23 @@ 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 = registry.get_plugin(slug)
|
||||
plugin = cast(BarcodeMixin, registry.get_plugin(slug))
|
||||
|
||||
return plugin.generate(model_instance)
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_supported_barcode_models() -> list[Type[InvenTreeBarcodeMixin]]:
|
||||
"""Returns a list of database models which support barcode functionality."""
|
||||
return InvenTree.helpers_model.getModelsWithMixin(InvenTreeBarcodeMixin)
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_supported_barcode_models_map():
|
||||
"""Return a mapping of barcode model types to the model class."""
|
||||
return {
|
||||
@ -48,6 +52,7 @@ def get_supported_barcode_models_map():
|
||||
}
|
||||
|
||||
|
||||
@lru_cache(maxsize=1)
|
||||
def get_supported_barcode_model_codes_map():
|
||||
"""Return a mapping of barcode model type codes to the model class."""
|
||||
return {
|
||||
|
@ -34,8 +34,8 @@ class InvenTreeInternalBarcodePlugin(SettingsMixin, BarcodeMixin, InvenTreePlugi
|
||||
'name': _('Internal Barcode Format'),
|
||||
'description': _('Select an internal barcode format'),
|
||||
'choices': [
|
||||
('json', _('JSON barcodes (require more space)')),
|
||||
('short', _('Short barcodes (made for optimized space)')),
|
||||
('json', _('JSON barcodes (human readable)')),
|
||||
('short', _('Short barcodes (space optimized)')),
|
||||
],
|
||||
'default': 'json',
|
||||
},
|
||||
|
@ -340,17 +340,17 @@ class TestInvenTreeBarcode(InvenTreeAPITestCase):
|
||||
self.assertEqual(response.data['part']['pk'], 5)
|
||||
|
||||
# Scan a SupplierPart instance with custom prefix
|
||||
self.set_plugin_setting('SHORT_BARCODE_PREFIX', 'TEST')
|
||||
response = self.scan({'barcode': 'TESTSP1'}, expected_code=200)
|
||||
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-')
|
||||
|
||||
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)
|
||||
|
||||
def test_generation_inventree_json(self):
|
||||
"""Test JSON barcode generation."""
|
||||
item = stock.models.StockLocation.objects.get(pk=5)
|
||||
@ -367,10 +367,11 @@ class TestInvenTreeBarcode(InvenTreeAPITestCase):
|
||||
data = self.generate('stocklocation', item.pk, expected_code=200).data
|
||||
self.assertEqual(data['barcode'], 'INV-SL5')
|
||||
|
||||
# test with custom prefix
|
||||
self.set_plugin_setting('SHORT_BARCODE_PREFIX', 'TEST')
|
||||
data = self.generate('stocklocation', item.pk, expected_code=200).data
|
||||
self.assertEqual(data['barcode'], 'TESTSL5')
|
||||
self.set_plugin_setting('SHORT_BARCODE_PREFIX', 'INV-')
|
||||
# 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')
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { t } from '@lingui/macro';
|
||||
import { Trans, t } from '@lingui/macro';
|
||||
import {
|
||||
Box,
|
||||
Code,
|
||||
@ -6,7 +6,8 @@ import {
|
||||
Image,
|
||||
Select,
|
||||
Skeleton,
|
||||
Stack
|
||||
Stack,
|
||||
Text
|
||||
} from '@mantine/core';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import QR from 'qrcode';
|
||||
@ -94,8 +95,23 @@ export const InvenTreeQRCode = ({
|
||||
return (
|
||||
<Stack>
|
||||
<QRCode data={data} ecl={ecl} {...props} />
|
||||
|
||||
{data && settings.getSetting('BARCODE_SHOW_TEXT', 'false') && (
|
||||
<Group justify={showEclSelector ? 'space-between' : 'center'}>
|
||||
<Group
|
||||
justify={showEclSelector ? 'space-between' : 'center'}
|
||||
align="flex-start"
|
||||
px={16}
|
||||
>
|
||||
<Stack gap={4} pt={2}>
|
||||
<Text size="sm" fw={500}>
|
||||
<Trans>Barcode Data:</Trans>
|
||||
</Text>
|
||||
<Group>
|
||||
<Code>{data}</Code>
|
||||
<CopyButton value={data} />
|
||||
</Group>
|
||||
</Stack>
|
||||
|
||||
{showEclSelector && (
|
||||
<Select
|
||||
allowDeselect={false}
|
||||
@ -107,11 +123,6 @@ export const InvenTreeQRCode = ({
|
||||
data={eclOptions}
|
||||
/>
|
||||
)}
|
||||
|
||||
<Group>
|
||||
<Code>{data}</Code>
|
||||
<CopyButton value={data} />
|
||||
</Group>
|
||||
</Group>
|
||||
)}
|
||||
</Stack>
|
||||
|
Reference in New Issue
Block a user