Commit a5eaf4a6 by Ali Mohammad

Removed get_mod_date and get_mod_date_many in favor of get and get_many,

since the semantics are essentially identical.
parent b1d62640
...@@ -32,7 +32,9 @@ class XBlockUserState(namedtuple('_XBlockUserState', ['username', 'block_key', ' ...@@ -32,7 +32,9 @@ class XBlockUserState(namedtuple('_XBlockUserState', ['username', 'block_key', '
* ``TYPE``: :class:`str` * ``TYPE``: :class:`str`
* ``ALL``: ``None`` * ``ALL``: ``None``
state: A dict mapping field names to the values of those fields for this XBlock. state: A dict mapping field names to the values of those fields for this XBlock.
updated: A :class:`datetime.datetime` that identifies when this state was stored. updated: A :class:`datetime.datetime`. We guarantee that the fields
that were returned in "state" have not been changed since
this time.
scope: A :class:`xblock.fields.Scope` identifying which XBlock scope this state is coming from. scope: A :class:`xblock.fields.Scope` identifying which XBlock scope this state is coming from.
""" """
__slots__ = () __slots__ = ()
...@@ -157,35 +159,6 @@ class XBlockUserStateClient(object): ...@@ -157,35 +159,6 @@ class XBlockUserStateClient(object):
@contract( @contract(
username="basestring", username="basestring",
block_key="block_key",
scope=ScopeBase,
fields="seq(basestring)|set(basestring)|None",
returns=XBlockUserState,
modify_docstring=False,
)
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.
Arguments:
username: The name of the user whose state should queried
block_key: The key identifying which xblock modification dates to retrieve.
scope (Scope): The scope to retrieve from.
fields: A list of fields to query. If None, query all fields.
Specific implementations are free to return the same modification date
for all fields, if they don't store changes individually per field.
Implementations may omit fields for which data has not been stored.
Returns: XBlockUserState instance. The updated attribute is a
guarantee that none of the fields have been modified since
then.
"""
results = self.get_mod_date_many(username, [block_key], scope, fields=fields)
return results.next()
@contract(
username="basestring",
block_keys="seq(block_key)|set(block_key)", block_keys="seq(block_key)|set(block_key)",
scope=ScopeBase, scope=ScopeBase,
fields="seq(basestring)|set(basestring)|None", fields="seq(basestring)|set(basestring)|None",
...@@ -251,35 +224,6 @@ class XBlockUserStateClient(object): ...@@ -251,35 +224,6 @@ class XBlockUserStateClient(object):
""" """
raise NotImplementedError() raise NotImplementedError()
@contract(
username="basestring",
block_keys="seq(block_key)|set(block_key)",
scope=ScopeBase,
fields="seq(basestring)|set(basestring)|None",
modify_docstring=False,
)
@abstractmethod
def get_mod_date_many(self, username, block_keys, scope=Scope.user_state, fields=None):
"""
Get the last modification date for fields from the specified blocks.
Arguments:
username: The name of the user whose state should be queried
block_key: The key identifying which xblock modification dates to retrieve.
scope (Scope): The scope to retrieve from.
fields: A list of fields to query. If None, returns dates for all
stored fields.
Specific implementations are free to return the same modification date
for all fields, if they don't store changes individually per field.
Implementations may omit fields for which data has not been stored.
Yields: XBlockUserState instances with the requested fields for each
key. The updated attribute is a guarantee that none of the
fields have been modified since then.
"""
raise NotImplementedError()
def get_history(self, username, block_key, scope=Scope.user_state): def get_history(self, username, block_key, scope=Scope.user_state):
""" """
Retrieve history of state changes for a given block for a given Retrieve history of state changes for a given block for a given
......
...@@ -210,38 +210,6 @@ class _UserStateClientTestUtils(TestCase): ...@@ -210,38 +210,6 @@ class _UserStateClientTestUtils(TestCase):
scope=self.scope, scope=self.scope,
) )
@contract(user=int, block="int", fields="list(string)|None")
def get_mod_date(self, user, block, fields=None):
"""
Get the modification dates for the fields for the specified block.
This wraps :meth:`~XBlockUserStateClient.get_mod_date`
to take indexes rather than actual values, to make tests easier
to write concisely.
"""
return self.client.get_mod_date(
username=self._user(user),
block_key=self._block(block),
scope=self.scope,
fields=fields,
)
@contract(user=int, blocks="list(int)", fields="list(string)|None")
def get_mod_date_many(self, user, blocks, fields=None):
"""
Get the modification times for the specified user, blocks, and fields.
This wraps :meth:`~XBlockUserStateClient.get_mod_date_many`
to take indexes rather than actual values to make tests easier
to write concisely.
"""
return self.client.get_mod_date_many(
username=self._user(user),
block_keys=[self._block(block) for block in blocks],
scope=self.scope,
fields=fields,
)
class _UserStateClientTestCRUD(_UserStateClientTestUtils): class _UserStateClientTestCRUD(_UserStateClientTestUtils):
""" """
...@@ -389,57 +357,24 @@ class _UserStateClientTestCRUD(_UserStateClientTestUtils): ...@@ -389,57 +357,24 @@ class _UserStateClientTestCRUD(_UserStateClientTestUtils):
self.set_many(user=0, block_to_state={0: {'a': 'b'}, 1: {'b': 'c'}}) self.set_many(user=0, block_to_state={0: {'a': 'b'}, 1: {'b': 'c'}})
end_time = datetime.now() end_time = datetime.now()
mod_dates = self.get_mod_date(user=0, block=0) mod_dates = self.get(user=0, block=0)
self.assertItemsEqual(mod_dates.state.keys(), ["a"]) self.assertItemsEqual(mod_dates.state.keys(), ["a"])
self.assertGreater(mod_dates.updated, start_time) self.assertGreater(mod_dates.updated, start_time)
self.assertLess(mod_dates.updated, end_time) self.assertLess(mod_dates.updated, end_time)
def test_get_mod_date_many(self): def test_get_many_mod_date(self):
self.set_many( start_time = datetime.now()
user=0,
block_to_state={0: {'a': 'b'}, 1: {'b': '_', 'c': 'd'}})
start_time1 = datetime.now()
self.set_many( self.set_many(
user=0, user=0,
block_to_state={1: {'b': 'c', 'c': 'd', 'd': 'e'}}) block_to_state={0: {'a': 'b'}})
end_time1 = datetime.now() mid_time = datetime.now()
mod_dates = list(self.get_mod_date_many(
user=0,
blocks=[0, 1],
fields=["b", "c"]))
self.assertItemsEqual(
[result.block_key for result in mod_dates],
[self._block(1)])
self.assertItemsEqual(
sorted(mod_dates[0].state.keys()),
["b", "c"])
self.assertGreater(mod_dates[0].updated, start_time1)
self.assertLess(mod_dates[0].updated, end_time1)
start_time2 = datetime.now()
self.set_many( self.set_many(
user=0, user=0,
block_to_state={1: {'a': 'new', 'b': 'c2', 'c': 'd'}}) block_to_state={1: {'a': 'c'}})
end_time2 = datetime.now() end_time = datetime.now()
mod_dates = list(self.get_mod_date_many(
user=0,
blocks=[0, 1],
fields=["b", "c"]))
self.assertItemsEqual(
[result.block_key for result in mod_dates],
[self._block(1)])
self.assertItemsEqual(
sorted(mod_dates[0].state.keys()),
["b", "c"])
self.assertGreater(mod_dates[0].updated, start_time2)
self.assertLess(mod_dates[0].updated, end_time2)
mod_dates = list(self.get_mod_date_many( mod_dates = list(self.get_many(
user=0, user=0,
blocks=[0, 1], blocks=[0, 1],
fields=["a"])) fields=["a"]))
...@@ -450,12 +385,13 @@ class _UserStateClientTestCRUD(_UserStateClientTestUtils): ...@@ -450,12 +385,13 @@ class _UserStateClientTestCRUD(_UserStateClientTestUtils):
self.assertItemsEqual( self.assertItemsEqual(
mod_dates[0].state.keys(), mod_dates[0].state.keys(),
["a"]) ["a"])
self.assertLess(mod_dates[0].updated, start_time1) self.assertGreater(mod_dates[0].updated, start_time)
self.assertLess(mod_dates[0].updated, mid_time)
self.assertItemsEqual( self.assertItemsEqual(
mod_dates[1].state.keys(), mod_dates[1].state.keys(),
["a"]) ["a"])
self.assertGreater(mod_dates[1].updated, start_time2) self.assertGreater(mod_dates[1].updated, mid_time)
self.assertLess(mod_dates[1].updated, end_time2) self.assertLess(mod_dates[1].updated, end_time)
class _UserStateClientTestHistory(_UserStateClientTestUtils): class _UserStateClientTestHistory(_UserStateClientTestUtils):
...@@ -734,33 +670,6 @@ class DictUserStateClient(XBlockUserStateClient): ...@@ -734,33 +670,6 @@ class DictUserStateClient(XBlockUserStateClient):
else: else:
self._add_state(username, key, scope, state) self._add_state(username, key, scope, state)
def get_mod_date_many(self, username, block_keys, scope=Scope.user_state, fields=None):
for key in block_keys:
if (username, key, scope) not in self._history:
continue
entry = self._history[(username, key, scope)][0]
if entry.state is None:
continue
if fields is None:
current_fields = entry.state.keys()
else:
current_fields = fields
filtered_state = {
field: entry.state[field]
for field in current_fields
if field in entry.state
}
if len(filtered_state) > 0:
yield XBlockUserState(username,
key,
filtered_state,
entry.updated,
entry.scope)
def get_history(self, username, block_key, scope=Scope.user_state): def get_history(self, username, block_key, scope=Scope.user_state):
""" """
Retrieve history of state changes for a given block for a given Retrieve history of state changes for a given block for a given
......
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