From 0f26114ca0ebdd868ca67ad6b6801b8a3458d7dc Mon Sep 17 00:00:00 2001 From: Trenton H <797416+stumpylog@users.noreply.github.com> Date: Tue, 1 Oct 2024 13:48:28 -0700 Subject: [PATCH] Fixes some tests, and adds migration for StoragePaths --- src/documents/file_handling.py | 4 +- .../1054_alter_storage_path_templates.py | 53 +++++++++++ src/documents/tests/test_file_handling.py | 94 ++----------------- 3 files changed, 62 insertions(+), 89 deletions(-) create mode 100644 src/documents/migrations/1054_alter_storage_path_templates.py diff --git a/src/documents/file_handling.py b/src/documents/file_handling.py index 38cf6c9a1..1d21b1358 100644 --- a/src/documents/file_handling.py +++ b/src/documents/file_handling.py @@ -195,7 +195,7 @@ def get_basic_metadata_context( def get_tags_context(tags: Iterable[Tag]) -> dict[str, str]: return { - "tags_list": pathvalidate.sanitize_filename( + "tag_list": pathvalidate.sanitize_filename( ",".join( sorted(tag.name for tag in tags), ), @@ -327,7 +327,7 @@ def generate_filename( ) # backward compatibility rendered_filename = ( - rendered_filename.strip(os.sep).replace("\n", "").replace("\r", "") + rendered_filename.strip(os.sep).replace("\n", "").replace("\r", "").strip() ) return rendered_filename diff --git a/src/documents/migrations/1054_alter_storage_path_templates.py b/src/documents/migrations/1054_alter_storage_path_templates.py new file mode 100644 index 000000000..4831d0dbb --- /dev/null +++ b/src/documents/migrations/1054_alter_storage_path_templates.py @@ -0,0 +1,53 @@ +# Generated by Django 5.1.1 on 2024-10-01 20:42 + +import re + +from django.db import migrations +from django.db import transaction + + +def convert_from_format_to_template(apps, schema_editor): + # TODO: Is there a signal to disable? I don't want documents getting moved while this is running + + StoragePath = apps.get_model("documents", "StoragePath") + + def convert_to_django_template_format(old_format): + """ + Converts old Python string format (with {}) to Django template style (with {{ }}), + while ignoring existing {{ ... }} placeholders. + + :param old_format: The old style format string (e.g., "{title} by {author}") + :return: Converted string in Django Template style (e.g., "{{ title }} by {{ author }}") + """ + + # Step 1: Match placeholders with single curly braces but not those with double braces + pattern = r"(?