Skip to content

Commit

Permalink
chore: add taxiform updates to trigger course data_modified_timestamp…
Browse files Browse the repository at this point in the history
… changes (#4468)
  • Loading branch information
hamza-56 authored Oct 16, 2024
1 parent c8e1664 commit 9f8b59a
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
11 changes: 11 additions & 0 deletions course_discovery/apps/course_metadata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"

Expand Down
6 changes: 4 additions & 2 deletions course_discovery/apps/course_metadata/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down
57 changes: 57 additions & 0 deletions course_discovery/apps/course_metadata/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 9f8b59a

Please sign in to comment.