From 9f8b59a4b74e63f3773cf96892d1645b22be3e9e Mon Sep 17 00:00:00 2001 From: Hamza Shafique <41573849+hamza-56@users.noreply.github.com> Date: Wed, 16 Oct 2024 18:36:32 +0500 Subject: [PATCH] chore: add taxiform updates to trigger course data_modified_timestamp changes (#4468) --- .../apps/course_metadata/models.py | 11 ++++ .../apps/course_metadata/signals.py | 6 +- .../apps/course_metadata/tests/test_models.py | 57 +++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/course_discovery/apps/course_metadata/models.py b/course_discovery/apps/course_metadata/models.py index da455cb94e..c0dad00d84 100644 --- a/course_discovery/apps/course_metadata/models.py +++ b/course_discovery/apps/course_metadata/models.py @@ -882,6 +882,17 @@ def has_changed(self): return False return self.has_model_changed() + def update_product_data_modified_timestamp(self, bypass_has_changed=False): + if self.has_changed or bypass_has_changed: + logger.info( + f"TaxiForm update_product_data_modified_timestamp triggered for {self.form_id}." + f"Updating data modified timestamp for related courses." + ) + if self.additional_metadata: + self.additional_metadata.related_courses.all().update( + data_modified_timestamp=datetime.datetime.now(pytz.UTC) + ) + def __str__(self): return f"{self.title}({self.form_id})" diff --git a/course_discovery/apps/course_metadata/signals.py b/course_discovery/apps/course_metadata/signals.py index 00c6f4ed69..d5988a68b1 100644 --- a/course_discovery/apps/course_metadata/signals.py +++ b/course_discovery/apps/course_metadata/signals.py @@ -21,7 +21,7 @@ from course_discovery.apps.course_metadata.models import ( AdditionalMetadata, CertificateInfo, Course, CourseEditor, CourseEntitlement, CourseLocationRestriction, CourseRun, Curriculum, CurriculumCourseMembership, CurriculumProgramMembership, Fact, GeoLocation, Organization, ProductMeta, - ProductValue, Program, Seat + ProductValue, Program, Seat, TaxiForm ) from course_discovery.apps.course_metadata.publishers import ProgramMarketingSitePublisher from course_discovery.apps.course_metadata.salesforce import ( @@ -508,6 +508,7 @@ def connect_course_data_modified_timestamp_related_models(): ProductValue, Seat, Fact, + TaxiForm, ]: pre_save.connect(data_modified_timestamp_update, sender=model) @@ -528,7 +529,8 @@ def disconnect_course_data_modified_timestamp_related_models(): ProductMeta, ProductValue, Seat, - Fact + Fact, + TaxiForm ]: pre_save.disconnect(data_modified_timestamp_update, sender=model) diff --git a/course_discovery/apps/course_metadata/tests/test_models.py b/course_discovery/apps/course_metadata/tests/test_models.py index 37c4a947dd..4dcfd1b6ee 100644 --- a/course_discovery/apps/course_metadata/tests/test_models.py +++ b/course_discovery/apps/course_metadata/tests/test_models.py @@ -2528,6 +2528,16 @@ class TestAbstractHeadingBlurbModel(AbstractHeadingBlurbModel): class TaxiFormTests(TestCase): """ Tests for the `TaxiForm` model. """ + @classmethod + def setUpClass(cls): + super().setUpClass() + disconnect_course_data_modified_timestamp_related_models() + + @classmethod + def tearDownClass(cls): + connect_course_data_modified_timestamp_related_models() + super().tearDownClass() + def setUp(self): super().setUp() self.taxi_form = factories.TaxiFormFactory() @@ -2548,6 +2558,53 @@ def test_no_subtitle(self): self.taxi_form = factories.TaxiFormFactory(subtitle=None) assert self.taxi_form.subtitle is None + def test_update_product_data_modified_timestamp(self): + """ Verify updating TaxiForm updates data_modified_timestamp of related courses """ + taxi_form = factories.TaxiFormFactory() + additional_metadata = AdditionalMetadataFactory(taxi_form=taxi_form) + course1 = CourseFactory(additional_metadata=additional_metadata) + course2 = CourseFactory(additional_metadata=additional_metadata) + + course1_timestamp = course1.data_modified_timestamp + course2_timestamp = course2.data_modified_timestamp + + taxi_form.title = "Updated Title" + taxi_form.update_product_data_modified_timestamp() + taxi_form.save() + + course1.refresh_from_db() + course2.refresh_from_db() + + assert course1_timestamp < course1.data_modified_timestamp + assert course2_timestamp < course2.data_modified_timestamp + + def test_update_product_data_modified_timestamp_no_change(self): + """ Verify TaxiForm update doesn't change data_modified_timestamp if no fields changed """ + taxi_form = factories.TaxiFormFactory() + additional_metadata = AdditionalMetadataFactory(taxi_form=taxi_form) + course = CourseFactory(additional_metadata=additional_metadata) + + course_timestamp = course.data_modified_timestamp + + taxi_form.update_product_data_modified_timestamp() + taxi_form.save() + + course.refresh_from_db() + + assert course.data_modified_timestamp == course_timestamp + + def test_update_product_data_modified_timestamp_no_related_courses(self): + """ Verify TaxiForm update doesn't cause issues when there are no related courses """ + taxi_form = factories.TaxiFormFactory() + AdditionalMetadataFactory(taxi_form=taxi_form) + + taxi_form.title = "Updated Title" + + taxi_form.update_product_data_modified_timestamp() + taxi_form.save() + + assert Course.objects.filter(additional_metadata__taxi_form=taxi_form).count() == 0 + @ddt.ddt @pytest.mark.django_db