feat: add static full url to asset response

This commit is contained in:
Kristin Aoki
2022-08-24 09:02:04 -04:00
committed by GitHub
parent a868646e98
commit 36439bb047
6 changed files with 45 additions and 22 deletions

View File

@@ -356,7 +356,8 @@ def _get_assets_in_json_format(assets, course_key):
asset['uploadDate'],
asset['asset_key'],
thumbnail_asset_key,
asset_is_locked
asset_is_locked,
course_key,
)
assets_in_json_format.append(asset_in_json)
@@ -426,7 +427,8 @@ def _upload_asset(request, course_key):
readback.last_modified_at,
content.location,
content.thumbnail_location,
locked
locked,
course_key,
),
'msg': _('Upload completed')
})
@@ -588,21 +590,23 @@ def _delete_thumbnail(thumbnail_location, course_key, asset_key): # lint-amnest
logging.warning('Could not delete thumbnail: %s', thumbnail_location)
def _get_asset_json(display_name, content_type, date, location, thumbnail_location, locked):
def _get_asset_json(display_name, content_type, date, location, thumbnail_location, locked, course_key):
'''
Helper method for formatting the asset information to send to client.
'''
asset_url = StaticContent.serialize_asset_key_with_slash(location)
external_url = urljoin(configuration_helpers.get_value('LMS_ROOT_URL', settings.LMS_ROOT_URL), asset_url)
portable_url = StaticContent.get_static_path_from_location(location)
return {
'display_name': display_name,
'content_type': content_type,
'date_added': get_default_time_display(date),
'url': asset_url,
'external_url': external_url,
'portable_url': StaticContent.get_static_path_from_location(location),
'portable_url': portable_url,
'thumbnail': StaticContent.serialize_asset_key_with_slash(thumbnail_location) if thumbnail_location else None,
'locked': locked,
'static_full_url': StaticContent.get_canonicalized_asset_path(course_key, portable_url, '', []),
# needed for Backbone delete/update.
'id': str(location)
}

View File

@@ -231,7 +231,8 @@ class PaginationTestCase(AssetsTestCase):
"portable_url": "/static/test.jpg",
"thumbnail": None,
"thumbnail_location": thumbnail_location,
"locked": None
"locked": None,
"static_full_url": "/assets/courseware/v1/asset-v1:org+class+run+type@asset+block@my_file_name.jpg"
}
],
1
@@ -419,7 +420,8 @@ class AssetToJsonTestCase(AssetsTestCase):
thumbnail_location = course_key.make_asset_key('thumbnail', 'my_file_name_thumb.jpg')
# pylint: disable=protected-access
output = assets._get_asset_json("my_file", content_type, upload_date, location, thumbnail_location, True)
output = assets._get_asset_json("my_file", content_type, upload_date, location,
thumbnail_location, True, course_key)
self.assertEqual(output["display_name"], "my_file")
self.assertEqual(output["date_added"], "Jun 01, 2013 at 10:30 UTC")
@@ -431,8 +433,9 @@ class AssetToJsonTestCase(AssetsTestCase):
self.assertEqual(output["thumbnail"], "/asset-v1:org+class+run+type@thumbnail+block@my_file_name_thumb.jpg")
self.assertEqual(output["id"], str(location))
self.assertEqual(output['locked'], True)
self.assertEqual(output['static_full_url'], '/asset-v1:org+class+run+type@asset+block@my_file_name.jpg')
output = assets._get_asset_json("name", content_type, upload_date, location, None, False)
output = assets._get_asset_json("name", content_type, upload_date, location, None, False, course_key)
self.assertIsNone(output["thumbnail"])
@@ -454,6 +457,7 @@ class LockAssetTestCase(AssetsTestCase):
def post_asset_update(lock, course):
""" Helper method for posting asset update. """
content_type = 'application/txt'
course_key = CourseLocator('org', 'class', 'run')
upload_date = datetime(2013, 6, 1, 10, 30, tzinfo=UTC)
asset_location = course.id.make_asset_key('asset', 'sample_static.html')
url = reverse_course_url(
@@ -464,7 +468,7 @@ class LockAssetTestCase(AssetsTestCase):
url,
# pylint: disable=protected-access
json.dumps(assets._get_asset_json(
"sample_static.html", content_type, upload_date, asset_location, None, lock)),
"sample_static.html", content_type, upload_date, asset_location, None, lock, course_key)),
"application/json"
)