Skip to content

Commit

Permalink
Fix queries for counts in course data export (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
smartspot2 authored Dec 6, 2024
1 parent 21563c3 commit 31c3e10
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
4 changes: 3 additions & 1 deletion csm_web/scheduler/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,9 @@ def get_fields(self, request, obj=None):
@admin.display(description="Section count")
def get_section_count(self, obj: Course):
"""Retrieve the number of sections associated with this course."""
return obj.mentor_set.count() # one-to-one between mentor objects and sections
# only count mentors that have a section associated with it;
# at most one section can be associated with a given mentor object
return obj.mentor_set.filter(~Q(section=None)).count()

@admin.display(description="Student count")
def get_student_count(self, obj: Course):
Expand Down
8 changes: 5 additions & 3 deletions csm_web/scheduler/views/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,18 +365,20 @@ def prepare_course_data(
export_headers.append("Course title")
if "num_sections" in fields:
course_queryset = course_queryset.annotate(
num_sections=Count("mentor__section")
num_sections=Count("mentor__section", distinct=True)
)
export_fields.append("num_sections")
export_headers.append("Number of sections")
if "num_students" in fields:
course_queryset = course_queryset.annotate(
num_students=Count("mentor__section__students")
num_students=Count("mentor__section__students", distinct=True)
)
export_fields.append("num_students")
export_headers.append("Number of students")
if "num_mentors" in fields:
course_queryset = course_queryset.annotate(num_mentors=Count("mentor"))
course_queryset = course_queryset.annotate(
num_mentors=Count("mentor", distinct=True)
)
export_fields.append("num_mentors")
export_headers.append("Number of mentors")

Expand Down

0 comments on commit 31c3e10

Please sign in to comment.