This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/passbook/flows/forms.py

75 lines
2 KiB
Python
Raw Normal View History

2020-05-08 17:46:39 +00:00
"""Flow and Stage forms"""
2019-10-07 14:33:48 +00:00
2020-05-08 16:29:18 +00:00
from django import forms
from django.forms import ValidationError
2020-05-08 16:29:18 +00:00
from django.utils.translation import gettext_lazy as _
from passbook.flows.models import Flow, FlowStageBinding, Stage
from passbook.flows.transfer.importer import FlowImporter
from passbook.lib.widgets import GroupedModelChoiceField
2020-05-08 16:29:18 +00:00
class FlowForm(forms.ModelForm):
"""Flow Form"""
class Meta:
model = Flow
fields = [
"name",
2020-08-28 13:23:03 +00:00
"title",
2020-05-08 16:29:18 +00:00
"slug",
"designation",
]
2020-05-13 09:57:10 +00:00
help_texts = {
2020-08-28 13:23:03 +00:00
"title": _("Shown as the Title in Flow pages."),
2020-05-13 09:57:10 +00:00
"slug": _("Visible in the URL."),
"designation": _(
(
"Decides what this Flow is used for. For example, the Authentication flow "
"is redirect to when an un-authenticated user visits passbook."
)
),
}
2020-05-08 16:29:18 +00:00
widgets = {
"name": forms.TextInput(),
"title": forms.TextInput(),
2020-05-08 16:29:18 +00:00
}
2020-05-08 17:46:39 +00:00
class FlowStageBindingForm(forms.ModelForm):
"""FlowStageBinding Form"""
2020-05-08 16:29:18 +00:00
stage = GroupedModelChoiceField(
queryset=Stage.objects.all().select_subclasses(), to_field_name="stage_uuid"
)
2020-05-08 16:29:18 +00:00
class Meta:
2020-05-08 17:46:39 +00:00
model = FlowStageBinding
2020-05-08 16:29:18 +00:00
fields = [
"target",
2020-05-08 17:46:39 +00:00
"stage",
"evaluate_on_plan",
"evaluate_on_call",
2020-05-08 16:29:18 +00:00
"order",
]
widgets = {
"name": forms.TextInput(),
}
class FlowImportForm(forms.Form):
"""Form used for flow importing"""
flow = forms.FileField()
def clean_flow(self):
"""Check if the flow is valid and rewind the file to the start"""
flow = self.cleaned_data["flow"].read()
valid = FlowImporter(flow.decode()).validate()
if not valid:
raise ValidationError(_("Flow invalid."))
self.cleaned_data["flow"].seek(0)
return self.cleaned_data["flow"]