diff --git a/intranet/apps/eighth/views/profile.py b/intranet/apps/eighth/views/profile.py index 5c3d4eae11..a65159f42b 100644 --- a/intranet/apps/eighth/views/profile.py +++ b/intranet/apps/eighth/views/profile.py @@ -34,10 +34,6 @@ def edit_profile_view(request, user_id=None): # address_form = AddressForm(request.POST, instance=user.properties.address) if user_form.is_valid(): user = user_form.save() - counselor_id = user_form.cleaned_data["counselor_id"] - if counselor_id: - counselor = get_user_model().objects.get(id=counselor_id) - user.counselor = counselor # user.properties.address = address_form.save() # user.properties.save() user.save() @@ -45,7 +41,7 @@ def edit_profile_view(request, user_id=None): else: messages.error(request, "An error occurred updating user profile.") else: - user_form = ProfileEditForm(initial={"counselor_id": "" if not user.counselor else user.counselor.id}, instance=user) + user_form = ProfileEditForm(instance=user) # address_form = AddressForm(instance=user.properties.address) context = {"profile_user": user, "user_form": user_form, "address_form": address_form} diff --git a/intranet/apps/users/forms.py b/intranet/apps/users/forms.py index a7ad762554..1926f88333 100644 --- a/intranet/apps/users/forms.py +++ b/intranet/apps/users/forms.py @@ -4,6 +4,14 @@ from .models import Address +class UserChoiceField(forms.ModelChoiceField): + """A ModelChoiceField that returns a user's full name instead of their TJ username (which is the + default string representation).""" + + def label_from_instance(self, obj): + return obj.full_name + + class ProfileEditForm(forms.ModelForm): """A form containing editable fields in the User model.""" @@ -17,19 +25,27 @@ class ProfileEditForm(forms.ModelForm): 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) + counselor = UserChoiceField( + queryset=get_user_model().objects.filter(user_type="counselor").order_by("last_name", "first_name"), + label="Counselor", + required=False, + empty_label="No Counselor", + ) class Meta: model = get_user_model() - fields = ["admin_comments", "student_id", "first_name", "middle_name", "last_name", "title", "nickname", "graduation_year", "gender"] - - -class UserChoiceField(forms.ModelChoiceField): - """A ModelChoiceField that returns a user's full name instead of their TJ username (which is the - default string representation).""" - - def label_from_instance(self, obj): - return obj.full_name + fields = [ + "admin_comments", + "student_id", + "first_name", + "middle_name", + "last_name", + "title", + "nickname", + "graduation_year", + "gender", + "counselor", + ] class SortedUserChoiceField(forms.ModelChoiceField): diff --git a/intranet/templates/eighth/edit_profile.html b/intranet/templates/eighth/edit_profile.html index 43b7f3db2e..560cb18896 100644 --- a/intranet/templates/eighth/edit_profile.html +++ b/intranet/templates/eighth/edit_profile.html @@ -11,12 +11,19 @@ {% stylesheet 'eighth.admin' %} {% stylesheet 'profile' %} {% stylesheet 'eighth.profile' %} + {% endblock %} {% block js %} {{ block.super }} + +