2020-02-18 14:12:22 +00:00
|
|
|
"""passbook Expression Policy forms"""
|
|
|
|
|
|
|
|
from django import forms
|
|
|
|
|
2020-05-20 11:00:45 +00:00
|
|
|
from passbook.admin.fields import CodeMirrorWidget
|
2020-06-05 10:00:27 +00:00
|
|
|
from passbook.policies.expression.evaluator import PolicyEvaluator
|
2020-02-18 14:12:22 +00:00
|
|
|
from passbook.policies.expression.models import ExpressionPolicy
|
|
|
|
from passbook.policies.forms import GENERAL_FIELDS
|
|
|
|
|
|
|
|
|
|
|
|
class ExpressionPolicyForm(forms.ModelForm):
|
|
|
|
"""ExpressionPolicy Form"""
|
|
|
|
|
|
|
|
template_name = "policy/expression/form.html"
|
|
|
|
|
2020-05-20 14:13:36 +00:00
|
|
|
def clean_expression(self):
|
2020-06-05 10:00:27 +00:00
|
|
|
"""Test Syntax"""
|
2020-05-20 14:13:36 +00:00
|
|
|
expression = self.cleaned_data.get("expression")
|
2020-06-05 10:00:27 +00:00
|
|
|
PolicyEvaluator(self.instance.name).validate(expression)
|
2020-05-20 14:13:36 +00:00
|
|
|
return expression
|
|
|
|
|
2020-02-18 14:12:22 +00:00
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = ExpressionPolicy
|
|
|
|
fields = GENERAL_FIELDS + [
|
|
|
|
"expression",
|
|
|
|
]
|
|
|
|
widgets = {
|
|
|
|
"name": forms.TextInput(),
|
2020-06-05 10:00:27 +00:00
|
|
|
"expression": CodeMirrorWidget(mode="python"),
|
2020-02-18 14:12:22 +00:00
|
|
|
}
|