-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add migration to generate probleminstances
- Loading branch information
1 parent
3484f91
commit fd5257f
Showing
1 changed file
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Generated by Django 4.1.2 on 2023-02-19 11:02 | ||
|
||
from django.db import migrations | ||
from django.db import models | ||
|
||
|
||
def generate_probleminstances(apps, schema_editor): | ||
# We can't import the Person model directly as it may be a newer | ||
# version than this migration expects. We use the historical version. | ||
Problem = apps.get_model('problems', 'Problem') | ||
ProblemInstance = apps.get_model('courses', 'ProblemInstance') | ||
problems = Problem.objects.values_list('id', 'problem_set_id', 'visible', '_order') | ||
instances = [ProblemInstance( | ||
problem_id=id, | ||
problem_set_id=problem_set_id, | ||
visible=visible, | ||
_order=_order | ||
) for id, problem_set_id, visible, _order in problems | ||
] | ||
instances = ProblemInstance.objects.bulk_create(instances, batch_size=1000) | ||
problem_id_to_instance_id = {instance.problem_id: instance.id for instance in instances} | ||
for class_name in ["Attempt", "HistoricalAttempt"]: | ||
Attempt = apps.get_model('attempts', class_name) | ||
attempts = Attempt.objects.annotate(problem_id=models.F('part__problem_id')) | ||
for attempt in attempts: | ||
attempt.problem_instance_id = problem_id_to_instance_id[attempt.problem_id] | ||
Attempt.objects.bulk_update(attempts, ['problem_instance_id'], batch_size=1000) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("courses", "0004_probleminstance"), | ||
("attempts", "0004_attempt_problem_instance_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(generate_probleminstances), | ||
] |