From 79092c27c562a1f5bbd8bf9b4b6f4b22bd93626a Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Mon, 28 Oct 2024 07:02:09 -0700 Subject: [PATCH] Fix: Make customfieldinstance soft delete, fix filepath when deleted (#8067) --- ...customfieldinstance_deleted_at_and_more.py | 58 +++++++++++++++++++ src/documents/models.py | 6 +- src/documents/serialisers.py | 2 + src/documents/templating/filepath.py | 2 +- 4 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/documents/migrations/1056_customfieldinstance_deleted_at_and_more.py diff --git a/src/documents/migrations/1056_customfieldinstance_deleted_at_and_more.py b/src/documents/migrations/1056_customfieldinstance_deleted_at_and_more.py new file mode 100644 index 000000000..eba1e4281 --- /dev/null +++ b/src/documents/migrations/1056_customfieldinstance_deleted_at_and_more.py @@ -0,0 +1,58 @@ +# Generated by Django 5.1.2 on 2024-10-28 01:55 + +from django.db import migrations +from django.db import models + + +class Migration(migrations.Migration): + dependencies = [ + ("documents", "1055_alter_storagepath_path"), + ] + + operations = [ + migrations.AddField( + model_name="customfieldinstance", + name="deleted_at", + field=models.DateTimeField(blank=True, null=True), + ), + migrations.AddField( + model_name="customfieldinstance", + name="restored_at", + field=models.DateTimeField(blank=True, null=True), + ), + migrations.AddField( + model_name="customfieldinstance", + name="transaction_id", + field=models.UUIDField(blank=True, null=True), + ), + migrations.AddField( + model_name="note", + name="deleted_at", + field=models.DateTimeField(blank=True, null=True), + ), + migrations.AddField( + model_name="note", + name="restored_at", + field=models.DateTimeField(blank=True, null=True), + ), + migrations.AddField( + model_name="note", + name="transaction_id", + field=models.UUIDField(blank=True, null=True), + ), + migrations.AddField( + model_name="sharelink", + name="deleted_at", + field=models.DateTimeField(blank=True, null=True), + ), + migrations.AddField( + model_name="sharelink", + name="restored_at", + field=models.DateTimeField(blank=True, null=True), + ), + migrations.AddField( + model_name="sharelink", + name="transaction_id", + field=models.UUIDField(blank=True, null=True), + ), + ] diff --git a/src/documents/models.py b/src/documents/models.py index 37c86305c..4528d5127 100644 --- a/src/documents/models.py +++ b/src/documents/models.py @@ -710,7 +710,7 @@ class PaperlessTask(models.Model): return f"Task {self.task_id}" -class Note(models.Model): +class Note(SoftDeleteModel): note = models.TextField( _("content"), blank=True, @@ -750,7 +750,7 @@ class Note(models.Model): return self.note -class ShareLink(models.Model): +class ShareLink(SoftDeleteModel): class FileVersion(models.TextChoices): ARCHIVE = ("archive", _("Archive")) ORIGINAL = ("original", _("Original")) @@ -866,7 +866,7 @@ class CustomField(models.Model): return f"{self.name} : {self.data_type}" -class CustomFieldInstance(models.Model): +class CustomFieldInstance(SoftDeleteModel): """ A single instance of a field, attached to a CustomField for the name and type and attached to a single Document to be metadata for it diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 6f7dc8be0..e08daff09 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -855,6 +855,8 @@ class DocumentSerializer( super().update(instance, validated_data) else: super().update(instance, validated_data) + # hard delete custom field instances that were soft deleted + CustomFieldInstance.deleted_objects.filter(document=instance).delete() return instance def __init__(self, *args, **kwargs): diff --git a/src/documents/templating/filepath.py b/src/documents/templating/filepath.py index afb43ff4d..108ad0c81 100644 --- a/src/documents/templating/filepath.py +++ b/src/documents/templating/filepath.py @@ -296,7 +296,7 @@ def validate_filepath_template_and_render( else: # or use the real document information tags_list = document.tags.order_by("name").all() - custom_fields = document.custom_fields.all() + custom_fields = CustomFieldInstance.global_objects.filter(document=document) # Build the context dictionary context = (