authentik.stages.email.api

EmailStage API Views

 1"""EmailStage API Views"""
 2
 3from drf_spectacular.utils import extend_schema
 4from rest_framework.decorators import action
 5from rest_framework.request import Request
 6from rest_framework.response import Response
 7from rest_framework.serializers import ValidationError
 8from rest_framework.viewsets import ModelViewSet
 9
10from authentik.core.api.object_types import TypeCreateSerializer
11from authentik.core.api.used_by import UsedByMixin
12from authentik.flows.api.stages import StageSerializer
13from authentik.stages.email.models import EmailStage, get_template_choices
14
15
16class EmailStageSerializer(StageSerializer):
17    """EmailStage Serializer"""
18
19    def __init__(self, *args, **kwargs):
20        super().__init__(*args, **kwargs)
21        self.fields["template"].choices = get_template_choices()
22
23    def validate_template(self, value: str) -> str:
24        """Check validity of template"""
25        choices = get_template_choices()
26        for path, _ in choices:
27            if path == value:
28                return value
29        raise ValidationError(f"Invalid template '{value}' specified.")
30
31    class Meta:
32        model = EmailStage
33        fields = StageSerializer.Meta.fields + [
34            "use_global_settings",
35            "host",
36            "port",
37            "username",
38            "password",
39            "use_tls",
40            "use_ssl",
41            "timeout",
42            "from_address",
43            "token_expiry",
44            "subject",
45            "template",
46            "activate_user_on_success",
47            "recovery_max_attempts",
48            "recovery_cache_timeout",
49        ]
50        extra_kwargs = {"password": {"write_only": True}}
51
52
53class EmailStageViewSet(UsedByMixin, ModelViewSet):
54    """EmailStage Viewset"""
55
56    queryset = EmailStage.objects.all()
57    serializer_class = EmailStageSerializer
58    filterset_fields = [
59        "name",
60        "use_global_settings",
61        "host",
62        "port",
63        "username",
64        "use_tls",
65        "use_ssl",
66        "timeout",
67        "from_address",
68        "token_expiry",
69        "subject",
70        "template",
71        "activate_user_on_success",
72    ]
73    search_fields = ["name"]
74    ordering = ["name"]
75
76    @extend_schema(responses={200: TypeCreateSerializer(many=True)})
77    @action(detail=False, pagination_class=None, filter_backends=[])
78    def templates(self, request: Request) -> Response:
79        """Get all available templates, including custom templates"""
80        choices = []
81        for value, label in get_template_choices():
82            choices.append(
83                {
84                    "name": value,
85                    "description": label,
86                    "component": "",
87                    "model_name": "",
88                }
89            )
90        return Response(TypeCreateSerializer(choices, many=True).data)
class EmailStageSerializer(authentik.flows.api.stages.StageSerializer):
17class EmailStageSerializer(StageSerializer):
18    """EmailStage Serializer"""
19
20    def __init__(self, *args, **kwargs):
21        super().__init__(*args, **kwargs)
22        self.fields["template"].choices = get_template_choices()
23
24    def validate_template(self, value: str) -> str:
25        """Check validity of template"""
26        choices = get_template_choices()
27        for path, _ in choices:
28            if path == value:
29                return value
30        raise ValidationError(f"Invalid template '{value}' specified.")
31
32    class Meta:
33        model = EmailStage
34        fields = StageSerializer.Meta.fields + [
35            "use_global_settings",
36            "host",
37            "port",
38            "username",
39            "password",
40            "use_tls",
41            "use_ssl",
42            "timeout",
43            "from_address",
44            "token_expiry",
45            "subject",
46            "template",
47            "activate_user_on_success",
48            "recovery_max_attempts",
49            "recovery_cache_timeout",
50        ]
51        extra_kwargs = {"password": {"write_only": True}}

EmailStage Serializer

