57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
from django import forms
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from dal import autocomplete
|
|
|
|
from .models import Certification, CertificationDefinition
|
|
|
|
|
|
class CertificationForm(forms.ModelForm):
|
|
certification_definition = forms.ModelChoiceField(
|
|
queryset=CertificationDefinition.objects.all()
|
|
)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
instance = kwargs.get("instance")
|
|
if instance:
|
|
self.fields[
|
|
"certification_definition"
|
|
].initial = instance.certification_version.definition
|
|
|
|
def clean(self):
|
|
cleaned_data = super().clean()
|
|
certification_version = cleaned_data.get("certification_version")
|
|
certification_definition = cleaned_data.get("certification_definition")
|
|
|
|
if certification_version is None:
|
|
raise ValidationError("Certification Version missing")
|
|
if certification_definition is None:
|
|
raise ValidationError("Certification Definition missing")
|
|
|
|
if certification_version.definition != certification_definition:
|
|
raise ValidationError(
|
|
"Certification Version did not match Certification Definition!"
|
|
)
|
|
|
|
class Media:
|
|
js = ("paperwork/certification-form-clear-autocomplete.js",)
|
|
|
|
class Meta:
|
|
model = Certification
|
|
fields = [
|
|
"certification_definition",
|
|
"certification_version",
|
|
"name",
|
|
"member",
|
|
"certified_by",
|
|
"date",
|
|
"notes",
|
|
]
|
|
widgets = {
|
|
"certification_version": autocomplete.ModelSelect2(
|
|
url="paperwork:certification_version_autocomplete",
|
|
forward=["certification_definition"],
|
|
)
|
|
}
|