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: cast course_id to CourseKey when loading CourseOverview #29686

Merged
merged 1 commit into from
Apr 29, 2022

Conversation

mariajgrimaldi
Copy link
Member

@mariajgrimaldi mariajgrimaldi commented Dec 27, 2021

Description

When creating a course mode using the API for a course which course overview object version is less than the CourseOverview class version, an assertion error is raised.

  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/mongodb_proxy.py", line 55, in wrapper
    return func(*args, **kwargs)
  File "/edx/app/edxapp/edx-platform/common/lib/xmodule/xmodule/modulestore/mongo/base.py", line 1128, in has_course
    assert isinstance(course_key, CourseKey)
AssertionError

This happens because the API serializer calls CourseOverview.get_from_id (here) with a bare string, and then loads the course overview from the modulestore if the version condition is met (here). When the method tries to load the object with the string as an identifier, raises the assertion error(here).

Supporting information

The course modes API was created based on this Jira issue

Testing instructions

With a course that meets the requirement instance.version < CourseOverview.version make the following call

curl -X POST "https://<domain>/api/course_modes/v1/courses/course-v1%3Atesting-site%2BTS%2B00/" -H  "accept: application/json" -H  "Content-Type: application/json" -H  "X-CSRFToken: 5lillyFTelMhHy9ne4i8HJ7IbU7SQe4mpAsRr0OZBoWZrqYqNWhxEmP2BHHClYYd" -d "{  \"course_id\": \"course-v1:testing-site+TS+00\",  \"mode_slug\": \"honor\",  \"mode_display_name\": \"Honor\",  \"currency\": \"usd\"}"

That results in:

2021-12-27 19:52:55,381 INFO 2870331 [openedx.core.djangoapps.content.course_overviews.models] [user 261689] [ip 201.234.236.10] models.py:260 - Attempting to load CourseOverview for course course-v1:testing-site+TS+00 from modulestore.
2021-12-27 19:52:55,661 ERROR 2870331 [root] [user None] [ip None] signals.py:22 - Uncaught exception from None
Traceback (most recent call last):
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/usr/lib/python3.8/contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/rest_framework/generics.py", line 242, in post
    return self.create(request, *args, **kwargs)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/rest_framework/mixins.py", line 19, in create
    self.perform_create(serializer)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/rest_framework/mixins.py", line 24, in perform_create
    serializer.save()
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/rest_framework/serializers.py", line 205, in save
    self.instance = self.create(validated_data)
  File "/edx/app/edxapp/edx-platform/common/djangoapps/course_modes/rest_api/serializers.py", line 44, in create
    course_overview = CourseOverview.get_from_id(validated_data['course_id'])
  File "/edx/app/edxapp/edx-platform/openedx/core/lib/cache_utils.py", line 74, in decorator
    result = wrapped(*args, **kwargs)
  File "/edx/app/edxapp/edx-platform/openedx/core/djangoapps/content/course_overviews/models.py", line 373, in get_from_id
    course_overview = cls.load_from_module_store(course_id)
  File "/edx/app/edxapp/edx-platform/openedx/core/djangoapps/content/course_overviews/models.py", line 265, in load_from_module_store
    with store.bulk_operations(course_id):
  File "/usr/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/edx/app/edxapp/edx-platform/lms/djangoapps/ccx/modulestore.py", line 305, in bulk_operations
    with self._modulestore.bulk_operations(course_id, emit_signals=emit_signals, ignore_case=ignore_case):
  File "/usr/lib/python3.8/contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "/edx/app/edxapp/edx-platform/common/lib/xmodule/xmodule/modulestore/mixed.py", line 1046, in bulk_operations
    store = self._get_modulestore_for_courselike(course_id)
  File "/edx/app/edxapp/edx-platform/common/lib/xmodule/xmodule/modulestore/mixed.py", line 217, in _get_modulestore_for_courselike
    if has_locator(store):
  File "/edx/app/edxapp/edx-platform/common/lib/xmodule/xmodule/modulestore/mixed.py", line 215, in <lambda>
    has_locator = lambda store: store.has_course(locator)
  File "/edx/app/edxapp/venvs/edxapp/lib/python3.8/site-packages/mongodb_proxy.py", line 55, in wrapper
    return func(*args, **kwargs)
  File "/edx/app/edxapp/edx-platform/common/lib/xmodule/xmodule/modulestore/mongo/base.py", line 1128, in has_course
    assert isinstance(course_key, CourseKey)
