52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
from dal import autocomplete
|
|
|
|
from django import forms
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from .models import CertificationDefinition, Certification
|
|
|
|
|
|
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.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"],
|
|
)
|
|
}
|