Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-user-state-client
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-user-state-client
Commits
a5eaf4a6
Commit
a5eaf4a6
authored
Jul 30, 2015
by
Ali Mohammad
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Removed get_mod_date and get_mod_date_many in favor of get and get_many,
since the semantics are essentially identical.
parent
b1d62640
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
162 deletions
+15
-162
edx_user_state_client/interface.py
+3
-59
edx_user_state_client/tests.py
+12
-103
No files found.
edx_user_state_client/interface.py
View file @
a5eaf4a6
...
...
@@ -32,7 +32,9 @@ class XBlockUserState(namedtuple('_XBlockUserState', ['username', 'block_key', '
* ``TYPE``: :class:`str`
* ``ALL``: ``None``
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.
"""
__slots__
=
()
...
...
@@ -157,35 +159,6 @@ class XBlockUserStateClient(object):
@contract
(
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)"
,
scope
=
ScopeBase
,
fields
=
"seq(basestring)|set(basestring)|None"
,
...
...
@@ -251,35 +224,6 @@ class XBlockUserStateClient(object):
"""
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
):
"""
Retrieve history of state changes for a given block for a given
...
...
edx_user_state_client/tests.py
View file @
a5eaf4a6
...
...
@@ -210,38 +210,6 @@ class _UserStateClientTestUtils(TestCase):
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
):
"""
...
...
@@ -389,57 +357,24 @@ class _UserStateClientTestCRUD(_UserStateClientTestUtils):
self
.
set_many
(
user
=
0
,
block_to_state
=
{
0
:
{
'a'
:
'b'
},
1
:
{
'b'
:
'c'
}})
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
.
assertGreater
(
mod_dates
.
updated
,
start_time
)
self
.
assertLess
(
mod_dates
.
updated
,
end_time
)
def
test_get_mod_date_many
(
self
):
self
.
set_many
(
user
=
0
,
block_to_state
=
{
0
:
{
'a'
:
'b'
},
1
:
{
'b'
:
'_'
,
'c'
:
'd'
}})
start_time1
=
datetime
.
now
()
def
test_get_many_mod_date
(
self
):
start_time
=
datetime
.
now
()
self
.
set_many
(
user
=
0
,
block_to_state
=
{
1
:
{
'b'
:
'c'
,
'c'
:
'd'
,
'd'
:
'e'
}})
end_time1
=
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
()
block_to_state
=
{
0
:
{
'a'
:
'b'
}})
mid_time
=
datetime
.
now
()
self
.
set_many
(
user
=
0
,
block_to_state
=
{
1
:
{
'a'
:
'new'
,
'b'
:
'c2'
,
'c'
:
'd'
}})
end_time2
=
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
)
block_to_state
=
{
1
:
{
'a'
:
'c'
}})
end_time
=
datetime
.
now
()
mod_dates
=
list
(
self
.
get_m
od_date_m
any
(
mod_dates
=
list
(
self
.
get_many
(
user
=
0
,
blocks
=
[
0
,
1
],
fields
=
[
"a"
]))
...
...
@@ -450,12 +385,13 @@ class _UserStateClientTestCRUD(_UserStateClientTestUtils):
self
.
assertItemsEqual
(
mod_dates
[
0
]
.
state
.
keys
(),
[
"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
(
mod_dates
[
1
]
.
state
.
keys
(),
[
"a"
])
self
.
assertGreater
(
mod_dates
[
1
]
.
updated
,
start_time2
)
self
.
assertLess
(
mod_dates
[
1
]
.
updated
,
end_time
2
)
self
.
assertGreater
(
mod_dates
[
1
]
.
updated
,
mid_time
)
self
.
assertLess
(
mod_dates
[
1
]
.
updated
,
end_time
)
class
_UserStateClientTestHistory
(
_UserStateClientTestUtils
):
...
...
@@ -734,33 +670,6 @@ class DictUserStateClient(XBlockUserStateClient):
else
:
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
):
"""
Retrieve history of state changes for a given block for a given
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment