Skip to content
Snippets Groups Projects
Unverified Commit 793d935d authored by Jannis Berndt's avatar Jannis Berndt Committed by GitHub
Browse files

Prevent deleting contributors voted for in the form (#1651)

Contribution Formset: Hide delete button if entry can not be deleted. Fixes #1558.
parent b1a0d3fc
No related branches found
No related tags found
No related merge requests found
......@@ -66,7 +66,7 @@
{% include 'bootstrap_form_errors.html' with errors=form.label.errors %}
</td>
<td>
{% if editable %}
{% if editable and form.show_delete_button %}
{% include 'bootstrap_form_field_widget.html' with field=form.DELETE class="d-none" %}
{% endif %}
</td>
......
......
......@@ -517,6 +517,16 @@ class ContributionForm(forms.ModelForm):
if not self.cleaned_data.get("does_not_contribute") and not self.cleaned_data.get("questionnaires"):
self.add_error("does_not_contribute", _("Select either this option or at least one questionnaire!"))
@property
def show_delete_button(self):
if self.instance.pk is None:
return True # not stored in the DB. Required so temporary instances in the formset can be deleted.
if not self.instance.ratinganswercounter_set.exists() and not self.instance.textanswer_set.exists():
return True
return False
class ContributionCopyForm(ContributionForm):
def __init__(self, data=None, instance=None, evaluation=None, **kwargs):
......
......
......@@ -6,6 +6,7 @@ from model_bakery import baker
from evap.contributor.forms import EvaluationForm as ContributorEvaluationForm
from evap.evaluation.models import (
Answer,
Contribution,
Course,
Degree,
......@@ -13,7 +14,9 @@ from evap.evaluation.models import (
Evaluation,
Question,
Questionnaire,
RatingAnswerCounter,
Semester,
TextAnswer,
UserProfile,
)
from evap.evaluation.tests.tools import (
......@@ -554,6 +557,36 @@ class ContributionFormsetTests(TestCase):
form = CourseForm(instance=course)
self.assertIn(proxy_user, form.fields["responsibles"].queryset)
def test_prevent_contribution_deletion_with_answers(self):
"""
When answers for a contribution already exist, it should not be possible to remove that contribution.
"""
self.assertEqual(
set(Answer.__subclasses__()),
{RatingAnswerCounter, TextAnswer},
"This requires an update if a new answer type is added",
)
evaluation = baker.make(Evaluation)
contribution = baker.make(
Contribution,
evaluation=evaluation,
role=Contribution.Role.EDITOR,
textanswer_visibility=Contribution.TextAnswerVisibility.GENERAL_TEXTANSWERS,
_fill_optional=["contributor"],
)
contribution_formset = inlineformset_factory(
Evaluation, Contribution, formset=ContributionFormSet, form=ContributionForm, extra=1
)
formset = contribution_formset(instance=evaluation, form_kwargs={"evaluation": evaluation})
self.assertTrue(formset.forms[0].show_delete_button)
self.assertTrue(formset.forms[1].show_delete_button)
baker.make(RatingAnswerCounter, contribution=contribution)
self.assertFalse(formset.forms[0].show_delete_button)
self.assertTrue(formset.forms[1].show_delete_button)
class ContributionFormset775RegressionTests(TestCase):
"""
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment