mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-12 18:15:40 +00:00
Wrap custom filter validation in try/except blocks
This commit is contained in:
@ -5,6 +5,7 @@ import sys
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.conf.urls import url, include
|
||||
from django.core.exceptions import ValidationError, FieldError
|
||||
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
|
||||
@ -119,13 +120,20 @@ class StockItemLabelList(LabelListView, StockItemLabelMixin):
|
||||
matches = True
|
||||
|
||||
# Filter string defined for the StockItemLabel object
|
||||
filters = InvenTree.helpers.validateFilterString(label.filters)
|
||||
try:
|
||||
filters = InvenTree.helpers.validateFilterString(label.filters)
|
||||
except ValidationError:
|
||||
continue
|
||||
|
||||
for item in items:
|
||||
|
||||
item_query = StockItem.objects.filter(pk=item.pk)
|
||||
|
||||
if not item_query.filter(**filters).exists():
|
||||
try:
|
||||
if not item_query.filter(**filters).exists():
|
||||
matches = False
|
||||
break
|
||||
except FieldError:
|
||||
matches = False
|
||||
break
|
||||
|
||||
@ -273,13 +281,21 @@ class StockLocationLabelList(LabelListView, StockLocationLabelMixin):
|
||||
matches = True
|
||||
|
||||
# Filter string defined for the StockLocationLabel object
|
||||
filters = InvenTree.helpers.validateFilterString(label.filters)
|
||||
try:
|
||||
filters = InvenTree.helpers.validateFilterString(label.filters)
|
||||
except:
|
||||
# Skip if there was an error validating the filters...
|
||||
continue
|
||||
|
||||
for loc in locations:
|
||||
|
||||
loc_query = StockLocation.objects.filter(pk=loc.pk)
|
||||
|
||||
if not loc_query.filter(**filters).exists():
|
||||
try:
|
||||
if not loc_query.filter(**filters).exists():
|
||||
matches = False
|
||||
break
|
||||
except FieldError:
|
||||
matches = False
|
||||
break
|
||||
|
||||
|
@ -12,6 +12,7 @@ from blabel import LabelWriter
|
||||
|
||||
from django.db import models
|
||||
from django.core.validators import FileExtensionValidator
|
||||
from django.core.exceptions import ValidationError, FieldError
|
||||
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
@ -145,9 +146,12 @@ class StockItemLabel(LabelTemplate):
|
||||
Test if this label template matches a given StockItem object
|
||||
"""
|
||||
|
||||
filters = validateFilterString(self.filters)
|
||||
|
||||
items = stock.models.StockItem.objects.filter(**filters)
|
||||
try:
|
||||
filters = validateFilterString(self.filters)
|
||||
items = stock.models.StockItem.objects.filter(**filters)
|
||||
except (ValidationError, FieldError):
|
||||
# If an error exists with the "filters" field, return False
|
||||
return False
|
||||
|
||||
items = items.filter(pk=item.pk)
|
||||
|
||||
@ -198,9 +202,11 @@ class StockLocationLabel(LabelTemplate):
|
||||
Test if this label template matches a given StockLocation object
|
||||
"""
|
||||
|
||||
filters = validateFilterString(self.filters)
|
||||
|
||||
locs = stock.models.StockLocation.objects.filter(**filters)
|
||||
try:
|
||||
filters = validateFilterString(self.filters)
|
||||
locs = stock.models.StockLocation.objects.filter(**filters)
|
||||
except (ValidationError, FieldError):
|
||||
return False
|
||||
|
||||
locs = locs.filter(pk=location.pk)
|
||||
|
||||
|
Reference in New Issue
Block a user