Repository: e-valuation/EvaP

Issue:
Contributors missing in editor evaluation edit form
When editing an evaluation as an editor, contributors who have already been added (by managers) and who are marked as inactive or proxy users are not shown in the form anymore because they are not included in the contributor queryset. This leads to errors when saving the form.
The queryset should always include those people already added as contributors for this evaluation.


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
@@ -68,9 +68,13 @@ class EditorContributionForm(ContributionForm):
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
 
+        existing_contributor_pk = self.instance.contributor.pk if self.instance.contributor else None
+
         self.fields['questionnaires'].queryset = Questionnaire.objects.contributor_questionnaires().filter(
             Q(visibility=Questionnaire.EDITORS) | Q(contributions__evaluation=self.evaluation)).distinct()
-        self.fields['contributor'].queryset = UserProfile.objects.exclude(is_active=False).exclude(is_proxy_user=True)
+        self.fields['contributor'].queryset = UserProfile.objects.filter(
+            (Q(is_active=True) & Q(is_proxy_user=False)) | Q(pk=existing_contributor_pk)
+        )
 
 
 class DelegatesForm(forms.ModelForm):


Test patch:
diff --git a/evap/contributor/tests/test_forms.py b/evap/contributor/tests/test_forms.py
--- a/evap/contributor/tests/test_forms.py
+++ b/evap/contributor/tests/test_forms.py
@@ -65,6 +65,30 @@ def test_managers_only(self):
         self.assertEqual(expected, set(formset.forms[0].fields['questionnaires'].queryset))
         self.assertEqual(expected, set(formset.forms[1].fields['questionnaires'].queryset))
 
+    def test_existing_contributors_are_in_queryset(self):
+        """
+            Asserts that users that should normally not be in the contributor queryset are in it when they are already set.
+            Regression test for #1414.
+        """
+        evaluation = baker.make(Evaluation)
+        non_proxy_user = baker.make(UserProfile)
+        proxy_user = baker.make(UserProfile, is_proxy_user=True)
+        contribution1 = baker.make(Contribution, evaluation=evaluation, contributor=non_proxy_user, questionnaires=[])
+
+        InlineContributionFormset = inlineformset_factory(Evaluation, Contribution, formset=ContributionFormSet, form=EditorContributionForm, extra=1)
+        formset = InlineContributionFormset(instance=evaluation, form_kwargs={'evaluation': evaluation})
+
+        self.assertEqual({non_proxy_user}, set(formset.forms[0].fields['contributor'].queryset))
+
+        # now a manager adds the proxy user as a contributor.
+        contribution1.contributor = proxy_user
+        contribution1.save()
+
+        InlineContributionFormset = inlineformset_factory(Evaluation, Contribution, formset=ContributionFormSet, form=EditorContributionForm, extra=1)
+        formset = InlineContributionFormset(instance=evaluation, form_kwargs={'evaluation': evaluation})
+
+        self.assertEqual({proxy_user, non_proxy_user}, set(formset.forms[0].fields['contributor'].queryset))
+
 
 class ContributionFormsetWebTests(WebTest):
     csrf_checks = False