Skip to content

Commit

Permalink
chg: [survey] Initial work on custom free text answers
Browse files Browse the repository at this point in the history
Signed-off-by: Romain Kieffer <[email protected]>
  • Loading branch information
romainkieffer committed Oct 1, 2024
1 parent 06807b2 commit 702344b
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 20 deletions.
30 changes: 18 additions & 12 deletions survey/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class AnswerMChoice(forms.Form):

def __init__(self, tanswers=None, *args, **kwargs):
self.lang = kwargs.pop("lang")
answers_field_type = kwargs.pop("answers_field_type")
self.question_type = answers_field_type
question = kwargs.pop("question")
self.question_type = question.qtype
question_answers = kwargs.pop("question_answers")
data = kwargs.get("data")

super().__init__(*args, **kwargs)

if answers_field_type[0] == "M":
if self.question_type[0] == "M":
self.fields["answers"] = forms.MultipleChoiceField(
required=True,
choices=[],
Expand All @@ -30,42 +30,47 @@ def __init__(self, tanswers=None, *args, **kwargs):
),
label="",
)
elif answers_field_type == "SO":
elif self.question_type == "SO":
self.fields["answers"] = forms.ChoiceField(
required=False,
choices=question_answers,
widget=forms.Select(),
label="",
)
elif answers_field_type[0] == "S":
elif self.question_type[0] == "S":
self.fields["answers"] = forms.ChoiceField(
required=True,
choices=[],
widget=forms.RadioSelect(attrs={"class": "radio-buttons"}),
label="",
)
elif answers_field_type == "T":
elif self.question_type == "T":
self.fields["answers"] = forms.ChoiceField(
required=True,
choices=[],
widget=forms.RadioSelect(attrs={"class": "radio-buttons"}),
label="",
initial=tanswers[0][0],
)
elif answers_field_type == "CL":
elif self.question_type == "CL":
tanswers = None
self.fields["answers"] = CountryField().formfield(
label="",
required=True,
initial=CUSTOM.get("defaultCountryCode", "LU"),
error_messages={"required": _("Please select your country")},
)

self.fields["answers"].error_messages = {
"required": _("You need to choose at least one answer")
}
elif self.question_type == "CT":
self.fields["answers"] = forms.CharField(
label=question.label.lower(),
max_length=200,
required=True
)

if tanswers is not None:
self.fields["answers"].error_messages = {
"required": _("You need to choose at least one answer")
}
self.fields["answers"].choices = tanswers

answers_dependencies = []
Expand All @@ -74,7 +79,7 @@ def __init__(self, tanswers=None, *args, **kwargs):
isAnswerContentRequired = False
if data is not None and data["free_text_answer_id"] != 0:
selected_answers = data["answers"]
if answers_field_type[0] == "S":
if self.question_type[0] == "S":
selected_answers = [data["answers"]]
if data["free_text_answer_id"] in selected_answers:
isAnswerContentRequired = True
Expand All @@ -84,6 +89,7 @@ def __init__(self, tanswers=None, *args, **kwargs):
widget=forms.Textarea(
attrs={
"autofocus": True,
"row": 2
}
),
required=isAnswerContentRequired,
Expand Down
3 changes: 2 additions & 1 deletion survey/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
("MT", "Multiple Choice + Free Text"),
("ST", "Single Choice + Free Text"),
("CL", "Countries list"),
("CT", "Context with Text")
]

ANSWER_TYPES = [
("P", "Predefined answer"),
("T", "Free text"),
("T", "Free text")
]
13 changes: 7 additions & 6 deletions survey/management/commands/import_questions.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,13 @@ def handle(self, *args, **options): # noqa: C901
}
)

# Deactivate all the answers with index higher then max importing.
max_answer_index = max(question["answers"], key=lambda x: x["aindex"])
if max_answer_index:
SurveyQuestionAnswer.all_objects.filter(
question=question_obj, aindex__gt=max_answer_index["aindex"]
).update(is_active=False)
if question["answers"]:
# Deactivate all the answers with index higher then max importing.
max_answer_index = max(question["answers"], key=lambda x: x["aindex"])
if max_answer_index:
SurveyQuestionAnswer.all_objects.filter(
question=question_obj, aindex__gt=max_answer_index["aindex"]
).update(is_active=False)

# Process the answers' dependencies.
self.process_answers_dependencies(answers_dependencies)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Generated by Django 4.2.16 on 2024-09-30 13:19

from django.db import migrations, models
import django.db.models.deletion
import uuid


class Migration(migrations.Migration):

dependencies = [
("survey", "1014_alter_surveyuser_user_id_surveyquestionmaxscore_and_more"),
]

operations = [
migrations.AlterField(
model_name="surveyquestion",
name="qtype",
field=models.CharField(
choices=[
("M", "Multiple Choice"),
("S", "Single Choice"),
("SO", "Single Option Choice"),
("T", "Free text"),
("MT", "Multiple Choice + Free Text"),
("ST", "Single Choice + Free Text"),
("CL", "Countries list"),
("CT", "Context with Text"),
],
default="M",
max_length=2,
),
),
migrations.AlterField(
model_name="surveyuser",
name="user_id",
field=models.UUIDField(default=uuid.uuid4, unique=True),
),
migrations.CreateModel(
name="SurveyUserCustomAnswer",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("question", models.CharField(default="0", max_length=100)),
("answer", models.TextField(blank=True, default="")),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="survey.surveyuser",
),
),
],
),
]
11 changes: 11 additions & 0 deletions survey/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,17 @@ def __str__(self):
return str(self.answer)


class SurveyUserCustomAnswer(models.Model):
# AnswerID
# AnswerListID
user = models.ForeignKey(SurveyUser, on_delete=models.CASCADE)
question = models.CharField(default="0", max_length=100, null=False)
answer = models.TextField(null=False, blank=True, default="")

def __str__(self):
return str(self.answer)


class SurveyUserFeedback(models.Model):
user = models.ForeignKey(SurveyUser, on_delete=models.CASCADE)
question = models.ForeignKey(SurveyQuestion, on_delete=models.CASCADE, null=True)
Expand Down
4 changes: 3 additions & 1 deletion survey/viewLogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def handle_start_survey(request: HttpRequest, lang: str) -> Union[Dict, SurveyUs
tuple_answers,
lang=lang,
data=request.POST if request.method == "POST" else None,
answers_field_type=question.qtype,
question=question,
question_answers=question.surveyquestionanswer_set.filter(is_active=True),
prefix="form" + str(question.id),
)
Expand Down Expand Up @@ -302,6 +302,8 @@ def save_answers(
]
case "CL":
question_answers = [current_question.surveyquestionanswer_set.all()[0]]
case "CT":
question_answers = []
case _:
question_answers = current_question.surveyquestionanswer_set.filter(
is_active=True
Expand Down

0 comments on commit 702344b

Please sign in to comment.