Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions documentcloud/documents/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,23 @@ def test_modification_marks_cache_dirty(self, client, mocker):
document.refresh_from_db()
assert document.cache_dirty is True

def test_modification_queued_after_commit(self, client, mocker):
"""The modify job must not start before `pending` is committed - its first
processing PATCH would otherwise read the stale status and save it back
over `pending`"""
document = DocumentFactory(page_count=2)
mock_modify = mocker.patch("documentcloud.documents.views.modify.delay")
client.force_authenticate(user=document.user)
response = client.post(
f"/api/documents/{document.pk}/modifications/",
[{"page": "0"}],
format="json",
)
assert response.status_code == status.HTTP_201_CREATED
mock_modify.assert_not_called()
run_commit_hooks()
mock_modify.assert_called_once()

def _finish_processing(self, client, document):
"""Send the processing callback that marks a document as done"""
client.force_authenticate(user=None)
Expand Down
8 changes: 3 additions & 5 deletions documentcloud/documents/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1971,11 +1971,9 @@ def create(self, request, *args, **kwargs):
document.save()
document.index_on_commit(field_updates={"status": "set"})

modify.delay(
document.pk,
self.request.user.pk,
serializer.data,
)
transaction.on_commit(
lambda: modify.delay(document.pk, self.request.user.pk, serializer.data)
)
return Response(serializer.data, status=status.HTTP_201_CREATED)

@extend_schema(tags=["document_modifications"])
Expand Down
Loading