Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _user_state_key(self):
return xblock.runtime.KeyValueStore.Key(
scope=xblock.fields.Scope.user_state, user_id='123',
block_scope_id='456', field_name='my_field')
def test_for_export_transforms_value_for_student_data(self):
key = self.get_key(
xblock.fields.Scope.user_state, '1234567890',
'0d40c9be8e254416b4782bf8e19a538f', 'my_field')
orig_model = dbmodels.KeyValueEntity(key=key, data='{"value": 1}')
safe_model = orig_model.for_export(self.transform)
self.assertEquals('tr_{"value": 1}', safe_model.data)
def test_slider_block(self):
"""Test peristence of fields in user scope."""
usage_id = self.runtime.parse_xml_string(
'', self.id_generator)
block = self.runtime.get_block(usage_id)
block.value = 50
block.save()
key = xblock.runtime.KeyValueStore.Key(
scope=xblock.fields.Scope.user_state,
user_id=self.STUDENT_ID,
block_scope_id=block.scope_ids.usage_id,
field_name='value')
self.assertEqual(50, store.KeyValueStore().get(key))
is_valid = False
return Response(json={'isValid': is_valid, 'message': message})
@XBlock.needs('modulestore')
class PlaybackStateMixin(XBlock):
"""
PlaybackStateMixin encapsulates video-playback related data.
These fields are not visible to end-user.
"""
current_time = Float(
default=0,
scope=Scope.user_state,
help='Seconds played back after the start'
)
playback_rate = Float(
default=1,
scope=Scope.preferences,
help='Supported video playbacks speeds are: 0.5, 1, 1.5, 2'
)
volume = Float(
default=1,
scope=Scope.preferences,
help='Video volume: from 0 to 1'
)
muted = Boolean(
nb_upload_datetime = String(
help="UTC datetime the notebook was uploaded",
scope=Scope.settings,
default=""
)
raw_possible = Float(
help="Max possible score attainable",
scope=Scope.settings,
default=0.0
)
# ------- Internal Student Fields -------
student_attempts = Integer(
help="Number of times student has submitted problem",
scope=Scope.user_state,
default=0
)
student_score = Float(
help="Student Score",
scope=Scope.user_state,
default=0
)
student_section_scores = List(
help="Student Scores per section",
scope=Scope.user_state,
default=[]
)
student_submitted_dt = String(
)
# save completion_status for SCORM_2004
lesson_status = String(
scope=Scope.user_state,
default='not attempted'
)
success_status = String(
scope=Scope.user_state,
default='unknown'
)
data_scorm = Dict(
scope=Scope.user_state,
default={}
)
lesson_score = Float(
scope=Scope.user_state,
default=0
)
weight = Float(
default=1,
scope=Scope.settings
)
has_score = Boolean(
display_name=_("Scored"),
help=_("Select False if this component will not receive a numerical score from the Scorm"),
default=True,
scope=Scope.settings
)
icon_class = String(
default="video",
scope=Scope.settings,
)
return 'video'
@XBlock.needs('jinja')
class SequenceBlock(XBlock):
"""An XBlock which presents its children in a tabbed view.
See xmodule.seq_module.SequenceModule in
https://github.com/edx/edx-platform.
"""
has_children = True
position = Integer(
help='Last tab viewed in this sequence',
scope=Scope.user_state,
default=0)
def __init__(self, *args, **kwargs):
super(SequenceBlock, self).__init__(*args, **kwargs)
self.templates_dirs = [os.path.join(os.path.dirname(__file__), 'templates')]
self.get_template = self.runtime.service(self, 'jinja')
def student_view(self, context=None):
frag = Fragment()
frag.add_css_url(
self.runtime.local_resource_url(self, 'public/css/sequence.css'))
frag.add_javascript_url(
self.runtime.local_resource_url(self, 'public/js/sequence.js'))
frag.initialize_js('SequenceBlock')
from urlparse import urlparse
from xblock.core import XBlock
from xblock.fields import Scope, Integer, String
from xblock.fragment import Fragment
class SimpleVideoBlock(XBlock):
"""
An XBlock providing oEmbed capabilities for video (currently only supporting Vimeo)
"""
href = String(help="URL of the video page at the provider", default=None, scope=Scope.content)
maxwidth = Integer(help="Maximum width of the video", default=800, scope=Scope.content)
maxheight = Integer(help="Maximum height of the video", default=450, scope=Scope.content)
watched_count = Integer(help="The number of times the student watched the video", default=0, scope=Scope.user_state)
def student_view(self, context):
"""
Create a fragment used to display the XBlock to a student.
`context` is a dictionary used to configure the display (unused)
Returns a `Fragment` object specifying the HTML, CSS, and JavaScript
to display.
"""
provider, embed_code = self.get_embed_code_for_url(self.href)
# Load the HTML fragment from within the package and fill in the template
html_str = pkg_resources.resource_string(__name__, "static/html/simplevideo.html")
frag = Fragment(unicode(html_str).format(self=self, embed_code=embed_code))
# Load CSS
raw_possible = Float(
help="Max possible score attainable",
scope=Scope.settings,
default=0.0
)
# ------- Internal Student Fields -------
student_attempts = Integer(
help="Number of times student has submitted problem",
scope=Scope.user_state,
default=0
)
student_score = Float(
help="Student Score",
scope=Scope.user_state,
default=0
)
student_section_scores = List(
help="Student Scores per section",
scope=Scope.user_state,
default=[]
)
student_submitted_dt = String(
help="UTC datetime student last submitted notebook",
scope=Scope.user_state,
default=""
)
editable_fields = ('display_name', 'instructions', 'max_attempts',
def export_xml(self, node):
node.tag = self.xml_element_name()
# Set node attributes based on our fields.
for field_name, field in self.fields.items():
if field_name in ('children', 'parent', 'content', 'data'):
continue
# TODO(jorr): Restore export of user state fields once roundtripping
# fields of type Dict through XML is fixed. Currently this is broken
# in XBlock, and so as a work-around ommit all the user_state fields
# because these happen to be the only ones which are Dicts.
if field.is_set_on(self) and field.scope is not Scope.user_state:
node.set(field_name, unicode(field.read_from(self)))
for child in etree.fromstring(self.data):
node.append(child)