-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Validation added to restrict multiple course page with same course #3407
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @marslanabdulrauf for creating this PR. I have some questions:
In addition to applying validations, can we filter courses using the is_external field when creating CoursePage and ExternalCoursePage?
This would ensure that only internal courses appear in the course list for CoursePage, while external courses appear in the list for ExternalCoursePage.
cms/models_test.py
Outdated
def test_external_course_page_with_same_course_run_in_internal_course_page(): | ||
""" | ||
Tests that a CoursePage with the same course run as another CoursePage. | ||
""" | ||
course_page = CoursePageFactory.create() | ||
|
||
with pytest.raises(ValidationError) as context: | ||
ExternalCoursePageFactory.create(course=course_page.course) | ||
|
||
assert ( | ||
str(context.value) | ||
== "{'__all__': ['There is already an internal course page associated with this course.']}" | ||
) | ||
|
||
|
||
def test_internal_course_page_with_same_course_run_in_external_course_page(): | ||
""" | ||
Tests that an ExternalCoursePage with the same course run as another ExternalCoursePage. | ||
""" | ||
external_course_page = ExternalCoursePageFactory.create() | ||
|
||
with pytest.raises(ValidationError) as context: | ||
CoursePageFactory.create(course=external_course_page.course) | ||
|
||
assert ( | ||
str(context.value) | ||
== "{'__all__': ['There is already an external course page associated with this course.']}" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def test_external_course_page_with_same_course_run_in_internal_course_page(): | |
""" | |
Tests that a CoursePage with the same course run as another CoursePage. | |
""" | |
course_page = CoursePageFactory.create() | |
with pytest.raises(ValidationError) as context: | |
ExternalCoursePageFactory.create(course=course_page.course) | |
assert ( | |
str(context.value) | |
== "{'__all__': ['There is already an internal course page associated with this course.']}" | |
) | |
def test_internal_course_page_with_same_course_run_in_external_course_page(): | |
""" | |
Tests that an ExternalCoursePage with the same course run as another ExternalCoursePage. | |
""" | |
external_course_page = ExternalCoursePageFactory.create() | |
with pytest.raises(ValidationError) as context: | |
CoursePageFactory.create(course=external_course_page.course) | |
assert ( | |
str(context.value) | |
== "{'__all__': ['There is already an external course page associated with this course.']}" | |
) | |
@pytest.mark.parametrize( | |
"existing_factory, new_factory, expected_error", | |
[ | |
(CoursePageFactory, ExternalCoursePageFactory, | |
"{'__all__': ['There is already an internal course page associated with this course.']}"), | |
(ExternalCoursePageFactory, CoursePageFactory, | |
"{'__all__': ['There is already an external course page associated with this course.']}"), | |
] | |
) | |
def test_prevent_duplicate_course_pages(existing_factory, new_factory, expected_error): | |
""" | |
Tests that creating a course page with the same course run as another course page raises a ValidationError. | |
""" | |
existing_page = existing_factory.create() | |
with pytest.raises(ValidationError) as context: | |
new_factory.create(course=existing_page.course) | |
assert str(context.value) == expected_error |
We can parameterize the test here to avoid code duplication.
@@ -1507,6 +1507,24 @@ def product(self): | |||
"""Gets the product associated with this page""" | |||
return self.course | |||
|
|||
def clean(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a doc string
cms/models.py
Outdated
if ( | ||
isinstance(self, CoursePage) | ||
and ExternalCoursePage.objects.filter(course=self.course).exists() | ||
): | ||
raise ValidationError( | ||
"There is already an external course page associated with this course." | ||
) | ||
elif ( | ||
isinstance(self, ExternalCoursePage) | ||
and CoursePage.objects.filter(course=self.course).exists() | ||
): | ||
raise ValidationError( | ||
"There is already an internal course page associated with this course." | ||
) | ||
|
||
super().clean() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do we plan to handle the existing incorrect data? Should the wrong page be deleted or unpublished? In a similar case, Bon previously unpublished the course page: https://github.com/mitodl/hq/issues/6107#issuecomment-2632165868.
@cachob, I'd love to hear your thoughts on this.
If we decide to go with unpublishing, we may need to update the logic accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's delete the pages - one of our objectives to make the CMS tidy.
As per our discussion (@arslanashraf7, @Anas12091101), filtering courses based on the course page could lead to unexpected scenarios. For now, we will stick with raising a validation error. |
Hi @cachob, I queried the RC instance and did not find any duplicates. Since I don't have Production access, could you please run the following query on Production to identify any duplicate CoursePage or ExternalCoursePage instances? If duplicates exist, we can then determine which pages need to be deleted.
Let me know if you find any duplicates or if further help is needed. Thanks! |
What are the relevant tickets?
https://github.com/mitodl/hq/issues/6749
Description (What does it do?)
This PR adds Validation to restrict creating multiple course pages with same course object
Screenshots (if appropriate):
How can this be tested?
Scenario 1:
Scenario 2: