mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Wrap custom filter validation in try/except blocks
This commit is contained in:
parent
9385447761
commit
2fa7c8706b
@ -5,6 +5,7 @@ import sys
|
|||||||
|
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from django.conf.urls import url, include
|
from django.conf.urls import url, include
|
||||||
|
from django.core.exceptions import ValidationError, FieldError
|
||||||
|
|
||||||
from django_filters.rest_framework import DjangoFilterBackend
|
from django_filters.rest_framework import DjangoFilterBackend
|
||||||
|
|
||||||
@ -119,15 +120,22 @@ class StockItemLabelList(LabelListView, StockItemLabelMixin):
|
|||||||
matches = True
|
matches = True
|
||||||
|
|
||||||
# Filter string defined for the StockItemLabel object
|
# Filter string defined for the StockItemLabel object
|
||||||
|
try:
|
||||||
filters = InvenTree.helpers.validateFilterString(label.filters)
|
filters = InvenTree.helpers.validateFilterString(label.filters)
|
||||||
|
except ValidationError:
|
||||||
|
continue
|
||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
|
|
||||||
item_query = StockItem.objects.filter(pk=item.pk)
|
item_query = StockItem.objects.filter(pk=item.pk)
|
||||||
|
|
||||||
|
try:
|
||||||
if not item_query.filter(**filters).exists():
|
if not item_query.filter(**filters).exists():
|
||||||
matches = False
|
matches = False
|
||||||
break
|
break
|
||||||
|
except FieldError:
|
||||||
|
matches = False
|
||||||
|
break
|
||||||
|
|
||||||
# Matched all items
|
# Matched all items
|
||||||
if matches:
|
if matches:
|
||||||
@ -273,15 +281,23 @@ class StockLocationLabelList(LabelListView, StockLocationLabelMixin):
|
|||||||
matches = True
|
matches = True
|
||||||
|
|
||||||
# Filter string defined for the StockLocationLabel object
|
# Filter string defined for the StockLocationLabel object
|
||||||
|
try:
|
||||||
filters = InvenTree.helpers.validateFilterString(label.filters)
|
filters = InvenTree.helpers.validateFilterString(label.filters)
|
||||||
|
except:
|
||||||
|
# Skip if there was an error validating the filters...
|
||||||
|
continue
|
||||||
|
|
||||||
for loc in locations:
|
for loc in locations:
|
||||||
|
|
||||||
loc_query = StockLocation.objects.filter(pk=loc.pk)
|
loc_query = StockLocation.objects.filter(pk=loc.pk)
|
||||||
|
|
||||||
|
try:
|
||||||
if not loc_query.filter(**filters).exists():
|
if not loc_query.filter(**filters).exists():
|
||||||
matches = False
|
matches = False
|
||||||
break
|
break
|
||||||
|
except FieldError:
|
||||||
|
matches = False
|
||||||
|
break
|
||||||
|
|
||||||
# Matched all items
|
# Matched all items
|
||||||
if matches:
|
if matches:
|
||||||
|
@ -12,6 +12,7 @@ from blabel import LabelWriter
|
|||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.core.validators import FileExtensionValidator
|
from django.core.validators import FileExtensionValidator
|
||||||
|
from django.core.exceptions import ValidationError, FieldError
|
||||||
|
|
||||||
from django.utils.translation import gettext_lazy as _
|
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
|
Test if this label template matches a given StockItem object
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
filters = validateFilterString(self.filters)
|
filters = validateFilterString(self.filters)
|
||||||
|
|
||||||
items = stock.models.StockItem.objects.filter(**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)
|
items = items.filter(pk=item.pk)
|
||||||
|
|
||||||
@ -198,9 +202,11 @@ class StockLocationLabel(LabelTemplate):
|
|||||||
Test if this label template matches a given StockLocation object
|
Test if this label template matches a given StockLocation object
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
filters = validateFilterString(self.filters)
|
filters = validateFilterString(self.filters)
|
||||||
|
|
||||||
locs = stock.models.StockLocation.objects.filter(**filters)
|
locs = stock.models.StockLocation.objects.filter(**filters)
|
||||||
|
except (ValidationError, FieldError):
|
||||||
|
return False
|
||||||
|
|
||||||
locs = locs.filter(pk=location.pk)
|
locs = locs.filter(pk=location.pk)
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from django.conf.urls import url, include
|
from django.conf.urls import url, include
|
||||||
|
from django.core.exceptions import FieldError
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
|
||||||
from django_filters.rest_framework import DjangoFilterBackend
|
from django_filters.rest_framework import DjangoFilterBackend
|
||||||
@ -114,14 +115,21 @@ class StockItemTestReportList(ReportListView, StockItemReportMixin):
|
|||||||
matches = True
|
matches = True
|
||||||
|
|
||||||
# Filter string defined for the report object
|
# Filter string defined for the report object
|
||||||
|
try:
|
||||||
filters = InvenTree.helpers.validateFilterString(report.filters)
|
filters = InvenTree.helpers.validateFilterString(report.filters)
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
|
||||||
for item in items:
|
for item in items:
|
||||||
item_query = StockItem.objects.filter(pk=item.pk)
|
item_query = StockItem.objects.filter(pk=item.pk)
|
||||||
|
|
||||||
|
try:
|
||||||
if not item_query.filter(**filters).exists():
|
if not item_query.filter(**filters).exists():
|
||||||
matches = False
|
matches = False
|
||||||
break
|
break
|
||||||
|
except FieldError:
|
||||||
|
matches = False
|
||||||
|
break
|
||||||
|
|
||||||
if matches:
|
if matches:
|
||||||
valid_report_ids.add(report.pk)
|
valid_report_ids.add(report.pk)
|
||||||
|
@ -13,6 +13,7 @@ import datetime
|
|||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
from django.core.exceptions import ValidationError, FieldError
|
||||||
|
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
|
|
||||||
@ -262,9 +263,11 @@ class TestReport(ReportTemplateBase):
|
|||||||
Test if this report template matches a given StockItem objects
|
Test if this report template matches a given StockItem objects
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
filters = validateFilterString(self.filters)
|
filters = validateFilterString(self.filters)
|
||||||
|
|
||||||
items = stock.models.StockItem.objects.filter(**filters)
|
items = stock.models.StockItem.objects.filter(**filters)
|
||||||
|
except (ValidationError, FieldError):
|
||||||
|
return False
|
||||||
|
|
||||||
# Ensure the provided StockItem object matches the filters
|
# Ensure the provided StockItem object matches the filters
|
||||||
items = items.filter(pk=item.pk)
|
items = items.filter(pk=item.pk)
|
||||||
|
@ -9,7 +9,7 @@ from __future__ import unicode_literals
|
|||||||
import os
|
import os
|
||||||
|
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError, FieldError
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
||||||
from django.db import models, transaction
|
from django.db import models, transaction
|
||||||
@ -1365,10 +1365,13 @@ class StockItem(MPTTModel):
|
|||||||
|
|
||||||
for test_report in report.models.TestReport.objects.filter(enabled=True):
|
for test_report in report.models.TestReport.objects.filter(enabled=True):
|
||||||
|
|
||||||
|
# Attempt to validate report filter (skip if invalid)
|
||||||
|
try:
|
||||||
filters = helpers.validateFilterString(test_report.filters)
|
filters = helpers.validateFilterString(test_report.filters)
|
||||||
|
|
||||||
if item_query.filter(**filters).exists():
|
if item_query.filter(**filters).exists():
|
||||||
reports.append(test_report)
|
reports.append(test_report)
|
||||||
|
except (ValidationError, FieldError):
|
||||||
|
continue
|
||||||
|
|
||||||
return reports
|
return reports
|
||||||
|
|
||||||
@ -1391,10 +1394,13 @@ class StockItem(MPTTModel):
|
|||||||
|
|
||||||
for lbl in label.models.StockItemLabel.objects.filter(enabled=True):
|
for lbl in label.models.StockItemLabel.objects.filter(enabled=True):
|
||||||
|
|
||||||
|
try:
|
||||||
filters = helpers.validateFilterString(lbl.filters)
|
filters = helpers.validateFilterString(lbl.filters)
|
||||||
|
|
||||||
if item_query.filter(**filters).exists():
|
if item_query.filter(**filters).exists():
|
||||||
labels.append(lbl)
|
labels.append(lbl)
|
||||||
|
except (ValidationError, FieldError):
|
||||||
|
continue
|
||||||
|
|
||||||
return labels
|
return labels
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user