Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/backend/InvenTree/InvenTree/validators.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Custom field validators for InvenTree."""

import tokenize

from django.conf import settings
from django.core import validators
from django.core.exceptions import ValidationError
Expand All @@ -9,6 +11,7 @@
from moneyed import CURRENCIES

import InvenTree.conversion
import InvenTree.exceptions
from common.settings import get_global_setting


Expand All @@ -24,7 +27,17 @@ def validate_physical_units(unit):

try:
ureg(unit)
except (AssertionError, AttributeError, pint.errors.UndefinedUnitError):
except (
AssertionError,
AttributeError,
pint.errors.UndefinedUnitError,
tokenize.TokenError,
):
raise ValidationError(_('Invalid physical unit'))
except Exception:
# Pint parses unit expressions via the python tokenizer, so any
# other unexpected exception type may be raised for malformed input
InvenTree.exceptions.log_error('validate_physical_units', scope='validators')
raise ValidationError(_('Invalid physical unit'))


Expand Down
2 changes: 1 addition & 1 deletion src/backend/InvenTree/part/test_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def test_unit_validation(self):
tmp.full_clean()

# Test that invalid units fail
for unit in ['mmmmm', '-', 'x', int]:
for unit in ['mmmmm', '-', 'x', int, "piao's"]:
tmp = ParameterTemplate(name='test', units=unit)
with self.assertRaises(django_exceptions.ValidationError):
tmp.full_clean()
Expand Down
Loading