Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stale maximimum choices #166

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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: 19 additions & 0 deletions survey/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@


class ResponseForm(models.ModelForm):

FIELDS = {
Question.TEXT: forms.CharField,
Question.SHORT_TEXT: forms.CharField,
Question.SELECT_MULTIPLE: forms.MultipleChoiceField,
Question.INTEGER: forms.IntegerField,
Question.FLOAT: forms.FloatField,
Question.DATE: forms.DateField,
Question.MAX: forms.IntegerField,
}

WIDGETS = {
Expand Down Expand Up @@ -298,3 +300,20 @@ def save(self, commit=True):
answer.save()
survey_completed.send(sender=Response, instance=response, data=data)
return response

def clean_choices(self, s):
value = self.cleaned_data
print(value)
for q in s.questions.all():
if q.type in ["select-multiple"] and q.maximum_choices:
# Only for SELECT_MULTIPlE and if maximum_choices is set
question = q
max = question.maximum_choices
if value.get(f"question_{question.id}"):
"""Only if some answers are checked at all."""
number_of_choices = len(value.get(f"question_{question.id}"))
if number_of_choices > max:
LOGGER.info("Selected more Answers than allowed! Maximum is %d.", max)
# TODO create a meaningful dialog for the user!
return None
return value
2 changes: 2 additions & 0 deletions survey/models/question.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Question(models.Model):
INTEGER = "integer"
FLOAT = "float"
DATE = "date"
MAX = "maximum-coices"

QUESTION_TYPES = (
(TEXT, _("text (multiple line)")),
Expand All @@ -77,6 +78,7 @@ class Question(models.Model):
survey = models.ForeignKey(Survey, on_delete=models.CASCADE, verbose_name=_("Survey"), related_name="questions")
type = models.CharField(_("Type"), max_length=200, choices=QUESTION_TYPES, default=TEXT)
choices = models.TextField(_("Choices"), blank=True, null=True, help_text=CHOICES_HELP_TEXT)
maximum_choices = models.IntegerField(_("Maximum possible Answers"), blank=True, null=True)

class Meta:
verbose_name = _("question")
Expand Down
8 changes: 6 additions & 2 deletions survey/views/survey_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def post(self, request, *args, **kwargs):
return redirect(reverse("survey-list"))
context = {"response_form": form, "survey": survey, "categories": categories}
if form.is_valid():
return self.treat_valid_form(form, kwargs, request, survey)
if form.clean_choices(survey):
return self.treat_valid_form(form, kwargs, request, survey)
return self.handle_invalid_form(context, form, request, survey)

@staticmethod
Expand Down Expand Up @@ -87,7 +88,10 @@ def treat_valid_form(self, form, kwargs, request, survey):
if not form.has_next_step():
save_form = ResponseForm(request.session[session_key], survey=survey, user=request.user)
if save_form.is_valid():
response = save_form.save()
if form.clean_choices(survey):
response = save_form.save()
else:
return self.handle_invalid_form("context", form, request, survey)
else:
LOGGER.warning("A step of the multipage form failed but should have been discovered before.")
# if there is a next step
Expand Down
Loading