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

feat: [AXIMST-410] add possibility to fetch draft item #2497

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions lms/djangoapps/courseware/block_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
from common.djangoapps.edxmako.services import MakoService
from common.djangoapps.xblock_django.user_service import DjangoXBlockUserService
from openedx.core.lib.cache_utils import CacheService
from xmodule.modulestore import ModuleStoreEnum # lint-amnesty, pylint: disable=wrong-import-order


if TYPE_CHECKING:
from rest_framework.request import Request
Expand Down Expand Up @@ -813,7 +815,7 @@ def _get_usage_key_for_course(course_key, usage_id) -> UsageKey:
raise Http404("Invalid location") from exc


def _get_block_by_usage_key(usage_key):
def _get_block_by_usage_key(usage_key, request=None):
"""
Gets a block instance based on a mapped-to-course usage_key

Expand All @@ -828,7 +830,29 @@ def _get_block_by_usage_key(usage_key):
usage_key.course_key,
usage_key
)
raise Http404 from exc

if request is None:
# No request provided, cannot proceed
raise Http404 from exc

referer = request.META.get('HTTP_REFERER')
Copy link
Collaborator

@GlugovGrGlib GlugovGrGlib Feb 7, 2024

Choose a reason for hiding this comment

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

This approach eventually not working on the dev server

HTTP_REFERER header doesn't contain /course-authoring path in it, only domain

request.META content from debug log:

2024-02-07 13:15:25,286 ERROR 402008 [lms.djangoapps.courseware.block_render] [user 19] [ip 143.244.46.119] block_render.py:837 - ++++++<WSGIRequest: GET '/xblock/block-v1:eDx+567657+2024+type@html+block@1ba5aef177a547b983c2e3fdede6c395?show_title=0&show_bookmark=0&recheck_access=1&view=student_view&is_authoring_mfe=1'>+++++
2024-02-07 13:15:25,288 ERROR 402008 [lms.djangoapps.courseware.block_render] [user 19] [ip 143.244.46.119] block_render.py:838 - ++++++{'wsgi.errors': <gunicorn.http.wsgi.WSGIErrorsWrapper object at 0x7f8e056e1b80>, 'wsgi.version': (1, 0), 'wsgi.multithread': False, 'wsgi.multiprocess': True, 'wsgi.run_once': False, 'wsgi.file_wrapper': <class 'gunicorn.http.wsgi.FileWrapper'>, 'wsgi.input_terminated': True, 'SERVER_SOFTWARE': 'gunicorn/21.2.0', 'wsgi.input': <gunicorn.http.body.Body object at 0x7f8e056c0b20>, 'gunicorn.socket': <socket.socket fd=20, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 8000), raddr=('127.0.0.1', 21968)>, 'REQUEST_METHOD': 'GET', 'QUERY_STRING': 'show_title=0&show_bookmark=0&recheck_access=1&view=student_view&is_authoring_mfe=1', 'RAW_URI': '/xblock/block-v1:eDx+567657+2024+type@html+block@1ba5aef177a547b983c2e3fdede6c395?show_title=0&show_bookmark=0&recheck_access=1&view=student_view&is_authoring_mfe=1', 'SERVER_PROTOCOL': 'HTTP/1.0', 'HTTP_X_FORWARDED_PROTO': 'https', 'HTTP_X_FORWARDED_PORT': '443', 'HTTP_X_FORWARDED_FOR': '143.244.46.119', 'HTTP_X_QUEUE_START': 't=1707311724.863', 'HTTP_HOST': 'lms-studio-mfe-dev.raccoongang.com', 'HTTP_CONNECTION': 'close', 'HTTP_X_REQUEST_ID': '56356f8e77b6ea1ab2a81587c49666b3', 'HTTP_X_REAL_IP': '143.244.46.119', 'HTTP_X_FORWARDED_HOST': 'lms-studio-mfe-dev.raccoongang.com', 'HTTP_X_FORWARDED_SCHEME': 'https', 'HTTP_X_SCHEME': 'https', 'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0', 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/jxl,image/webp,*/*;q=0.8', 'HTTP_ACCEPT_LANGUAGE': 'en;q=1.0, en;q=1.0', 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, br', 'HTTP_REFERER': 'https://app-studio-mfe-dev.raccoongang.com/', 'HTTP_UPGRADE_INSECURE_REQUESTS': '1', 'HTTP_SEC_FETCH_DEST': 'iframe', 'HTTP_SEC_FETCH_MODE': 'navigate', 'HTTP_SEC_FETCH_SITE': 'same-site', 'HTTP_PRAGMA': 'no-cache', 'HTTP_CACHE_CONTROL': 'no-cache', 'HTTP_COOKIE': 'openedx-language-preference=en; _ga_test=GS1.1.1706185997.45.0.1706185998.0.0.0; _ga=GA1.1.773444425.1698787115;
...

authoring_mfe_query_key = 'is_authoring_mfe'
authoring_mfe_matches = [settings.COURSE_AUTHORING_MICROFRONTEND_URL, authoring_mfe_query_key]
is_fetched_from_authoring_mfe = referer and any(match in referer for match in authoring_mfe_matches)

if is_fetched_from_authoring_mfe:
try:
block = modulestore().get_item(usage_key, revision=ModuleStoreEnum.RevisionOption.draft_only)
block_orig_usage_key, block_orig_version = modulestore().get_block_original_usage(usage_key)
except ItemNotFoundError:
log.warning(
"Invalid draft location for course id %s: %s",
usage_key.course_key,
usage_key
)
raise Http404 from exc
else:
raise Http404 from exc

tracking_context = {
'module': {
Expand All @@ -855,7 +879,7 @@ def get_block_by_usage_id(request, course_id, usage_id, disable_staff_debug_info
"""
course_key = CourseKey.from_string(course_id)
usage_key = _get_usage_key_for_course(course_key, usage_id)
block, tracking_context = _get_block_by_usage_key(usage_key)
block, tracking_context = _get_block_by_usage_key(usage_key, request)

