From b75dfe8e117da980475f19edbfb39b8a6d846a91 Mon Sep 17 00:00:00 2001 From: Alexander Dusenbery Date: Fri, 18 Oct 2024 13:29:25 -0400 Subject: [PATCH] fix: clear and reset restricted run relationships --- enterprise_catalog/apps/catalog/models.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/enterprise_catalog/apps/catalog/models.py b/enterprise_catalog/apps/catalog/models.py index c13b65e3..95b284cf 100644 --- a/enterprise_catalog/apps/catalog/models.py +++ b/enterprise_catalog/apps/catalog/models.py @@ -883,16 +883,23 @@ def store_canonical_record(cls, course_metadata_dict): def store_record_with_query(cls, course_metadata_dict, catalog_query): filtered_metadata = cls.filter_restricted_runs(course_metadata_dict, catalog_query) course_record = cls._store_record(filtered_metadata, catalog_query) + relationships = [] + for course_run_dict in cls.restricted_runs_for_course(filtered_metadata, catalog_query): course_run_record, _ = ContentMetadata.objects.get_or_create( content_key=course_run_dict['key'], content_type=COURSE_RUN, defaults=_restricted_content_defaults(course_run_dict), ) - RestrictedRunAllowedForRestrictedCourse.objects.get_or_create( + relationship, _ = RestrictedRunAllowedForRestrictedCourse.objects.get_or_create( course=course_record, run=course_run_record, ) + relationships.append(relationship) + # We use clear=True to clear and then reset the related allowed runs + # for this restricted course. This is necessary in the case that a previously-allowed + # run becomes unassociated from the restricted course. + course_record.restricted_run_allowed_for_restricted_course.set(relationships, clear=True) return course_record @classmethod