Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

marslanabdulrauf
Copy link
Contributor

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):

image

How can this be tested?

Scenario 1:

  1. Create a CoursePage in CMS
  2. Try to create ExternalCoursePage with same course -- You should get error

Scenario 2:

  1. Create an ExternalCoursePage in CMS
  2. Try to create CoursePage with the same course -- You should get error

@Anas12091101 Anas12091101 self-assigned this Feb 24, 2025
@arslanashraf7 arslanashraf7 changed the title Validation added to restrict multiple course page with same course fix: Validation added to restrict multiple course page with same course Feb 25, 2025
Copy link
Contributor

@Anas12091101 Anas12091101 left a 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.

Comment on lines 2427 to 2454
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.']}"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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):
Copy link
Contributor

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
Comment on lines 1511 to 1527
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()

Copy link
Contributor

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.

Copy link

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.

@marslanabdulrauf
Copy link
Contributor Author

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.

@marslanabdulrauf
Copy link
Contributor Author

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.

from cms.models import CoursePage, ExternalCoursePage

# Find courses that have both an internal and external course page
duplicate_courses = CoursePage.objects.filter(
    course__in=ExternalCoursePage.objects.values_list("course", flat=True)
).values_list("course__id", flat=True)

# Fetch the duplicate CoursePage and ExternalCoursePage instances
duplicate_internal_pages = CoursePage.objects.filter(course_id__in=duplicate_courses)
duplicate_external_pages = ExternalCoursePage.objects.filter(course_id__in=duplicate_courses)

Let me know if you find any duplicates or if further help is needed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants