cmsmanage/paperwork/forms.py

57 lines
1.8 KiB
Python
Raw Normal View History

from django import forms
from django.core.exceptions import ValidationError
2024-05-04 16:38:51 -04:00
from dal.autocomplete import ModelSelect2
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 = {
2024-05-04 16:38:51 -04:00
"certification_version": ModelSelect2(
url="paperwork:certification_version_autocomplete",
forward=["certification_definition"],
)
}