_, user = setup_masquerade(request, course_key, has_access(request.user, 'staff', block, course_key))
field_data_cache = FieldDataCache.cache_for_block_descendents(
Expand Down Expand Up @@ -923,7 +947,7 @@ def _invoke_xblock_handler(request, course_id, usage_id, handler, suffix, course
# At the time of writing, this is only used by one handler. If this usage grows, we may want to re-evaluate
# how we do this to something more elegant. If you are the author of a third party block that decides it wants
# to set this too, please let us know so we can consider making this easier / better-documented.
block, _ = _get_block_by_usage_key(block_usage_key)
block, _ = _get_block_by_usage_key(block_usage_key, request)
handler_method = getattr(block, handler, False)
will_recheck_access = handler_method and getattr(handler_method, 'will_recheck_access', False)

Expand Down
3 changes: 2 additions & 1 deletion lms/djangoapps/courseware/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,7 @@ def render_xblock(request, usage_key_string, check_if_enrolled=True, disable_sta
set_custom_attribute('block_type', usage_key.block_type)

requested_view = request.GET.get('view', 'student_view')
is_authoring_mfe = request.GET.get('is_authoring_mfe')
if requested_view != 'student_view' and requested_view != 'public_view': # lint-amnesty, pylint: disable=consider-using-in
return HttpResponseBadRequest(
f"Rendering of the xblock view '{bleach.clean(requested_view, strip=True)}' is not supported."
Expand Down Expand Up @@ -1651,7 +1652,7 @@ def render_xblock(request, usage_key_string, check_if_enrolled=True, disable_sta
'web_app_course_url': get_learning_mfe_home_url(course_key=course.id, url_fragment='home'),
'on_courseware_page': True,
'verified_upgrade_link': verified_upgrade_deadline_link(request.user, course=course),
'is_learning_mfe': is_learning_mfe,
'is_learning_mfe': is_authoring_mfe or is_learning_mfe,
'is_mobile_app': is_mobile_app,
'render_course_wide_assets': True,

Expand Down
2 changes: 2 additions & 0 deletions lms/envs/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -5050,6 +5050,8 @@ def _make_locale_paths(settings): # pylint: disable=missing-function-docstring
# .. toggle_tickets: https://openedx.atlassian.net/browse/VAN-838
ENABLE_DYNAMIC_REGISTRATION_FIELDS = False

COURSE_AUTHORING_MICROFRONTEND_URL = None

############## Settings for EmailChangeMiddleware ###############

# .. toggle_name: ENFORCE_SESSION_EMAIL_MATCH
Expand Down
3 changes: 3 additions & 0 deletions lms/envs/devstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ def should_show_debug_toolbar(request): # lint-amnesty, pylint: disable=missing
########################## LEARNER HOME APP ##############################
LEARNER_HOME_MICROFRONTEND_URL = 'http://localhost:1996'

################### FRONTEND APPLICATION COURSE AUTHORING ###################
COURSE_AUTHORING_MICROFRONTEND_URL = 'http://localhost:2001'

###################### Cross-domain requests ######################
FEATURES['ENABLE_CORS_HEADERS'] = True
CORS_ALLOW_CREDENTIALS = True
Expand Down
7 changes: 7 additions & 0 deletions lms/templates/courseware/courseware-chromeless.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
var $$course_id = "${course.id | n, js_escaped_string}";
</script>

% if block:
<script type="text/javascript">
var $$xBlockLocation = "${block.location | n, js_escaped_string}";
</script>
% endif

</%block>

<%block name="js_extra">
Expand Down Expand Up @@ -183,6 +189,7 @@

window.parent.postMessage({
type: 'plugin.resize',
location: $$xBlockLocation || '',
payload: {
width: newWidth,
height: newHeight,
Expand Down
Loading