2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-16 03:55:41 +00:00

added suggestions from code review

This commit is contained in:
wolflu05
2024-07-19 22:45:11 +02:00
parent a6b3f4aa5c
commit 1e9ac3c8c9
4 changed files with 43 additions and 26 deletions

View File

@ -1,7 +1,8 @@
"""Helper functions for barcode generation.""" """Helper functions for barcode generation."""
import logging import logging
from typing import Type from functools import lru_cache
from typing import Type, cast
import InvenTree.helpers_model import InvenTree.helpers_model
from InvenTree.models import InvenTreeBarcodeMixin from InvenTree.models import InvenTreeBarcodeMixin
@ -27,20 +28,23 @@ def generate_barcode(model_instance: InvenTreeBarcodeMixin):
"""Generate a barcode for a given model instance.""" """Generate a barcode for a given model instance."""
from common.settings import get_global_setting from common.settings import get_global_setting
from plugin import registry from plugin import registry
from plugin.mixins import BarcodeMixin
# Find the selected barcode generation plugin # Find the selected barcode generation plugin
slug = get_global_setting('BARCODE_GENERATION_PLUGIN', create=False) 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) return plugin.generate(model_instance)
@lru_cache(maxsize=1)
def get_supported_barcode_models() -> list[Type[InvenTreeBarcodeMixin]]: def get_supported_barcode_models() -> list[Type[InvenTreeBarcodeMixin]]:
"""Returns a list of database models which support barcode functionality.""" """Returns a list of database models which support barcode functionality."""
return InvenTree.helpers_model.getModelsWithMixin(InvenTreeBarcodeMixin) return InvenTree.helpers_model.getModelsWithMixin(InvenTreeBarcodeMixin)
@lru_cache(maxsize=1)
def get_supported_barcode_models_map(): def get_supported_barcode_models_map():
"""Return a mapping of barcode model types to the model class.""" """Return a mapping of barcode model types to the model class."""
return { return {
@ -48,6 +52,7 @@ def get_supported_barcode_models_map():
} }
@lru_cache(maxsize=1)
def get_supported_barcode_model_codes_map(): def get_supported_barcode_model_codes_map():
"""Return a mapping of barcode model type codes to the model class.""" """Return a mapping of barcode model type codes to the model class."""
return { return {

View File

@ -34,8 +34,8 @@ class InvenTreeInternalBarcodePlugin(SettingsMixin, BarcodeMixin, InvenTreePlugi
'name': _('Internal Barcode Format'), 'name': _('Internal Barcode Format'),
'description': _('Select an internal barcode format'), 'description': _('Select an internal barcode format'),
'choices': [ 'choices': [
('json', _('JSON barcodes (require more space)')), ('json', _('JSON barcodes (human readable)')),
('short', _('Short barcodes (made for optimized space)')), ('short', _('Short barcodes (space optimized)')),
], ],
'default': 'json', 'default': 'json',
}, },

View File

@ -340,17 +340,17 @@ class TestInvenTreeBarcode(InvenTreeAPITestCase):
self.assertEqual(response.data['part']['pk'], 5) self.assertEqual(response.data['part']['pk'], 5)
# Scan a SupplierPart instance with custom prefix # Scan a SupplierPart instance with custom prefix
self.set_plugin_setting('SHORT_BARCODE_PREFIX', 'TEST') for prefix in ['TEST', '']:
response = self.scan({'barcode': 'TESTSP1'}, expected_code=200) 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.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): def test_generation_inventree_json(self):
"""Test JSON barcode generation.""" """Test JSON barcode generation."""
item = stock.models.StockLocation.objects.get(pk=5) 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 data = self.generate('stocklocation', item.pk, expected_code=200).data
self.assertEqual(data['barcode'], 'INV-SL5') self.assertEqual(data['barcode'], 'INV-SL5')
# test with custom prefix # test generation with custom prefix
self.set_plugin_setting('SHORT_BARCODE_PREFIX', 'TEST') for prefix in ['TEST', '']:
data = self.generate('stocklocation', item.pk, expected_code=200).data self.set_plugin_setting('SHORT_BARCODE_PREFIX', prefix)
self.assertEqual(data['barcode'], 'TESTSL5') data = self.generate('stocklocation', item.pk, expected_code=200).data
self.set_plugin_setting('SHORT_BARCODE_PREFIX', 'INV-') self.assertEqual(data['barcode'], f'{prefix}SL5')
self.set_plugin_setting('SHORT_BARCODE_PREFIX', 'INV-')
self.set_plugin_setting('INTERNAL_BARCODE_FORMAT', 'json') self.set_plugin_setting('INTERNAL_BARCODE_FORMAT', 'json')

View File

@ -1,4 +1,4 @@
import { t } from '@lingui/macro'; import { Trans, t } from '@lingui/macro';
import { import {
Box, Box,
Code, Code,
@ -6,7 +6,8 @@ import {
Image, Image,
Select, Select,
Skeleton, Skeleton,
Stack Stack,
Text
} from '@mantine/core'; } from '@mantine/core';
import { useQuery } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query';
import QR from 'qrcode'; import QR from 'qrcode';
@ -94,8 +95,23 @@ export const InvenTreeQRCode = ({
return ( return (
<Stack> <Stack>
<QRCode data={data} ecl={ecl} {...props} /> <QRCode data={data} ecl={ecl} {...props} />
{data && settings.getSetting('BARCODE_SHOW_TEXT', 'false') && ( {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 && ( {showEclSelector && (
<Select <Select
allowDeselect={false} allowDeselect={false}
@ -107,11 +123,6 @@ export const InvenTreeQRCode = ({
data={eclOptions} data={eclOptions}
/> />
)} )}
<Group>
<Code>{data}</Code>
<CopyButton value={data} />
</Group>
</Group> </Group>
)} )}
</Stack> </Stack>