2023-04-01 11:37:49 -04:00
|
|
|
from django import forms
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
|
2024-05-04 16:38:51 -04:00
|
|
|
from dal.autocomplete import ModelSelect2
|
2024-01-17 21:17:24 -05:00
|
|
|
|
|
|
|
from .models import Certification, CertificationDefinition
|
2023-04-01 11:37:49 -04:00
|
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
2023-12-22 15:41:16 -05:00
|
|
|
if certification_version is None:
|
|
|
|
raise ValidationError("Certification Version missing")
|
|
|
|
if certification_definition is None:
|
|
|
|
raise ValidationError("Certification Definition missing")
|
|
|
|
|
2023-04-01 11:37:49 -04:00
|
|
|
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 = {
|
2024-05-04 16:38:51 -04:00
|
|
|
"certification_version": ModelSelect2(
|
2023-04-01 11:37:49 -04:00
|
|
|
url="paperwork:certification_version_autocomplete",
|
|
|
|
forward=["certification_definition"],
|
|
|
|
)
|
|
|
|
}
|