Skip to content
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

Problem instance #286

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions web/attempts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@ class AttemptAdmin(SimpleHistoryAdmin):
fields = [
"user",
"part",
"problem",
"problem_instance",
"solution",
"valid",
"feedback",
]

list_display = (
"user",
"problem",
"problem_instance",
"part",
"valid",
)
list_filter = (
"part__problem__problem_set__course__institution",
"part__problem__problem_set__course",
"part__problem__problem_set",
"part__problem__visible",
"problem_instance__problem_set__course__institution",
"problem_instance__problem_set__course",
"problem_instance__problem_set",
"problem_instance__visible",
)
search_fields = (
"part__pk",
"problem__problem_set__title",
"problem__title",
"problem_instance__problem_set__title",
"problem_instance__problem__title",
"user__username",
"part__description",
)
date_hierarchy = "submission_date"

readonly_fields = [
"problem",
"problem_instance",
]

def problem(self, obj):
Expand Down
36 changes: 36 additions & 0 deletions web/attempts/migrations/0004_attempt_problem_instance_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 4.1.2 on 2023-02-19 11:00

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


class Migration(migrations.Migration):
dependencies = [
("courses", "0004_probleminstance"),
("attempts", "0003_alter_attempt_part"),
]

operations = [
migrations.AddField(
model_name="attempt",
name="problem_instance",
field=models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="attempts",
to="courses.probleminstance",
),
),
migrations.AddField(
model_name="historicalattempt",
name="problem_instance",
field=models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="courses.probleminstance",
),
),
]
6 changes: 6 additions & 0 deletions web/attempts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ class Attempt(models.Model):
part = models.ForeignKey(
"problems.Part", on_delete=models.CASCADE, related_name="attempts"
)
problem_instance = models.ForeignKey(
"courses.ProblemInstance",
on_delete=models.SET_NULL,
related_name="attempts",
null=True,
)
solution = models.TextField(blank=True)
valid = models.BooleanField(default=False)
feedback = models.TextField(default="[]", validators=[is_json_string_list])
Expand Down
31 changes: 31 additions & 0 deletions web/courses/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,37 @@ class InstitutionAdmin(admin.ModelAdmin):
search_fields = ("name",)


class ProblemInstanceAdmin(admin.ModelAdmin):
list_display = (
"problem",
"course",
"problem_set",
"visible",
)
list_display_links = ("title",)
list_filter = (
"problem_set__course__institution",
"problem_set__course",
"problem_set",
)
ordering = (
"problem_set__course",
"problem_set",
"_order",
)
search_fields = (
"title",
"description",
)

def title(self, obj):
return obj.problem.title

def course(self, obj):
return obj.problem_set.course

course.admin_order_field = "problem_set__course"

admin.site.register(Course, CourseAdmin)
admin.site.register(ProblemSet, ProblemSetAdmin)
admin.site.register(CourseGroup, CourseGroupAdmin)
Expand Down
49 changes: 49 additions & 0 deletions web/courses/migrations/0004_probleminstance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Generated by Django 4.1.2 on 2023-02-19 11:00

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


class Migration(migrations.Migration):
dependencies = [
("problems", "0005_alter_historicalproblem_visible_and_more"),
("courses", "0003_alter_coursegroup_options"),
]

operations = [
migrations.CreateModel(
name="ProblemInstance",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("visible", models.BooleanField(default=False, verbose_name="Visible")),
(
"problem",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="instances",
to="problems.problem",
),
),
(
"problem_set",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="courses.problemset",
),
),
],
options={
"order_with_respect_to": "problem_set",
},
bases=(utils.models.OrderWithRespectToMixin, models.Model),
),
]
38 changes: 38 additions & 0 deletions web/courses/migrations/0005_generate_probleminstances.py
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),
]
Loading