AssertionError
2021-12-27 19:52:56,423 ERROR 2870331 [django.request] [user 261689] [ip 201.234.236.10] log.py:222 - Internal Server Error: /api/course_modes/v1/courses/course-v1:testing-site+TS+00/

Then make the change, and you'll be able to create the course mode.

Other information

We found this error in our Lilac staging version while creating course modes through this new feature with course overviews taken from juniper DB backups.

@openedx-webhooks openedx-webhooks added needs triage open-source-contribution PR author is not from Axim or 2U labels Dec 27, 2021
@openedx-webhooks
Copy link

openedx-webhooks commented Dec 27, 2021

Thanks for the pull request, @mariajgrimaldi! I've created OSPR-6351 to keep track of it in JIRA, where we prioritize reviews. Please note that it may take us up to several weeks or months to complete a review and merge your PR.

Feel free to add as much of the following information to the ticket as you can:

  • supporting documentation
  • Open edX discussion forum threads
  • timeline information ("this must be merged by XX date", and why that is)
  • partner information ("this is a course on edx.org")
  • any other information that can help Product understand the context for the PR

All technical communication about the code itself will be done via the GitHub pull request interface. As a reminder, our process documentation is here.

Please let us know once your PR is ready for our review and all tests are green.

@mariajgrimaldi mariajgrimaldi marked this pull request as draft December 27, 2021 14:27
@mariajgrimaldi mariajgrimaldi changed the title fix: fix course_id usage in course modes API fix: cast course_id to CourseKey when loading CourseOverview Dec 27, 2021
@mariajgrimaldi mariajgrimaldi marked this pull request as ready for review December 27, 2021 20:16
@natabene
Copy link
Contributor

natabene commented Jan 6, 2022

@mariajgrimaldi Thank you for your contribution. This is ready for our review, correct?

@mariajgrimaldi
Copy link
Member Author

Hi @natabene! Happy new year. Yes, this is ready for review.

@Agrendalath
Copy link
Member

@natabene, can I review this one?

@natabene
Copy link
Contributor

@Agrendalath Thanks for offering, please feel free to review and provide feedback. Our team will also want to review.

@mariajgrimaldi
Copy link
Member Author

Hi there @Agrendalath! Thanks for the review. It took me a while to come back to this issue 😅

Let me know what do you need to move this forward:smile:

Copy link
Member

@Agrendalath Agrendalath left a comment

Choose a reason for hiding this comment

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

👍

  • I tested this: verified that this fixes the falsy assertion
  • I read through the code
  • I checked for accessibility issues: n/a
  • Includes documentation: n/a
  • I made sure any change in configuration variables is reflected in the corresponding client's configuration-secure repository: n/a

@Agrendalath
Copy link
Member

Hi @mariajgrimaldi. This looks good to me. Just please amend the CurseKey object typo in the commit message :)

Thank you for providing extensive info in the PR's description!

cc: @natabene

@mariajgrimaldi
Copy link
Member Author

Hi @natabene, you mentioned your team would want to review; who should we mention? 🤔

@natabene
Copy link
Contributor

@mariajgrimaldi It is actually my team :) We will look into this.

Copy link
Contributor

@pshiu pshiu left a comment

Choose a reason for hiding this comment

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

@openedx-webhooks
Copy link

@mariajgrimaldi 🎉 Your pull request was merged! Please take a moment to answer a two question survey so we can improve your experience in the future.

@edx-pipeline-bot
Copy link
Contributor

EdX Release Notice: This PR has been deployed to the staging environment in preparation for a release to production.

@edx-pipeline-bot
Copy link
Contributor

EdX Release Notice: This PR has been deployed to the production environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged open-source-contribution PR author is not from Axim or 2U
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants