Skip to content

Commit

Permalink
Merge pull request #208 from eduNEXT/MJG/fix-validate-org-async
Browse files Browse the repository at this point in the history
fix: get settings course_org_filter when validating org for async proc
  • Loading branch information
mariajgrimaldi authored Jun 3, 2022
2 parents a40a5ab + d4987ca commit c5a7e91
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
53 changes: 53 additions & 0 deletions eox_core/edxapp_wrapper/backends/coursekey_m_v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Backend for the CourseKey validations that works under the open-release/maple.master tag
"""
# pylint: disable=import-error, protected-access
from __future__ import absolute_import, unicode_literals

from django.conf import settings
from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from rest_framework.serializers import ValidationError

try:
from openedx.core.djangoapps.site_configuration.helpers import get_all_orgs, get_current_site_orgs
except ImportError:
get_all_orgs, get_current_site_orgs = object, object # pylint: disable=invalid-name


def get_valid_course_key(course_id):
"""
Return the CourseKey if the course_id is valid
"""
try:
return CourseKey.from_string(course_id)
except InvalidKeyError:
raise ValidationError(f"Invalid course_id {course_id}") from InvalidKeyError


def validate_org(course_id):
"""
Validate the course organization against all possible orgs for the site
To determine if the Org is valid we must look at 3 things
1 Orgs in the current site
2 Orgs in other sites
3 flag EOX_CORE_USER_ENABLE_MULTI_TENANCY
"""

if not settings.EOX_CORE_USER_ENABLE_MULTI_TENANCY:
return True

course_key = get_valid_course_key(course_id)
course_org_filter = getattr(settings, "course_org_filter", [])
course_org_filter = course_org_filter if isinstance(course_org_filter, list) else [course_org_filter]
current_site_orgs = get_current_site_orgs() or course_org_filter or []

if not current_site_orgs: # pylint: disable=no-else-return
if course_key.org in get_all_orgs():
return False
return True
else:
return course_key.org in current_site_orgs
2 changes: 1 addition & 1 deletion eox_core/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def plugin_settings(settings):
settings.DATA_API_DEF_PAGE_SIZE = 1000
settings.DATA_API_MAX_PAGE_SIZE = 5000
settings.EOX_CORE_COURSES_BACKEND = "eox_core.edxapp_wrapper.backends.courses_h_v1"
settings.EOX_CORE_COURSEKEY_BACKEND = "eox_core.edxapp_wrapper.backends.coursekey_h_v1"
settings.EOX_CORE_COURSEKEY_BACKEND = "eox_core.edxapp_wrapper.backends.coursekey_m_v1"
settings.EOX_CORE_SITE_CONFIGURATION = "eox_core.edxapp_wrapper.backends.site_configuration_h_v1"
settings.EOX_CORE_COURSE_MANAGEMENT_REQUEST_TIMEOUT = 1000
settings.EOX_CORE_USER_ENABLE_MULTI_TENANCY = True
Expand Down

0 comments on commit c5a7e91

Please sign in to comment.