Skip to content

Commit

Permalink
fix: handle draft only courses properly in archive_courses command (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zawan-ila authored Jan 17, 2025
1 parent 01a8aaa commit 2acb71a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def handle(self, *args, **options):
conf = ArchiveCoursesConfig.current()
mangle_end_date = conf.mangle_end_date
mangle_title = conf.mangle_title
courses = Course.objects.filter(uuid__in=self.get_uuids_from_database())
courses = Course.objects.filter_drafts(uuid__in=self.get_uuids_from_database())
elif course_type:
courses = Course.objects.filter(
courses = Course.objects.filter_drafts(
type__in=[CourseType.objects.get(slug__in=[course_type, course_type.lower()])]
)
else:
Expand All @@ -90,8 +90,8 @@ def handle(self, *args, **options):
course_title = course.title
try:
self.archive(course, mangle_end_date, mangle_title)
if course.draft_version:
self.archive(course.draft_version, mangle_end_date, mangle_title)
if course.official_version:
self.archive(course.official_version, mangle_end_date, mangle_title)
except Exception as exc: # pylint: disable=broad-exception-caught
self.report['failures'].append(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ def prepare_cmd_args(self, from_db, mangle_title, mangle_end_date):
args.append('--mangle-end-date')
return args

def verify_archived(self, course, mangle_title, mangle_end_date):
for c in [course, course.draft_version]:
def verify_archived(self, course, mangle_title, mangle_end_date, is_official=True):
# is_official specifies if the course is an official version
for c in ([course, course.draft_version] if is_official else [course]):
assert c.additional_metadata.product_status == ExternalProductStatus.Archived
assert c.additional_metadata.end_date < timezone.now() + timedelta(minutes=1)
assert c.course_runs.last().status == CourseRunStatus.Unpublished
Expand All @@ -75,8 +76,9 @@ def verify_archived(self, course, mangle_title, mangle_end_date):
is_end_date_mangled = c.course_runs.first().end < timezone.now() + timedelta(minutes=1)
assert is_end_date_mangled if mangle_end_date else not is_end_date_mangled

def verify_not_archived(self, course):
for c in [course, course.draft_version]:
def verify_not_archived(self, course, is_official=True):
# is_official specifies if the course is an official version
for c in ([course, course.draft_version] if is_official else [course]):
assert c.additional_metadata.product_status == ExternalProductStatus.Published
assert c.additional_metadata.end_date > timezone.now() + timedelta(minutes=1)
assert c.course_runs.last().status == CourseRunStatus.Published
Expand All @@ -85,16 +87,23 @@ def verify_not_archived(self, course):
assert not c.course_runs.first().end < timezone.now() + timedelta(minutes=1)

@ddt.data(
*list(product([0, 1], repeat=3))
*list(product([0, 1], repeat=4))
)
@ddt.unpack
@responses.activate
def test_success(self, from_db, mangle_title, mangle_end_date):
# Some sanity checks on counts
def test_success(self, from_db, mangle_title, mangle_end_date, draft_only):
# draft_only specifies the case when the objects only have draft versions
for model in [Course, CourseRun]:
assert model.objects.count() == 2
assert model.everything.count() == 4

if draft_only:
self.course1.delete()
self.course2.delete()
for model in [Course, CourseRun]:
assert model.objects.count() == 0
assert model.everything.count() == 2

if from_db:
ArchiveCoursesConfigFactory.create(
csv_file=self.csv_file,
Expand All @@ -112,13 +121,16 @@ def test_success(self, from_db, mangle_title, mangle_end_date):
args = ['--from-db']
call_command('archive_courses', *args)

self.course1.refresh_from_db()
self.course2.refresh_from_db()
self.verify_archived(self.course1, mangle_title, mangle_end_date)
self.verify_not_archived(self.course2)
course1 = Course.everything.get(uuid=self.course1.uuid, draft=draft_only)
course2 = Course.everything.get(uuid=self.course2.uuid, draft=draft_only)
course1.refresh_from_db()
course2.refresh_from_db()
self.verify_archived(course1, mangle_title, mangle_end_date, not draft_only)
self.verify_not_archived(course2, not draft_only)

mangle_call_count = 1 if draft_only else 2
if mangle_end_date:
assert responses.assert_call_count(self.courserun1_studio_url, 2) is True
assert responses.assert_call_count(self.courserun1_studio_url, mangle_call_count) is True
else:
assert responses.assert_call_count(self.courserun1_studio_url, 0) is True

Expand Down

0 comments on commit 2acb71a

Please sign in to comment.