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
436b32a6
Commit
436b32a6
authored
Nov 04, 2014
by
Don Mitchell
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #5800 from edx/dhm/xml_assetstore
Abstract asset methods into own interface class
parents
bc20ccb3
aa07355e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
732 additions
and
632 deletions
+732
-632
common/lib/xmodule/xmodule/modulestore/__init__.py
+637
-630
common/lib/xmodule/xmodule/modulestore/tests/test_assetstore.py
+19
-1
common/lib/xmodule/xmodule/modulestore/tests/test_cross_modulestore_import_export.py
+27
-1
common/lib/xmodule/xmodule/modulestore/xml.py
+49
-0
No files found.
common/lib/xmodule/xmodule/modulestore/__init__.py
View file @
436b32a6
...
@@ -266,7 +266,290 @@ class BulkOperationsMixin(object):
...
@@ -266,7 +266,290 @@ class BulkOperationsMixin(object):
return
self
.
_get_bulk_ops_record
(
course_key
,
ignore_case
)
.
active
return
self
.
_get_bulk_ops_record
(
course_key
,
ignore_case
)
.
active
class
ModuleStoreRead
(
object
):
class
ModuleStoreAssetInterface
(
object
):
"""
The methods for accessing assets and their metadata
"""
def
_find_course_assets
(
self
,
course_key
):
"""
Base method to override.
"""
raise
NotImplementedError
()
def
_find_course_asset
(
self
,
course_key
,
filename
,
get_thumbnail
=
False
):
"""
Internal; finds or creates course asset info -and- finds existing asset (or thumbnail) metadata.
Arguments:
course_key (CourseKey): course identifier
filename (str): filename of the asset or thumbnail
get_thumbnail (bool): True gets thumbnail data, False gets asset data
Returns:
Asset info for the course, index of asset/thumbnail in list (None if asset/thumbnail does not exist)
"""
course_assets
=
self
.
_find_course_assets
(
course_key
)
if
course_assets
is
None
:
return
None
,
None
if
get_thumbnail
:
all_assets
=
course_assets
[
'thumbnails'
]
else
:
all_assets
=
course_assets
[
'assets'
]
# See if this asset already exists by checking the external_filename.
# Studio doesn't currently support using multiple course assets with the same filename.
# So use the filename as the unique identifier.
for
idx
,
asset
in
enumerate
(
all_assets
):
if
asset
[
'filename'
]
==
filename
:
return
course_assets
,
idx
return
course_assets
,
None
@contract
(
asset_key
=
'AssetKey'
)
def
_find_asset_info
(
self
,
asset_key
,
thumbnail
=
False
,
**
kwargs
):
"""
Find the info for a particular course asset/thumbnail.
Arguments:
asset_key (AssetKey): key containing original asset filename
thumbnail (bool): True if finding thumbnail, False if finding asset metadata
Returns:
asset/thumbnail metadata (AssetMetadata/AssetThumbnailMetadata) -or- None if not found
"""
course_assets
,
asset_idx
=
self
.
_find_course_asset
(
asset_key
.
course_key
,
asset_key
.
path
,
thumbnail
)
if
asset_idx
is
None
:
return
None
if
thumbnail
:
info
=
'thumbnails'
mdata
=
AssetThumbnailMetadata
(
asset_key
,
asset_key
.
path
,
**
kwargs
)
else
:
info
=
'assets'
mdata
=
AssetMetadata
(
asset_key
,
asset_key
.
path
,
**
kwargs
)
all_assets
=
course_assets
[
info
]
mdata
.
from_mongo
(
all_assets
[
asset_idx
])
return
mdata
@contract
(
asset_key
=
'AssetKey'
)
def
find_asset_metadata
(
self
,
asset_key
,
**
kwargs
):
"""
Find the metadata for a particular course asset.
Arguments:
asset_key (AssetKey): key containing original asset filename
Returns:
asset metadata (AssetMetadata) -or- None if not found
"""
return
self
.
_find_asset_info
(
asset_key
,
thumbnail
=
False
,
**
kwargs
)
@contract
(
asset_key
=
'AssetKey'
)
def
find_asset_thumbnail_metadata
(
self
,
asset_key
,
**
kwargs
):
"""
Find the metadata for a particular course asset.
Arguments:
asset_key (AssetKey): key containing original asset filename
Returns:
asset metadata (AssetMetadata) -or- None if not found
"""
return
self
.
_find_asset_info
(
asset_key
,
thumbnail
=
True
,
**
kwargs
)
@contract
(
course_key
=
'CourseKey'
,
start
=
'int | None'
,
maxresults
=
'int | None'
,
sort
=
'list | None'
,
get_thumbnails
=
'bool'
)
def
_get_all_asset_metadata
(
self
,
course_key
,
start
=
0
,
maxresults
=-
1
,
sort
=
None
,
get_thumbnails
=
False
,
**
kwargs
):
"""
Returns a list of static asset (or thumbnail) metadata for a course.
Args:
course_key (CourseKey): course identifier
start (int): optional - start at this asset number
maxresults (int): optional - return at most this many, -1 means no limit
sort (array): optional - None means no sort
(sort_by (str), sort_order (str))
sort_by - one of 'uploadDate' or 'displayname'
sort_order - one of 'ascending' or 'descending'
get_thumbnails (bool): True if getting thumbnail metadata, else getting asset metadata
Returns:
List of AssetMetadata or AssetThumbnailMetadata objects.
"""
course_assets
=
self
.
_find_course_assets
(
course_key
)
if
course_assets
is
None
:
# If no course assets are found, return None instead of empty list
# to distinguish zero assets from "not able to retrieve assets".
return
None
if
get_thumbnails
:
all_assets
=
course_assets
.
get
(
'thumbnails'
,
[])
else
:
all_assets
=
course_assets
.
get
(
'assets'
,
[])
# DO_NEXT: Add start/maxresults/sort functionality as part of https://openedx.atlassian.net/browse/PLAT-74
if
start
and
maxresults
and
sort
:
pass
ret_assets
=
[]
for
asset
in
all_assets
:
if
get_thumbnails
:
thumb
=
AssetThumbnailMetadata
(
course_key
.
make_asset_key
(
'thumbnail'
,
asset
[
'filename'
]),
internal_name
=
asset
[
'filename'
],
**
kwargs
)
ret_assets
.
append
(
thumb
)
else
:
asset
=
AssetMetadata
(
course_key
.
make_asset_key
(
'asset'
,
asset
[
'filename'
]),
basename
=
asset
[
'filename'
],
edited_on
=
asset
[
'edit_info'
][
'edited_on'
],
contenttype
=
asset
[
'contenttype'
],
md5
=
str
(
asset
[
'md5'
]),
**
kwargs
)
ret_assets
.
append
(
asset
)
return
ret_assets
@contract
(
course_key
=
'CourseKey'
,
start
=
'int | None'
,
maxresults
=
'int | None'
,
sort
=
'list | None'
)
def
get_all_asset_metadata
(
self
,
course_key
,
start
=
0
,
maxresults
=-
1
,
sort
=
None
,
**
kwargs
):
"""
Returns a list of static assets for a course.
By default all assets are returned, but start and maxresults can be provided to limit the query.
Args:
course_key (CourseKey): course identifier
start (int): optional - start at this asset number
maxresults (int): optional - return at most this many, -1 means no limit
sort (array): optional - None means no sort
(sort_by (str), sort_order (str))
sort_by - one of 'uploadDate' or 'displayname'
sort_order - one of 'ascending' or 'descending'
Returns:
List of AssetMetadata objects.
"""
return
self
.
_get_all_asset_metadata
(
course_key
,
start
,
maxresults
,
sort
,
get_thumbnails
=
False
,
**
kwargs
)
@contract
(
course_key
=
'CourseKey'
)
def
get_all_asset_thumbnail_metadata
(
self
,
course_key
,
**
kwargs
):
"""
Returns a list of thumbnails for all course assets.
Args:
course_key (CourseKey): course identifier
Returns:
List of AssetThumbnailMetadata objects.
"""
return
self
.
_get_all_asset_metadata
(
course_key
,
get_thumbnails
=
True
,
**
kwargs
)
class
ModuleStoreAssetWriteInterface
(
ModuleStoreAssetInterface
):
"""
The write operations for assets and asset metadata
"""
def
_save_asset_info
(
self
,
course_key
,
asset_metadata
,
user_id
,
thumbnail
=
False
):
"""
Base method to over-ride in modulestore.
"""
raise
NotImplementedError
()
@contract
(
course_key
=
'CourseKey'
,
asset_metadata
=
'AssetMetadata'
)
def
save_asset_metadata
(
self
,
course_key
,
asset_metadata
,
user_id
):
"""
Saves the asset metadata for a particular course's asset.
Arguments:
course_key (CourseKey): course identifier
asset_metadata (AssetMetadata): data about the course asset data
Returns:
True if metadata save was successful, else False
"""
return
self
.
_save_asset_info
(
course_key
,
asset_metadata
,
user_id
,
thumbnail
=
False
)
@contract
(
course_key
=
'CourseKey'
,
asset_thumbnail_metadata
=
'AssetThumbnailMetadata'
)
def
save_asset_thumbnail_metadata
(
self
,
course_key
,
asset_thumbnail_metadata
,
user_id
):
"""
Saves the asset thumbnail metadata for a particular course asset's thumbnail.
Arguments:
course_key (CourseKey): course identifier
asset_thumbnail_metadata (AssetThumbnailMetadata): data about the course asset thumbnail
Returns:
True if thumbnail metadata save was successful, else False
"""
return
self
.
_save_asset_info
(
course_key
,
asset_thumbnail_metadata
,
user_id
,
thumbnail
=
True
)
def
set_asset_metadata_attrs
(
self
,
asset_key
,
attrs
,
user_id
):
"""
Base method to over-ride in modulestore.
"""
raise
NotImplementedError
()
def
_delete_asset_data
(
self
,
asset_key
,
user_id
,
thumbnail
=
False
):
"""
Base method to over-ride in modulestore.
"""
raise
NotImplementedError
()
@contract
(
asset_key
=
'AssetKey'
,
attr
=
str
)
def
set_asset_metadata_attr
(
self
,
asset_key
,
attr
,
value
,
user_id
):
"""
Add/set the given attr on the asset at the given location. Value can be any type which pymongo accepts.
Arguments:
asset_key (AssetKey): asset identifier
attr (str): which attribute to set
value: the value to set it to (any type pymongo accepts such as datetime, number, string)
Raises:
ItemNotFoundError if no such item exists
AttributeError is attr is one of the build in attrs.
"""
return
self
.
set_asset_metadata_attrs
(
asset_key
,
{
attr
:
value
},
user_id
)
@contract
(
asset_key
=
'AssetKey'
)
def
delete_asset_metadata
(
self
,
asset_key
,
user_id
):
"""
Deletes a single asset's metadata.
Arguments:
asset_key (AssetKey): locator containing original asset filename
Returns:
Number of asset metadata entries deleted (0 or 1)
"""
return
self
.
_delete_asset_data
(
asset_key
,
user_id
,
thumbnail
=
False
)
@contract
(
asset_key
=
'AssetKey'
)
def
delete_asset_thumbnail_metadata
(
self
,
asset_key
,
user_id
):
"""
Deletes a single asset's metadata.
Arguments:
asset_key (AssetKey): locator containing original asset filename
Returns:
Number of asset metadata entries deleted (0 or 1)
"""
return
self
.
_delete_asset_data
(
asset_key
,
user_id
,
thumbnail
=
True
)
@contract
(
source_course_key
=
'CourseKey'
,
dest_course_key
=
'CourseKey'
)
def
copy_all_asset_metadata
(
self
,
source_course_key
,
dest_course_key
,
user_id
):
"""
Copy all the course assets from source_course_key to dest_course_key.
Arguments:
source_course_key (CourseKey): identifier of course to copy from
dest_course_key (CourseKey): identifier of course to copy to
"""
pass
# pylint: disable=abstract-method
class
ModuleStoreRead
(
ModuleStoreAssetInterface
):
"""
"""
An abstract interface for a database backend that stores XModuleDescriptor
An abstract interface for a database backend that stores XModuleDescriptor
instances and extends read-only functionality
instances and extends read-only functionality
...
@@ -316,18 +599,13 @@ class ModuleStoreRead(object):
...
@@ -316,18 +599,13 @@ class ModuleStoreRead(object):
pass
pass
@abstractmethod
@abstractmethod
def
get_items
(
self
,
location
,
course_id
=
None
,
depth
=
0
,
qualifiers
=
None
,
**
kwargs
):
def
get_items
(
self
,
course_id
,
qualifiers
=
None
,
**
kwargs
):
"""
"""
Returns a list of XModuleDescriptor instances for the items
Returns a list of XModuleDescriptor instances for the items
that match location. Any element of location that is None is treated
that match location. Any element of location that is None is treated
as a wildcard that matches any value
as a wildcard that matches any value
location: Something that can be passed to Location
location: Something that can be passed to Location
depth: An argument that some module stores may use to prefetch
descendents of the queried modules for more efficient results later
in the request. The depth is counted in the number of calls to
get_children() to cache. None indicates to cache all descendents
"""
"""
pass
pass
...
@@ -387,7 +665,7 @@ class ModuleStoreRead(object):
...
@@ -387,7 +665,7 @@ class ModuleStoreRead(object):
'''
'''
if
isinstance
(
target
,
list
):
if
isinstance
(
target
,
list
):
return
any
(
self
.
_value_matches
(
ele
,
criteria
)
for
ele
in
target
)
return
any
(
self
.
_value_matches
(
ele
,
criteria
)
for
ele
in
target
)
elif
isinstance
(
criteria
,
re
.
_pattern_type
):
elif
isinstance
(
criteria
,
re
.
_pattern_type
):
# pylint: disable=protected-access
return
criteria
.
search
(
target
)
is
not
None
return
criteria
.
search
(
target
)
is
not
None
elif
callable
(
criteria
):
elif
callable
(
criteria
):
return
criteria
(
target
)
return
criteria
(
target
)
...
@@ -395,762 +673,491 @@ class ModuleStoreRead(object):
...
@@ -395,762 +673,491 @@ class ModuleStoreRead(object):
# note isn't handling any other things in the dict other than in
# note isn't handling any other things in the dict other than in
return
any
(
self
.
_value_matches
(
target
,
test_val
)
for
test_val
in
criteria
[
'$in'
])
return
any
(
self
.
_value_matches
(
target
,
test_val
)
for
test_val
in
criteria
[
'$in'
])
elif
isinstance
(
criteria
,
dict
)
and
'$nin'
in
criteria
:
elif
isinstance
(
criteria
,
dict
)
and
'$nin'
in
criteria
:
# note isn't handling any other things in the dict other than nin
# note isn't handling any other things in the dict other than nin
return
not
any
(
self
.
_value_matches
(
target
,
test_val
)
for
test_val
in
criteria
[
'$nin'
])
return
not
any
(
self
.
_value_matches
(
target
,
test_val
)
for
test_val
in
criteria
[
'$nin'
])
else
:
else
:
return
criteria
==
target
return
criteria
==
target
@abstractmethod
def
make_course_key
(
self
,
org
,
course
,
run
):
"""
Return a valid :class:`~opaque_keys.edx.keys.CourseKey` for this modulestore
that matches the supplied `org`, `course`, and `run`.
This key may represent a course that doesn't exist in this modulestore.
"""
pass
@abstractmethod
def
get_courses
(
self
,
**
kwargs
):
'''
Returns a list containing the top level XModuleDescriptors of the courses
in this modulestore.
'''
pass
@abstractmethod
def
get_course
(
self
,
course_id
,
depth
=
0
,
**
kwargs
):
'''
Look for a specific course by its id (:class:`CourseKey`).
Returns the course descriptor, or None if not found.
'''
pass
@abstractmethod
def
has_course
(
self
,
course_id
,
ignore_case
=
False
,
**
kwargs
):
'''
Look for a specific course id. Returns whether it exists.
Args:
course_id (CourseKey):
ignore_case (boolean): some modulestores are case-insensitive. Use this flag
to search for whether a potentially conflicting course exists in that case.
'''
pass
@abstractmethod
def
get_parent_location
(
self
,
location
,
**
kwargs
):
'''
Find the location that is the parent of this location in this
course. Needed for path_to_location().
'''
pass
@abstractmethod
def
get_orphans
(
self
,
course_key
,
**
kwargs
):
"""
Get all of the xblocks in the given course which have no parents and are not of types which are
usually orphaned. NOTE: may include xblocks which still have references via xblocks which don't
use children to point to their dependents.
"""
pass
@abstractmethod
def
get_errored_courses
(
self
):
"""
Return a dictionary of course_dir -> [(msg, exception_str)], for each
course_dir where course loading failed.
"""
pass
@abstractmethod
def
get_modulestore_type
(
self
,
course_id
):
"""
Returns a type which identifies which modulestore is servicing the given
course_id. The return can be either "xml" (for XML based courses) or "mongo" for MongoDB backed courses
"""
pass
@abstractmethod
def
get_courses_for_wiki
(
self
,
wiki_slug
,
**
kwargs
):
"""
Return the list of courses which use this wiki_slug
:param wiki_slug: the course wiki root slug
:return: list of course keys
"""
pass
@abstractmethod
def
has_published_version
(
self
,
xblock
):
"""
Returns true if this xblock exists in the published course regardless of whether it's up to date
"""
pass
@abstractmethod
def
close_connections
(
self
):
"""
Closes any open connections to the underlying databases
"""
pass
@contextmanager
def
bulk_operations
(
self
,
course_id
):
"""
A context manager for notifying the store of bulk operations. This affects only the current thread.
"""
yield
def
ensure_indexes
(
self
):
"""
Ensure that all appropriate indexes are created that are needed by this modulestore, or raise
an exception if unable to.
This method is intended for use by tests and administrative commands, and not
to be run during server startup.
"""
pass
class
ModuleStoreWrite
(
ModuleStoreRead
):
"""
An abstract interface for a database backend that stores XModuleDescriptor
instances and extends both read and write functionality
"""
__metaclass__
=
ABCMeta
@abstractmethod
def
update_item
(
self
,
xblock
,
user_id
,
allow_not_found
=
False
,
force
=
False
,
**
kwargs
):
"""
Update the given xblock's persisted repr. Pass the user's unique id which the persistent store
should save with the update if it has that ability.
:param allow_not_found: whether this method should raise an exception if the given xblock
has not been persisted before.
:param force: fork the structure and don't update the course draftVersion if there's a version
conflict (only applicable to version tracking and conflict detecting persistence stores)
:raises VersionConflictError: if org, course, run, and version_guid given and the current
version head != version_guid and force is not True. (only applicable to version tracking stores)
"""
pass
@abstractmethod
def
delete_item
(
self
,
location
,
user_id
,
**
kwargs
):
"""
Delete an item and its subtree from persistence. Remove the item from any parents (Note, does not
affect parents from other branches or logical branches; thus, in old mongo, deleting something
whose parent cannot be draft, deletes it from both but deleting a component under a draft vertical
only deletes it from the draft.
Pass the user's unique id which the persistent store
should save with the update if it has that ability.
:param force: fork the structure and don't update the course draftVersion if there's a version
conflict (only applicable to version tracking and conflict detecting persistence stores)
:raises VersionConflictError: if org, course, run, and version_guid given and the current
version head != version_guid and force is not True. (only applicable to version tracking stores)
"""
pass
@abstractmethod
def
create_course
(
self
,
org
,
course
,
run
,
user_id
,
fields
=
None
,
**
kwargs
):
"""
Creates and returns the course.
Args:
org (str): the organization that owns the course
course (str): the name of the course
run (str): the name of the run
user_id: id of the user creating the course
fields (dict): Fields to set on the course at initialization
kwargs: Any optional arguments understood by a subset of modulestores to customize instantiation
Returns: a CourseDescriptor
"""
pass
@abstractmethod
def
create_item
(
self
,
user_id
,
course_key
,
block_type
,
block_id
=
None
,
fields
=
None
,
**
kwargs
):
"""
Creates and saves a new item in a course.
Returns the newly created item.
Args:
user_id: ID of the user creating and saving the xmodule
course_key: A :class:`~opaque_keys.edx.CourseKey` identifying which course to create
this item in
block_type: The type of block to create
block_id: a unique identifier for the new item. If not supplied,
a new identifier will be generated
fields (dict): A dictionary specifying initial values for some or all fields
in the newly created block
"""
pass
@abstractmethod
@abstractmethod
def
clone_course
(
self
,
source_course_id
,
dest_course_id
,
user_id
,
fields
=
None
):
def
make_course_key
(
self
,
org
,
course
,
run
):
"""
"""
Sets up source_course_id to point a course with the same content as the desct_course_id. This
Return a valid :class:`~opaque_keys.edx.keys.CourseKey` for this modulestore
operation may be cheap or expensive. It may have to copy all assets and all xblock content or
that matches the supplied `org`, `course`, and `run`.
merely setup new pointers.
Backward compatibility: this method used to require in some modulestores that dest_course_id
pointed to an empty but already created course. Implementers should support this or should
enable creating the course from scratch.
Raises:
This key may represent a course that doesn't exist in this modulestore.
ItemNotFoundError: if the source course doesn't exist (or any of its xblocks aren't found)
DuplicateItemError: if the destination course already exists (with content in some cases)
"""
"""
pass
pass
@abstractmethod
@abstractmethod
def
delete_course
(
self
,
course_key
,
user_id
,
**
kwargs
):
def
get_courses
(
self
,
**
kwargs
):
"""
'''
Deletes the course. It may be a soft or hard delete. It may or may not remove the xblock definitions
Returns a list containing the top level XModuleDescriptors of the courses
depending on the persistence layer and how tightly bound the xblocks are to the course.
in this modulestore.
'''
Args:
course_key (CourseKey): which course to delete
user_id: id of the user deleting the course
"""
pass
pass
@abstractmethod
@abstractmethod
def
_drop_database
(
self
):
def
get_course
(
self
,
course_id
,
depth
=
0
,
**
kwargs
):
"""
'''
A destructive operation to drop the underlying database and close all connections
.
Look for a specific course by its id (:class:`CourseKey`)
.
Intended to be used by test code for cleanup
.
Returns the course descriptor, or None if not found
.
"""
'''
pass
pass
@abstractmethod
def
has_course
(
self
,
course_id
,
ignore_case
=
False
,
**
kwargs
):
'''
Look for a specific course id. Returns whether it exists.
Args:
course_id (CourseKey):
ignore_case (boolean): some modulestores are case-insensitive. Use this flag
to search for whether a potentially conflicting course exists in that case.
'''
pass
class
ModuleStoreReadBase
(
BulkOperationsMixin
,
ModuleStoreRead
):
@abstractmethod
'''
def
get_parent_location
(
self
,
location
,
**
kwargs
):
Implement interface functionality that can be shared.
'''
# pylint: disable=W0613
def
__init__
(
self
,
contentstore
=
None
,
doc_store_config
=
None
,
# ignore if passed up
metadata_inheritance_cache_subsystem
=
None
,
request_cache
=
None
,
xblock_mixins
=
(),
xblock_select
=
None
,
# temporary parms to enable backward compatibility. remove once all envs migrated
db
=
None
,
collection
=
None
,
host
=
None
,
port
=
None
,
tz_aware
=
True
,
user
=
None
,
password
=
None
,
# allow lower level init args to pass harmlessly
**
kwargs
):
'''
'''
Set up the error-tracking logic.
Find the location that is the parent of this location in this
course. Needed for path_to_location().
'''
'''
super
(
ModuleStoreReadBase
,
self
)
.
__init__
(
**
kwargs
)
pass
self
.
_course_errors
=
defaultdict
(
make_error_tracker
)
# location -> ErrorLog
# TODO move the inheritance_cache_subsystem to classes which use it
self
.
metadata_inheritance_cache_subsystem
=
metadata_inheritance_cache_subsystem
self
.
request_cache
=
request_cache
self
.
xblock_mixins
=
xblock_mixins
self
.
xblock_select
=
xblock_select
self
.
contentstore
=
contentstore
def
get_course_errors
(
self
,
course_key
):
@abstractmethod
def
get_orphans
(
self
,
course_key
,
**
kwargs
):
"""
"""
Return list of errors for this :class:`.CourseKey`, if any. Raise the same
Get all of the xblocks in the given course which have no parents and are not of types which are
errors as get_item if course_key isn't present.
usually orphaned. NOTE: may include xblocks which still have references via xblocks which don't
use children to point to their dependents.
"""
"""
# check that item is present and raise the promised exceptions if needed
pass
# TODO (vshnayder): post-launch, make errors properties of items
# self.get_item(location)
assert
(
isinstance
(
course_key
,
CourseKey
))
return
self
.
_course_errors
[
course_key
]
.
errors
@abstractmethod
def
get_errored_courses
(
self
):
def
get_errored_courses
(
self
):
"""
"""
Returns an empty dict.
Return a dictionary of course_dir -> [(msg, exception_str)], for each
course_dir where course loading failed.
It is up to subclasses to extend this method if the concept
of errored courses makes sense for their implementation.
"""
"""
return
{}
pass
def
get_course
(
self
,
course_id
,
depth
=
0
,
**
kwargs
):
@abstractmethod
def
get_modulestore_type
(
self
,
course_id
):
"""
"""
See ModuleStoreRead.get_course
Returns a type which identifies which modulestore is servicing the given
course_id. The return can be either "xml" (for XML based courses) or "mongo" for MongoDB backed courses
Default impl--linear search through course list
"""
"""
assert
(
isinstance
(
course_id
,
CourseKey
))
pass
for
course
in
self
.
get_courses
(
**
kwargs
):
if
course
.
id
==
course_id
:
return
course
return
None
def
has_course
(
self
,
course_id
,
ignore_case
=
False
,
**
kwargs
):
@abstractmethod
def
get_courses_for_wiki
(
self
,
wiki_slug
,
**
kwargs
):
"""
"""
Returns the course_id of the course if it was found, else None
Return the list of courses which use this wiki_slug
Args:
:param wiki_slug: the course wiki root slug
course_id (CourseKey):
:return: list of course keys
ignore_case (boolean): some modulestores are case-insensitive. Use this flag
to search for whether a potentially conflicting course exists in that case.
"""
"""
# linear search through list
pass
assert
(
isinstance
(
course_id
,
CourseKey
))
if
ignore_case
:
return
next
(
(
c
.
id
for
c
in
self
.
get_courses
()
if
c
.
id
.
org
.
lower
()
==
course_id
.
org
.
lower
()
and
c
.
id
.
course
.
lower
()
==
course_id
.
course
.
lower
()
and
c
.
id
.
run
.
lower
()
==
course_id
.
run
.
lower
()
),
None
)
else
:
return
next
(
(
c
.
id
for
c
in
self
.
get_courses
()
if
c
.
id
==
course_id
),
None
)
@abstractmethod
def
has_published_version
(
self
,
xblock
):
def
has_published_version
(
self
,
xblock
):
"""
"""
Returns True since this is a read-only store.
Returns true if this xblock exists in the published course regardless of whether it's up to date
"""
return
True
def
heartbeat
(
self
):
"""
Is this modulestore ready?
"""
"""
# default is to say yes by not raising an exception
pass
return
{
'default_impl'
:
True
}
@abstractmethod
def
close_connections
(
self
):
def
close_connections
(
self
):
"""
"""
Closes any open connections to the underlying databases
Closes any open connections to the underlying databases
"""
"""
if
self
.
contentstore
:
pass
self
.
contentstore
.
close_connections
()
super
(
ModuleStoreReadBase
,
self
)
.
close_connections
()
@contextmanager
@contextmanager
def
default_store
(
self
,
store_type
):
def
bulk_operations
(
self
,
course_id
):
"""
"""
A context manager for
temporarily changing the default store
A context manager for
notifying the store of bulk operations. This affects only the current thread.
"""
"""
if
self
.
get_modulestore_type
(
None
)
!=
store_type
:
raise
ValueError
(
u"Cannot set default store to type {}"
.
format
(
store_type
))
yield
yield
@staticmethod
def
ensure_indexes
(
self
):
def
memoize_request_cache
(
func
):
"""
Memoize a function call results on the request_cache if there's one. Creates the cache key by
joining the unicode of all the args with &; so, if your arg may use the default &, it may
have false hits
"""
@functools.wraps
(
func
)
def
wrapper
(
self
,
*
args
,
**
kwargs
):
"""
Wraps a method to memoize results.
"""
if
self
.
request_cache
:
cache_key
=
'&'
.
join
([
hashvalue
(
arg
)
for
arg
in
args
])
if
cache_key
in
self
.
request_cache
.
data
.
setdefault
(
func
.
__name__
,
{}):
return
self
.
request_cache
.
data
[
func
.
__name__
][
cache_key
]
result
=
func
(
self
,
*
args
,
**
kwargs
)
self
.
request_cache
.
data
[
func
.
__name__
][
cache_key
]
=
result
return
result
else
:
return
func
(
self
,
*
args
,
**
kwargs
)
return
wrapper
def
hashvalue
(
arg
):
"""
If arg is an xblock, use its location. otherwise just turn it into a string
"""
if
isinstance
(
arg
,
XBlock
):
return
unicode
(
arg
.
location
)
else
:
return
unicode
(
arg
)
class
ModuleStoreWriteBase
(
ModuleStoreReadBase
,
ModuleStoreWrite
):
'''
Implement interface functionality that can be shared.
'''
def
__init__
(
self
,
contentstore
,
**
kwargs
):
super
(
ModuleStoreWriteBase
,
self
)
.
__init__
(
contentstore
=
contentstore
,
**
kwargs
)
# TODO: Don't have a runtime just to generate the appropriate mixin classes (cpennington)
# This is only used by partition_fields_by_scope, which is only needed because
# the split mongo store is used for item creation as well as item persistence
self
.
mixologist
=
Mixologist
(
self
.
xblock_mixins
)
def
partition_fields_by_scope
(
self
,
category
,
fields
):
"""
"""
Return dictionary of {scope: {field1: val, ..}..} for the fields of this potential xblock
Ensure that all appropriate indexes are created that are needed by this modulestore, or raise
an exception if unable to.
:param category: the xblock category
This method is intended for use by tests and administrative commands, and not
:param fields: the dictionary of {fieldname: value}
to be run during server startup.
"""
"""
result
=
collections
.
defaultdict
(
dict
)
pass
if
fields
is
None
:
return
result
cls
=
self
.
mixologist
.
mix
(
XBlock
.
load_class
(
category
,
select
=
prefer_xmodules
))
for
field_name
,
value
in
fields
.
iteritems
():
field
=
getattr
(
cls
,
field_name
)
result
[
field
.
scope
][
field_name
]
=
value
return
result
def
create_course
(
self
,
org
,
course
,
run
,
user_id
,
fields
=
None
,
runtime
=
None
,
**
kwargs
):
"""
Creates any necessary other things for the course as a side effect and doesn't return
anything useful. The real subclass should call this before it returns the course.
"""
# clone a default 'about' overview module as well
about_location
=
self
.
make_course_key
(
org
,
course
,
run
)
.
make_usage_key
(
'about'
,
'overview'
)
about_descriptor
=
XBlock
.
load_class
(
'about'
)
# pylint: disable=abstract-method
overview_template
=
about_descriptor
.
get_template
(
'overview.yaml'
)
class
ModuleStoreWrite
(
ModuleStoreRead
,
ModuleStoreAssetWriteInterface
):
self
.
create_item
(
"""
user_id
,
An abstract interface for a database backend that stores XModuleDescriptor
about_location
.
course_key
,
instances and extends both read and write functionality
about_location
.
block_type
,
"""
block_id
=
about_location
.
block_id
,
definition_data
=
{
'data'
:
overview_template
.
get
(
'data'
)},
metadata
=
overview_template
.
get
(
'metadata'
),
runtime
=
runtime
,
continue_version
=
True
,
)
def
clone_course
(
self
,
source_course_id
,
dest_course_id
,
user_id
,
fields
=
None
,
**
kwargs
):
__metaclass__
=
ABCMeta
@abstractmethod
def
update_item
(
self
,
xblock
,
user_id
,
allow_not_found
=
False
,
force
=
False
,
**
kwargs
):
"""
"""
This base method just copies the assets. The lower level impls must do the actual cloning of
Update the given xblock's persisted repr. Pass the user's unique id which the persistent store
content.
should save with the update if it has that ability.
:param allow_not_found: whether this method should raise an exception if the given xblock
has not been persisted before.
:param force: fork the structure and don't update the course draftVersion if there's a version
conflict (only applicable to version tracking and conflict detecting persistence stores)
:raises VersionConflictError: if org, course, run, and version_guid given and the current
version head != version_guid and force is not True. (only applicable to version tracking stores)
"""
"""
# copy the assets
pass
if
self
.
contentstore
:
self
.
contentstore
.
copy_all_course_assets
(
source_course_id
,
dest_course_id
)
return
dest_course_id
def
delete_course
(
self
,
course_key
,
user_id
,
**
kwargs
):
@abstractmethod
def
delete_item
(
self
,
location
,
user_id
,
**
kwargs
):
"""
"""
This base method just deletes the assets. The lower level impls must do the actual deleting of
Delete an item and its subtree from persistence. Remove the item from any parents (Note, does not
content.
affect parents from other branches or logical branches; thus, in old mongo, deleting something
whose parent cannot be draft, deletes it from both but deleting a component under a draft vertical
only deletes it from the draft.
Pass the user's unique id which the persistent store
should save with the update if it has that ability.
:param force: fork the structure and don't update the course draftVersion if there's a version
conflict (only applicable to version tracking and conflict detecting persistence stores)
:raises VersionConflictError: if org, course, run, and version_guid given and the current
version head != version_guid and force is not True. (only applicable to version tracking stores)
"""
"""
# delete the assets
pass
if
self
.
contentstore
:
self
.
contentstore
.
delete_all_course_assets
(
course_key
)
super
(
ModuleStoreWriteBase
,
self
)
.
delete_course
(
course_key
,
user_id
)
def
_drop_database
(
self
):
@abstractmethod
def
create_course
(
self
,
org
,
course
,
run
,
user_id
,
fields
=
None
,
**
kwargs
):
"""
"""
A destructive operation to drop the underlying database and close all connections.
Creates and returns the course.
Intended to be used by test code for cleanup.
Args:
org (str): the organization that owns the course
course (str): the name of the course
run (str): the name of the run
user_id: id of the user creating the course
fields (dict): Fields to set on the course at initialization
kwargs: Any optional arguments understood by a subset of modulestores to customize instantiation
Returns: a CourseDescriptor
"""
"""
if
self
.
contentstore
:
pass
self
.
contentstore
.
_drop_database
()
# pylint: disable=protected-access
super
(
ModuleStoreWriteBase
,
self
)
.
_drop_database
()
# pylint: disable=protected-access
def
create_child
(
self
,
user_id
,
parent_usage_key
,
block_type
,
block_id
=
None
,
fields
=
None
,
**
kwargs
):
@abstractmethod
def
create_item
(
self
,
user_id
,
course_key
,
block_type
,
block_id
=
None
,
fields
=
None
,
**
kwargs
):
"""
"""
Creates and saves a new
xblock that as a child of the specified block
Creates and saves a new
item in a course.
Returns the newly created item.
Returns the newly created item.
Args:
Args:
user_id: ID of the user creating and saving the xmodule
user_id: ID of the user creating and saving the xmodule
parent_usage_key: a :class:`~opaque_key.edx.UsageKey` identifing th
e
course_key: A :class:`~opaque_keys.edx.CourseKey` identifying which course to creat
e
block that this item should be parented under
this item in
block_type: The type of block to create
block_type: The type of block to create
block_id: a unique identifier for the new item. If not supplied,
block_id: a unique identifier for the new item. If not supplied,
a new identifier will be generated
a new identifier will be generated
fields (dict): A dictionary specifying initial values for some or all fields
fields (dict): A dictionary specifying initial values for some or all fields
in the newly created block
in the newly created block
"""
"""
item
=
self
.
create_item
(
user_id
,
parent_usage_key
.
course_key
,
block_type
,
block_id
=
block_id
,
fields
=
fields
,
**
kwargs
)
pass
parent
=
self
.
get_item
(
parent_usage_key
)
parent
.
children
.
append
(
item
.
location
)
self
.
update_item
(
parent
,
user_id
)
def
_find_course_assets
(
self
,
course_key
):
@abstractmethod
def
clone_course
(
self
,
source_course_id
,
dest_course_id
,
user_id
,
fields
=
None
):
"""
"""
Base method to override.
Sets up source_course_id to point a course with the same content as the desct_course_id. This
operation may be cheap or expensive. It may have to copy all assets and all xblock content or
merely setup new pointers.
Backward compatibility: this method used to require in some modulestores that dest_course_id
pointed to an empty but already created course. Implementers should support this or should
enable creating the course from scratch.
Raises:
ItemNotFoundError: if the source course doesn't exist (or any of its xblocks aren't found)
DuplicateItemError: if the destination course already exists (with content in some cases)
"""
"""
raise
NotImplementedError
()
pass
def
_find_course_asset
(
self
,
course_key
,
filename
,
get_thumbnail
=
False
):
@abstractmethod
def
delete_course
(
self
,
course_key
,
user_id
,
**
kwargs
):
"""
"""
Internal; finds or creates course asset info -and- finds existing asset (or thumbnail) metadata.
Deletes the course. It may be a soft or hard delete. It may or may not remove the xblock definitions
depending on the persistence layer and how tightly bound the xblocks are to the course.
Arguments:
Args:
course_key (CourseKey): course identifier
course_key (CourseKey): which course to delete
filename (str): filename of the asset or thumbnail
user_id: id of the user deleting the course
get_thumbnail (bool): True gets thumbnail data, False gets asset data
"""
pass
Returns:
@abstractmethod
Asset info for the course, index of asset/thumbnail in list (None if asset/thumbnail does not exist)
def
_drop_database
(
self
):
"""
"""
course_assets
=
self
.
_find_course_assets
(
course_key
)
A destructive operation to drop the underlying database and close all connections.
if
course_assets
is
None
:
Intended to be used by test code for cleanup.
return
None
,
None
"""
pass
if
get_thumbnail
:
all_assets
=
course_assets
[
'thumbnails'
]
else
:
all_assets
=
course_assets
[
'assets'
]
# See if this asset already exists by checking the external_filename.
# pylint: disable=abstract-method
# Studio doesn't currently support using multiple course assets with the same filename.
class
ModuleStoreReadBase
(
BulkOperationsMixin
,
ModuleStoreRead
):
# So use the filename as the unique identifier.
'''
for
idx
,
asset
in
enumerate
(
all_assets
):
Implement interface functionality that can be shared.
if
asset
[
'filename'
]
==
filename
:
'''
return
course_assets
,
idx
return
course_assets
,
None
# pylint: disable=invalid-name
def
__init__
(
self
,
contentstore
=
None
,
doc_store_config
=
None
,
# ignore if passed up
metadata_inheritance_cache_subsystem
=
None
,
request_cache
=
None
,
xblock_mixins
=
(),
xblock_select
=
None
,
# temporary parms to enable backward compatibility. remove once all envs migrated
db
=
None
,
collection
=
None
,
host
=
None
,
port
=
None
,
tz_aware
=
True
,
user
=
None
,
password
=
None
,
# allow lower level init args to pass harmlessly
**
kwargs
):
'''
Set up the error-tracking logic.
'''
super
(
ModuleStoreReadBase
,
self
)
.
__init__
(
**
kwargs
)
self
.
_course_errors
=
defaultdict
(
make_error_tracker
)
# location -> ErrorLog
# pylint: disable=fixme
# TODO move the inheritance_cache_subsystem to classes which use it
self
.
metadata_inheritance_cache_subsystem
=
metadata_inheritance_cache_subsystem
self
.
request_cache
=
request_cache
self
.
xblock_mixins
=
xblock_mixins
self
.
xblock_select
=
xblock_select
self
.
contentstore
=
contentstore
def
_save_asset_info
(
self
,
course_key
,
asset_metadata
,
user_id
,
thumbnail
=
False
):
def
get_course_errors
(
self
,
course_key
):
"""
"""
Base method to over-ride in modulestore.
Return list of errors for this :class:`.CourseKey`, if any. Raise the same
errors as get_item if course_key isn't present.
"""
"""
raise
NotImplementedError
()
# check that item is present and raise the promised exceptions if needed
# pylint: disable=fixme
# TODO (vshnayder): post-launch, make errors properties of items
# self.get_item(location)
assert
(
isinstance
(
course_key
,
CourseKey
))
return
self
.
_course_errors
[
course_key
]
.
errors
@contract
(
course_key
=
'CourseKey'
,
asset_metadata
=
'AssetMetadata'
)
def
get_errored_courses
(
self
):
def
save_asset_metadata
(
self
,
course_key
,
asset_metadata
,
user_id
):
"""
"""
Saves the asset metadata for a particular course's asset.
Returns an empty dict.
Arguments:
course_key (CourseKey): course identifier
asset_metadata (AssetMetadata): data about the course asset data
Returns:
It is up to subclasses to extend this method if the concept
True if metadata save was successful, else False
of errored courses makes sense for their implementation.
"""
"""
return
self
.
_save_asset_info
(
course_key
,
asset_metadata
,
user_id
,
thumbnail
=
False
)
return
{}
@contract
(
course_key
=
'CourseKey'
,
asset_thumbnail_metadata
=
'AssetThumbnailMetadata'
)
def
get_course
(
self
,
course_id
,
depth
=
0
,
**
kwargs
):
def
save_asset_thumbnail_metadata
(
self
,
course_key
,
asset_thumbnail_metadata
,
user_id
):
"""
"""
Saves the asset thumbnail metadata for a particular course asset's thumbnail.
See ModuleStoreRead.get_course
Arguments:
course_key (CourseKey): course identifier
asset_thumbnail_metadata (AssetThumbnailMetadata): data about the course asset thumbnail
Returns:
Default impl--linear search through course list
True if thumbnail metadata save was successful, else False
"""
"""
return
self
.
_save_asset_info
(
course_key
,
asset_thumbnail_metadata
,
user_id
,
thumbnail
=
True
)
assert
(
isinstance
(
course_id
,
CourseKey
))
for
course
in
self
.
get_courses
(
**
kwargs
):
if
course
.
id
==
course_id
:
return
course
return
None
@contract
(
asset_key
=
'AssetKey'
)
def
has_course
(
self
,
course_id
,
ignore_case
=
False
,
**
kwargs
):
def
_find_asset_info
(
self
,
asset_key
,
thumbnail
=
False
,
**
kwargs
):
"""
"""
Find the info for a particular course asset/thumbnail.
Returns the course_id of the course if it was found, else None
Args:
Arguments:
course_id (CourseKey):
asset_key (AssetKey): key containing original asset filename
ignore_case (boolean): some modulestores are case-insensitive. Use this flag
thumbnail (bool): True if finding thumbnail, False if finding asset metadata
to search for whether a potentially conflicting course exists in that case.
Returns:
asset/thumbnail metadata (AssetMetadata/AssetThumbnailMetadata) -or- None if not found
"""
"""
course_assets
,
asset_idx
=
self
.
_find_course_asset
(
asset_key
.
course_key
,
asset_key
.
path
,
thumbnail
)
# linear search through list
if
asset_idx
is
None
:
assert
(
isinstance
(
course_id
,
CourseKey
))
return
None
if
ignore_case
:
return
next
(
if
thumbnail
:
(
info
=
'thumbnails'
c
.
id
for
c
in
self
.
get_courses
()
mdata
=
AssetThumbnailMetadata
(
asset_key
,
asset_key
.
path
,
**
kwargs
)
if
c
.
id
.
org
.
lower
()
==
course_id
.
org
.
lower
()
and
c
.
id
.
course
.
lower
()
==
course_id
.
course
.
lower
()
and
c
.
id
.
run
.
lower
()
==
course_id
.
run
.
lower
()
),
None
)
else
:
else
:
info
=
'assets'
return
next
(
mdata
=
AssetMetadata
(
asset_key
,
asset_key
.
path
,
**
kwargs
)
(
c
.
id
for
c
in
self
.
get_courses
()
if
c
.
id
==
course_id
),
all_assets
=
course_assets
[
info
]
None
mdata
.
from_mongo
(
all_assets
[
asset_idx
])
)
return
mdata
@contract
(
asset_key
=
'AssetKey'
)
def
find_asset_metadata
(
self
,
asset_key
,
**
kwargs
):
"""
Find the metadata for a particular course asset.
Arguments:
asset_key (AssetKey): key containing original asset filename
Returns:
def
has_published_version
(
self
,
xblock
):
asset metadata (AssetMetadata) -or- None if not found
"""
"""
return
self
.
_find_asset_info
(
asset_key
,
thumbnail
=
False
,
**
kwargs
)
Returns True since this is a read-only store.
@contract
(
asset_key
=
'AssetKey'
)
def
find_asset_thumbnail_metadata
(
self
,
asset_key
,
**
kwargs
):
"""
"""
Find the metadata for a particular course asset.
return
True
Arguments:
asset_key (AssetKey): key containing original asset filename
Returns:
def
heartbeat
(
self
):
asset metadata (AssetMetadata) -or- None if not found
"""
"""
return
self
.
_find_asset_info
(
asset_key
,
thumbnail
=
True
,
**
kwargs
)
Is this modulestore ready?
@contract
(
course_key
=
'CourseKey'
,
start
=
'int | None'
,
maxresults
=
'int | None'
,
sort
=
'list | None'
,
get_thumbnails
=
'bool'
)
def
_get_all_asset_metadata
(
self
,
course_key
,
start
=
0
,
maxresults
=-
1
,
sort
=
None
,
get_thumbnails
=
False
,
**
kwargs
):
"""
"""
Returns a list of static asset (or thumbnail) metadata for a course.
# default is to say yes by not raising an exception
return
{
'default_impl'
:
True
}
Args:
def
close_connections
(
self
):
course_key (CourseKey): course identifier
"""
start (int): optional - start at this asset number
Closes any open connections to the underlying databases
maxresults (int): optional - return at most this many, -1 means no limit
"""
sort (array): optional - None means no sort
if
self
.
contentstore
:
(sort_by (str), sort_order (str))
self
.
contentstore
.
close_connections
()
sort_by - one of 'uploadDate' or 'displayname'
super
(
ModuleStoreReadBase
,
self
)
.
close_connections
()
sort_order - one of 'ascending' or 'descending'
get_thumbnails (bool): True if getting thumbnail metadata, else getting asset metadata
Returns:
@contextmanager
List of AssetMetadata or AssetThumbnailMetadata objects.
def
default_store
(
self
,
store_type
):
"""
"""
course_assets
=
self
.
_find_course_assets
(
course_key
)
A context manager for temporarily changing the default store
if
course_assets
is
None
:
"""
# If no course assets are found, return None instead of empty list
if
self
.
get_modulestore_type
(
None
)
!=
store_type
:
# to distinguish zero assets from "not able to retrieve assets".
raise
ValueError
(
u"Cannot set default store to type {}"
.
format
(
store_type
))
return
None
yield
if
get_thumbnails
:
@staticmethod
all_assets
=
course_assets
.
get
(
'thumbnails'
,
[])
def
memoize_request_cache
(
func
):
else
:
"""
all_assets
=
course_assets
.
get
(
'assets'
,
[])
Memoize a function call results on the request_cache if there's one. Creates the cache key by
joining the unicode of all the args with &; so, if your arg may use the default &, it may
have false hits
"""
@functools.wraps
(
func
)
def
wrapper
(
self
,
*
args
,
**
kwargs
):
"""
Wraps a method to memoize results.
"""
if
self
.
request_cache
:
cache_key
=
'&'
.
join
([
hashvalue
(
arg
)
for
arg
in
args
])
if
cache_key
in
self
.
request_cache
.
data
.
setdefault
(
func
.
__name__
,
{}):
return
self
.
request_cache
.
data
[
func
.
__name__
][
cache_key
]
# DO_NEXT: Add start/maxresults/sort functionality as part of https://openedx.atlassian.net/browse/PLAT-74
result
=
func
(
self
,
*
args
,
**
kwargs
)
if
start
and
maxresults
and
sort
:
pass
ret_assets
=
[]
self
.
request_cache
.
data
[
func
.
__name__
][
cache_key
]
=
result
for
asset
in
all_assets
:
return
result
if
get_thumbnails
:
thumb
=
AssetThumbnailMetadata
(
course_key
.
make_asset_key
(
'thumbnail'
,
asset
[
'filename'
]),
internal_name
=
asset
[
'filename'
],
**
kwargs
)
ret_assets
.
append
(
thumb
)
else
:
else
:
asset
=
AssetMetadata
(
return
func
(
self
,
*
args
,
**
kwargs
)
course_key
.
make_asset_key
(
'asset'
,
asset
[
'filename'
]),
return
wrapper
basename
=
asset
[
'filename'
],
edited_on
=
asset
[
'edit_info'
][
'edited_on'
],
contenttype
=
asset
[
'contenttype'
],
md5
=
str
(
asset
[
'md5'
]),
**
kwargs
)
ret_assets
.
append
(
asset
)
return
ret_assets
@contract
(
course_key
=
'CourseKey'
,
start
=
'int | None'
,
maxresults
=
'int | None'
,
sort
=
'list | None'
)
def
get_all_asset_metadata
(
self
,
course_key
,
start
=
0
,
maxresults
=-
1
,
sort
=
None
,
**
kwargs
):
"""
Returns a list of static assets for a course.
By default all assets are returned, but start and maxresults can be provided to limit the query.
Args:
course_key (CourseKey): course identifier
start (int): optional - start at this asset number
maxresults (int): optional - return at most this many, -1 means no limit
sort (array): optional - None means no sort
(sort_by (str), sort_order (str))
sort_by - one of 'uploadDate' or 'displayname'
sort_order - one of 'ascending' or 'descending'
Returns:
def
hashvalue
(
arg
):
List of AssetMetadata objects.
"""
"""
If arg is an xblock, use its location. otherwise just turn it into a string
return
self
.
_get_all_asset_metadata
(
course_key
,
start
,
maxresults
,
sort
,
get_thumbnails
=
False
,
**
kwargs
)
"""
if
isinstance
(
arg
,
XBlock
):
return
unicode
(
arg
.
location
)
else
:
return
unicode
(
arg
)
@contract
(
course_key
=
'CourseKey'
)
def
get_all_asset_thumbnail_metadata
(
self
,
course_key
,
**
kwargs
):
"""
Returns a list of thumbnails for all course assets.
Args:
# pylint: disable=abstract-method
course_key (CourseKey): course identifier
class
ModuleStoreWriteBase
(
ModuleStoreReadBase
,
ModuleStoreWrite
):
'''
Implement interface functionality that can be shared.
'''
def
__init__
(
self
,
contentstore
,
**
kwargs
):
super
(
ModuleStoreWriteBase
,
self
)
.
__init__
(
contentstore
=
contentstore
,
**
kwargs
)
self
.
mixologist
=
Mixologist
(
self
.
xblock_mixins
)
Returns:
def
partition_fields_by_scope
(
self
,
category
,
fields
):
List of AssetThumbnailMetadata objects.
"""
"""
return
self
.
_get_all_asset_metadata
(
course_key
,
get_thumbnails
=
True
,
**
kwargs
)
Return dictionary of {scope: {field1: val, ..}..} for the fields of this potential xblock
def
set_asset_metadata_attrs
(
self
,
asset_key
,
attrs
,
user_id
):
:param category: the xblock category
"""
:param fields: the dictionary of {fieldname: value}
Base method to over-ride in modulestore.
"""
"""
raise
NotImplementedError
()
result
=
collections
.
defaultdict
(
dict
)
if
fields
is
None
:
return
result
cls
=
self
.
mixologist
.
mix
(
XBlock
.
load_class
(
category
,
select
=
prefer_xmodules
))
for
field_name
,
value
in
fields
.
iteritems
():
field
=
getattr
(
cls
,
field_name
)
result
[
field
.
scope
][
field_name
]
=
value
return
result
def
_delete_asset_data
(
self
,
asset_key
,
user_id
,
thumbnail
=
False
):
def
create_course
(
self
,
org
,
course
,
run
,
user_id
,
fields
=
None
,
runtime
=
None
,
**
kwargs
):
"""
Base method to over-ride in modulestore.
"""
"""
raise
NotImplementedError
()
Creates any necessary other things for the course as a side effect and doesn't return
anything useful. The real subclass should call this before it returns the course.
@contract
(
asset_key
=
'AssetKey'
,
attr
=
str
)
def
set_asset_metadata_attr
(
self
,
asset_key
,
attr
,
value
,
user_id
):
"""
"""
Add/set the given attr on the asset at the given location. Value can be any type which pymongo accepts.
# clone a default 'about' overview module as well
about_location
=
self
.
make_course_key
(
org
,
course
,
run
)
.
make_usage_key
(
'about'
,
'overview'
)
Arguments:
about_descriptor
=
XBlock
.
load_class
(
'about'
)
asset_key (AssetKey): asset identifier
overview_template
=
about_descriptor
.
get_template
(
'overview.yaml'
)
attr (str): which attribute to set
self
.
create_item
(
value: the value to set it to (any type pymongo accepts such as datetime, number, string)
user_id
,
about_location
.
course_key
,
about_location
.
block_type
,
block_id
=
about_location
.
block_id
,
definition_data
=
{
'data'
:
overview_template
.
get
(
'data'
)},
metadata
=
overview_template
.
get
(
'metadata'
),
runtime
=
runtime
,
continue_version
=
True
,
)
Raises:
def
clone_course
(
self
,
source_course_id
,
dest_course_id
,
user_id
,
fields
=
None
,
**
kwargs
):
ItemNotFoundError if no such item exists
AttributeError is attr is one of the build in attrs.
"""
"""
return
self
.
set_asset_metadata_attrs
(
asset_key
,
{
attr
:
value
},
user_id
)
This base method just copies the assets. The lower level impls must do the actual cloning of
content.
@contract
(
asset_key
=
'AssetKey'
)
def
delete_asset_metadata
(
self
,
asset_key
,
user_id
):
"""
"""
Deletes a single asset's metadata.
# copy the assets
if
self
.
contentstore
:
Arguments:
self
.
contentstore
.
copy_all_course_assets
(
source_course_id
,
dest_course_id
)
asset_key (AssetKey): locator containing original asset filename
return
dest_course_id
Returns:
def
delete_course
(
self
,
course_key
,
user_id
,
**
kwargs
):
Number of asset metadata entries deleted (0 or 1)
"""
"""
return
self
.
_delete_asset_data
(
asset_key
,
user_id
,
thumbnail
=
False
)
This base method just deletes the assets. The lower level impls must do the actual deleting of
content.
@contract
(
asset_key
=
'AssetKey'
)
def
delete_asset_thumbnail_metadata
(
self
,
asset_key
,
user_id
):
"""
"""
Deletes a single asset's metadata.
# delete the assets
if
self
.
contentstore
:
Arguments:
self
.
contentstore
.
delete_all_course_assets
(
course_key
)
asset_key (AssetKey): locator containing original asset filename
super
(
ModuleStoreWriteBase
,
self
)
.
delete_course
(
course_key
,
user_id
)
Returns:
def
_drop_database
(
self
):
Number of asset metadata entries deleted (0 or 1)
"""
"""
return
self
.
_delete_asset_data
(
asset_key
,
user_id
,
thumbnail
=
True
)
A destructive operation to drop the underlying database and close all connections.
Intended to be used by test code for cleanup.
"""
if
self
.
contentstore
:
self
.
contentstore
.
_drop_database
()
# pylint: disable=protected-access
super
(
ModuleStoreWriteBase
,
self
)
.
_drop_database
()
# pylint: disable=protected-access
@contract
(
source_course_key
=
'CourseKey'
,
dest_course_key
=
'CourseKey'
)
def
create_child
(
self
,
user_id
,
parent_usage_key
,
block_type
,
block_id
=
None
,
fields
=
None
,
**
kwargs
):
def
copy_all_asset_metadata
(
self
,
source_course_key
,
dest_course_key
,
user_id
):
"""
"""
C
opy all the course assets from source_course_key to dest_course_key.
C
reates and saves a new xblock that as a child of the specified block
Arguments:
Returns the newly created item.
source_course_key (CourseKey): identifier of course to copy from
dest_course_key (CourseKey): identifier of course to copy to
Args:
user_id: ID of the user creating and saving the xmodule
parent_usage_key: a :class:`~opaque_key.edx.UsageKey` identifing the
block that this item should be parented under
block_type: The type of block to create
block_id: a unique identifier for the new item. If not supplied,
a new identifier will be generated
fields (dict): A dictionary specifying initial values for some or all fields
in the newly created block
"""
"""
pass
item
=
self
.
create_item
(
user_id
,
parent_usage_key
.
course_key
,
block_type
,
block_id
=
block_id
,
fields
=
fields
,
**
kwargs
)
parent
=
self
.
get_item
(
parent_usage_key
)
parent
.
children
.
append
(
item
.
location
)
self
.
update_item
(
parent
,
user_id
)
def
only_xmodules
(
identifier
,
entry_points
):
def
only_xmodules
(
identifier
,
entry_points
):
...
...
common/lib/xmodule/xmodule/modulestore/tests/test_assetstore.py
View file @
436b32a6
...
@@ -12,7 +12,7 @@ from xmodule.modulestore import ModuleStoreEnum
...
@@ -12,7 +12,7 @@ from xmodule.modulestore import ModuleStoreEnum
from
xmodule.modulestore.tests.factories
import
CourseFactory
from
xmodule.modulestore.tests.factories
import
CourseFactory
from
xmodule.modulestore.tests.test_cross_modulestore_import_export
import
(
from
xmodule.modulestore.tests.test_cross_modulestore_import_export
import
(
MODULESTORE_SETUPS
,
MongoContentstoreBuilder
,
MODULESTORE_SETUPS
,
MongoContentstoreBuilder
,
XmlModulestoreBuilder
,
MixedModulestoreBuilder
)
)
...
@@ -392,3 +392,21 @@ class TestMongoAssetMetadataStorage(unittest.TestCase):
...
@@ -392,3 +392,21 @@ class TestMongoAssetMetadataStorage(unittest.TestCase):
def
test_copy_all_assets
(
self
):
def
test_copy_all_assets
(
self
):
pass
pass
@ddt.data
(
XmlModulestoreBuilder
(),
MixedModulestoreBuilder
([(
'xml'
,
XmlModulestoreBuilder
())]))
def
test_xml_not_yet_implemented
(
self
,
storebuilder
):
"""
Test coverage which shows that for now xml read operations are not implemented
"""
with
storebuilder
.
build
(
None
)
as
store
:
course_key
=
store
.
make_course_key
(
"org"
,
"course"
,
"run"
)
asset_key
=
course_key
.
make_asset_key
(
'asset'
,
'foo.jpg'
)
for
method
in
[
'_find_asset_info'
,
'find_asset_metadata'
,
'find_asset_thumbnail_metadata'
]:
with
self
.
assertRaises
(
NotImplementedError
):
getattr
(
store
,
method
)(
asset_key
)
with
self
.
assertRaises
(
NotImplementedError
):
# pylint: disable=protected-access
store
.
_find_course_asset
(
course_key
,
asset_key
.
block_id
)
for
method
in
[
'_get_all_asset_metadata'
,
'get_all_asset_metadata'
,
'get_all_asset_thumbnail_metadata'
]:
with
self
.
assertRaises
(
NotImplementedError
):
getattr
(
store
,
method
)(
course_key
)
common/lib/xmodule/xmodule/modulestore/tests/test_cross_modulestore_import_export.py
View file @
436b32a6
...
@@ -17,6 +17,7 @@ import random
...
@@ -17,6 +17,7 @@ import random
from
contextlib
import
contextmanager
,
nested
from
contextlib
import
contextmanager
,
nested
from
shutil
import
rmtree
from
shutil
import
rmtree
from
tempfile
import
mkdtemp
from
tempfile
import
mkdtemp
from
path
import
path
from
xmodule.tests
import
CourseComparisonTest
from
xmodule.tests
import
CourseComparisonTest
...
@@ -30,13 +31,14 @@ from xmodule.modulestore.split_mongo.split_draft import DraftVersioningModuleSto
...
@@ -30,13 +31,14 @@ from xmodule.modulestore.split_mongo.split_draft import DraftVersioningModuleSto
from
xmodule.modulestore.tests.mongo_connection
import
MONGO_PORT_NUM
,
MONGO_HOST
from
xmodule.modulestore.tests.mongo_connection
import
MONGO_PORT_NUM
,
MONGO_HOST
from
xmodule.modulestore.inheritance
import
InheritanceMixin
from
xmodule.modulestore.inheritance
import
InheritanceMixin
from
xmodule.x_module
import
XModuleMixin
from
xmodule.x_module
import
XModuleMixin
from
xmodule.modulestore.xml
import
XMLModuleStore
COMMON_DOCSTORE_CONFIG
=
{
COMMON_DOCSTORE_CONFIG
=
{
'host'
:
MONGO_HOST
,
'host'
:
MONGO_HOST
,
'port'
:
MONGO_PORT_NUM
,
'port'
:
MONGO_PORT_NUM
,
}
}
DATA_DIR
=
path
(
__file__
)
.
dirname
()
.
parent
.
parent
.
parent
.
parent
.
parent
/
"test"
/
"data"
XBLOCK_MIXINS
=
(
InheritanceMixin
,
XModuleMixin
)
XBLOCK_MIXINS
=
(
InheritanceMixin
,
XModuleMixin
)
...
@@ -163,6 +165,30 @@ class VersioningModulestoreBuilder(object):
...
@@ -163,6 +165,30 @@ class VersioningModulestoreBuilder(object):
return
'SplitModulestoreBuilder()'
return
'SplitModulestoreBuilder()'
class
XmlModulestoreBuilder
(
object
):
"""
A builder class for a XMLModuleStore.
"""
# pylint: disable=unused-argument
@contextmanager
def
build
(
self
,
contentstore
=
None
,
course_ids
=
None
):
"""
A contextmanager that returns an isolated xml modulestore
Args:
contentstore: The contentstore that this modulestore should use to store
all of its assets.
"""
modulestore
=
XMLModuleStore
(
DATA_DIR
,
course_ids
=
course_ids
,
default_class
=
'xmodule.hidden_module.HiddenDescriptor'
,
xblock_mixins
=
XBLOCK_MIXINS
,
)
yield
modulestore
class
MixedModulestoreBuilder
(
object
):
class
MixedModulestoreBuilder
(
object
):
"""
"""
A builder class for a MixedModuleStore.
A builder class for a MixedModuleStore.
...
...
common/lib/xmodule/xmodule/modulestore/xml.py
View file @
436b32a6
...
@@ -846,3 +846,52 @@ class XMLModuleStore(ModuleStoreReadBase):
...
@@ -846,3 +846,52 @@ class XMLModuleStore(ModuleStoreReadBase):
if
branch_setting
!=
ModuleStoreEnum
.
Branch
.
published_only
:
if
branch_setting
!=
ModuleStoreEnum
.
Branch
.
published_only
:
raise
ValueError
(
u"Cannot set branch setting to {} on a ReadOnly store"
.
format
(
branch_setting
))
raise
ValueError
(
u"Cannot set branch setting to {} on a ReadOnly store"
.
format
(
branch_setting
))
yield
yield
def
_find_course_asset
(
self
,
course_key
,
filename
,
get_thumbnail
=
False
):
"""
For now this is not implemented, but others should feel free to implement using the asset.json
which export produces.
"""
raise
NotImplementedError
()
def
_find_asset_info
(
self
,
asset_key
,
thumbnail
=
False
,
**
kwargs
):
"""
For now this is not implemented, but others should feel free to implement using the asset.json
which export produces.
"""
raise
NotImplementedError
()
def
find_asset_metadata
(
self
,
asset_key
,
**
kwargs
):
"""
For now this is not implemented, but others should feel free to implement using the asset.json
which export produces.
"""
raise
NotImplementedError
()
def
find_asset_thumbnail_metadata
(
self
,
asset_key
,
**
kwargs
):
"""
For now this is not implemented, but others should feel free to implement using the asset.json
which export produces.
"""
raise
NotImplementedError
()
def
_get_all_asset_metadata
(
self
,
course_key
,
start
=
0
,
maxresults
=-
1
,
sort
=
None
,
get_thumbnails
=
False
,
**
kwargs
):
"""
For now this is not implemented, but others should feel free to implement using the asset.json
which export produces.
"""
raise
NotImplementedError
()
def
get_all_asset_metadata
(
self
,
course_key
,
start
=
0
,
maxresults
=-
1
,
sort
=
None
,
**
kwargs
):
"""
For now this is not implemented, but others should feel free to implement using the asset.json
which export produces.
"""
raise
NotImplementedError
()
def
get_all_asset_thumbnail_metadata
(
self
,
course_key
,
**
kwargs
):
"""
For now this is not implemented, but others should feel free to implement using the asset.json
which export produces.
"""
raise
NotImplementedError
()
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