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():
vendors =
<enum 'DeviceConnectorVendors'>
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.
def
get_vendor( self, instance: authentik.endpoints.models.DeviceFactSnapshot) -> authentik.endpoints.api.device_fact_snapshots.DeviceConnectorVendors:
Inherited Members
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 }
model =
<class 'authentik.endpoints.models.DeviceFactSnapshot'>