Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions evap/evaluation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from django.db import IntegrityError, models, transaction
from django.db.models import CheckConstraint, Count, Exists, F, Manager, OuterRef, Q, QuerySet, Subquery, Value
from django.db.models.functions import Coalesce, Lower, NullIf, TruncDate
from django.db.models.signals import post_delete
from django.dispatch import Signal, receiver
from django.http import HttpRequest
from django.template import Context, Template
Expand Down Expand Up @@ -1373,18 +1374,14 @@ class Meta:
ordering = ["order"]
unique_together = [("question", "questionnaire")]

def delete(self, using=None, keep_parents=False) -> tuple[int, dict[str, int]]:
if not self.question.is_heading_question:
assert not self.question.answer_class.objects.filter(assignment=self).exists(), (
"cannot delete question with answers"
)
count = 0
meta: dict[str, int] = {}

if not self.question.questionnaires.exclude(pk=self.questionnaire.pk).exists():
count, meta = self.question.delete(using=using, keep_parents=False) # garbage-collect unused questions
self_count, self_meta = super().delete(using=using, keep_parents=keep_parents)
return count + self_count, meta | self_meta
@receiver(post_delete, sender=QuestionAssignment)
@transaction.atomic
def _question_assignment_post_delete(*, instance: QuestionAssignment, **_kwargs) -> None:
"""Garbage-collect unused questions once no questionnaires reference them anymore."""

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record: Based on the current django source code, we expect this signal to fire within a transaction that deletes the "parent" object. So, a failure in the handler should also undo the deletion of the object that sent the signal (which is probably good for us)

if not QuestionAssignment.objects.filter(question=instance.question.pk).exists():
Question.objects.filter(pk=instance.question.pk).delete()


@dataclass
Expand Down
33 changes: 26 additions & 7 deletions evap/evaluation/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Evaluation,
NotArchivableError,
Question,
QuestionAssignment,
Questionnaire,
QuestionType,
Semester,
Expand Down Expand Up @@ -58,14 +59,38 @@ def test_can_be_deleted_by_manager(self):
self.assertTrue(semester.can_be_deleted_by_manager)


class TestQuestionnaire(WebTest):
class TestQuestionnaire(TestCase):
def test_can_be_deleted_by_manager(self):
questionnaire = baker.make(Questionnaire)
self.assertTrue(questionnaire.can_be_deleted_by_manager)

baker.make(Contribution, questionnaires=[questionnaire])
self.assertFalse(questionnaire.can_be_deleted_by_manager)

def test_locked_contributor_questionnaire(self):
questionnaire = baker.prepare(Questionnaire, is_locked=True, type=Questionnaire.Type.CONTRIBUTOR)
self.assertRaises(ValidationError, questionnaire.clean)


class TestQuestionAssignment(TestCase):
@classmethod
def setUpTestData(cls):
cls.assignment = baker.make(QuestionAssignment)
cls.question = cls.assignment.question
cls.questionnaire = cls.assignment.questionnaire

def test_assignment_delete_gc(self):
self.assignment.delete()
self.assertRaises(Question.DoesNotExist, self.question.refresh_from_db)

def test_assignment_queryset_delete_gc(self):
QuestionAssignment.objects.filter(pk=self.assignment.pk).delete()
self.assertRaises(Question.DoesNotExist, self.question.refresh_from_db)

def test_questionnaire_cascading_delete_gc(self):
self.questionnaire.delete()
self.assertRaises(Question.DoesNotExist, self.question.refresh_from_db)


@override_settings(EVALUATION_END_OFFSET_HOURS=0)
class TestEvaluations(WebTest):
Expand Down Expand Up @@ -1148,9 +1173,3 @@ def test_recipient_list_filtering(self):
evaluation, [EmailTemplate.Recipients.CONTRIBUTORS], filter_users_in_cc=True
)
self.assertCountEqual(recipient_list, [contributor2, contributor3])


class QuestionnaireTests(TestCase):
def test_locked_contributor_questionnaire(self):
questionnaire = baker.prepare(Questionnaire, is_locked=True, type=Questionnaire.Type.CONTRIBUTOR)
self.assertRaises(ValidationError, questionnaire.clean)
Loading