authentik.endpoints.api.device_fact_snapshots

 1from enum import StrEnum
 2
 3from rest_framework.fields import SerializerMethodField
 4
 5from authentik.core.api.utils import ModelSerializer
 6from authentik.endpoints.controller import MERGED_VENDOR
 7from authentik.endpoints.facts import DeviceFacts
 8from authentik.endpoints.models import Connector, DeviceFactSnapshot
 9from authentik.lib.utils.reflection import all_subclasses
10
11
12def get_vendor_choices():
13    choices = [(MERGED_VENDOR, MERGED_VENDOR)]
14    for connector_type in all_subclasses(Connector):
15        ident = connector_type().controller.vendor_identifier()
16        choices.append((ident, ident))
17    return choices
18
19
20vendors = StrEnum("DeviceConnectorVendors", get_vendor_choices())
21
22
23class DeviceFactSnapshotSerializer(ModelSerializer):
24
25    data = DeviceFacts()
26    vendor = SerializerMethodField()
27
28    def get_vendor(self, instance: DeviceFactSnapshot) -> vendors:
29        return self.context.get("vendor", MERGED_VENDOR)
30
31    class Meta:
32        model = DeviceFactSnapshot
33        fields = [
34            "data",
35            "connection",
36            "created",
37            "expires",
38            "vendor",
39        ]
40        extra_kwargs = {
41            "created": {"read_only": True},
42            "expires": {"read_only": True},
43        }
def get_vendor_choices():
13def get_vendor_choices():
14    choices = [(MERGED_VENDOR, MERGED_VENDOR)]
15    for connector_type in all_subclasses(Connector):
16        ident = connector_type().controller.vendor_identifier()
17        choices.append((ident, ident))
18    return choices
vendors = <enum 'DeviceConnectorVendors'>
class DeviceFactSnapshotSerializer(authentik.core.api.utils.ModelSerializer):
24class DeviceFactSnapshotSerializer(ModelSerializer):
25
26    data = DeviceFacts()
27    vendor = SerializerMethodField()
28
29    def get_vendor(self, instance: DeviceFactSnapshot) -> vendors:
30        return self.context.get("vendor", MERGED_VENDOR)
31
32    class Meta:
33        model = DeviceFactSnapshot
34        fields = [
35            "data",
36            "connection",
37            "created",
38            "expires",
39            "vendor",
40        ]
41        extra_kwargs = {
42            "created": {"read_only": True},
43            "expires": {"read_only": True},
44        }

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.

data
584    @property
585    def data(self):
586        ret = super().data
587        return ReturnDict(ret, serializer=self)
vendor
def get_vendor( self, instance: authentik.endpoints.models.DeviceFactSnapshot) -> authentik.endpoints.api.device_fact_snapshots.DeviceConnectorVendors:
29    def get_vendor(self, instance: DeviceFactSnapshot) -> vendors:
30        return self.context.get("vendor", MERGED_VENDOR)
class DeviceFactSnapshotSerializer.Meta:
32    class Meta:
33        model = DeviceFactSnapshot
34        fields = [
35            "data",
36            "connection",
37            "created",
38            "expires",
39            "vendor",
40        ]
41        extra_kwargs = {
42            "created": {"read_only": True},
43            "expires": {"read_only": True},
44        }
fields = ['data', 'connection', 'created', 'expires', 'vendor']
extra_kwargs = {'created': {'read_only': True}, 'expires': {'read_only': True}}