Skip to content

Commit

Permalink
Add trans and migration
Browse files Browse the repository at this point in the history
  • Loading branch information
cuom1999 committed Sep 3, 2024
1 parent c833dc0 commit a230441
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 153 deletions.
29 changes: 29 additions & 0 deletions judge/migrations/0193_remove_old_course_problems.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.2.18 on 2024-09-03 14:33

from django.db import migrations, models


def migrate_problems_to_courselessonproblem(apps, schema_editor):
CourseLesson = apps.get_model("judge", "CourseLesson")
CourseLessonProblem = apps.get_model("judge", "CourseLessonProblem")

for lesson in CourseLesson.objects.all():
for problem in lesson.problems.all():
CourseLessonProblem.objects.create(
lesson=lesson, problem=problem, order=1, score=1
)


class Migration(migrations.Migration):

dependencies = [
("judge", "0192_course_lesson_problem"),
]

operations = [
migrations.RunPython(migrate_problems_to_courselessonproblem),
migrations.RemoveField(
model_name="courselesson",
name="problems",
),
]
10 changes: 9 additions & 1 deletion judge/models/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,18 @@ class CourseLesson(models.Model):
)
title = models.TextField(verbose_name=_("course title"))
content = models.TextField(verbose_name=_("course content"))
problems = models.ManyToManyField(Problem, verbose_name=_("problem"), blank=True)
order = models.IntegerField(verbose_name=_("order"), default=0)
points = models.IntegerField(verbose_name=_("points"))

def get_absolute_url(self):
return reverse(
"course_lesson_detail",
args=(
self.course.slug,
self.id,
),
)


class CourseLessonProblem(models.Model):
lesson = models.ForeignKey(
Expand Down
17 changes: 10 additions & 7 deletions judge/views/course.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def get_object(self):

def get_context_data(self, **kwargs):
context = super(CourseDetail, self).get_context_data(**kwargs)
lessons = self.course.lessons.prefetch_related("problems").all()
lessons = self.course.lessons.prefetch_related("lesson_problems").all()
context["title"] = self.course.name
context["page_type"] = "home"
context["lessons"] = lessons
Expand Down Expand Up @@ -190,7 +190,7 @@ def get_context_data(self, **kwargs):
class CourseLessonForm(forms.ModelForm):
class Meta:
model = CourseLesson
fields = ["order", "title", "points", "content", "problems"]
fields = ["order", "title", "points", "content"]
widgets = {
"title": forms.TextInput(),
"content": HeavyPreviewPageDownWidget(preview=reverse_lazy("blog_preview")),
Expand Down Expand Up @@ -312,13 +312,13 @@ def get_object(self):
def get_grades(self):
students = self.course.get_students()
students.sort(key=lambda u: u.username.lower())
lessons = self.course.lessons.prefetch_related("problems").all()
lessons = self.course.lessons.prefetch_related("lesson_problems").all()
grades = {s: calculate_lessons_progress(s, lessons) for s in students}
return grades

def get_context_data(self, **kwargs):
context = super(CourseStudentResults, self).get_context_data(**kwargs)
context["title"] = _("Grades in %(course_name)s</a>") % {
context["title"] = _("Grades in %(course_name)s") % {
"course_name": self.course.name,
}
context["content_title"] = mark_safe(
Expand Down Expand Up @@ -374,16 +374,19 @@ def get_lesson_grades(self):
def get_context_data(self, **kwargs):
context = super(CourseStudentResultsLesson, self).get_context_data(**kwargs)
context["lesson"] = self.lesson
context["title"] = _("Grades of %(lesson_name)s</a> in %(course_name)s</a>") % {
context["title"] = _("Grades of %(lesson_name)s in %(course_name)s") % {
"course_name": self.course.name,
"lesson_name": self.lesson.title,
}
context["content_title"] = mark_safe(
_("Grades of %(lesson_name)s</a> in <a href='%(url)s'>%(course_name)s</a>")
_(
"Grades of <a href='%(url_lesson)s'>%(lesson_name)s</a> in <a href='%(url_course)s'>%(course_name)s</a>"
)
% {
"course_name": self.course.name,
"lesson_name": self.lesson.title,
"url": self.course.get_absolute_url(),
"url_course": self.course.get_absolute_url(),
"url_lesson": self.lesson.get_absolute_url(),
}
)
context["page_type"] = "grades"
Expand Down
Loading

0 comments on commit a230441

Please sign in to comment.