🗃️(backend) make document/template creation atomic

When creating a new document/template via the API, we add the
logged-in user as owner of the created object. This should be
done atomically with the object creation to make sure we don't
end-up with an orphan object that the creator can't access
anymore.
This commit is contained in:
Samuel Paccoud - DINUM
2025-01-27 21:20:16 +01:00
committed by Anthony LC
parent 5042f4ca47
commit 5c5763a0ef
2 changed files with 3 additions and 9 deletions

View File

@@ -14,15 +14,6 @@ from django.core.exceptions import ValidationError
from django.core.files.storage import default_storage
from django.db import models as db
from django.db import transaction
from django.db.models import (
Exists,
F,
Func,
OuterRef,
Q,
Subquery,
Value,
)
from django.db.models.expressions import RawSQL
from django.db.models.functions import Left, Length
from django.http import Http404
@@ -579,6 +570,7 @@ class DocumentViewSet(
return drf.response.Response(serializer.data)
@transaction.atomic
def perform_create(self, serializer):
"""Set the current user as creator and owner of the newly created object."""
obj = models.Document.add_root(
@@ -1250,6 +1242,7 @@ class TemplateViewSet(
serializer = self.get_serializer(queryset, many=True)
return drf.response.Response(serializer.data)
@transaction.atomic
def perform_create(self, serializer):
"""Set the current user as owner of the newly created object."""
obj = serializer.save()

View File

@@ -85,6 +85,7 @@ def test_api_documents_trashbin_format():
"media_auth": True,
"move": False, # Can't move a deleted document
"partial_update": True,
"restore": True,
"retrieve": True,
"update": True,
"versions_destroy": True,