Skip to content

Commit

Permalink
Questionnaires: Correct nested object deletions (DefectDojo#9574)
Browse files Browse the repository at this point in the history
* Questionnaires: Correct nested object deletions

* Fix Flake8
  • Loading branch information
Maffooch authored and quirinziessler committed Feb 21, 2024
1 parent 0359221 commit 11d7ca6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
10 changes: 9 additions & 1 deletion dojo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from django.utils.html import escape
from pytz import all_timezones
from polymorphic.models import PolymorphicModel
from polymorphic.managers import PolymorphicManager
from multiselectfield import MultiSelectField
from django import forms
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -4355,6 +4356,8 @@ class Meta:
help_text=_("If selected, user doesn't have to answer this question"))

text = models.TextField(blank=False, help_text=_('The question text'), default='')
objects = models.Manager()
polymorphic = PolymorphicManager()

def __str__(self):
return self.text
Expand All @@ -4364,6 +4367,7 @@ class TextQuestion(Question):
'''
Question with a text answer
'''
objects = PolymorphicManager()

def get_form(self):
'''
Expand Down Expand Up @@ -4397,8 +4401,8 @@ class ChoiceQuestion(Question):

multichoice = models.BooleanField(default=False,
help_text=_("Select one or more"))

choices = models.ManyToManyField(Choice)
objects = PolymorphicManager()

def get_form(self):
'''
Expand Down Expand Up @@ -4476,13 +4480,16 @@ class Answer(PolymorphicModel, TimeStampedModel):
null=False,
blank=False,
on_delete=models.CASCADE)
objects = models.Manager()
polymorphic = PolymorphicManager()


class TextAnswer(Answer):
answer = models.TextField(
blank=False,
help_text=_('The answer text'),
default='')
objects = PolymorphicManager()

def __str__(self):
return self.answer
Expand All @@ -4492,6 +4499,7 @@ class ChoiceAnswer(Answer):
answer = models.ManyToManyField(
Choice,
help_text=_('The selected choices as the answer'))
objects = PolymorphicManager()

def __str__(self):
if len(self.answer.all()):
Expand Down
21 changes: 12 additions & 9 deletions dojo/survey/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def delete_engagement_survey(request, eid, sid):
if request.method == 'POST':
form = Delete_Questionnaire_Form(request.POST, instance=survey)
if form.is_valid():
answers = Answer.objects.filter(
answers = Answer.polymorphic.filter(
question__in=[
question.id for question in survey.survey.questions.all()],
question.id for question in Question.polymorphic.filter(engagement_survey=survey.survey)],
answered_survey=survey)
for answer in answers:
answer.delete()
Expand Down Expand Up @@ -95,7 +95,7 @@ def answer_questionnaire(request, eid, sid):
prefix=str(q.id),
answered_survey=survey,
question=q, form_tag=False)
for q in survey.survey.questions.all()]
for q in Question.polymorphic.filter(engagement_survey=survey.survey)]

questions_are_valid = []

Expand Down Expand Up @@ -184,7 +184,7 @@ def get_answered_questions(survey=None, read_only=False):
answered_survey=survey,
question=q,
form_tag=False)
for q in survey.survey.questions.all()]
for q in Question.polymorphic.filter(engagement_survey=survey.survey)]

if read_only:
for question in questions:
Expand Down Expand Up @@ -416,7 +416,7 @@ def questionnaire(request):

@user_is_configuration_authorized('dojo.view_question')
def questions(request):
questions = Question.objects.all()
questions = Question.polymorphic.all()
questions = QuestionFilter(request.GET, queryset=questions)
paged_questions = get_page_items(request, questions.qs, 25)
add_breadcrumb(title="Questions", top_level=False, request=request)
Expand Down Expand Up @@ -500,7 +500,10 @@ def create_question(request):

@user_is_configuration_authorized('dojo.change_question')
def edit_question(request, qid):
question = get_object_or_404(Question, id=qid)
try:
question = Question.polymorphic.get(id=qid)
except Question.DoesNotExist:
return Http404()
survey = Engagement_Survey.objects.filter(questions__in=[question])
reverted = False
answered = []
Expand Down Expand Up @@ -652,7 +655,7 @@ def delete_empty_questionnaire(request, esid):
form = Delete_Questionnaire_Form(request.POST, instance=survey)
if form.is_valid():
answers = Answer.objects.filter(
question__in=[question.id for question in survey.survey.questions.all()],
question__in=[question.id for question in Question.polymorphic.filter(engagement_survey=survey.survey)],
answered_survey=survey)
for answer in answers:
answer.delete()
Expand Down Expand Up @@ -740,7 +743,7 @@ def answer_empty_survey(request, esid):
engagement_survey=engagement_survey,
question=q,
form_tag=False)
for q in engagement_survey.questions.all()
for q in Question.polymorphic.filter(engagement_survey=engagement_survey)
]

if request.method == 'POST':
Expand All @@ -753,7 +756,7 @@ def answer_empty_survey(request, esid):
answered_survey=survey,
question=q,
form_tag=False)
for q in survey.survey.questions.all()
for q in Question.polymorphic.filter(engagement_survey=survey.survey)
]

questions_are_valid = []
Expand Down

0 comments on commit 11d7ca6

Please sign in to comment.