def test_models_course_run_mark_dirty_direct_course_field(self): """ Changing the course to which a course run is related should mark both the source and the target course pages dirty (waiting to be published). """ course_run = CourseRunFactory() course_source = course_run.direct_course course_target = CourseFactory(should_publish=True) self.assertTrue(course_source.extended_object.publish("en")) title_obj_source = course_source.extended_object.title_set.first() title_obj_target = course_target.extended_object.title_set.first() course_run.direct_course = course_target course_run.save() self.assertEqual(title_obj_source.publisher_state, PUBLISHER_STATE_DEFAULT) self.assertEqual(title_obj_target.publisher_state, PUBLISHER_STATE_DEFAULT) course_run.mark_course_dirty() title_obj_source.refresh_from_db() title_obj_target.refresh_from_db() self.assertEqual(title_obj_source.publisher_state, PUBLISHER_STATE_DIRTY) self.assertEqual(title_obj_target.publisher_state, PUBLISHER_STATE_DIRTY)
def test_models_course_run_mark_dirty_parler(self): """ Updating the value of a field translatable via parler should mark the related course page dirty (waiting to be published) only in the impacted language. """ course = CourseFactory(page_languages=["en", "fr"]) course_run = CourseRunFactory(direct_course=course) CourseRunTranslation.objects.create(master=course_run, language_code="fr", title="mon titre") self.assertTrue(course_run.direct_course.extended_object.publish("en")) self.assertTrue(course_run.direct_course.extended_object.publish("fr")) course_run.refresh_from_db() self.assertIsNotNone(course_run.public_course_run) title_query = course_run.direct_course.extended_object.title_set title_obj_en = title_query.get(language="en") title_obj_fr = title_query.get(language="fr") self.assertEqual(title_query.count(), 2) with switch_language(course_run, "fr"): course_run.title = "nouveau titre" course_run.save() self.assertEqual(title_obj_en.publisher_state, PUBLISHER_STATE_DEFAULT) self.assertEqual(title_obj_fr.publisher_state, PUBLISHER_STATE_DEFAULT) course_run.mark_course_dirty() title_obj_en.refresh_from_db() title_obj_fr.refresh_from_db() self.assertEqual(title_obj_en.publisher_state, PUBLISHER_STATE_DEFAULT) self.assertEqual(title_obj_fr.publisher_state, PUBLISHER_STATE_DIRTY)
def test_models_course_run_copy_translations_one_language(self): """ The "copy_translations" method called for a specific language should only port parler's translations for this language from one course run to the other. """ course_run = CourseRunFactory(title="my title") CourseRunTranslation.objects.create(master=course_run, language_code="fr", title="mon titre") old_course_run = CourseRun.objects.get(pk=course_run.pk) self.assertEqual(CourseRunTranslation.objects.count(), 2) with switch_language(old_course_run, "fr"): self.assertEqual(old_course_run.title, "mon titre") course_run.pk = None course_run.save() self.assertEqual(CourseRunTranslation.objects.count(), 2) course_run.copy_translations(old_course_run, language="fr") self.assertEqual(CourseRun.objects.count(), 2) self.assertEqual(CourseRunTranslation.objects.count(), 3) self.assertEqual( CourseRunTranslation.objects.filter(master=course_run).count(), 1) course_run.refresh_from_db() self.assertEqual(course_run.title, "mon titre") # Fallback to french with switch_language(course_run, "fr"): self.assertEqual(course_run.title, "mon titre")
def test_models_course_run_copy_translations_all_languages(self): """ The "copy_translations" method should port parler's translations for all languages from one course run to the other. """ course_run = CourseRunFactory(title="my title") CourseRunTranslation.objects.create(master=course_run, language_code="fr", title="mon titre") old_course_run = CourseRun.objects.get(pk=course_run.pk) self.assertEqual(CourseRunTranslation.objects.count(), 2) with switch_language(old_course_run, "fr"): self.assertEqual(old_course_run.title, "mon titre") course_run.pk = None course_run.save() self.assertEqual(CourseRunTranslation.objects.count(), 2) course_run.copy_translations(old_course_run) self.assertEqual(CourseRun.objects.count(), 2) self.assertEqual(CourseRunTranslation.objects.count(), 4) self.assertEqual( CourseRunTranslation.objects.filter(master=course_run).count(), 2) course_run.refresh_from_db() self.assertEqual(course_run.title, "my title") with switch_language(course_run, "fr"): self.assertEqual(course_run.title, "mon titre")
def test_models_course_run_mark_dirty_any_field(self): """ Updating the value of an editable field on the course run should mark the related course page dirty (waiting to be published). """ course_run = CourseRunFactory() self.assertTrue(course_run.direct_course.extended_object.publish("en")) title_obj = course_run.direct_course.extended_object.title_set.first() field = random.choice([ f for f in course_run._meta.fields if f.editable and not f.auto_created and not f.name == "direct_course" ]).name stub = CourseRunFactory( sync_mode="manual") # New random values to update our course run setattr(course_run, field, getattr(stub, field)) course_run.save() self.assertEqual(title_obj.publisher_state, PUBLISHER_STATE_DEFAULT) course_run.mark_course_dirty() title_obj.refresh_from_db() self.assertEqual(title_obj.publisher_state, PUBLISHER_STATE_DIRTY)
def test_models_course_run_mark_dirty_update_to_be_scheduled_remain(self): """ Modifying a course run to be scheduled but keeping its state "to be scheduled" should not mark the related course page dirty. """ course_run = CourseRunFactory(start=None) self.assertTrue(course_run.direct_course.extended_object.publish("en")) title_obj = course_run.direct_course.extended_object.title_set.first() course_run.end = timezone.now() course_run.save() title_obj.refresh_from_db() self.assertEqual(title_obj.publisher_state, PUBLISHER_STATE_DEFAULT)
def test_models_course_run_mark_dirty_update_to_be_scheduled_to(self): """ Resetting a scheduled course run to a state "to be scheduled" should mark the related course page dirty. """ course_run = CourseRunFactory() self.assertTrue(course_run.direct_course.extended_object.publish("en")) title_obj = course_run.direct_course.extended_object.title_set.first() course_run.start = None course_run.save() title_obj.refresh_from_db() self.assertEqual(title_obj.publisher_state, PUBLISHER_STATE_DIRTY)
def test_models_course_run_mark_dirty_update_to_be_scheduled_from(self): """ Scheduling a course run that was to be scheduled should mark the related course page dirty. """ now = timezone.now() course_run = CourseRunFactory(start=None, enrollment_start=now) self.assertTrue(course_run.direct_course.extended_object.publish("en")) title_obj = course_run.direct_course.extended_object.title_set.first() course_run.start = now course_run.save() title_obj.refresh_from_db() self.assertEqual(title_obj.publisher_state, PUBLISHER_STATE_DIRTY)
def test_models_course_run_mark_dirty_any_field(self): """ Updating the value of any editable field on the course run should mark the related course page dirty (waiting to be published). """ fields = map( lambda f: f.name, filter( lambda f: f.editable and not f.auto_created and not f.name == "direct_course", CourseRun._meta.fields, ), ) stub = CourseRunFactory( sync_mode="manual", catalog_visibility=CourseRunCatalogVisibility.COURSE_ONLY, ) # New random values to update our course run for field in fields: course_run = CourseRunFactory() self.assertTrue( course_run.direct_course.extended_object.publish("en")) title_obj = course_run.direct_course.extended_object.title_set.first( ) setattr(course_run, field, getattr(stub, field)) course_run.save() self.assertEqual( title_obj.publisher_state, PUBLISHER_STATE_DEFAULT, msg=f"Before refreshing from db {field:s}", ) course_run.mark_course_dirty() title_obj.refresh_from_db() self.assertEqual( title_obj.publisher_state, PUBLISHER_STATE_DIRTY, msg=f"After refreshing from db {field:s}", )