Move LicenseMixin and accompanying code to openedx/core/lib.

This commit is contained in:
John Eskew
2017-05-23 17:58:54 -04:00
parent 643d9eda7d
commit f1369b912e
11 changed files with 25 additions and 18 deletions

View File

@@ -16,7 +16,7 @@ from pytz import utc
from xblock.fields import Boolean, Dict, Float, Integer, List, Scope, String
from xmodule import course_metadata_utils
from xmodule.graders import grader_from_conf
from xmodule.mixin import LicenseMixin
from openedx.core.lib.license import LicenseMixin
from xmodule.seq_module import SequenceDescriptor, SequenceModule
from xmodule.tabs import CourseTabList, InvalidTabsException

View File

@@ -1,64 +0,0 @@
"""
Reusable mixins for XBlocks and/or XModules
"""
from xblock.fields import Scope, String, XBlockMixin
# Make '_' a no-op so we can scrape strings. Using lambda instead of
# `django.utils.translation.ugettext_noop` because Django cannot be imported in this file
_ = lambda text: text
class LicenseMixin(XBlockMixin):
"""
Mixin that allows an author to indicate a license on the contents of an
XBlock. For example, a video could be marked as Creative Commons SA-BY
licensed. You can even indicate the license on an entire course.
If this mixin is not applied to an XBlock, or if the license field is
blank, then the content is subject to whatever legal licensing terms that
apply to content by default. For example, in the United States, that content
is exclusively owned by the creator of the content by default. Other
countries may have similar laws.
"""
license = String(
display_name=_("License"),
help=_("A license defines how the contents of this block can be shared and reused."),
default=None,
scope=Scope.content,
)
@classmethod
def parse_license_from_xml(cls, definition, node):
"""
When importing an XBlock from XML, this method will parse the license
information out of the XML and attach it to the block.
It is defined here so that classes that use this mixin can simply refer
to this method, rather than reimplementing it in their XML import
functions.
"""
license = node.get('license', default=None) # pylint: disable=redefined-builtin
if license:
definition['license'] = license
return definition
def add_license_to_xml(self, node, default=None):
"""
When generating XML from an XBlock, this method will add the XBlock's
license to the XML representation before it is serialized.
It is defined here so that classes that use this mixin can simply refer
to this method, rather than reimplementing it in their XML export
functions.
"""
if getattr(self, "license", default):
node.set('license', self.license)
def wrap_with_license(block, view, frag, context): # pylint: disable=unused-argument
"""
In the LMS, display the custom license underneath the XBlock.
"""
license = getattr(block, "license", None) # pylint: disable=redefined-builtin
if license:
context = {"license": license}
frag.content += block.runtime.render_template('license_wrapper.html', context)
return frag

View File

@@ -46,7 +46,7 @@ from .video_xfields import VideoFields
from .video_handlers import VideoStudentViewHandlers, VideoStudioViewHandlers
from xmodule.video_module import manage_video_subtitles_save
from xmodule.mixin import LicenseMixin
from openedx.core.lib.license import LicenseMixin
# The following import/except block for edxval is temporary measure until
# edxval is a proper XBlock Runtime Service.

View File

@@ -1,60 +0,0 @@
<%page args="license, button=False, button_size='88x31'"/>
## keep this synchronized with the contents of cms/templates/js/license-selector.underscore
<%!
from django.utils.translation import ugettext as _
def parse_license(lic):
"""
Returns a two-tuple: license type, and options.
"""
if not lic:
return None, {}
if ":" not in lic:
# no options, so the entire thing is the license type
return lic, {}
ltype, option_str = lic.split(":", 1)
options = {}
for part in option_str.split():
if "=" in part:
key, value = part.split("=", 1)
options[key] = value
else:
options[part] = True
return ltype, options
%>
<% license_type, license_options = parse_license(license) %>
% if license_type == "all-rights-reserved":
© <span class="license-text">${_("All Rights Reserved")}</span>
% elif license_type == "creative-commons":
<%
possible = ["by", "nc", "nd", "sa"]
names = {
"by": _("Attribution"), "nc": _("Noncommercial"),
"nd": _("No Derivatives"), "sa": _("Share Alike")
}
enabled = [opt for opt in possible
if license_options.get(opt) or license_options.get(opt.upper())]
version = license_options.get("ver", "4.0")
if len(enabled) == 0:
enabled = ["zero"]
version = license_options.get("ver", "1.0")
%>
<a rel="license" href="https://creativecommons.org/licenses/${'-'.join(enabled)}/${version}/" target="_blank">
% if button:
<img src="https://licensebuttons.net/l/${'-'.join(enabled)}/${version}/${button_size}.png"
alt="${license}"
/>
</a>
% else:
## <span> must come before <i> icon or else spacing gets messed up
<span class="sr">${_("Creative Commons licensed content, with terms as follow:")}&nbsp;</span><span aria-hidden="true" class="icon-cc"></span>
% for option in enabled:
<span class="sr">${names[option]}&nbsp;</span><span aria-hidden="true" class="icon-cc-${option}"></span>
% endfor
<span class="license-text">${_("Some Rights Reserved")}</span>
% endif
</a>
% else:
${license}
% endif

View File

@@ -1,3 +0,0 @@
<div class="xblock-license">
<%include file="license.html" args="license=license" />
</div>