EmailStageSerializer(*args, **kwargs)
20    def __init__(self, *args, **kwargs):
21        super().__init__(*args, **kwargs)
22        self.fields["template"].choices = get_template_choices()
def validate_template(self, value: str) -> str:
24    def validate_template(self, value: str) -> str:
25        """Check validity of template"""
26        choices = get_template_choices()
27        for path, _ in choices:
28            if path == value:
29                return value
30        raise ValidationError(f"Invalid template '{value}' specified.")

Check validity of template

class EmailStageSerializer.Meta:
32    class Meta:
33        model = EmailStage
34        fields = StageSerializer.Meta.fields + [
35            "use_global_settings",
36            "host",
37            "port",
38            "username",
39            "password",
40            "use_tls",
41            "use_ssl",
42            "timeout",
43            "from_address",
44            "token_expiry",
45            "subject",
46            "template",
47            "activate_user_on_success",
48            "recovery_max_attempts",
49            "recovery_cache_timeout",
50        ]
51        extra_kwargs = {"password": {"write_only": True}}
fields = ['pk', 'name', 'component', 'verbose_name', 'verbose_name_plural', 'meta_model_name', 'flow_set', 'use_global_settings', 'host', 'port', 'username', 'password', 'use_tls', 'use_ssl', 'timeout', 'from_address', 'token_expiry', 'subject', 'template', 'activate_user_on_success', 'recovery_max_attempts', 'recovery_cache_timeout']
extra_kwargs = {'password': {'write_only': True}}
class EmailStageViewSet(authentik.core.api.used_by.UsedByMixin, rest_framework.viewsets.ModelViewSet):
54class EmailStageViewSet(UsedByMixin, ModelViewSet):
55    """EmailStage Viewset"""
56
57    queryset = EmailStage.objects.all()
58    serializer_class = EmailStageSerializer
59    filterset_fields = [
60        "name",
61        "use_global_settings",
62        "host",
63        "port",
64        "username",
65        "use_tls",
66        "use_ssl",
67        "timeout",
68        "from_address",
69        "token_expiry",
70        "subject",
71        "template",
72        "activate_user_on_success",
73    ]
74    search_fields = ["name"]
75    ordering = ["name"]
76
77    @extend_schema(responses={200: TypeCreateSerializer(many=True)})
78    @action(detail=False, pagination_class=None, filter_backends=[])
79    def templates(self, request: Request) -> Response:
80        """Get all available templates, including custom templates"""
81        choices = []
82        for value, label in get_template_choices():
83            choices.append(
84                {
85                    "name": value,
86                    "description": label,
87                    "component": "",
88                    "model_name": "",
89                }
90            )
91        return Response(TypeCreateSerializer(choices, many=True).data)

EmailStage Viewset

queryset = <InheritanceQuerySet []>
serializer_class = <class 'EmailStageSerializer'>
filterset_fields = ['name', 'use_global_settings', 'host', 'port', 'username', 'use_tls', 'use_ssl', 'timeout', 'from_address', 'token_expiry', 'subject', 'template', 'activate_user_on_success']
search_fields = ['name']
ordering = ['name']
@extend_schema(responses={200: TypeCreateSerializer(many=True)})
@action(detail=False, pagination_class=None, filter_backends=[])
def templates( self, request: rest_framework.request.Request) -> rest_framework.response.Response:
77    @extend_schema(responses={200: TypeCreateSerializer(many=True)})
78    @action(detail=False, pagination_class=None, filter_backends=[])
79    def templates(self, request: Request) -> Response:
80        """Get all available templates, including custom templates"""
81        choices = []
82        for value, label in get_template_choices():
83            choices.append(
84                {
85                    "name": value,
86                    "description": label,
87                    "component": "",
88                    "model_name": "",
89                }
90            )
91        return Response(TypeCreateSerializer(choices, many=True).data)

Get all available templates, including custom templates

name = None
description = None
suffix = None
detail = None
basename = None