Source code for intranet.apps.eighth.forms.admin.sponsors

from django import forms
from django.contrib.auth import get_user_model
from django.core.exceptions import ValidationError

from ...models import EighthSponsor


[docs]class AutoCreateUserField(forms.ChoiceField): widget = forms.TextInput
[docs] def clean(self, value): if value in self.empty_values: return None try: id_value = int(value) except (ValueError, TypeError) as e: raise ValidationError(self.error_messages["invalid_choice"], code="invalid_choice", params={"value": value}) from e try: user = get_user_model().objects.get(id=id_value) except get_user_model().DoesNotExist as e: raise ValidationError(self.error_messages["invalid_choice"], code="invalid_choice", params={"value": value}) from e return user
[docs]class SponsorForm(forms.ModelForm): user = AutoCreateUserField(label="User ID", required=False)
[docs] class Meta: model = EighthSponsor fields = ["first_name", "last_name", "user", "department", "full_time", "online_attendance", "contracted_eighth", "show_full_name"]