Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
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-platform
Commits
ebb3c906
Commit
ebb3c906
authored
Apr 15, 2015
by
Calen Pennington
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Push `set_many` down into FieldDataCache from DjangoKeyValueStore
parent
951c19d7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
35 deletions
+51
-35
lms/djangoapps/courseware/model_data.py
+51
-35
No files found.
lms/djangoapps/courseware/model_data.py
View file @
ebb3c906
...
@@ -16,7 +16,7 @@ import logging
...
@@ -16,7 +16,7 @@ import logging
from
opaque_keys.edx.keys
import
CourseKey
from
opaque_keys.edx.keys
import
CourseKey
from
opaque_keys.edx.block_types
import
BlockTypeKeyV1
from
opaque_keys.edx.block_types
import
BlockTypeKeyV1
from
opaque_keys.edx.asides
import
AsideUsageKeyV1
from
opaque_keys.edx.asides
import
AsideUsageKeyV1
from
contracts
import
contract
from
contracts
import
contract
,
new_contract
from
django.db
import
DatabaseError
from
django.db
import
DatabaseError
...
@@ -148,41 +148,12 @@ class DjangoKeyValueStore(KeyValueStore):
...
@@ -148,41 +148,12 @@ class DjangoKeyValueStore(KeyValueStore):
xblock.KvsFieldData._key : value
xblock.KvsFieldData._key : value
"""
"""
saved_fields
=
[]
for
key
in
kv_dict
:
# field_objects maps a field_object to a list of associated fields
# Check key for validity
field_objects
=
dict
()
if
key
.
scope
not
in
self
.
_allowed_scopes
:
for
field
in
kv_dict
:
raise
InvalidScopeError
(
key
)
# Check field for validity
if
field
.
scope
not
in
self
.
_allowed_scopes
:
raise
InvalidScopeError
(
field
)
# If the field is valid and isn't already in the dictionary, add it.
field_object
=
self
.
_field_data_cache
.
find_or_create
(
field
)
if
field_object
not
in
field_objects
.
keys
():
field_objects
[
field_object
]
=
[]
# Update the list of associated fields
field_objects
[
field_object
]
.
append
(
field
)
# Special case when scope is for the user state, because this scope saves fields in a single row
if
field
.
scope
==
Scope
.
user_state
:
state
=
json
.
loads
(
field_object
.
state
)
state
[
field
.
field_name
]
=
kv_dict
[
field
]
field_object
.
state
=
json
.
dumps
(
state
)
else
:
# The remaining scopes save fields on different rows, so
# we don't have to worry about conflicts
field_object
.
value
=
json
.
dumps
(
kv_dict
[
field
])
for
field_object
in
field_objects
:
self
.
_field_data_cache
.
set_many
(
kv_dict
)
try
:
# Save the field object that we made above
field_object
.
save
()
# If save is successful on this scope, add the saved fields to
# the list of successful saves
saved_fields
.
extend
([
field
.
field_name
for
field
in
field_objects
[
field_object
]])
except
DatabaseError
:
log
.
exception
(
'Error saving fields
%
r'
,
field_objects
[
field_object
])
raise
KeyValueMultiSaveError
(
saved_fields
)
def
delete
(
self
,
key
):
def
delete
(
self
,
key
):
if
key
.
scope
not
in
self
.
_allowed_scopes
:
if
key
.
scope
not
in
self
.
_allowed_scopes
:
...
@@ -213,6 +184,9 @@ class DjangoKeyValueStore(KeyValueStore):
...
@@ -213,6 +184,9 @@ class DjangoKeyValueStore(KeyValueStore):
else
:
else
:
return
True
return
True
new_contract
(
"DjangoKeyValueStore"
,
DjangoKeyValueStore
)
new_contract
(
"DjangoKeyValueStore_Key"
,
DjangoKeyValueStore
.
Key
)
class
DjangoOrmFieldCache
(
object
):
class
DjangoOrmFieldCache
(
object
):
"""
"""
...
@@ -635,6 +609,48 @@ class FieldDataCache(object):
...
@@ -635,6 +609,48 @@ class FieldDataCache(object):
else
:
else
:
return
json
.
loads
(
field_object
.
value
)
return
json
.
loads
(
field_object
.
value
)
@contract
(
kv_dict
=
"dict(DjangoKeyValueStore_Key: *)"
)
def
set_many
(
self
,
kv_dict
):
"""
Set all of the fields specified by the keys of `kv_dict` to the values
in that dict.
Arguments:
kv_dict (dict): dict mapping from `DjangoKeyValueStore.Key`s to field values
Raises: DatabaseError if any fields fail to save
"""
saved_fields
=
[]
# field_objects maps a field_object to a list of associated fields
field_objects
=
dict
()
for
field
in
kv_dict
:
# If the field is valid and isn't already in the dictionary, add it.
field_object
=
self
.
find_or_create
(
field
)
if
field_object
not
in
field_objects
.
keys
():
field_objects
[
field_object
]
=
[]
# Update the list of associated fields
field_objects
[
field_object
]
.
append
(
field
)
# Special case when scope is for the user state, because this scope saves fields in a single row
if
field
.
scope
==
Scope
.
user_state
:
state
=
json
.
loads
(
field_object
.
state
)
state
[
field
.
field_name
]
=
kv_dict
[
field
]
field_object
.
state
=
json
.
dumps
(
state
)
else
:
# The remaining scopes save fields on different rows, so
# we don't have to worry about conflicts
field_object
.
value
=
json
.
dumps
(
kv_dict
[
field
])
for
field_object
in
field_objects
:
try
:
# Save the field object that we made above
field_object
.
save
()
# If save is successful on this scope, add the saved fields to
# the list of successful saves
saved_fields
.
extend
([
field
.
field_name
for
field
in
field_objects
[
field_object
]])
except
DatabaseError
:
log
.
exception
(
'Error saving fields
%
r'
,
field_objects
[
field_object
])
raise
KeyValueMultiSaveError
(
saved_fields
)
def
find
(
self
,
key
):
def
find
(
self
,
key
):
'''
'''
Look for a model data object using an DjangoKeyValueStore.Key object
Look for a model data object using an DjangoKeyValueStore.Key object
...
...
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