mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-08-16 00:36:22 +00:00
Duh, actually use the settings
This commit is contained in:
@@ -22,6 +22,7 @@ from documents.plugins.helpers import ProgressStatusOptions
|
|||||||
from documents.utils import copy_basic_file_stats
|
from documents.utils import copy_basic_file_stats
|
||||||
from documents.utils import copy_file_with_basic_stats
|
from documents.utils import copy_file_with_basic_stats
|
||||||
from documents.utils import maybe_override_pixel_limit
|
from documents.utils import maybe_override_pixel_limit
|
||||||
|
from paperless.config import BarcodeConfig
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
@@ -46,7 +47,7 @@ class Barcode:
|
|||||||
Returns True if the barcode value equals the configured separation value,
|
Returns True if the barcode value equals the configured separation value,
|
||||||
False otherwise
|
False otherwise
|
||||||
"""
|
"""
|
||||||
return self.value == settings.CONSUMER_BARCODE_STRING
|
return self.value == self.settings.CONSUMER_BARCODE_STRING
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_asn(self) -> bool:
|
def is_asn(self) -> bool:
|
||||||
@@ -54,7 +55,7 @@ class Barcode:
|
|||||||
Returns True if the barcode value matches the configured ASN prefix,
|
Returns True if the barcode value matches the configured ASN prefix,
|
||||||
False otherwise
|
False otherwise
|
||||||
"""
|
"""
|
||||||
return self.value.startswith(settings.CONSUMER_ASN_BARCODE_PREFIX)
|
return self.value.startswith(self.settings.barcode_asn_prefix)
|
||||||
|
|
||||||
|
|
||||||
class BarcodePlugin(ConsumeTaskPlugin):
|
class BarcodePlugin(ConsumeTaskPlugin):
|
||||||
@@ -67,17 +68,23 @@ class BarcodePlugin(ConsumeTaskPlugin):
|
|||||||
- ASN from barcode detection is enabled or
|
- ASN from barcode detection is enabled or
|
||||||
- Barcode support is enabled and the mime type is supported
|
- 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"}
|
supported_mimes: set[str] = {"application/pdf", "image/tiff"}
|
||||||
else:
|
else:
|
||||||
supported_mimes = {"application/pdf"}
|
supported_mimes = {"application/pdf"}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
settings.CONSUMER_ENABLE_ASN_BARCODE
|
self.settings.barcode_enable_asn
|
||||||
or settings.CONSUMER_ENABLE_BARCODES
|
or self.settings.barcodes_enabled
|
||||||
or settings.CONSUMER_ENABLE_TAG_BARCODE
|
or self.settings.barcode_enable_tag
|
||||||
) and self.input_doc.mime_type in supported_mimes
|
) 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:
|
def setup(self) -> None:
|
||||||
self.temp_dir = tempfile.TemporaryDirectory(
|
self.temp_dir = tempfile.TemporaryDirectory(
|
||||||
dir=self.base_tmp_dir,
|
dir=self.base_tmp_dir,
|
||||||
@@ -86,6 +93,7 @@ class BarcodePlugin(ConsumeTaskPlugin):
|
|||||||
self.pdf_file: Path = self.input_doc.original_file
|
self.pdf_file: Path = self.input_doc.original_file
|
||||||
self._tiff_conversion_done = False
|
self._tiff_conversion_done = False
|
||||||
self.barcodes: list[Barcode] = []
|
self.barcodes: list[Barcode] = []
|
||||||
|
self.settings = self.get_settings()
|
||||||
|
|
||||||
def run(self) -> None:
|
def run(self) -> None:
|
||||||
# Some operations may use PIL, override pixel setting if needed
|
# Some operations may use PIL, override pixel setting if needed
|
||||||
@@ -99,7 +107,7 @@ class BarcodePlugin(ConsumeTaskPlugin):
|
|||||||
|
|
||||||
# try reading tags from barcodes
|
# try reading tags from barcodes
|
||||||
if (
|
if (
|
||||||
settings.CONSUMER_ENABLE_TAG_BARCODE
|
self.settings.barcode_enable_tag
|
||||||
and (tags := self.tags) is not None
|
and (tags := self.tags) is not None
|
||||||
and len(tags) > 0
|
and len(tags) > 0
|
||||||
):
|
):
|
||||||
@@ -110,7 +118,7 @@ class BarcodePlugin(ConsumeTaskPlugin):
|
|||||||
logger.info(f"Found tags in barcode: {tags}")
|
logger.info(f"Found tags in barcode: {tags}")
|
||||||
|
|
||||||
# Lastly attempt to split documents
|
# Lastly attempt to split documents
|
||||||
if settings.CONSUMER_ENABLE_BARCODES and (
|
if self.settings.barcodes_enabled and (
|
||||||
separator_pages := self.get_separation_pages()
|
separator_pages := self.get_separation_pages()
|
||||||
):
|
):
|
||||||
# We have pages to split against
|
# We have pages to split against
|
||||||
@@ -155,10 +163,7 @@ class BarcodePlugin(ConsumeTaskPlugin):
|
|||||||
|
|
||||||
# Update/overwrite an ASN if possible
|
# Update/overwrite an ASN if possible
|
||||||
# After splitting, as otherwise each split document gets the same ASN
|
# After splitting, as otherwise each split document gets the same ASN
|
||||||
if (
|
if self.settings.barcode_enable_asn and (located_asn := self.asn) is not None:
|
||||||
settings.CONSUMER_ENABLE_ASN_BARCODE
|
|
||||||
and (located_asn := self.asn) is not None
|
|
||||||
):
|
|
||||||
logger.info(f"Found ASN in barcode: {located_asn}")
|
logger.info(f"Found ASN in barcode: {located_asn}")
|
||||||
self.metadata.asn = located_asn
|
self.metadata.asn = located_asn
|
||||||
|
|
||||||
@@ -245,8 +250,8 @@ class BarcodePlugin(ConsumeTaskPlugin):
|
|||||||
# Get limit from configuration
|
# Get limit from configuration
|
||||||
barcode_max_pages: int = (
|
barcode_max_pages: int = (
|
||||||
num_of_pages
|
num_of_pages
|
||||||
if settings.CONSUMER_BARCODE_MAX_PAGES == 0
|
if self.settings.barcode_max_pages == 0
|
||||||
else settings.CONSUMER_BARCODE_MAX_PAGES
|
else self.settings.barcode_max_pages
|
||||||
)
|
)
|
||||||
|
|
||||||
if barcode_max_pages < num_of_pages: # pragma: no cover
|
if barcode_max_pages < num_of_pages: # pragma: no cover
|
||||||
@@ -261,7 +266,7 @@ class BarcodePlugin(ConsumeTaskPlugin):
|
|||||||
# Convert page to image
|
# Convert page to image
|
||||||
page = convert_from_path(
|
page = convert_from_path(
|
||||||
self.pdf_file,
|
self.pdf_file,
|
||||||
dpi=settings.CONSUMER_BARCODE_DPI,
|
dpi=self.settings.barcode_dpi,
|
||||||
output_folder=self.temp_dir.name,
|
output_folder=self.temp_dir.name,
|
||||||
first_page=current_page_number + 1,
|
first_page=current_page_number + 1,
|
||||||
last_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}")
|
logger.debug(f"Image is at {page_filepath}")
|
||||||
|
|
||||||
# Upscale image if configured
|
# Upscale image if configured
|
||||||
factor = settings.CONSUMER_BARCODE_UPSCALE
|
factor = self.settings.barcode_upscale
|
||||||
if factor > 1.0:
|
if factor > 1.0:
|
||||||
logger.debug(
|
logger.debug(
|
||||||
f"Upscaling image by {factor} for better barcode detection",
|
f"Upscaling image by {factor} for better barcode detection",
|
||||||
@@ -308,7 +313,7 @@ class BarcodePlugin(ConsumeTaskPlugin):
|
|||||||
def asn(self) -> int | None:
|
def asn(self) -> int | None:
|
||||||
"""
|
"""
|
||||||
Search the parsed barcodes for any ASNs.
|
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.
|
is considered the ASN to be used.
|
||||||
Returns the detected ASN (or None)
|
Returns the detected ASN (or None)
|
||||||
"""
|
"""
|
||||||
@@ -317,7 +322,7 @@ class BarcodePlugin(ConsumeTaskPlugin):
|
|||||||
# Ensure the barcodes have been read
|
# Ensure the barcodes have been read
|
||||||
self.detect()
|
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(
|
asn_text: str | None = next(
|
||||||
(x.value for x in self.barcodes if x.is_asn),
|
(x.value for x in self.barcodes if x.is_asn),
|
||||||
None,
|
None,
|
||||||
@@ -326,7 +331,7 @@ class BarcodePlugin(ConsumeTaskPlugin):
|
|||||||
if asn_text:
|
if asn_text:
|
||||||
logger.debug(f"Found ASN Barcode: {asn_text}")
|
logger.debug(f"Found ASN Barcode: {asn_text}")
|
||||||
# remove the prefix and remove whitespace
|
# 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
|
# remove non-numeric parts of the remaining string
|
||||||
asn_text = re.sub(r"\D", "", asn_text)
|
asn_text = re.sub(r"\D", "", asn_text)
|
||||||
@@ -356,9 +361,9 @@ class BarcodePlugin(ConsumeTaskPlugin):
|
|||||||
for raw in tag_texts.split(","):
|
for raw in tag_texts.split(","):
|
||||||
try:
|
try:
|
||||||
tag_str: str | None = None
|
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):
|
if re.match(regex, raw, flags=re.IGNORECASE):
|
||||||
sub = settings.CONSUMER_TAG_BARCODE_MAPPING[regex]
|
sub = self.settings.barcode_tag_mapping[regex]
|
||||||
tag_str = (
|
tag_str = (
|
||||||
re.sub(regex, sub, raw, flags=re.IGNORECASE)
|
re.sub(regex, sub, raw, flags=re.IGNORECASE)
|
||||||
if sub
|
if sub
|
||||||
@@ -394,13 +399,13 @@ class BarcodePlugin(ConsumeTaskPlugin):
|
|||||||
"""
|
"""
|
||||||
# filter all barcodes for the separator string
|
# filter all barcodes for the separator string
|
||||||
# get the page numbers of the separating barcodes
|
# get the page numbers of the separating barcodes
|
||||||
retain = settings.CONSUMER_BARCODE_RETAIN_SPLIT_PAGES
|
retain = self.settings.barcode_retain_split_pages
|
||||||
separator_pages = {
|
separator_pages = {
|
||||||
bc.page: retain
|
bc.page: retain
|
||||||
for bc in self.barcodes
|
for bc in self.barcodes
|
||||||
if bc.is_separator and (not retain or (retain and bc.page > 0))
|
if bc.is_separator and (not retain or (retain and bc.page > 0))
|
||||||
} # as below, dont include the first page if retain is enabled
|
} # 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
|
return separator_pages
|
||||||
|
|
||||||
# add the page numbers of the ASN barcodes
|
# add the page numbers of the ASN barcodes
|
||||||
|
@@ -55,17 +55,6 @@ class OcrConfig(OutputTypeConfig):
|
|||||||
max_image_pixel: float | None = dataclasses.field(init=False)
|
max_image_pixel: float | None = dataclasses.field(init=False)
|
||||||
color_conversion_strategy: str = dataclasses.field(init=False)
|
color_conversion_strategy: str = dataclasses.field(init=False)
|
||||||
user_args: dict[str, str] | None = 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:
|
def __post_init__(self) -> None:
|
||||||
super().__post_init__()
|
super().__post_init__()
|
||||||
@@ -109,6 +98,28 @@ class OcrConfig(OutputTypeConfig):
|
|||||||
user_args = {}
|
user_args = {}
|
||||||
self.user_args = 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 = (
|
self.barcodes_enabled = (
|
||||||
app_config.barcodes_enabled or settings.CONSUMER_ENABLE_BARCODES
|
app_config.barcodes_enabled or settings.CONSUMER_ENABLE_BARCODES
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user