Skip to content

Commit 9f8b59a

Browse files
authored
chore: add taxiform updates to trigger course data_modified_timestamp changes (#4468)
1 parent c8e1664 commit 9f8b59a

File tree

3 files changed

+72
-2
lines changed

3 files changed

+72
-2
lines changed

course_discovery/apps/course_metadata/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,17 @@ def has_changed(self):
882882
return False
883883
return self.has_model_changed()
884884

885+
def update_product_data_modified_timestamp(self, bypass_has_changed=False):
886+
if self.has_changed or bypass_has_changed:
887+
logger.info(
888+
f"TaxiForm update_product_data_modified_timestamp triggered for {self.form_id}."
889+
f"Updating data modified timestamp for related courses."
890+
)
891+
if self.additional_metadata:
892+
self.additional_metadata.related_courses.all().update(
893+
data_modified_timestamp=datetime.datetime.now(pytz.UTC)
894+
)
895+
885896
def __str__(self):
886897
return f"{self.title}({self.form_id})"
887898

course_discovery/apps/course_metadata/signals.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from course_discovery.apps.course_metadata.models import (
2222
AdditionalMetadata, CertificateInfo, Course, CourseEditor, CourseEntitlement, CourseLocationRestriction, CourseRun,
2323
Curriculum, CurriculumCourseMembership, CurriculumProgramMembership, Fact, GeoLocation, Organization, ProductMeta,
24-
ProductValue, Program, Seat
24+
ProductValue, Program, Seat, TaxiForm
2525
)
2626
from course_discovery.apps.course_metadata.publishers import ProgramMarketingSitePublisher
2727
from course_discovery.apps.course_metadata.salesforce import (
@@ -508,6 +508,7 @@ def connect_course_data_modified_timestamp_related_models():
508508
ProductValue,
509509
Seat,
510510
Fact,
511+
TaxiForm,
511512
]:
512513
pre_save.connect(data_modified_timestamp_update, sender=model)
513514

@@ -528,7 +529,8 @@ def disconnect_course_data_modified_timestamp_related_models():
528529
ProductMeta,
529530
ProductValue,
530531
Seat,
531-
Fact
532+
Fact,
533+
TaxiForm
532534
]:
533535
pre_save.disconnect(data_modified_timestamp_update, sender=model)
534536

course_discovery/apps/course_metadata/tests/test_models.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2528,6 +2528,16 @@ class TestAbstractHeadingBlurbModel(AbstractHeadingBlurbModel):
25282528
class TaxiFormTests(TestCase):
25292529
""" Tests for the `TaxiForm` model. """
25302530

2531+
@classmethod
2532+
def setUpClass(cls):
2533+
super().setUpClass()
2534+
disconnect_course_data_modified_timestamp_related_models()
2535+
2536+
@classmethod
2537+
def tearDownClass(cls):
2538+
connect_course_data_modified_timestamp_related_models()
2539+
super().tearDownClass()
2540+
25312541
def setUp(self):
25322542
super().setUp()
25332543
self.taxi_form = factories.TaxiFormFactory()
@@ -2548,6 +2558,53 @@ def test_no_subtitle(self):
25482558
self.taxi_form = factories.TaxiFormFactory(subtitle=None)
25492559
assert self.taxi_form.subtitle is None
25502560

2561+
def test_update_product_data_modified_timestamp(self):
2562+
""" Verify updating TaxiForm updates data_modified_timestamp of related courses """
2563+
taxi_form = factories.TaxiFormFactory()
2564+
additional_metadata = AdditionalMetadataFactory(taxi_form=taxi_form)
2565+
course1 = CourseFactory(additional_metadata=additional_metadata)
2566+
course2 = CourseFactory(additional_metadata=additional_metadata)
2567+
2568+
course1_timestamp = course1.data_modified_timestamp
2569+
course2_timestamp = course2.data_modified_timestamp
2570+
2571+
taxi_form.title = "Updated Title"
2572+
taxi_form.update_product_data_modified_timestamp()
2573+
taxi_form.save()
2574+
2575+
course1.refresh_from_db()
2576+
course2.refresh_from_db()
2577+
2578+
assert course1_timestamp < course1.data_modified_timestamp
2579+
assert course2_timestamp < course2.data_modified_timestamp
2580+
2581+
def test_update_product_data_modified_timestamp_no_change(self):
2582+
""" Verify TaxiForm update doesn't change data_modified_timestamp if no fields changed """
2583+
taxi_form = factories.TaxiFormFactory()
2584+
additional_metadata = AdditionalMetadataFactory(taxi_form=taxi_form)
2585+
course = CourseFactory(additional_metadata=additional_metadata)
2586+
2587+
course_timestamp = course.data_modified_timestamp
2588+
2589+
taxi_form.update_product_data_modified_timestamp()
2590+
taxi_form.save()
2591+
2592+
course.refresh_from_db()
2593+
2594+
assert course.data_modified_timestamp == course_timestamp
2595+
2596+
def test_update_product_data_modified_timestamp_no_related_courses(self):
2597+
""" Verify TaxiForm update doesn't cause issues when there are no related courses """
2598+
taxi_form = factories.TaxiFormFactory()
2599+
AdditionalMetadataFactory(taxi_form=taxi_form)
2600+
2601+
taxi_form.title = "Updated Title"
2602+
2603+
taxi_form.update_product_data_modified_timestamp()
2604+
taxi_form.save()
2605+
2606+
assert Course.objects.filter(additional_metadata__taxi_form=taxi_form).count() == 0
2607+
25512608

25522609
@ddt.ddt
25532610
@pytest.mark.django_db

0 commit comments

Comments
 (0)