Skip to content

Commit

Permalink
stable 3
Browse files Browse the repository at this point in the history
  • Loading branch information
farhan committed Feb 25, 2025
1 parent db6be1e commit abc3f48
Show file tree
Hide file tree
Showing 3 changed files with 987 additions and 57 deletions.
4 changes: 4 additions & 0 deletions xblocks_contrib/video/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@
ATTR_KEY_USER_ID = 'edx-platform.user_id'
# The country code determined from the user's request IP address.
ATTR_KEY_REQUEST_COUNTRY_CODE = 'edx-platform.request_country_code'

COURSE_VIDEO_SHARING_PER_VIDEO = 'per-video'
COURSE_VIDEO_SHARING_ALL_VIDEOS = 'all-on'
COURSE_VIDEO_SHARING_NONE = 'all-off'
125 changes: 110 additions & 15 deletions xblocks_contrib/video/utils.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,115 @@
from openedx_learning.api import authoring as authoring_api
from collections import namedtuple
from urllib.parse import urlencode

def get_component_from_usage_key(usage_key: UsageKeyV2) -> Component:
from django.conf import settings
from django.utils.translation import gettext_lazy as _

# from openedx_learning.api import authoring as authoring_api

SharingSiteConfig = namedtuple(
'SharingSiteConfig',
[
'name',
'fa_icon_name',
'url_param_name',
'base_share_url',
'additional_site_params'
],
defaults=[{}]
)

TWITTER = SharingSiteConfig(
name='twitter',
fa_icon_name='fa-twitter-square',
url_param_name='url',
base_share_url='https://twitter.com/intent/tweet',
)

FACEBOOK = SharingSiteConfig(
name='facebook',
fa_icon_name='fa-facebook-square',
url_param_name='u',
base_share_url='https://www.facebook.com/sharer/sharer.php'
)

LINKEDIN = SharingSiteConfig(
name='linkedin',
fa_icon_name='fa-linkedin-square',
url_param_name='url',
base_share_url='https://www.linkedin.com/sharing/share-offsite/'
)

ALL_SHARING_SITES = [
TWITTER,
FACEBOOK,
LINKEDIN,
]


def get_share_text(social_account_handle, organization_name):
"""
Fetch the Component object for a given usage key.
Generate the text we will pre-populate when sharing a post to social media.
Raises a ObjectDoesNotExist error if no such Component exists.
NOTE: Most of the time, we will have all info of these, but have provided
reasonable fallbacks in case some are missing.
"""
if social_account_handle and organization_name:
return _(
"Here's a fun clip from a class I'm taking on {social_account_handle} from {organization_name}.\n\n"
).format(
social_account_handle=social_account_handle,
organization_name=organization_name,
)
elif social_account_handle:
return _(
"Here's a fun clip from a class I'm taking on {social_account_handle}.\n\n"
).format(social_account_handle=social_account_handle)
elif organization_name:
return _(
"Here's a fun clip from a class I'm taking from {organization_name}.\n\n"
).format(organization_name=organization_name)
else:
return _(
"Here's a fun clip from a class I'm taking on {platform_name}.\n\n"
).format(platform_name=settings.PLATFORM_NAME)

This is a lower-level function that will return a Component even if there is
no current draft version of that Component (because it's been soft-deleted).

def sharing_url(video_public_url, sharing_site_config, organization=None):
"""
Returns the sharing url with the appropriate parameters
"""
share_params = {
'utm_source': sharing_site_config.name,
'utm_medium': 'social',
'utm_campaign': 'social-share-exp',
sharing_site_config.url_param_name: video_public_url
}

# Special handling for Twitter, pre-populate share text
if sharing_site_config.name == "twitter":
twitter_handle = settings.PLATFORM_TWITTER_ACCOUNT
org_name = organization['name'] if organization else None
share_params.update({"text": get_share_text(twitter_handle, org_name)})
else:
share_params.update(sharing_site_config.additional_site_params)

return sharing_site_config.base_share_url + '?' + urlencode(share_params)


def sharing_sites_info_for_video(video_public_url, organization=None):
"""
Returns a list of dicts, each containing the name, fa_icon_name, and sharing_url
"""
learning_package = authoring_api.get_learning_package_by_key(
str(usage_key.context_key)
)
return authoring_api.get_component_by_key(
learning_package.id,
namespace='xblock.v1',
type_name=usage_key.block_type,
local_key=usage_key.block_id,
)
result = []
for sharing_site_config in ALL_SHARING_SITES:
sharing_site_info = {
'name': sharing_site_config.name,
'fa_icon_name': sharing_site_config.fa_icon_name,
'sharing_url': sharing_url(
video_public_url,
sharing_site_config,
organization=organization
),
}
result.append(sharing_site_info)
return result
Loading

0 comments on commit abc3f48

Please sign in to comment.