feat: the ability to filter active courses in course listing api (#31502)

* feat: the ability to filter active courses in course listing api
This commit is contained in:
Syed Muhammad Dawoud Sheraz Ali
2023-01-09 20:37:30 +05:00
committed by GitHub
parent df47f9df95
commit d2fd26ee91
10 changed files with 60 additions and 13 deletions

View File

@@ -5,8 +5,10 @@ Declaration of CourseOverview model
import json
import logging
from datetime import datetime
from urllib.parse import urlparse, urlunparse
import pytz
from ccx_keys.locator import CCXLocator
from config_models.models import ConfigurationModel
from django.conf import settings
@@ -655,7 +657,7 @@ class CourseOverview(TimeStampedModel):
log.info('Finished generating course overviews.')
@classmethod
def get_all_courses(cls, orgs=None, filter_=None):
def get_all_courses(cls, orgs=None, filter_=None, active_only=False):
"""
Return a queryset containing all CourseOverview objects in the database.
@@ -663,6 +665,7 @@ class CourseOverview(TimeStampedModel):
orgs (list[string]): Optional parameter that allows case-insensitive
filtering by organization.
filter_ (dict): Optional parameter that allows custom filtering.
active_only (bool): If provided, only the courses that have not ended will be returned.
"""
# Note: If a newly created course is not returned in this QueryList,
# make sure the "publish" signal was emitted when the course was
@@ -680,6 +683,10 @@ class CourseOverview(TimeStampedModel):
if filter_:
course_overviews = course_overviews.filter(**filter_)
if active_only:
course_overviews = course_overviews.filter(
Q(end__isnull=True) | Q(end__gte=datetime.now().replace(tzinfo=pytz.UTC))
)
return course_overviews

View File

@@ -554,6 +554,20 @@ class CourseOverviewTestCase(CatalogIntegrationMixin, ModuleStoreTestCase, Cache
assert {course_overview.id for course_overview in CourseOverview.get_all_courses(filter_=filter_)} ==\
expected_courses, f'testing CourseOverview.get_all_courses with filter_={filter_}'
def test_get_all_active_courses(self):
"""
Verify active courses or courses with null end date are returned if active_only is provided.
"""
active_course = CourseFactory.create(emit_signals=True, end=self.DATES[self.NEXT_MONTH])
missing_end_date = CourseFactory.create(emit_signals=True, end=None)
inactive_course = CourseFactory.create(emit_signals=True, end=self.DATES[self.LAST_MONTH])
output_ids = {course.id for course in CourseOverview.get_all_courses(active_only=True)}
assert len(output_ids) == 2
assert inactive_course.id not in output_ids
assert {active_course.id, missing_end_date.id} == output_ids
def test_get_from_ids(self):
"""
Assert that CourseOverviews.get_from_ids works as expected.