本文整理汇总了Python中opaque_keys.edx.keys.AssetKey.from_string方法的典型用法代码示例。如果您正苦于以下问题:Python AssetKey.from_string方法的具体用法?Python AssetKey.from_string怎么用?Python AssetKey.from_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类opaque_keys.edx.keys.AssetKey
的用法示例。
在下文中一共展示了AssetKey.from_string方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_location_from_path
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def get_location_from_path(path):
"""
Generate an AssetKey for the given path (old c4x/org/course/asset/name syntax)
"""
try:
return AssetKey.from_string(path)
except InvalidKeyError:
# TODO - re-address this once LMS-11198 is tackled.
if path.startswith('/'):
# try stripping off the leading slash and try again
return AssetKey.from_string(path[1:])
示例2: test_can_delete_signatory
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def test_can_delete_signatory(self, signatory_path):
"""
Delete an existing certificate signatory
"""
self._add_course_certificates(count=2, signatory_count=3, asset_path_format=signatory_path)
certificates = self.course.certificates['certificates']
signatory = certificates[1].get("signatories")[1]
image_asset_location = AssetKey.from_string(signatory['signature_image_path'])
content = contentstore().find(image_asset_location)
self.assertIsNotNone(content)
test_url = '{}/signatories/1'.format(self._url(cid=1))
response = self.client.delete(
test_url,
content_type="application/json",
HTTP_ACCEPT="application/json",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
self.assertEqual(response.status_code, 204)
self.reload_course()
# Verify that certificates are properly updated in the course.
certificates = self.course.certificates['certificates']
self.assertEqual(len(certificates[1].get("signatories")), 2)
# make sure signatory signature image is deleted too
self.assertRaises(NotFoundError, contentstore().find, image_asset_location)
示例3: get_asset_key_from_path
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def get_asset_key_from_path(course_key, path):
"""
Parses a path, extracting an asset key or creating one.
Args:
course_key: key to the course which owns this asset
path: the path to said content
Returns:
AssetKey: the asset key that represents the path
"""
# Clean up the path, removing any static prefix and any leading slash.
if path.startswith('/static/'):
path = path[len('/static/'):]
# Old-style asset keys start with `/`, so don't try and strip it
# in that case.
if not path.startswith('/c4x'):
path = path.lstrip('/')
try:
return AssetKey.from_string(path)
except InvalidKeyError:
# If we couldn't parse the path, just let compute_location figure it out.
# It's most likely a path like /image.png or something.
return StaticContent.compute_location(course_key, path)
示例4: from_xml
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def from_xml(self, node):
"""
Walk the etree XML node and fill in the asset metadata.
The node should be a top-level "asset" element.
"""
for child in node:
qname = etree.QName(child)
tag = qname.localname
if tag in self.ALL_ATTRS:
value = child.text
if tag == 'asset_id':
# Locator.
value = AssetKey.from_string(value)
elif tag == 'locked':
# Boolean.
value = True if value == "true" else False
elif tag in ('created_on', 'edited_on'):
# ISO datetime.
value = dateutil.parser.parse(value)
elif tag in ('created_by', 'edited_by'):
# Integer representing user id.
value = int(value)
elif tag == 'fields':
# Dictionary.
value = json.loads(value)
elif value == 'None':
# None.
value = None
setattr(self, tag, value)
示例5: test_asset_with_special_character
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def test_asset_with_special_character(self, paths):
for path in paths:
asset_locator = AssetKey.from_string(path)
self.assertEquals(
path,
unicode(asset_locator),
)
示例6: _create_fake_images
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def _create_fake_images(self, asset_keys):
"""
Creates fake image files for a list of asset_keys.
"""
for asset_key_string in asset_keys:
asset_key = AssetKey.from_string(asset_key_string)
content = StaticContent(asset_key, "Fake asset", "image/png", "data")
contentstore().save(content)
示例7: test_replace
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def test_replace(self):
asset_key = AssetKey.from_string('/c4x/o/c/asset/path')
self.assertEquals(
'foo',
asset_key.replace(path='foo').path
)
self.assertEquals(
'bar',
asset_key.replace(asset_type='bar').asset_type
)
示例8: get_course_ids_from_link
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def get_course_ids_from_link(link):
"""
Get course Ids from link.
Arguments:
link(str): The link to extract course id from.
Returns:
Course Ids
"""
asset_str = link.partition('asset-v1')[1] + link.partition('asset-v1')[2]
return AssetKey.from_string(asset_str).course_key
示例9: _delete_asset
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def _delete_asset(course_key, asset_key_string):
"""
Internal method used to create asset key from string and
remove asset by calling delete_asset method of assets module.
"""
if asset_key_string:
try:
asset_key = AssetKey.from_string(asset_key_string)
except InvalidKeyError:
# remove first slash in asset path
# otherwise it generates InvalidKeyError in case of split modulestore
if '/' == asset_key_string[0]:
asset_key_string = asset_key_string[1:]
try:
asset_key = AssetKey.from_string(asset_key_string)
except InvalidKeyError:
# Unable to parse the asset key, log and return
LOGGER.info(
"In course %r, unable to parse asset key %r, not attempting to delete signatory.",
course_key,
asset_key_string,
)
return
else:
# Unable to parse the asset key, log and return
LOGGER.info(
"In course %r, unable to parse asset key %r, not attempting to delete signatory.",
course_key,
asset_key_string,
)
return
try:
delete_asset(course_key, asset_key)
# If the asset was not found, it doesn't have to be deleted...
except AssetNotFoundException:
pass
示例10: _delete_asset
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def _delete_asset(course_key, asset_key_string):
"""
Internal method used to create asset key from string and
remove asset by calling delete_asset method of assets module.
"""
if asset_key_string:
# remove first slash in asset path
# otherwise it generates InvalidKeyError in case of split modulestore
if '/' == asset_key_string[0]:
asset_key_string = asset_key_string[1:]
asset_key = AssetKey.from_string(asset_key_string)
try:
delete_asset(course_key, asset_key)
# If the asset was not found, it doesn't have to be deleted...
except AssetNotFoundException:
pass
示例11: test_get_all_content
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def test_get_all_content(self, deprecated):
"""
Test get_all_content_for_course
"""
self.set_up_assets(deprecated)
course1_assets, count = self.contentstore.get_all_content_for_course(self.course1_key)
self.assertEqual(count, len(self.course1_files), course1_assets)
for asset in course1_assets:
parsed = AssetKey.from_string(asset['filename'])
self.assertIn(parsed.block_id, self.course1_files)
course1_assets, __ = self.contentstore.get_all_content_for_course(self.course1_key, 1, 1)
self.assertEqual(len(course1_assets), 1, course1_assets)
fake_course = CourseLocator('test', 'fake', 'non')
course_assets, count = self.contentstore.get_all_content_for_course(fake_course)
self.assertEqual(count, 0)
self.assertEqual(course_assets, [])
示例12: copy_all_course_assets
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def copy_all_course_assets(self, source_course_key, dest_course_key):
"""
See :meth:`.ContentStore.copy_all_course_assets`
This implementation fairly expensively copies all of the data
"""
source_query = query_for_course(source_course_key)
# it'd be great to figure out how to do all of this on the db server and not pull the bits over
for asset in self.fs_files.find(source_query):
asset_key = self.make_id_son(asset)
# don't convert from string until fs access
source_content = self.fs.get(asset_key)
if isinstance(asset_key, basestring):
asset_key = AssetKey.from_string(asset_key)
__, asset_key = self.asset_db_key(asset_key)
asset_key["org"] = dest_course_key.org
asset_key["course"] = dest_course_key.course
if getattr(dest_course_key, "deprecated", False): # remove the run if exists
if "run" in asset_key:
del asset_key["run"]
asset_id = asset_key
else: # add the run, since it's the last field, we're golden
asset_key["run"] = dest_course_key.run
asset_id = unicode(
dest_course_key.make_asset_key(asset_key["category"], asset_key["name"]).for_branch(None)
)
self.fs.put(
source_content.read(),
_id=asset_id,
filename=asset["filename"],
content_type=asset["contentType"],
displayname=asset["displayname"],
content_son=asset_key,
# thumbnail is not technically correct but will be functionally correct as the code
# only looks at the name which is not course relative.
thumbnail_location=asset["thumbnail_location"],
import_path=asset["import_path"],
# getattr b/c caching may mean some pickled instances don't have attr
locked=asset.get("locked", False),
)
示例13: assets_handler
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def assets_handler(request, course_key_string=None, asset_key_string=None):
'''
The restful handler for assets.
It allows retrieval of all the assets (as an HTML page), as well as uploading new assets,
deleting assets, and changing the 'locked' state of an asset.
GET
html: return an html page which will show all course assets. Note that only the asset container
is returned and that the actual assets are filled in with a client-side request.
json: returns a page of assets. The following parameters are supported:
page: the desired page of results (defaults to 0)
page_size: the number of items per page (defaults to 50)
sort: the asset field to sort by (defaults to 'date_added')
direction: the sort direction (defaults to 'descending')
asset_type: the file type to filter items to (defaults to All)
text_search: string to filter results by file name (defaults to '')
POST
json: create (or update?) an asset. The only updating that can be done is changing the lock state.
PUT
json: update the locked state of an asset
DELETE
json: delete an asset
'''
course_key = CourseKey.from_string(course_key_string)
if not has_course_author_access(request.user, course_key):
raise PermissionDenied()
response_format = _get_response_format(request)
if _request_response_format_is_json(request, response_format):
if request.method == 'GET':
return _assets_json(request, course_key)
asset_key = AssetKey.from_string(asset_key_string) if asset_key_string else None
return _update_asset(request, course_key, asset_key)
elif request.method == 'GET': # assume html
return _asset_index(request, course_key)
return HttpResponseNotFound()
示例14: assets_handler
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def assets_handler(request, course_key_string=None, asset_key_string=None):
"""
The restful handler for assets.
It allows retrieval of all the assets (as an HTML page), as well as uploading new assets,
deleting assets, and changing the "locked" state of an asset.
GET
html: return an html page which will show all course assets. Note that only the asset container
is returned and that the actual assets are filled in with a client-side request.
json: returns a page of assets. The following parameters are supported:
page: the desired page of results (defaults to 0)
page_size: the number of items per page (defaults to 50)
sort: the asset field to sort by (defaults to "date_added")
direction: the sort direction (defaults to "descending")
POST
json: create (or update?) an asset. The only updating that can be done is changing the lock state.
PUT
json: update the locked state of an asset
DELETE
json: delete an asset
"""
course_key = CourseKey.from_string(course_key_string)
if not has_course_access(request.user, course_key):
raise PermissionDenied()
response_format = request.REQUEST.get('format', 'html')
if response_format == 'json' or 'application/json' in request.META.get('HTTP_ACCEPT', 'application/json'):
if request.method == 'GET':
return _assets_json(request, course_key)
else:
asset_key = AssetKey.from_string(asset_key_string) if asset_key_string else None
return _update_asset(request, course_key, asset_key)
elif request.method == 'GET': # assume html
return _asset_index(request, course_key)
else:
return HttpResponseNotFound()
示例15: test_asset_with_special_character
# 需要导入模块: from opaque_keys.edx.keys import AssetKey [as 别名]
# 或者: from opaque_keys.edx.keys.AssetKey import from_string [as 别名]
def test_asset_with_special_character(self, path):
asset_locator = AssetKey.from_string(path)
self.assertEqual(
path,
text_type(asset_locator),
)