-
Notifications
You must be signed in to change notification settings - Fork 167
Separate questions and questionnaires #2513
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
Changes from all commits
83d55c7
41001eb
bef0d02
a422feb
b3bbdd4
6086938
c936f97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| # Generated by Django 5.2 on 2025-08-12 22:21 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| def questions_to_question_assignments(apps, _schema_editor): | ||
| Question = apps.get_model("evaluation", "Question") | ||
| NewQuestion = apps.get_model("evaluation", "NewQuestion") | ||
| QuestionAssignment = apps.get_model("evaluation", "QuestionAssignment") | ||
| RatingAnswerCounter = apps.get_model("evaluation", "RatingAnswerCounter") | ||
| TextAnswer = apps.get_model("evaluation", "TextAnswer") | ||
| assignments = {} | ||
| new_questions = {} | ||
| for question in Question.objects.all(): | ||
| new_question = new_questions.setdefault( | ||
| (question.text_de, question.text_en, question.allows_additional_textanswers, question.type), | ||
| NewQuestion( | ||
| # pk=question.pk, # was only active when the test data jsons were regenerated to minimize the diff | ||
| text_de=question.text_de, | ||
|
Kakadus marked this conversation as resolved.
|
||
| text_en=question.text_en, | ||
| allows_additional_textanswers=question.allows_additional_textanswers, | ||
| type=question.type, | ||
| ), | ||
| ) | ||
| assignments[question.pk] = QuestionAssignment( | ||
| question=new_question, questionnaire_id=question.questionnaire_id, order=question.order | ||
| ) | ||
|
Comment on lines
+16
to
+28
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. from ai review:
Question deduplication is not bad per se, and the "copy on write" logic mimics current behavior. from django.db.models import CharField, Count, F, Value
from django.db.models.functions import Concat
questionnaires_with_duplicates = Questionnaire.objects.annotate(
total_questions=Count('questions'),
unique_combinations=Count(
Concat(
'questions__text_de',
Value('|==||==|'),
'questions__text_en',
Value('|==||==|'),
'questions__allows_additional_textanswers',
Value('|==||==|'),
'questions__type',
output_field=CharField()
),
distinct=True
)
).filter(total_questions__gt=F('unique_combinations'))
print(len(questionnaires_with_duplicates), "questionnaires ask the same question multiple times.")in production?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I cannot imagine a situation where we intentionally included the same question twice in a questionnaire, but it probably doesn't hurt to double check |
||
| NewQuestion.objects.bulk_create(new_questions.values()) | ||
| QuestionAssignment.objects.bulk_create(assignments.values()) | ||
|
|
||
| def set_question_assignment(answer): | ||
| answer.assignment = assignments[answer.question_id] | ||
| return answer | ||
|
|
||
| for model in (RatingAnswerCounter, TextAnswer): | ||
| model.objects.bulk_update( | ||
| map(set_question_assignment, model.objects.all()), | ||
| fields=["assignment"], | ||
| ) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("evaluation", "0160_evaluation_staff_notes"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name="NewQuestion", | ||
| fields=[ | ||
| ("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
| ("text_de", models.CharField(max_length=1024, verbose_name="question text (german)")), | ||
| ("text_en", models.CharField(max_length=1024, verbose_name="question text (english)")), | ||
| ( | ||
| "allows_additional_textanswers", | ||
| models.BooleanField(default=True, verbose_name="allow additional text answers"), | ||
| ), | ||
| ( | ||
| "type", | ||
| models.PositiveSmallIntegerField( | ||
| choices=[ | ||
| ("Text", [(0, "Text question")]), | ||
| ( | ||
| "Unipolar Likert", | ||
| [(1, "Positive agreement question"), (12, "Negative agreement question")], | ||
| ), | ||
| ("Grade", [(2, "Grade question")]), | ||
| ( | ||
| "Bipolar Likert", | ||
| [ | ||
| (6, "Easy-difficult question"), | ||
| (7, "Few-many question"), | ||
| (8, "Little-much question"), | ||
| (9, "Small-large question"), | ||
| (10, "Slow-fast question"), | ||
| (11, "Short-long question"), | ||
| ], | ||
| ), | ||
| ("Yes-no", [(3, "Positive yes-no question"), (4, "Negative yes-no question")]), | ||
| ("Layout", [(5, "Heading")]), | ||
| ], | ||
| verbose_name="question type", | ||
| ), | ||
| ), | ||
| ], | ||
| options={ | ||
| "verbose_name": "question", | ||
| "verbose_name_plural": "questions", | ||
| }, | ||
| ), | ||
| migrations.CreateModel( | ||
| name="QuestionAssignment", | ||
| fields=[ | ||
| ("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
| ("order", models.IntegerField(default=-1, verbose_name="question order")), | ||
| ( | ||
| "question", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="assignments", | ||
| to="evaluation.newquestion", | ||
| ), | ||
| ), | ||
| ( | ||
| "questionnaire", | ||
| models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="question_assignments", | ||
| to="evaluation.questionnaire", | ||
| ), | ||
| ), | ||
| ], | ||
| options={"ordering": ["order"]}, | ||
| ), | ||
| migrations.AlterUniqueTogether( | ||
| name="questionassignment", | ||
| unique_together={("question", "questionnaire")}, | ||
| ), | ||
| migrations.AddField( | ||
| model_name="newquestion", | ||
| name="questionnaires", | ||
| field=models.ManyToManyField( | ||
| related_name="questions", through="evaluation.QuestionAssignment", to="evaluation.questionnaire" | ||
| ), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="ratinganswercounter", | ||
| name="question", | ||
| field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to="evaluation.question", null=True), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="textanswer", | ||
| name="question", | ||
| field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to="evaluation.question", null=True), | ||
| ), | ||
| migrations.AddField( | ||
| model_name="ratinganswercounter", | ||
| name="assignment", | ||
| field=models.ForeignKey( | ||
| on_delete=django.db.models.deletion.PROTECT, to="evaluation.questionassignment", null=True | ||
| ), | ||
| ), | ||
| migrations.AddField( | ||
| model_name="textanswer", | ||
| name="assignment", | ||
| field=models.ForeignKey( | ||
| on_delete=django.db.models.deletion.PROTECT, to="evaluation.questionassignment", null=True | ||
| ), | ||
| ), | ||
| migrations.AddConstraint( | ||
| model_name="newquestion", | ||
| constraint=models.CheckConstraint( | ||
| condition=models.Q( | ||
| models.Q(("type", 0), ("type", 5), _connector="OR", _negated=True), | ||
| models.Q(("allows_additional_textanswers", True), _negated=True), | ||
| _connector="OR", | ||
| ), | ||
| name="check_evaluation_textanswer_or_heading_question_has_no_additional_textanswers_new", | ||
| ), | ||
| ), | ||
| migrations.RunPython( | ||
| code=questions_to_question_assignments, | ||
| reverse_code=migrations.RunPython.noop, | ||
| ), | ||
| ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # Generated by Django 5.2 on 2025-10-27 17:17 | ||
|
|
||
| import django.db.models.deletion | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| def question_assignments_to_questions(apps, _schema_editor): | ||
| Question = apps.get_model("evaluation", "Question") | ||
| QuestionAssignment = apps.get_model("evaluation", "QuestionAssignment") | ||
| RatingAnswerCounter = apps.get_model("evaluation", "RatingAnswerCounter") | ||
| TextAnswer = apps.get_model("evaluation", "TextAnswer") | ||
| questions = {} | ||
| for question_assignment in QuestionAssignment.objects.all().prefetch_related("question"): | ||
| new_question = question_assignment.question | ||
| questions[question_assignment.pk] = Question( | ||
| text_de=new_question.text_de, | ||
| text_en=new_question.text_en, | ||
| allows_additional_textanswers=new_question.allows_additional_textanswers, | ||
| type=new_question.type, | ||
| questionnaire=question_assignment.questionnaire, | ||
| order=question_assignment.order, | ||
| ) | ||
| Question.objects.bulk_create(questions.values()) | ||
|
|
||
| def set_question(answer): | ||
| answer.question_id = questions[answer.assignment_id].pk | ||
| return answer | ||
|
|
||
| for model in (RatingAnswerCounter, TextAnswer): | ||
| model.objects.bulk_update( | ||
| map(set_question, model.objects.all()), | ||
| fields=["question"], | ||
| ) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("evaluation", "0161_temporary_newquestion_relation"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.RunPython( | ||
| code=migrations.RunPython.noop, | ||
| reverse_code=question_assignments_to_questions, | ||
| ), | ||
| migrations.AlterUniqueTogether( | ||
| name="ratinganswercounter", | ||
| unique_together=set(), | ||
| ), | ||
| migrations.RemoveField( | ||
| model_name="ratinganswercounter", | ||
| name="question", | ||
| ), | ||
| migrations.RemoveField( | ||
| model_name="textanswer", | ||
| name="question", | ||
| ), | ||
| migrations.DeleteModel( | ||
| name="Question", | ||
| ), | ||
| migrations.RenameModel(old_name="NewQuestion", new_name="Question"), | ||
| migrations.RemoveConstraint( | ||
| model_name="question", | ||
| name="check_evaluation_textanswer_or_heading_question_has_no_additional_textanswers_new", | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="questionassignment", | ||
| name="questionnaire", | ||
| field=models.ForeignKey( | ||
| on_delete=django.db.models.deletion.CASCADE, | ||
| related_name="question_assignments", | ||
| to="evaluation.questionnaire", | ||
| ), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="ratinganswercounter", | ||
| name="assignment", | ||
| field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to="evaluation.questionassignment"), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="textanswer", | ||
| name="assignment", | ||
| field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to="evaluation.questionassignment"), | ||
| ), | ||
| migrations.AddConstraint( | ||
| model_name="question", | ||
| constraint=models.CheckConstraint( | ||
| condition=models.Q( | ||
| models.Q(("type", 0), ("type", 5), _connector="OR", _negated=True), | ||
| models.Q(("allows_additional_textanswers", True), _negated=True), | ||
| _connector="OR", | ||
| ), | ||
| name="check_evaluation_textanswer_or_heading_question_has_no_additional_textanswers", | ||
| ), | ||
| ), | ||
| migrations.AddConstraint( | ||
| model_name="question", | ||
| constraint=models.CheckConstraint( | ||
| condition=models.Q(("type__in", [0, 1, 12, 2, 6, 7, 8, 9, 10, 11, 3, 4, 5])), | ||
| name="Question_type_choices", | ||
| ), | ||
| ), | ||
| migrations.AlterUniqueTogether( | ||
| name="ratinganswercounter", | ||
| unique_together={("assignment", "contribution", "answer")}, | ||
| ), | ||
| ] |
|
Kakadus marked this conversation as resolved.
|
Uh oh!
There was an error while loading. Please reload this page.