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

Fix queries for counts in course data export #501

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
Loading