mitodl / mitxonline

BSD 3-Clause "New" or "Revised" License
4 stars 2 forks source link

Use hasattr() rather than directly referencing the property when checking for course.page #2236

Closed JenniWhitman closed 1 month ago

JenniWhitman commented 1 month ago

What are the relevant tickets?

In response to https://github.com/mitodl/mitxonline/pull/2235 which is a fix for issues with the deploy of https://github.com/mitodl/mitxonline/pull/2228

Why?

Since the deploy of the above pull request in RC, you'll see a 500 on the API and the catalog page will not load due to a RelatedObjectDoesNotExist error. There was a PR put up to fix this, but I found a couple of issues so I put up this PR as a response. This PR is meant to address the 500 errors without disrupting other parts of the application and to fully address the issue as the other PR only covered the courses API and the programs API is seeing the same issue.

Description (What does it do?)

I reverted the change to the property on the Course model and instead I changed the get_topics function in the serializers to use if hasattr(instance, "page") and instance.page is not None: rather than instance.page as it's possible there is no page and invoking the property without it existing causes the error in question, but since hasattr actually retrieves the attribute via getattr and checks for an exception, it does the check more gracefully. An alternative would be to try/catch in its place, but it's technically the same operation.

How can this be tested?

Prior to pulling the branch, on your local instance, remove the CoursePage of one course that is presently available for enrollment. This can be accomplished in the CMS, admin, or database. The pages area of the CMS is fairly quick, navigate to courses, find a course that you would like to remove the page for, click the button with three dots and select the delete option. Navigate to the following: http://mitxonline.odl.local:8013/api/v2/courses/ http://mitxonline.odl.local:8013/api/v2/programs/ http://mitxonline.odl.local:8013/ http://mitxonline.odl.local:8013/catalog and anywhere else that might be affected by changes to courses, coursepages, and topics.

You will see blank pages and/or 500 errors. If you look at logs you will see a RelatedObjectDoesNotExist error

Pull the branch, and with no changes to your data, revisit those same locations. Things should now work. Put the course page for the course back and double check that things still work once this has been restored.