Commit f3b39ea4 by Xavier Antoviaque Committed by dragonfi

opaque-keys: Adds support for opaque keys

Inspired from https://github.com/edx/edx-ora2/pull/330
parent de15c188
......@@ -31,7 +31,7 @@ from xblock.fragment import Fragment
from .light_children import LightChild, Boolean, Scope, String, Integer
from .models import Answer
from .utils import render_template
from .utils import render_template, serialize_opaque_key
# Globals ###########################################################
......@@ -141,7 +141,7 @@ class AnswerBlock(LightChild):
# TODO: Why do we need to use `xmodule_runtime` and not `runtime`?
student_id = self.xmodule_runtime.anonymous_student_id
course_id = self.xmodule_runtime.course_id
course_id = serialize_opaque_key(self.xmodule_runtime.course_id)
answer_data, created = Answer.objects.get_or_create(
student_id=student_id,
......
......@@ -31,7 +31,7 @@ from xblock.core import XBlock
from xblock.fragment import Fragment
from .models import Answer
from .utils import list2csv, render_template
from .utils import list2csv, render_template, serialize_opaque_key
# Globals ###########################################################
......@@ -70,7 +70,7 @@ class MentoringDataExportBlock(XBlock):
return response
def get_csv(self):
course_id = self.xmodule_runtime.course_id
course_id = serialize_opaque_key(self.xmodule_runtime.course_id)
answers = Answer.objects.filter(course_id=course_id).order_by('student_id', 'name')
answers_names = answers.values_list('name', flat=True).distinct().order_by('name')
......
......@@ -46,7 +46,7 @@ except:
# TODO-WORKBENCH-WORKAROUND: To allow to load from the workbench
replace_jump_to_id_urls = lambda a,b,c,d,frag,f: frag
from .utils import XBlockWithChildrenFragmentsMixin
from .utils import serialize_opaque_key, XBlockWithChildrenFragmentsMixin
# Globals ###########################################################
......@@ -188,7 +188,7 @@ class XBlockWithLightChildren(LightChildrenMixin, XBlock):
"""
# TODO: Why do we need to use `xmodule_runtime` and not `runtime`?
try:
course_id = self.xmodule_runtime.course_id
course_id = serialize_opaque_key(self.xmodule_runtime.course_id)
except AttributeError:
# TODO-WORKBENCH-WORKAROUND: To allow to load from the workbench
course_id = 'sample-course'
......
......@@ -94,6 +94,28 @@ def load_scenarios_from_path(scenarios_path):
"""
return get_scenarios_from_path(scenarios_path, include_identifier=True)
def serialize_opaque_key(key):
"""
Gracefully handle opaque keys, both before and after the transition.
https://github.com/edx/edx-platform/wiki/Opaque-Keys
From https://github.com/edx/edx-ora2/pull/330
Currently uses `to_deprecated_string()` to ensure that new keys
are backwards-compatible with keys we store in ORA2 database models.
Args:
key (unicode or OpaqueKey subclass): The key to serialize.
Returns:
unicode
"""
if hasattr(key, 'to_deprecated_string'):
return key.to_deprecated_string()
else:
return unicode(key)
# Classes ###########################################################
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment