mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 13:15:43 +00:00 
			
		
		
		
	Label printing errors (#6450)
* Enhance error handling for label printing * Better feedback in forms * Raise error to user - no need to log
This commit is contained in:
		| @@ -4,6 +4,7 @@ from django.core.exceptions import FieldError, ValidationError | |||||||
| from django.http import JsonResponse | from django.http import JsonResponse | ||||||
| from django.urls import include, path, re_path | from django.urls import include, path, re_path | ||||||
| from django.utils.decorators import method_decorator | from django.utils.decorators import method_decorator | ||||||
|  | from django.utils.translation import gettext_lazy as _ | ||||||
| from django.views.decorators.cache import cache_page, never_cache | from django.views.decorators.cache import cache_page, never_cache | ||||||
|  |  | ||||||
| from django_filters.rest_framework import DjangoFilterBackend | from django_filters.rest_framework import DjangoFilterBackend | ||||||
| @@ -13,6 +14,7 @@ from rest_framework.request import clone_request | |||||||
|  |  | ||||||
| import build.models | import build.models | ||||||
| import common.models | import common.models | ||||||
|  | import InvenTree.exceptions | ||||||
| import InvenTree.helpers | import InvenTree.helpers | ||||||
| import label.models | import label.models | ||||||
| import label.serializers | import label.serializers | ||||||
| @@ -232,9 +234,14 @@ class LabelPrintMixin(LabelFilterMixin): | |||||||
|         # At this point, we offload the label(s) to the selected plugin. |         # At this point, we offload the label(s) to the selected plugin. | ||||||
|         # The plugin is responsible for handling the request and returning a response. |         # The plugin is responsible for handling the request and returning a response. | ||||||
|  |  | ||||||
|  |         try: | ||||||
|             result = plugin.print_labels( |             result = plugin.print_labels( | ||||||
|                 label, items_to_print, request, printing_options=request.data |                 label, items_to_print, request, printing_options=request.data | ||||||
|             ) |             ) | ||||||
|  |         except ValidationError as e: | ||||||
|  |             raise (e) | ||||||
|  |         except Exception as e: | ||||||
|  |             raise ValidationError([_('Error printing label'), str(e)]) | ||||||
|  |  | ||||||
|         if isinstance(result, JsonResponse): |         if isinstance(result, JsonResponse): | ||||||
|             result['plugin'] = plugin.plugin_slug() |             result['plugin'] = plugin.plugin_slug() | ||||||
|   | |||||||
| @@ -2,13 +2,16 @@ | |||||||
|  |  | ||||||
| from typing import Union | from typing import Union | ||||||
|  |  | ||||||
|  | from django.core.exceptions import ValidationError | ||||||
| from django.http import JsonResponse | from django.http import JsonResponse | ||||||
|  | from django.utils.translation import gettext_lazy as _ | ||||||
|  |  | ||||||
| import pdf2image | import pdf2image | ||||||
| from rest_framework import serializers | from rest_framework import serializers | ||||||
| from rest_framework.request import Request | from rest_framework.request import Request | ||||||
|  |  | ||||||
| from common.models import InvenTreeSetting | from common.models import InvenTreeSetting | ||||||
|  | from InvenTree.exceptions import log_error | ||||||
| from InvenTree.tasks import offload_task | from InvenTree.tasks import offload_task | ||||||
| from label.models import LabelTemplate | from label.models import LabelTemplate | ||||||
| from plugin.base.label import label as plugin_label | from plugin.base.label import label as plugin_label | ||||||
| @@ -47,7 +50,11 @@ class LabelPrintingMixin: | |||||||
|             label: The LabelTemplate object to render |             label: The LabelTemplate object to render | ||||||
|             request: The HTTP request object which triggered this print job |             request: The HTTP request object which triggered this print job | ||||||
|         """ |         """ | ||||||
|  |         try: | ||||||
|             return label.render(request) |             return label.render(request) | ||||||
|  |         except Exception as e: | ||||||
|  |             log_error('label.render_to_pdf') | ||||||
|  |             raise ValidationError(_('Error rendering label to PDF')) | ||||||
|  |  | ||||||
|     def render_to_html(self, label: LabelTemplate, request, **kwargs): |     def render_to_html(self, label: LabelTemplate, request, **kwargs): | ||||||
|         """Render this label to HTML format. |         """Render this label to HTML format. | ||||||
| @@ -56,7 +63,11 @@ class LabelPrintingMixin: | |||||||
|             label: The LabelTemplate object to render |             label: The LabelTemplate object to render | ||||||
|             request: The HTTP request object which triggered this print job |             request: The HTTP request object which triggered this print job | ||||||
|         """ |         """ | ||||||
|  |         try: | ||||||
|             return label.render_as_string(request) |             return label.render_as_string(request) | ||||||
|  |         except Exception as e: | ||||||
|  |             log_error('label.render_to_html') | ||||||
|  |             raise ValidationError(_('Error rendering label to HTML')) | ||||||
|  |  | ||||||
|     def render_to_png(self, label: LabelTemplate, request=None, **kwargs): |     def render_to_png(self, label: LabelTemplate, request=None, **kwargs): | ||||||
|         """Render this label to PNG format.""" |         """Render this label to PNG format.""" | ||||||
| @@ -71,8 +82,11 @@ class LabelPrintingMixin: | |||||||
|         dpi = kwargs.get('dpi', InvenTreeSetting.get_setting('LABEL_DPI', 300)) |         dpi = kwargs.get('dpi', InvenTreeSetting.get_setting('LABEL_DPI', 300)) | ||||||
|  |  | ||||||
|         # Convert to png data |         # Convert to png data | ||||||
|         png = pdf2image.convert_from_bytes(pdf_data, dpi=dpi)[0] |         try: | ||||||
|         return png |             return pdf2image.convert_from_bytes(pdf_data, dpi=dpi)[0] | ||||||
|  |         except Exception as e: | ||||||
|  |             log_error('label.render_to_png') | ||||||
|  |             raise ValidationError(_('Error rendering label to PNG')) | ||||||
|  |  | ||||||
|     def print_labels( |     def print_labels( | ||||||
|         self, |         self, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user