From 742b4e2e8c6c93eb20c1264d7f5595478131cb68 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 10 Feb 2026 15:04:48 +0000 Subject: [PATCH] refactor: improve code quality and documentation clarity Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com> --- app/tasks/upload_to_paperless.py | 33 ++++++++++++++++++++++++++------ docs/ConfigurationGuide.md | 2 +- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/app/tasks/upload_to_paperless.py b/app/tasks/upload_to_paperless.py index c096de1d..0a760cc3 100644 --- a/app/tasks/upload_to_paperless.py +++ b/app/tasks/upload_to_paperless.py @@ -16,7 +16,8 @@ logger = logging.getLogger(__name__) POLL_MAX_ATTEMPTS = 10 POLL_INTERVAL_SEC = 3 -UNKNOWN_VALUE = "Unknown" # Magic value used to indicate missing metadata +# Sentinel value used to indicate missing/unknown metadata that should not be set as custom fields +METADATA_UNKNOWN_PLACEHOLDER = "Unknown" def _get_headers(): @@ -35,6 +36,25 @@ def _paperless_api_url(path: str) -> str: return f"{host}{path}" +def normalize_metadata_value(value) -> str: + """ + Normalizes a metadata value to a string suitable for Paperless custom fields. + + Args: + value: The metadata value to normalize + + Returns: + Empty string if value should be excluded, otherwise the string representation + """ + if value is None: + return "" + str_value = str(value) + # Exclude empty strings and the "Unknown" placeholder + if not str_value or str_value == METADATA_UNKNOWN_PLACEHOLDER: + return "" + return str_value + + def poll_task_for_document_id(task_id: str) -> int: """ Polls /api/tasks/?task_id= until we get status=SUCCESS or FAILURE, @@ -127,13 +147,14 @@ def set_document_custom_fields(doc_id: int, custom_fields: dict, task_id: str) - # Build custom_fields array for PATCH request custom_fields_array = [] for field_name, value in custom_fields.items(): - # Ensure value is a string for consistent comparison - str_value = str(value) if value is not None else "" - if str_value and str_value != UNKNOWN_VALUE: # Only set non-empty, non-Unknown values + normalized_value = normalize_metadata_value(value) + if normalized_value: # Only set non-empty values try: field_id = get_custom_field_id(field_name) - custom_fields_array.append({"field": field_id, "value": str_value}) - logger.info(f"[{task_id}] Mapped custom field '{field_name}' to ID {field_id} with value '{str_value}'") + custom_fields_array.append({"field": field_id, "value": normalized_value}) + logger.info( + f"[{task_id}] Mapped custom field '{field_name}' to ID {field_id} with value '{normalized_value}'" + ) except ValueError as e: logger.warning(f"[{task_id}] {str(e)}, skipping this field") continue diff --git a/docs/ConfigurationGuide.md b/docs/ConfigurationGuide.md index a68459a6..f32d2b95 100644 --- a/docs/ConfigurationGuide.md +++ b/docs/ConfigurationGuide.md @@ -250,7 +250,7 @@ DocuElevate extracts the following fields that can be mapped to Paperless custom PAPERLESS_CUSTOM_FIELDS_MAPPING='{"absender": "Sender", "empfaenger": "Recipient", "correspondent": "Correspondent", "language": "Language", "reference_number": "ReferenceNumber"}' ``` -**Note**: Custom fields must be created in your Paperless-ngx instance before DocuElevate can use them. The field names in the mapping (right side of the JSON) must exactly match the names in Paperless. +**Note**: Custom fields must be created in your Paperless-ngx instance before DocuElevate can use them. The field names in the mapping (right side of the JSON) must **exactly** match the names in Paperless (case-sensitive). ### Dropbox