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

Optimize summaries page with queryable properties #3890

Merged
merged 1 commit into from
Jan 22, 2025
Merged
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
11 changes: 11 additions & 0 deletions website/education/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import datetime

from django.db import models
from django.db.models import Count, Q
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from queryable_properties.managers import QueryablePropertiesManager
from queryable_properties.properties import AggregateProperty

from members.models import Member
from utils.snippets import datetime_to_lectureyear

Expand All @@ -29,6 +33,8 @@ class Meta:
class Course(models.Model):
"""Describes a course."""

objects = QueryablePropertiesManager()

name = models.CharField(max_length=255)

categories = models.ManyToManyField(
Expand All @@ -52,6 +58,11 @@ def __str__(self):
def get_absolute_url(self):
return reverse("education:course", args=[str(self.pk)])

summary_count = AggregateProperty(
Count("summary", filter=Q(summary__accepted=True))
)
exam_count = AggregateProperty(Count("exam", filter=Q(exam__accepted=True)))

class Meta:
ordering = ["-pk"]
verbose_name = _("course")
Expand Down
23 changes: 16 additions & 7 deletions website/education/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, DetailView, ListView, TemplateView

from queryable_properties.utils import prefetch_queryable_properties

from members.decorators import membership_required
from utils.media.services import get_media_url

Expand All @@ -23,9 +25,17 @@
class CourseIndexView(ListView):
"""Render an overview of the courses."""

queryset = Course.objects.filter(until=None).prefetch_related(
"categories", "old_courses"
)
def get_queryset(self):
queryset = (
Course.objects.filter(until=None)
.prefetch_related("categories", "old_courses")
.select_properties("summary_count", "exam_count")
.order_by("name")
)
prefetch_queryable_properties(queryset, "old_courses__summary_count")
prefetch_queryable_properties(queryset, "old_courses__exam_count")
return queryset

template_name = "education/courses.html"

def get_ordering(self) -> str:
Expand All @@ -42,12 +52,11 @@ def get_context_data(self, **kwargs) -> dict:
"categories": x.categories.all(),
"document_count": sum(
[
x.summary_set.filter(accepted=True).count(),
x.exam_set.filter(accepted=True).count(),
x.summary_count,
x.exam_count,
]
+ [
c.summary_set.filter(accepted=True).count()
+ c.exam_set.filter(accepted=True).count()
c.summary_count + c.exam_count
for c in x.old_courses.all()
]
),
Expand Down
Loading