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 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 f"{name} ({obj.username})" if self.show_username else name