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
9f45dd74
Commit
9f45dd74
authored
Jul 03, 2012
by
Calen Pennington
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use _id to store the location, rather than location
parent
42144ee9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
32 deletions
+37
-32
common/lib/xmodule/xmodule/modulestore/mongo.py
+37
-32
No files found.
common/lib/xmodule/xmodule/modulestore/mongo.py
View file @
9f45dd74
import
pymongo
from
bson.objectid
import
ObjectId
from
importlib
import
import_module
from
xmodule.x_module
import
XModuleDescriptor
from
xmodule.mako_module
import
MakoDescriptorSystem
...
...
@@ -8,6 +9,19 @@ from . import ModuleStore, Location
from
.exceptions
import
ItemNotFoundError
,
InsufficientSpecificationError
# TODO (cpennington): This code currently operates under the assumption that
# there is only one revision for each item. Once we start versioning inside the CMS,
# that assumption will have to change
def
location_to_query
(
loc
):
query
=
{}
for
key
,
val
in
Location
(
loc
)
.
dict
()
.
iteritems
():
if
val
is
not
None
:
query
[
'_id.{key}'
.
format
(
key
=
key
)]
=
val
return
query
class
MongoModuleStore
(
ModuleStore
):
"""
A Mongodb backed ModuleStore
...
...
@@ -17,7 +31,6 @@ class MongoModuleStore(ModuleStore):
host
=
host
,
port
=
port
)[
db
][
collection
]
self
.
collection
.
ensure_index
(
'location'
)
# Force mongo to report errors, at the expense of performance
self
.
collection
.
safe
=
True
...
...
@@ -26,6 +39,18 @@ class MongoModuleStore(ModuleStore):
class_
=
getattr
(
import_module
(
module_path
),
class_name
)
self
.
default_class
=
class_
# TODO (cpennington): Pass a proper resources_fs to the system
self
.
system
=
MakoDescriptorSystem
(
load_item
=
self
.
get_item
,
resources_fs
=
None
,
render_template
=
render_to_string
)
def
_load_item
(
self
,
item
):
item
[
'location'
]
=
item
[
'_id'
]
del
item
[
'_id'
]
return
XModuleDescriptor
.
load_from_json
(
item
,
self
.
system
,
self
.
default_class
)
def
get_item
(
self
,
location
):
"""
Returns an XModuleDescriptor instance for the item at location.
...
...
@@ -39,47 +64,26 @@ class MongoModuleStore(ModuleStore):
location: Something that can be passed to Location
"""
query
=
{}
for
key
,
val
in
Location
(
location
)
.
dict
()
.
iteritems
():
if
key
!=
'revision'
and
val
is
None
:
raise
InsufficientSpecificationError
(
location
)
if
val
is
not
None
:
query
[
'location.{key}'
.
format
(
key
=
key
)]
=
val
item
=
self
.
collection
.
find_one
(
query
,
location_to_query
(
location
)
,
sort
=
[(
'revision'
,
pymongo
.
ASCENDING
)],
)
if
item
is
None
:
raise
ItemNotFoundError
(
location
)
# TODO (cpennington): Pass a proper resources_fs to the system
return
XModuleDescriptor
.
load_from_json
(
item
,
MakoDescriptorSystem
(
load_item
=
self
.
get_item
,
resources_fs
=
None
,
render_template
=
render_to_string
),
self
.
default_class
)
return
self
.
_load_item
(
item
)
def
get_items
(
self
,
location
,
default_class
=
None
):
query
=
{}
for
key
,
val
in
Location
(
location
)
.
dict
()
.
iteritems
():
if
val
is
not
None
:
query
[
'location.{key}'
.
format
(
key
=
key
)]
=
val
print
location_to_query
(
location
)
items
=
self
.
collection
.
find
(
query
,
location_to_query
(
location
)
,
sort
=
[(
'revision'
,
pymongo
.
ASCENDING
)],
)
# TODO (cpennington): Pass a proper resources_fs to the system
system
=
MakoDescriptorSystem
(
load_item
=
self
.
get_item
,
resources_fs
=
None
,
render_template
=
render_to_string
)
return
[
XModuleDescriptor
.
load_from_json
(
item
,
system
,
self
.
default_class
)
for
item
in
items
]
return
[
self
.
_load_item
(
item
)
for
item
in
items
]
def
create_item
(
self
,
location
):
"""
...
...
@@ -88,7 +92,7 @@ class MongoModuleStore(ModuleStore):
location: Something that can be passed to Location
"""
self
.
collection
.
insert
({
'
location
'
:
Location
(
location
)
.
dict
(),
'
_id
'
:
Location
(
location
)
.
dict
(),
})
def
update_item
(
self
,
location
,
data
):
...
...
@@ -103,8 +107,9 @@ class MongoModuleStore(ModuleStore):
# See http://www.mongodb.org/display/DOCS/Updating for
# atomic update syntax
self
.
collection
.
update
(
{
'location'
:
Location
(
location
)
.
dict
()},
{
'$set'
:
{
'definition.data'
:
data
}}
{
'_id'
:
Location
(
location
)
.
dict
()},
{
'$set'
:
{
'definition.data'
:
data
}},
)
def
update_children
(
self
,
location
,
children
):
...
...
@@ -119,7 +124,7 @@ class MongoModuleStore(ModuleStore):
# See http://www.mongodb.org/display/DOCS/Updating for
# atomic update syntax
self
.
collection
.
update
(
{
'
location
'
:
Location
(
location
)
.
dict
()},
{
'
_id
'
:
Location
(
location
)
.
dict
()},
{
'$set'
:
{
'definition.children'
:
children
}}
)
...
...
@@ -135,6 +140,6 @@ class MongoModuleStore(ModuleStore):
# See http://www.mongodb.org/display/DOCS/Updating for
# atomic update syntax
self
.
collection
.
update
(
{
'
location
'
:
Location
(
location
)
.
dict
()},
{
'
_id
'
:
Location
(
location
)
.
dict
()},
{
'$set'
:
{
'metadata'
:
metadata
}}
)
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