Debian Codegraph @

src/polymorphic/contrib/guardian.py

raw
1 from typing import Any
2
3 from django.contrib.contenttypes.models import ContentType
4
5 from ..models import PolymorphicModel
6 from ..utils import get_base_polymorphic_model
7
8
9 def get_polymorphic_base_content_type(obj: Any) -> ContentType:
10 """
11 Helper function to return the base polymorphic content type id. This should used
12 with django-guardian and the ``GUARDIAN_GET_CONTENT_TYPE`` option.
13
14 See the django-guardian documentation for more information:
15
16 https://django-guardian.readthedocs.io/en/latest/configuration
17 """
18 model_type = obj if isinstance(obj, type) else type(obj)
19 if issubclass(model_type, PolymorphicModel) and (
20 base := get_base_polymorphic_model(model_type)
21 ):
22 return ContentType.objects.get_for_model(base)
23 return ContentType.objects.get_for_model(model_type)