authentik.enterprise.lifecycle.review.utils
1from datetime import datetime 2from urllib import parse 3 4from django.contrib.contenttypes.models import ContentType 5from django.db.models import Model 6from django.urls import reverse 7from rest_framework.serializers import ChoiceField, Serializer, UUIDField 8 9from authentik.core.models import Application, Group 10from authentik.rbac.models import Role 11 12 13def parse_content_type(value: str) -> dict: 14 app_label, model = value.split(".") 15 return {"app_label": app_label, "model": model} 16 17 18def model_choices() -> list[tuple[str, str]]: 19 return [ 20 ("authentik_core.application", "Application"), 21 ("authentik_core.group", "Group"), 22 ("authentik_rbac.role", "Role"), 23 ] 24 25 26def admin_link_for_model(model: Model) -> str: 27 if isinstance(model, Application): 28 url = f"/core/applications/{model.slug}" 29 elif isinstance(model, Group): 30 url = f"/identity/groups/{model.pk}" 31 elif isinstance(model, Role): 32 url = f"/identity/roles/{model.pk}" 33 else: 34 raise TypeError("Unsupported model") 35 return url + ";" + parse.quote('{"page":"page-lifecycle"}') 36 37 38def link_for_model(model: Model) -> str: 39 return f"{reverse("authentik_core:if-admin")}#{admin_link_for_model(model)}" 40 41 42def start_of_day(dt: datetime) -> datetime: 43 return dt.replace(hour=0, minute=0, second=0, microsecond=0) 44 45 46class ContentTypeField(ChoiceField): 47 def __init__(self, **kwargs): 48 super().__init__(choices=model_choices(), **kwargs) 49 50 def to_representation(self, content_type: ContentType) -> str: 51 return f"{content_type.app_label}.{content_type.model}" 52 53 def to_internal_value(self, data: str) -> ContentType: 54 return ContentType.objects.get(**parse_content_type(data)) 55 56 57class GenericForeignKeySerializer(Serializer): 58 content_type = ContentTypeField() 59 object_id = UUIDField()
27def admin_link_for_model(model: Model) -> str: 28 if isinstance(model, Application): 29 url = f"/core/applications/{model.slug}" 30 elif isinstance(model, Group): 31 url = f"/identity/groups/{model.pk}" 32 elif isinstance(model, Role): 33 url = f"/identity/roles/{model.pk}" 34 else: 35 raise TypeError("Unsupported model") 36 return url + ";" + parse.quote('{"page":"page-lifecycle"}')
47class ContentTypeField(ChoiceField): 48 def __init__(self, **kwargs): 49 super().__init__(choices=model_choices(), **kwargs) 50 51 def to_representation(self, content_type: ContentType) -> str: 52 return f"{content_type.app_label}.{content_type.model}" 53 54 def to_internal_value(self, data: str) -> ContentType: 55 return ContentType.objects.get(**parse_content_type(data))
616 def __new__(cls, *args, **kwargs): 617 """ 618 When a field is instantiated, we store the arguments that were used, 619 so that we can present a helpful representation of the object. 620 """ 621 instance = super().__new__(cls) 622 instance._args = args 623 instance._kwargs = kwargs 624 return instance
When a field is instantiated, we store the arguments that were used, so that we can present a helpful representation of the object.
58class GenericForeignKeySerializer(Serializer): 59 content_type = ContentTypeField() 60 object_id = UUIDField()
The BaseSerializer class provides a minimal class which may be used for writing custom serializer implementations.
Note that we strongly restrict the ordering of operations/properties that may be used on the serializer in order to enforce correct usage.
In particular, if a data= argument is passed then:
.is_valid() - Available.
.initial_data - Available.
.validated_data - Only available after calling is_valid()
.errors - Only available after calling is_valid()
.data - Only available after calling is_valid()
If a data= argument is not passed then:
.is_valid() - Not available. .initial_data - Not available. .validated_data - Not available. .errors - Not available. .data - Available.