diff --git a/src/documents/file_handling.py b/src/documents/file_handling.py index 700a16d8b..456f90a44 100644 --- a/src/documents/file_handling.py +++ b/src/documents/file_handling.py @@ -9,6 +9,8 @@ from django.template.defaultfilters import slugify from django.utils import timezone from documents.models import Document +from documents.models import CustomField +from documents.models import CustomFieldInstance logger = logging.getLogger("paperless.filehandling") @@ -194,33 +196,40 @@ def generate_filename( # Convert UTC database datetime to localized date local_added = timezone.localdate(doc.added) local_created = timezone.localdate(doc.created) + placeholders = { + "title":pathvalidate.sanitize_filename(doc.title, replacement_text="-"), + "correspondent":correspondent, + "document_type":document_type, + "created":local_created.isoformat(), + "created_year":local_created.strftime("%Y"), + "created_year_short":local_created.strftime("%y"), + "created_month":local_created.strftime("%m"), + "created_month_name":local_created.strftime("%B"), + "created_month_name_short":local_created.strftime("%b"), + "created_day":local_created.strftime("%d"), + "added":local_added.isoformat(), + "added_year":local_added.strftime("%Y"), + "added_year_short":local_added.strftime("%y"), + "added_month":local_added.strftime("%m"), + "added_month_name":local_added.strftime("%B"), + "added_month_name_short":local_added.strftime("%b"), + "added_day":local_added.strftime("%d"), + "asn":asn, + "tags":tags, + "tag_list":tag_list, + "owner_username":owner_username_str, + "original_name":original_name, + "doc_pk":f"{doc.pk:07}", + } - path = filename_format.format( - title=pathvalidate.sanitize_filename(doc.title, replacement_text="-"), - correspondent=correspondent, - document_type=document_type, - created=local_created.isoformat(), - created_year=local_created.strftime("%Y"), - created_year_short=local_created.strftime("%y"), - created_month=local_created.strftime("%m"), - created_month_name=local_created.strftime("%B"), - created_month_name_short=local_created.strftime("%b"), - created_day=local_created.strftime("%d"), - added=local_added.isoformat(), - added_year=local_added.strftime("%Y"), - added_year_short=local_added.strftime("%y"), - added_month=local_added.strftime("%m"), - added_month_name=local_added.strftime("%B"), - added_month_name_short=local_added.strftime("%b"), - added_day=local_added.strftime("%d"), - asn=asn, - tags=tags, - tag_list=tag_list, - owner_username=owner_username_str, - original_name=original_name, - doc_pk=f"{doc.pk:07}", - ).strip() + for field in CustomField.objects.all(): + placeholders['customfield' + f'{field.pk}'] = '-none-' + for field_instance in CustomFieldInstance.objects.filter(document=doc): + placeholders['customfield' + f'{field_instance.field.id}'] = field_instance.value + + + path = filename_format.format(**placeholders).strip() if settings.FILENAME_FORMAT_REMOVE_NONE: path = path.replace("/-none-/", "/") # remove empty directories path = path.replace(" -none-", "") # remove when spaced, with space diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 5ea0e21c8..08c7daee0 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -1145,32 +1145,37 @@ class StoragePathSerializer(MatchingModelSerializer, OwnedObjectSerializer): def validate_path(self, path): try: - path.format( - title="title", - correspondent="correspondent", - document_type="document_type", - created="created", - created_year="created_year", - created_year_short="created_year_short", - created_month="created_month", - created_month_name="created_month_name", - created_month_name_short="created_month_name_short", - created_day="created_day", - added="added", - added_year="added_year", - added_year_short="added_year_short", - added_month="added_month", - added_month_name="added_month_name", - added_month_name_short="added_month_name_short", - added_day="added_day", - asn="asn", - tags="tags", - tag_list="tag_list", - owner_username="someone", - original_name="testfile", - doc_pk="doc_pk", - ) + placeholders = { + "title": "title", + "correspondent": "correspondent", + "document_type":"document_type", + "created":"created", + "created_year":"created_year", + "created_year_short":"created_year_short", + "created_month":"created_month", + "created_month_name":"created_month_name", + "created_month_name_short":"created_month_name_short", + "created_day":"created_day", + "added":"added", + "added_year":"added_year", + "added_year_short":"added_year_short", + "added_month":"added_month", + "added_month_name":"added_month_name", + "added_month_name_short":"added_month_name_short", + "added_day":"added_day", + "asn":"asn", + "tags":"tags", + "tag_list":"tag_list", + "owner_username":"someone", + "original_name":"testfile", + "doc_pk":"doc_pk" + } + + for field in CustomField.objects.all(): + placeholders['customfield' + f'{field.pk}'] = '-none-' + + path.format(**placeholders) except KeyError as err: raise serializers.ValidationError(_("Invalid variable detected.")) from err diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py index 85e8126c1..df63b1680 100644 --- a/src/documents/signals/handlers.py +++ b/src/documents/signals/handlers.py @@ -378,6 +378,11 @@ def validate_move(instance, old_path, new_path): raise CannotMoveFilesException +@receiver(models.signals.post_save, sender=CustomFieldInstance) +def update_custom_field_instance(sender, instance: CustomFieldInstance, **kwargs): + doc = Document.objects.get(pk=instance.document.pk) + update_filename_and_move_files(sender,doc) + @receiver(models.signals.m2m_changed, sender=Document.tags.through) @receiver(models.signals.post_save, sender=Document) def update_filename_and_move_files(sender, instance: Document, **kwargs):