From 7b6f81e53e6885707cd912f818849fb1a6076e9f Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Wed, 23 Apr 2025 09:54:48 -0700 Subject: [PATCH] Duh, actually use the settings --- src/documents/barcodes.py | 51 +++++++++++++++++++++------------------ src/paperless/config.py | 33 ++++++++++++++++--------- 2 files changed, 50 insertions(+), 34 deletions(-) diff --git a/src/documents/barcodes.py b/src/documents/barcodes.py index 3b0c1d33b..50859f00c 100644 --- a/src/documents/barcodes.py +++ b/src/documents/barcodes.py @@ -22,6 +22,7 @@ from documents.plugins.helpers import ProgressStatusOptions from documents.utils import copy_basic_file_stats from documents.utils import copy_file_with_basic_stats from documents.utils import maybe_override_pixel_limit +from paperless.config import BarcodeConfig if TYPE_CHECKING: from collections.abc import Callable @@ -46,7 +47,7 @@ class Barcode: Returns True if the barcode value equals the configured separation value, False otherwise """ - return self.value == settings.CONSUMER_BARCODE_STRING + return self.value == self.settings.CONSUMER_BARCODE_STRING @property def is_asn(self) -> bool: @@ -54,7 +55,7 @@ class Barcode: Returns True if the barcode value matches the configured ASN prefix, False otherwise """ - return self.value.startswith(settings.CONSUMER_ASN_BARCODE_PREFIX) + return self.value.startswith(self.settings.barcode_asn_prefix) class BarcodePlugin(ConsumeTaskPlugin): @@ -67,17 +68,23 @@ class BarcodePlugin(ConsumeTaskPlugin): - ASN from barcode detection is enabled or - Barcode support is enabled and the mime type is supported """ - if settings.CONSUMER_BARCODE_TIFF_SUPPORT: + if self.settings.barcode_enable_tiff_support: supported_mimes: set[str] = {"application/pdf", "image/tiff"} else: supported_mimes = {"application/pdf"} return ( - settings.CONSUMER_ENABLE_ASN_BARCODE - or settings.CONSUMER_ENABLE_BARCODES - or settings.CONSUMER_ENABLE_TAG_BARCODE + self.settings.barcode_enable_asn + or self.settings.barcodes_enabled + or self.settings.barcode_enable_tag ) and self.input_doc.mime_type in supported_mimes + def get_settings(self) -> BarcodeConfig: + """ + Returns the settings for this plugin (Django settings or app config) + """ + return BarcodeConfig() + def setup(self) -> None: self.temp_dir = tempfile.TemporaryDirectory( dir=self.base_tmp_dir, @@ -86,6 +93,7 @@ class BarcodePlugin(ConsumeTaskPlugin): self.pdf_file: Path = self.input_doc.original_file self._tiff_conversion_done = False self.barcodes: list[Barcode] = [] + self.settings = self.get_settings() def run(self) -> None: # Some operations may use PIL, override pixel setting if needed @@ -99,7 +107,7 @@ class BarcodePlugin(ConsumeTaskPlugin): # try reading tags from barcodes if ( - settings.CONSUMER_ENABLE_TAG_BARCODE + self.settings.barcode_enable_tag and (tags := self.tags) is not None and len(tags) > 0 ): @@ -110,7 +118,7 @@ class BarcodePlugin(ConsumeTaskPlugin): logger.info(f"Found tags in barcode: {tags}") # Lastly attempt to split documents - if settings.CONSUMER_ENABLE_BARCODES and ( + if self.settings.barcodes_enabled and ( separator_pages := self.get_separation_pages() ): # We have pages to split against @@ -155,10 +163,7 @@ class BarcodePlugin(ConsumeTaskPlugin): # Update/overwrite an ASN if possible # After splitting, as otherwise each split document gets the same ASN - if ( - settings.CONSUMER_ENABLE_ASN_BARCODE - and (located_asn := self.asn) is not None - ): + if self.settings.barcode_enable_asn and (located_asn := self.asn) is not None: logger.info(f"Found ASN in barcode: {located_asn}") self.metadata.asn = located_asn @@ -245,8 +250,8 @@ class BarcodePlugin(ConsumeTaskPlugin): # Get limit from configuration barcode_max_pages: int = ( num_of_pages - if settings.CONSUMER_BARCODE_MAX_PAGES == 0 - else settings.CONSUMER_BARCODE_MAX_PAGES + if self.settings.barcode_max_pages == 0 + else self.settings.barcode_max_pages ) if barcode_max_pages < num_of_pages: # pragma: no cover @@ -261,7 +266,7 @@ class BarcodePlugin(ConsumeTaskPlugin): # Convert page to image page = convert_from_path( self.pdf_file, - dpi=settings.CONSUMER_BARCODE_DPI, + dpi=self.settings.barcode_dpi, output_folder=self.temp_dir.name, first_page=current_page_number + 1, last_page=current_page_number + 1, @@ -272,7 +277,7 @@ class BarcodePlugin(ConsumeTaskPlugin): logger.debug(f"Image is at {page_filepath}") # Upscale image if configured - factor = settings.CONSUMER_BARCODE_UPSCALE + factor = self.settings.barcode_upscale if factor > 1.0: logger.debug( f"Upscaling image by {factor} for better barcode detection", @@ -308,7 +313,7 @@ class BarcodePlugin(ConsumeTaskPlugin): def asn(self) -> int | None: """ Search the parsed barcodes for any ASNs. - The first barcode that starts with CONSUMER_ASN_BARCODE_PREFIX + The first barcode that starts with barcode_asn_prefix is considered the ASN to be used. Returns the detected ASN (or None) """ @@ -317,7 +322,7 @@ class BarcodePlugin(ConsumeTaskPlugin): # Ensure the barcodes have been read self.detect() - # get the first barcode that starts with CONSUMER_ASN_BARCODE_PREFIX + # get the first barcode that starts with barcode_asn_prefix asn_text: str | None = next( (x.value for x in self.barcodes if x.is_asn), None, @@ -326,7 +331,7 @@ class BarcodePlugin(ConsumeTaskPlugin): if asn_text: logger.debug(f"Found ASN Barcode: {asn_text}") # remove the prefix and remove whitespace - asn_text = asn_text[len(settings.CONSUMER_ASN_BARCODE_PREFIX) :].strip() + asn_text = asn_text[len(self.settings.barcode_asn_prefix) :].strip() # remove non-numeric parts of the remaining string asn_text = re.sub(r"\D", "", asn_text) @@ -356,9 +361,9 @@ class BarcodePlugin(ConsumeTaskPlugin): for raw in tag_texts.split(","): try: tag_str: str | None = None - for regex in settings.CONSUMER_TAG_BARCODE_MAPPING: + for regex in self.settings.barcode_tag_mapping: if re.match(regex, raw, flags=re.IGNORECASE): - sub = settings.CONSUMER_TAG_BARCODE_MAPPING[regex] + sub = self.settings.barcode_tag_mapping[regex] tag_str = ( re.sub(regex, sub, raw, flags=re.IGNORECASE) if sub @@ -394,13 +399,13 @@ class BarcodePlugin(ConsumeTaskPlugin): """ # filter all barcodes for the separator string # get the page numbers of the separating barcodes - retain = settings.CONSUMER_BARCODE_RETAIN_SPLIT_PAGES + retain = self.settings.barcode_retain_split_pages separator_pages = { bc.page: retain for bc in self.barcodes if bc.is_separator and (not retain or (retain and bc.page > 0)) } # as below, dont include the first page if retain is enabled - if not settings.CONSUMER_ENABLE_ASN_BARCODE: + if not self.settings.barcode_enable_asn: return separator_pages # add the page numbers of the ASN barcodes diff --git a/src/paperless/config.py b/src/paperless/config.py index 85e681009..fb3139d79 100644 --- a/src/paperless/config.py +++ b/src/paperless/config.py @@ -55,17 +55,6 @@ class OcrConfig(OutputTypeConfig): max_image_pixel: float | None = dataclasses.field(init=False) color_conversion_strategy: str = dataclasses.field(init=False) user_args: dict[str, str] | None = dataclasses.field(init=False) - barcodes_enabled: bool = dataclasses.field(init=False) - barcode_enable_tiff_support: bool = dataclasses.field(init=False) - barcode_string: str = dataclasses.field(init=False) - barcode_retain_split_pages: bool = dataclasses.field(init=False) - barcode_enable_asn: bool = dataclasses.field(init=False) - barcode_asn_prefix: str = dataclasses.field(init=False) - barcode_upscale: float = dataclasses.field(init=False) - barcode_dpi: int = dataclasses.field(init=False) - barcode_max_pages: int = dataclasses.field(init=False) - barcode_enable_tag: bool = dataclasses.field(init=False) - barcode_tag_mapping: dict[str, str] = dataclasses.field(init=False) def __post_init__(self) -> None: super().__post_init__() @@ -109,6 +98,28 @@ class OcrConfig(OutputTypeConfig): user_args = {} self.user_args = user_args + +@dataclasses.dataclass +class BarcodeConfig(BaseConfig): + """ + Barcodes settings + """ + + barcodes_enabled: bool = dataclasses.field(init=False) + barcode_enable_tiff_support: bool = dataclasses.field(init=False) + barcode_string: str = dataclasses.field(init=False) + barcode_retain_split_pages: bool = dataclasses.field(init=False) + barcode_enable_asn: bool = dataclasses.field(init=False) + barcode_asn_prefix: str = dataclasses.field(init=False) + barcode_upscale: float = dataclasses.field(init=False) + barcode_dpi: int = dataclasses.field(init=False) + barcode_max_pages: int = dataclasses.field(init=False) + barcode_enable_tag: bool = dataclasses.field(init=False) + barcode_tag_mapping: dict[str, str] = dataclasses.field(init=False) + + def __post_init__(self) -> None: + app_config = self._get_config_instance() + self.barcodes_enabled = ( app_config.barcodes_enabled or settings.CONSUMER_ENABLE_BARCODES )