Source code for intranet.apps.users.forms

from django import forms
from django.contrib.auth import get_user_model

from .models import Address


[docs]class ProfileEditForm(forms.ModelForm): """A form containing editable fields in the User model.""" GENDERS = (("male", "Male"), ("female", "Female"), ("non-binary", "Non-Binary")) admin_comments = forms.CharField(label="Admin Comments", widget=forms.Textarea, required=False) student_id = forms.IntegerField(label="FCPS Student ID", required=False) first_name = forms.CharField(label="First Name") middle_name = forms.CharField(label="Middle Name", required=False) last_name = forms.CharField(label="Last Name") nickname = forms.CharField(label="Nickname", required=False) graduation_year = forms.IntegerField(label="Graduation Year", required=False) gender = forms.ChoiceField(choices=GENDERS, label="Sex (Male, Female, or Non-Binary)") counselor_id = forms.IntegerField(label="Counselor ID", required=False)
[docs] class Meta: model = get_user_model() fields = ["admin_comments", "student_id", "first_name", "middle_name", "last_name", "title", "nickname", "graduation_year", "gender"]
[docs]class UserChoiceField(forms.ModelChoiceField): """A ModelChoiceField that returns a user's full name instead of their TJ username (which is the default string representation)."""
[docs] def label_from_instance(self, obj): return obj.full_name
[docs]class SortedUserChoiceField(forms.ModelChoiceField): """A ModelChoiceField that returns a user's Last, First name instead of their TJ username (which is the default string representation)."""
[docs] def label_from_instance(self, obj): return obj.last_first_id
[docs]class UserMultipleChoiceField(forms.ModelMultipleChoiceField): """A ModelMultipleChoiceField that returns a user's full name instead of their TJ username (which is the default string representation)."""
[docs] def label_from_instance(self, obj): return obj.full_name
[docs]class SortedUserMultipleChoiceField(forms.ModelMultipleChoiceField): """A ModelMultipleChoiceField that returns a user's Last, First name instead of their TJ username (which is the default string representation)."""
[docs] def label_from_instance(self, obj): return obj.last_first_id
[docs]class SortedTeacherMultipleChoiceField(forms.ModelMultipleChoiceField): """A ModelMultipleChoiceField that returns a user's Last, First initial instead of their TJ username (which is the default string representation).""" def __init__(self, *args, **kwargs): self.show_username = False if "show_username" in kwargs: self.show_username = kwargs["show_username"] del kwargs["show_username"] super().__init__(*args, **kwargs)
[docs] def label_from_instance(self, obj): name = obj.last_first_initial return "{} ({})".format(name, obj.username) if self.show_username else name
[docs]class AddressForm(forms.ModelForm): """Form for user address""" street = forms.CharField(label="Street") city = forms.CharField(label="City") state = forms.CharField(label="State") postal_code = forms.CharField(label="ZIP")
[docs] class Meta: model = Address fields = ["street", "city", "state", "postal_code"]