refactor: Introduce VideoConfig service, move video sharing methods in it

This commit is contained in:
farhan
2025-10-10 18:56:17 +05:00
parent bbd53a0399
commit 0cd62bf786
9 changed files with 182 additions and 83 deletions

View File

@@ -0,0 +1,64 @@
"""
Video Configuration Service for XBlock runtime.
This service provides video-related configuration and feature flags
that are specific to the edx-platform implementation
for the extracted video block in xblocks-contrib repository.
"""
import logging
from opaque_keys.edx.keys import CourseKey, UsageKey
from openedx.core.djangoapps.video_config import sharing
from organizations.api import get_course_organization
log = logging.getLogger(__name__)
class VideoConfigService:
"""
Service for providing video-related configuration and feature flags.
This service abstracts away edx-platform specific functionality
that the Video XBlock needs, allowing the Video XBlock to be
extracted to a separate repository.
"""
def get_public_video_url(self, usage_id: UsageKey) -> str:
"""
Returns the public video url
"""
return sharing.get_public_video_url(usage_id)
def get_public_sharing_context(self, video_block, course_key: CourseKey) -> dict:
"""
Get the complete public sharing context for a video.
Args:
video_block: The video XBlock instance
course_key: The course identifier
Returns:
dict: Context dictionary with sharing information, empty if sharing is disabled
"""
context = {}
if not sharing.is_public_sharing_enabled(video_block.location, video_block.public_access):
return context
public_video_url = sharing.get_public_video_url(video_block.location)
context['public_sharing_enabled'] = True
context['public_video_url'] = public_video_url
organization = get_course_organization(course_key)
from openedx.core.djangoapps.video_config.sharing_sites import sharing_sites_info_for_video
sharing_sites_info = sharing_sites_info_for_video(
public_video_url,
organization=organization
)
context['sharing_sites_info'] = sharing_sites_info
return context

View File

@@ -0,0 +1,81 @@
"""
Provides utility methods for video sharing functionality.
"""
import logging
from django.conf import settings
from opaque_keys.edx.keys import UsageKey
from openedx.core.djangoapps.video_config.toggles import PUBLIC_VIDEO_SHARE
from openedx.core.lib.courses import get_course_by_id
log = logging.getLogger(__name__)
# Video sharing constants
COURSE_VIDEO_SHARING_PER_VIDEO = 'per-video'
COURSE_VIDEO_SHARING_ALL_VIDEOS = 'all-on'
COURSE_VIDEO_SHARING_NONE = 'all-off'
@staticmethod
def get_public_video_url(usage_id: UsageKey) -> str:
"""
Returns the public video url
"""
return fr'{settings.LMS_ROOT_URL}/videos/{str(usage_id)}'
@staticmethod
def is_public_sharing_enabled(usage_key: UsageKey, public_access: bool) -> bool:
"""
Check if public sharing is enabled for a video.
Args:
usage_key: The usage key of the video block
public_access: Whether the video block has public access enabled
"""
if not usage_key.context_key.is_course:
return False # Only courses support this feature (not libraries)
try:
# Video share feature must be enabled for sharing settings to take effect
feature_enabled = PUBLIC_VIDEO_SHARE.is_enabled(usage_key.context_key)
except Exception as err: # pylint: disable=broad-except
log.exception(f"Error retrieving course for course ID: {usage_key.context_key}")
return False
if not feature_enabled:
return False
# Check if the course specifies a general setting
course_video_sharing_option = get_course_video_sharing_override(usage_key)
# Course can override all videos to be shared
if course_video_sharing_option == COURSE_VIDEO_SHARING_ALL_VIDEOS:
return True
# ... or no videos to be shared
elif course_video_sharing_option == COURSE_VIDEO_SHARING_NONE:
return False
# ... or can fall back to per-video setting
# Equivalent to COURSE_VIDEO_SHARING_PER_VIDEO or None / unset
else:
return public_access
@staticmethod
def get_course_video_sharing_override(usage_key: UsageKey) -> str | None:
"""
Return course video sharing options override
"""
if not usage_key.context_key.is_course:
return False # Only courses support this feature (not libraries)
try:
course = get_course_by_id(usage_key.context_key)
return getattr(course, 'video_sharing_options', None)
except Exception as err: # pylint: disable=broad-except
log.exception(f"Error retrieving course for course ID: {usage_key.context_key}")
return None