Skip to content

Commit

Permalink
Processed saving of the suctom fields data on the context questions p…
Browse files Browse the repository at this point in the history
…age, fixed the migration and model.
  • Loading branch information
ruslanbaidan committed Oct 1, 2024
1 parent fb64ed1 commit c6abf2a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
6 changes: 5 additions & 1 deletion survey/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import html

from django import forms
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -62,7 +63,7 @@ def __init__(self, tanswers=None, *args, **kwargs):
)
elif self.question_type == "CT":
self.fields["answers"] = forms.CharField(
label=question.label.lower(),
label="",
max_length=200,
required=True
)
Expand Down Expand Up @@ -135,6 +136,9 @@ def clean_answers(self):
if self.fields["answers"].widget.input_type == "radio":
answers = [answers]

if self.fields["answers"].widget.input_type == "text":
return html.escape(answers)

if len(answers) > 1:
question_answers = SurveyQuestionAnswer.objects.filter(
pk__in=answers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ class Migration(migrations.Migration):
max_length=2,
),
),
migrations.AlterField(
model_name="surveyuser",
name="user_id",
field=models.UUIDField(default=uuid.uuid4, unique=True),
),
migrations.CreateModel(
name="SurveyUserCustomAnswer",
fields=[
Expand All @@ -47,15 +42,30 @@ class Migration(migrations.Migration):
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",
),
),
(
"question",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="survey.surveyquestion",
),
),
(
"answer_label",
models.TextField(
blank=False,
default="",
null=False,
max_length=100
)
),
("answer_value", models.TextField(blank=True, null=False, default="")),
],
),
]
10 changes: 5 additions & 5 deletions survey/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,14 @@ def __str__(self):


class SurveyUserCustomAnswer(models.Model):
# AnswerID
# AnswerListID
# CustomAnswerID
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="")
question = models.ForeignKey(SurveyQuestion, on_delete=models.CASCADE)
answer_label = models.TextField(max_length=100, null=False, blank=False, default="")
answer_value = models.TextField(null=False, blank=True, default="")

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


class SurveyUserFeedback(models.Model):
Expand Down
20 changes: 20 additions & 0 deletions survey/viewLogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from survey.models import SurveyUserAnswer
from survey.models import SurveyUserFeedback
from survey.models import SurveyUserQuestionSequence
from survey.models import SurveyUserCustomAnswer

LOCAL_DEFAULT_LANG = LANGUAGE_CODE

Expand Down Expand Up @@ -133,6 +134,11 @@ def handle_start_survey(request: HttpRequest, lang: str) -> Union[Dict, SurveyUs
for question in questions:
form = forms[question.id]
answers = form.cleaned_data["answers"]
# For the custom field type the value is saved in a separate table
if question.qtype == 'CT':
save_custom_answer(user, question, question.label.lower(), answers)
continue

answer_content = ""
if "answer_content" in form.cleaned_data:
answer_content = form.cleaned_data["answer_content"]
Expand Down Expand Up @@ -370,6 +376,20 @@ def save_answers(
user_answer.save()


def save_custom_answer(
user: SurveyUser,
question: SurveyQuestion,
answer_label: str,
answer_value: str
) -> None:
user_custom_answer = SurveyUserCustomAnswer()
user_custom_answer.user = user
user_custom_answer.question = question
user_custom_answer.answer_label = answer_label
user_custom_answer.answer_value = answer_value
user_custom_answer.save()


def save_feedback(user: SurveyUser, question: SurveyQuestion, feedback: str) -> None:
user_feedback = SurveyUserFeedback.objects.filter(user=user, question=question)[:1]
if not user_feedback:
Expand Down

0 comments on commit c6abf2a

Please sign in to comment.