Commit 7d2909c5 by Calen Pennington

Add contracts to DjangoXBlockUserStateClient interface methods

parent 2bfbd57e
...@@ -11,9 +11,13 @@ try: ...@@ -11,9 +11,13 @@ try:
except ImportError: except ImportError:
import json import json
from xblock.fields import Scope from xblock.fields import Scope, ScopeBase
from xblock_user_state.interface import XBlockUserStateClient from xblock_user_state.interface import XBlockUserStateClient
from courseware.models import StudentModule from courseware.models import StudentModule
from contracts import contract, new_contract
from opaque_keys.edx.keys import UsageKey
new_contract('UsageKey', UsageKey)
class DjangoXBlockUserStateClient(XBlockUserStateClient): class DjangoXBlockUserStateClient(XBlockUserStateClient):
...@@ -42,6 +46,12 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient): ...@@ -42,6 +46,12 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient):
def __init__(self, user): def __init__(self, user):
self.user = user self.user = user
@contract(
username="basestring",
block_key=UsageKey,
scope=ScopeBase,
fields="seq(basestring)|set(basestring)|None"
)
def get(self, username, block_key, scope=Scope.user_state, fields=None): def get(self, username, block_key, scope=Scope.user_state, fields=None):
""" """
Retrieve the stored XBlock state for a single xblock usage. Retrieve the stored XBlock state for a single xblock usage.
...@@ -66,6 +76,7 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient): ...@@ -66,6 +76,7 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient):
return state return state
@contract(username="basestring", block_key=UsageKey, state="dict(basestring: *)", scope=ScopeBase)
def set(self, username, block_key, state, scope=Scope.user_state): def set(self, username, block_key, state, scope=Scope.user_state):
""" """
Set fields for a particular XBlock. Set fields for a particular XBlock.
...@@ -79,6 +90,12 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient): ...@@ -79,6 +90,12 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient):
assert self.user.username == username assert self.user.username == username
self.set_many(username, {block_key: state}, scope) self.set_many(username, {block_key: state}, scope)
@contract(
username="basestring",
block_key=UsageKey,
scope=ScopeBase,
fields="seq(basestring)|set(basestring)|None"
)
def delete(self, username, block_key, scope=Scope.user_state, fields=None): def delete(self, username, block_key, scope=Scope.user_state, fields=None):
""" """
Delete the stored XBlock state for a single xblock usage. Delete the stored XBlock state for a single xblock usage.
...@@ -92,7 +109,12 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient): ...@@ -92,7 +109,12 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient):
assert self.user.username == username assert self.user.username == username
return self.delete_many(username, [block_key], scope, fields=fields) return self.delete_many(username, [block_key], scope, fields=fields)
@contract(username="basestring", block_key=UsageKey, scope=ScopeBase, fields="seq(basestring)|set(basestring)|None") @contract(
username="basestring",
block_key=UsageKey,
scope=ScopeBase,
fields="seq(basestring)|set(basestring)|None"
)
def get_mod_date(self, username, block_key, scope=Scope.user_state, fields=None): def get_mod_date(self, username, block_key, scope=Scope.user_state, fields=None):
""" """
Get the last modification date for fields from the specified blocks. Get the last modification date for fields from the specified blocks.
...@@ -140,7 +162,12 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient): ...@@ -140,7 +162,12 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient):
usage_key = student_module.module_state_key.map_into_course(student_module.course_id) usage_key = student_module.module_state_key.map_into_course(student_module.course_id)
yield (student_module, usage_key) yield (student_module, usage_key)
@contract(
username="basestring",
block_keys="seq(UsageKey)|set(UsageKey)",
scope=ScopeBase,
fields="seq(basestring)|set(basestring)|None"
)
def get_many(self, username, block_keys, scope=Scope.user_state, fields=None): def get_many(self, username, block_keys, scope=Scope.user_state, fields=None):
""" """
Retrieve the stored XBlock state for a single xblock usage. Retrieve the stored XBlock state for a single xblock usage.
...@@ -167,6 +194,7 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient): ...@@ -167,6 +194,7 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient):
state = json.loads(module.state) state = json.loads(module.state)
yield (usage_key, state) yield (usage_key, state)
@contract(username="basestring", block_keys_to_state="dict(UsageKey: dict(basestring: *))", scope=ScopeBase)
def set_many(self, username, block_keys_to_state, scope=Scope.user_state): def set_many(self, username, block_keys_to_state, scope=Scope.user_state):
""" """
Set fields for a particular XBlock. Set fields for a particular XBlock.
...@@ -208,6 +236,12 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient): ...@@ -208,6 +236,12 @@ class DjangoXBlockUserStateClient(XBlockUserStateClient):
# We just read this object, so we know that we can do an update # We just read this object, so we know that we can do an update
student_module.save(force_update=True) student_module.save(force_update=True)
@contract(
username="basestring",
block_keys="seq(UsageKey)|set(UsageKey)",
scope=ScopeBase,
fields="seq(basestring)|set(basestring)|None"
)
def delete_many(self, username, block_keys, scope=Scope.user_state, fields=None): def delete_many(self, username, block_keys, scope=Scope.user_state, fields=None):
""" """
Delete the stored XBlock state for a many xblock usages. Delete the stored XBlock state for a many xblock usages.
......
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