authentik.endpoints.api.connectors
1from rest_framework import mixins 2from rest_framework.fields import SerializerMethodField 3from rest_framework.viewsets import GenericViewSet 4 5from authentik.core.api.object_types import TypesMixin 6from authentik.core.api.used_by import UsedByMixin 7from authentik.core.api.utils import MetaNameSerializer, ModelSerializer 8from authentik.endpoints.models import Connector 9 10 11class ConnectorSerializer(ModelSerializer, MetaNameSerializer): 12 13 component = SerializerMethodField() 14 15 def get_component(self, obj: Connector) -> str: # pragma: no cover 16 """Get object component so that we know how to edit the object""" 17 if obj.__class__ == Connector: 18 return "" 19 return obj.component 20 21 class Meta: 22 model = Connector 23 fields = [ 24 "connector_uuid", 25 "name", 26 "enabled", 27 "component", 28 "verbose_name", 29 "verbose_name_plural", 30 "meta_model_name", 31 ] 32 33 34class ConnectorViewSet( 35 TypesMixin, 36 mixins.RetrieveModelMixin, 37 mixins.DestroyModelMixin, 38 UsedByMixin, 39 mixins.ListModelMixin, 40 GenericViewSet, 41): 42 """Connector Viewset""" 43 44 queryset = Connector.objects.none() 45 serializer_class = ConnectorSerializer 46 search_fields = [ 47 "name", 48 ] 49 50 def get_queryset(self): # pragma: no cover 51 return Connector.objects.select_subclasses()
12class ConnectorSerializer(ModelSerializer, MetaNameSerializer): 13 14 component = SerializerMethodField() 15 16 def get_component(self, obj: Connector) -> str: # pragma: no cover 17 """Get object component so that we know how to edit the object""" 18 if obj.__class__ == Connector: 19 return "" 20 return obj.component 21 22 class Meta: 23 model = Connector 24 fields = [ 25 "connector_uuid", 26 "name", 27 "enabled", 28 "component", 29 "verbose_name", 30 "verbose_name_plural", 31 "meta_model_name", 32 ]
A ModelSerializer is just a regular Serializer, except that:
- A set of default fields are automatically populated.
- A set of default validators are automatically populated.
- Default
.create()and.update()implementations are provided.
The process of automatically determining a set of serializer fields based on the model fields is reasonably complex, but you almost certainly don't need to dig into the implementation.
If the ModelSerializer class doesn't generate the set of fields that
you need you should either declare the extra/differing fields explicitly on
the serializer class, or simply use a Serializer class.
22 class Meta: 23 model = Connector 24 fields = [ 25 "connector_uuid", 26 "name", 27 "enabled", 28 "component", 29 "verbose_name", 30 "verbose_name_plural", 31 "meta_model_name", 32 ]
35class ConnectorViewSet( 36 TypesMixin, 37 mixins.RetrieveModelMixin, 38 mixins.DestroyModelMixin, 39 UsedByMixin, 40 mixins.ListModelMixin, 41 GenericViewSet, 42): 43 """Connector Viewset""" 44 45 queryset = Connector.objects.none() 46 serializer_class = ConnectorSerializer 47 search_fields = [ 48 "name", 49 ] 50 51 def get_queryset(self): # pragma: no cover 52 return Connector.objects.select_subclasses()
Connector Viewset
Get the list of items for this view.
This must be an iterable, and may be a queryset.
Defaults to using self.queryset.
This method should always be used rather than accessing self.queryset
directly, as self.queryset gets evaluated only once, and those results
are cached for all subsequent requests.
You may want to override this if you need to provide different querysets depending on the incoming request.
(Eg. return a list of items that is specific to the user)