Repository: e-valuation/EvaP

Issue:
Show username in all user selection fields
#866 implemented that the username is shown along the first and last name in the user merge selection fields. This should also be done for all other user selection fields, e.g. on the lecturer's course edit page.
Show username in all user selection fields
#866 implemented that the username is shown along the first and last name in the user merge selection fields. This should also be done for all other user selection fields, e.g. on the lecturer's course edit page.


Discussion:

Resolving patch:
diff --git a/evap/contributor/forms.py b/evap/contributor/forms.py
--- a/evap/contributor/forms.py
+++ b/evap/contributor/forms.py
@@ -9,7 +9,7 @@
 
 from evap.evaluation.models import Course, UserProfile, Questionnaire, Semester
 from evap.staff.forms import ContributionForm
-
+from evap.evaluation.forms import UserModelMultipleChoiceField
 
 logger = logging.getLogger(__name__)
 
@@ -80,12 +80,16 @@ def __init__(self, *args, **kwargs):
 
 
 class DelegatesForm(forms.ModelForm):
-    delegate_of = forms.ModelMultipleChoiceField(None, required=False, disabled=True)
-    cc_user_of = forms.ModelMultipleChoiceField(None, required=False, disabled=True)
+    delegate_of = UserModelMultipleChoiceField(None, required=False, disabled=True)
+    cc_user_of = UserModelMultipleChoiceField(None, required=False, disabled=True)
 
     class Meta:
         model = UserProfile
         fields = ('delegates', 'cc_users',)
+        field_classes = {
+            'delegates': UserModelMultipleChoiceField,
+            'cc_users': UserModelMultipleChoiceField,
+        }
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
diff --git a/evap/evaluation/forms.py b/evap/evaluation/forms.py
--- a/evap/evaluation/forms.py
+++ b/evap/evaluation/forms.py
@@ -76,3 +76,13 @@ def clean_email(self):
 
     def get_user(self):
         return self.user_cache
+
+
+class UserModelChoiceField(forms.ModelChoiceField):
+    def label_from_instance(self, obj):
+        return obj.full_name_with_username
+
+
+class UserModelMultipleChoiceField(forms.ModelMultipleChoiceField):
+    def label_from_instance(self, obj):
+        return obj.full_name_with_username
diff --git a/evap/staff/forms.py b/evap/staff/forms.py
--- a/evap/staff/forms.py
+++ b/evap/staff/forms.py
@@ -13,7 +13,7 @@
 from django.forms.widgets import CheckboxSelectMultiple
 from evap.evaluation.models import Contribution, Course, Question, Questionnaire, Semester, UserProfile, FaqSection, \
                                    FaqQuestion, EmailTemplate, TextAnswer, Degree, RatingAnswerCounter, CourseType
-
+from evap.evaluation.forms import UserModelChoiceField, UserModelMultipleChoiceField
 
 logger = logging.getLogger(__name__)
 
@@ -128,6 +128,9 @@ class Meta:
         fields = ('name_de', 'name_en', 'type', 'degrees', 'is_graded', 'is_private', 'is_required_for_reward', 'vote_start_date',
                   'vote_end_date', 'participants', 'general_questions', 'last_modified_time_2', 'last_modified_user_2', 'semester')
         localized_fields = ('vote_start_date', 'vote_end_date')
+        field_classes = {
+            'participants': UserModelMultipleChoiceField,
+        }
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
@@ -169,7 +172,7 @@ class SingleResultForm(forms.ModelForm):
     last_modified_time_2 = forms.DateTimeField(label=_("Last modified"), required=False, localize=True, disabled=True)
     last_modified_user_2 = forms.CharField(label=_("Last modified by"), required=False, disabled=True)
     event_date = forms.DateField(label=_("Event date"), localize=True)
-    responsible = forms.ModelChoiceField(label=_("Responsible"), queryset=UserProfile.objects.all())
+    responsible = UserModelChoiceField(label=_("Responsible"), queryset=UserProfile.objects.all())
     answer_1 = forms.IntegerField(label=_("# very good"), initial=0)
     answer_2 = forms.IntegerField(label=_("# good"), initial=0)
     answer_3 = forms.IntegerField(label=_("# neutral"), initial=0)
@@ -253,6 +256,9 @@ class Meta:
         model = Contribution
         fields = ('course', 'contributor', 'questionnaires', 'order', 'responsibility', 'comment_visibility', 'label')
         widgets = {'order': forms.HiddenInput(), 'comment_visibility': forms.RadioSelect(choices=Contribution.COMMENT_VISIBILITY_CHOICES)}
+        field_classes = {
+            'contributor': UserModelChoiceField,
+        }
 
     def __init__(self, *args, **kwargs):
         # work around https://code.djangoproject.com/ticket/25880
@@ -466,6 +472,10 @@ class UserForm(forms.ModelForm):
     class Meta:
         model = UserProfile
         fields = ('username', 'title', 'first_name', 'last_name', 'email', 'delegates', 'cc_users')
+        field_classes = {
+            'delegates': UserModelMultipleChoiceField,
+            'cc_users': UserModelMultipleChoiceField,
+        }
 
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
@@ -520,11 +530,6 @@ def save(self, *args, **kw):
             self.instance.groups.remove(grade_user_group)
 
 
-class UserModelChoiceField(forms.ModelChoiceField):
-    def label_from_instance(self, obj):
-        return obj.full_name_with_username
-
-
 class UserMergeSelectionForm(forms.Form):
     main_user = UserModelChoiceField(UserProfile.objects.all())
     other_user = UserModelChoiceField(UserProfile.objects.all())


Test patch: