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
|
2020-11-15 21:42:02 +00:00
|
|
|
from django.core.validators import FileExtensionValidator
|
2020-08-28 13:06:25 +00:00
|
|
|
from django.forms import ValidationError
|
2020-05-08 16:29:18 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2020-07-04 13:02:21 +00:00
|
|
|
from passbook.flows.models import Flow, FlowStageBinding, Stage
|
2020-08-28 13:06:25 +00:00
|
|
|
from passbook.flows.transfer.importer import FlowImporter
|
2020-07-04 13:02:21 +00:00
|
|
|
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-12-02 13:24:24 +00:00
|
|
|
"background",
|
2020-05-08 16:29:18 +00:00
|
|
|
]
|
|
|
|
widgets = {
|
|
|
|
"name": forms.TextInput(),
|
2020-09-08 15:38:41 +00:00
|
|
|
"title": forms.TextInput(),
|
2020-12-02 13:24:24 +00:00
|
|
|
"background": forms.FileInput(),
|
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
|
|
|
|
2020-09-08 16:18:06 +00:00
|
|
|
stage = GroupedModelChoiceField(
|
|
|
|
queryset=Stage.objects.all().select_subclasses(), to_field_name="stage_uuid"
|
|
|
|
)
|
2020-07-04 13:02:21 +00:00
|
|
|
|
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 = [
|
2020-07-03 21:34:44 +00:00
|
|
|
"target",
|
2020-05-08 17:46:39 +00:00
|
|
|
"stage",
|
2020-10-20 13:06:36 +00:00
|
|
|
"evaluate_on_plan",
|
2020-10-20 13:41:50 +00:00
|
|
|
"re_evaluate_policies",
|
2020-05-08 16:29:18 +00:00
|
|
|
"order",
|
|
|
|
]
|
|
|
|
widgets = {
|
|
|
|
"name": forms.TextInput(),
|
|
|
|
}
|
2020-08-28 13:06:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FlowImportForm(forms.Form):
|
|
|
|
"""Form used for flow importing"""
|
|
|
|
|
2020-11-15 21:42:02 +00:00
|
|
|
flow = forms.FileField(
|
|
|
|
validators=[FileExtensionValidator(allowed_extensions=["pbflow"])]
|
|
|
|
)
|
2020-08-28 13:06:25 +00:00
|
|
|
|
|
|
|
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"]
|