from django.db.models.signals import m2m_changed
from django.db.models.signals import post_save
from filelock import FileLock
+from guardian.shortcuts import clear_ct_cache
from documents.file_handling import create_source_path_directory
from documents.management.commands.base import PaperlessCommand
self.stdout.write(self.style.ERROR(self._import_error_context_message()))
raise
+ # ContentType/Permission rows were deleted and reinserted above; stale
+ # in-process caches must be invalidated so permission checks use the
+ # new IDs rather than pre-import PKs.
+ ContentType.objects.clear_cache()
+ clear_ct_cache()
+
def handle(self, *args, **options) -> None:
logging.getLogger().handlers[0].level = logging.ERROR
import filelock
import pytest
from django.contrib.auth import get_user_model
+from django.contrib.contenttypes.models import ContentType
+from guardian.shortcuts import clear_ct_cache
from pytest_django.fixtures import SettingsWrapper
from rest_framework.test import APIClient
return rest_api_client
+@pytest.fixture(autouse=True)
+def _clear_content_type_caches() -> None:
+ """Clear Django's ContentType cache and guardian's lru_cache before each test.
+
+ Tests that delete and reinsert ContentType/Permission rows (e.g. the
+ importer) corrupt both caches. Without this fixture a subsequent test on
+ the same xdist worker sees stale ContentType objects and guardian raises
+ MixedContentTypeError.
+ """
+ ContentType.objects.clear_cache()
+ clear_ct_cache()
+
+
@pytest.fixture(scope="session", autouse=True)
def faker_session_locale():
"""Set Faker locale for reproducibility."""