diff --git a/survey/forms.py b/survey/forms.py index 526855fb..67e25b1f 100644 --- a/survey/forms.py +++ b/survey/forms.py @@ -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=[], @@ -30,21 +30,21 @@ 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=[], @@ -52,7 +52,7 @@ def __init__(self, tanswers=None, *args, **kwargs): label="", initial=tanswers[0][0], ) - elif answers_field_type == "CL": + elif self.question_type == "CL": tanswers = None self.fields["answers"] = CountryField().formfield( label="", @@ -60,12 +60,17 @@ def __init__(self, tanswers=None, *args, **kwargs): 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 = [] @@ -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 @@ -84,6 +89,7 @@ def __init__(self, tanswers=None, *args, **kwargs): widget=forms.Textarea( attrs={ "autofocus": True, + "row": 2 } ), required=isAnswerContentRequired, diff --git a/survey/globals.py b/survey/globals.py index 4e5b4572..4896d4ff 100644 --- a/survey/globals.py +++ b/survey/globals.py @@ -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") ] diff --git a/survey/management/commands/import_questions.py b/survey/management/commands/import_questions.py index 2371fea7..0839f63d 100644 --- a/survey/management/commands/import_questions.py +++ b/survey/management/commands/import_questions.py @@ -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) diff --git a/survey/migrations/1015_alter_surveyquestion_qtype_alter_surveyuser_user_id_and_more.py b/survey/migrations/1015_alter_surveyquestion_qtype_alter_surveyuser_user_id_and_more.py new file mode 100644 index 00000000..b73f683c --- /dev/null +++ b/survey/migrations/1015_alter_surveyquestion_qtype_alter_surveyuser_user_id_and_more.py @@ -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", + ), + ), + ], + ), + ] diff --git a/survey/models.py b/survey/models.py index 7d8febb4..0f51228c 100644 --- a/survey/models.py +++ b/survey/models.py @@ -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) diff --git a/survey/viewLogic.py b/survey/viewLogic.py index d90e36f4..001f9384 100644 --- a/survey/viewLogic.py +++ b/survey/viewLogic.py @@ -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), ) @@ -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