def _get_asset_json(display_name, content_type, date, location, thumbnail_location, locked): ''' Helper method for formatting the asset information to send to client. ''' # Colaraz Custom: overriding this to get site base web link of the asset(s) asset_url = StaticContent.serialize_asset_key_with_slash(location) external_url = 'https://' + configuration_helpers.get_value( 'LMS_BASE') + asset_url 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), 'thumbnail': StaticContent.serialize_asset_key_with_slash(thumbnail_location) if thumbnail_location else None, 'locked': locked, # needed for Backbone delete/update. 'id': unicode(location) }
def _get_asset_json_for_editorjs(display_name, content_type, date, location, thumbnail_location, locked): ''' Helper method for formatting the asset information to send to client. ''' asset_url = StaticContent.serialize_asset_key_with_slash(location) external_url = settings.LMS_BASE + asset_url return { "success": 1, "file": { 'url': asset_url, 'display_name': display_name, 'content_type': content_type, 'date_added': get_default_time_display(date), 'asset_url': asset_url, 'external_url': external_url, 'portable_url': StaticContent.get_static_path_from_location(location), 'thumbnail': StaticContent.serialize_asset_key_with_slash(thumbnail_location) if thumbnail_location else None, 'locked': locked, 'id': six.text_type(location) } }
def _get_asset_json(display_name, content_type, date, location, thumbnail_location, locked): ''' Helper method for formatting the asset information to send to client. ''' asset_url = StaticContent.serialize_asset_key_with_slash(location) external_url = settings.LMS_BASE + asset_url 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), 'thumbnail': StaticContent.serialize_asset_key_with_slash(thumbnail_location) if thumbnail_location else None, 'locked': locked, # needed for Backbone delete/update. 'id': unicode(location) }
def _get_asset_json(display_name, content_type, date, location, thumbnail_location, locked): """ Helper method for formatting the asset information to send to client. """ asset_url = StaticContent.serialize_asset_key_with_slash(location) # eduNEXT 14.07.2016 lms_base = microsite.get_value_for_org(location.org, 'SITE_NAME', settings.LMS_BASE) external_url = lms_base + asset_url 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), 'thumbnail': StaticContent.serialize_asset_key_with_slash(thumbnail_location) if thumbnail_location else None, 'locked': locked, # Needed for Backbone delete/update. 'id': unicode(location) }
def course_image_url(course): """Try to look up the image url for the course. If it's not found, log an error and return the dead link""" if course.static_asset_path or modulestore().get_modulestore_type( course.id) == ModuleStoreEnum.Type.xml: # If we are a static course with the course_image attribute # set different than the default, return that path so that # courses can use custom course image paths, otherwise just # return the default static path. url = '/static/' + (course.static_asset_path or getattr(course, 'data_dir', '')) if hasattr( course, 'course_image' ) and course.course_image != course.fields['course_image'].default: url += '/' + course.course_image else: url += '/images/course_image.jpg' elif course.course_image == '': # if course_image is empty the url will be blank as location # of the course_image does not exist url = '' else: loc = StaticContent.compute_location(course.id, course.course_image) url = StaticContent.serialize_asset_key_with_slash(loc) return url
def course_image_url(course, image_key='course_image'): """Try to look up the image url for the course. If it's not found, log an error and return the dead link. image_key can be one of the three: 'course_image', 'hero_image', 'thumbnail_image' """ if course.static_asset_path: # If we are a static course with the image_key attribute # set different than the default, return that path so that # courses can use custom course image paths, otherwise just # return the default static path. url = '/static/' + (course.static_asset_path or getattr(course, 'data_dir', '')) if hasattr(course, image_key) and getattr( course, image_key) != course.fields[image_key].default: url += '/' + getattr(course, image_key) else: url += '/images/' + image_key + '.jpg' elif not getattr(course, image_key): # if image_key is empty, use the default image url from settings url = settings.STATIC_URL + settings.DEFAULT_COURSE_ABOUT_IMAGE_URL else: loc = StaticContent.compute_location(course.id, getattr(course, image_key)) url = StaticContent.serialize_asset_key_with_slash(loc) return url
def studio_image_upload(self, request, suffix=''): """ Called when uploading image trough tinymce. """ data = request.POST if not isinstance(data['image'], basestring): upload = data['image'] filename = self._file_storage_name(upload.file.name) content_location = StaticContent.compute_location(self.location.course_key, filename) chunked = upload.file.multiple_chunks() sc_partial = partial(StaticContent, content_location, filename, upload.file.content_type) if chunked: content = sc_partial(upload.file.chunks()) tempfile_path = upload.file.temporary_file_path() else: content = sc_partial(upload.file.read()) tempfile_path = None contentstore().save(content) # readback the saved content - we need the database timestamp readback = contentstore().find(content.location) locked = getattr(content, 'locked', False) # self.background_url = StaticContent.serialize_asset_key_with_slash(content.location) return Response(json_body={'result': 'success', 'url': StaticContent.serialize_asset_key_with_slash(content.location)})
def _get_asset_json(display_name, content_type, date, location, thumbnail_location, locked,name_creater): """ Helper method for formatting the asset information to send to client. """ asset_url = StaticContent.serialize_asset_key_with_slash(location) external_url = 'localhost:8000' + asset_url 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), 'thumbnail': StaticContent.serialize_asset_key_with_slash(thumbnail_location) if thumbnail_location else None, 'locked': locked, 'name_creater':name_creater }
def course_image_url(course): """Returns the image url for the course.""" try: loc = StaticContent.compute_location(course.location.course_key, course.course_image) except InvalidKeyError: return '' path = StaticContent.serialize_asset_key_with_slash(loc) return path
def _get_asset_json(display_name, date, location, thumbnail_location, locked): """ Helper method for formatting the asset information to send to client. """ asset_url = StaticContent.serialize_asset_key_with_slash(location) external_url = settings.LMS_BASE + asset_url return { 'display_name': display_name, 'date_added': get_default_time_display(date), 'url': asset_url, 'external_url': external_url, 'portable_url': StaticContent.get_static_path_from_location(location), 'thumbnail': StaticContent.serialize_asset_key_with_slash(thumbnail_location) if thumbnail_location else None, 'locked': locked, # Needed for Backbone delete/update. 'id': unicode(location) }
def _get_asset_json(display_name, content_type, date, location, thumbnail_location, locked): ''' Helper method for formatting the asset information to send to client. ''' asset_url = StaticContent.serialize_asset_key_with_slash(location) lms_base = microsite.get_value_for_org(location.org, 'SITE_NAME', settings.LMS_BASE) external_url = lms_base + asset_url 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), 'thumbnail': StaticContent.serialize_asset_key_with_slash(thumbnail_location) if thumbnail_location else None, 'locked': locked, # needed for Backbone delete/update. 'id': unicode(location) }
def create_course_image_thumbnail(course, dimensions): """Create a course image thumbnail and return the URL. - dimensions is a tuple of (width, height) """ course_image_asset_key = StaticContent.compute_location(course.id, course.course_image) course_image = AssetManager.find(course_image_asset_key) # a StaticContent obj _content, thumb_loc = contentstore().generate_thumbnail(course_image, dimensions=dimensions) return StaticContent.serialize_asset_key_with_slash(thumb_loc)
def course_image_url(course): """ Return url of course image. Args: course(CourseDescriptor) : The course id to retrieve course image url. Returns: Absolute url of course image. """ loc = StaticContent.compute_location(course.id, course.course_image) url = StaticContent.serialize_asset_key_with_slash(loc) return url
def course_image_url(course): """Try to look up the image url for the course. If it's not found, log an error and return the dead link""" if course.static_asset_path or modulestore().get_modulestore_type(course.id) == ModuleStoreEnum.Type.xml: # If we are a static course with the course_image attribute # set different than the default, return that path so that # courses can use custom course image paths, otherwise just # return the default static path. url = '/static/' + (course.static_asset_path or getattr(course, 'data_dir', '')) if hasattr(course, 'course_image') and course.course_image != course.fields['course_image'].default: url += '/' + course.course_image else: url += '/images/course_image.jpg' else: loc = StaticContent.compute_location(course.id, course.course_image) url = StaticContent.serialize_asset_key_with_slash(loc) return url
def store_jacket_image(course_key, img_path, filename): # set initial values content_name = asset_url = None # if image url is available then proceed if img_path: content_loc = StaticContent.compute_location(course_key, filename) mime_type = mimetypes.types_map['.' + filename.split('.')[-1]] sc_partial = partial(StaticContent, content_loc, filename, mime_type) content = None with open(img_path + filename) as file_obj: file_content = file_obj.read() if file_content: try: content = sc_partial( urllib.urlopen(img_path + filename).read()) except: pass if content: tempfile_path = None (thumbnail_content, thumbnail_location) = contentstore().generate_thumbnail( content, tempfile_path=tempfile_path, ) # delete cached thumbnail even if one couldn't be created this time (else # the old thumbnail will continue to show) del_cached_content(thumbnail_location) # now store thumbnail location only if we could create it if thumbnail_content is not None: content.thumbnail_location = thumbnail_location # then commit the content contentstore().save(content) del_cached_content(content.location) content_name = content.name asset_url = StaticContent.serialize_asset_key_with_slash( content.location) # return content name and asset URL return content_name, asset_url
def course_image_url(course): """Try to look up the image url for the course. If it's not found, log an error and return the dead link""" if course.static_asset_path or modulestore().get_modulestore_type(course.id) == ModuleStoreEnum.Type.xml: # If we are a static course with the course_image attribute # set different than the default, return that path so that # courses can use custom course image paths, otherwise just # return the default static path. url = '/static/' + (course.static_asset_path or getattr(course, 'data_dir', '')) if hasattr(course, 'course_image') and course.course_image != course.fields['course_image'].default: url += '/' + course.course_image else: url += '/images/course_image.jpg' elif not course.course_image: # if course_image is empty, use the default image url from settings url = settings.STATIC_URL + settings.DEFAULT_COURSE_ABOUT_IMAGE_URL else: loc = StaticContent.compute_location(course.id, course.course_image) url = StaticContent.serialize_asset_key_with_slash(loc) return url
def course_image_url(course): """Try to look up the image url for the course. If it's not found, log an error and return the dead link""" if course.static_asset_path: # If we are a static course with the course_image attribute # set different than the default, return that path so that # courses can use custom course image paths, otherwise just # return the default static path. url = '/static/' + (course.static_asset_path or getattr(course, 'data_dir', '')) if hasattr(course, 'course_image') and course.course_image != course.fields['course_image'].default: url += '/' + course.course_image else: url += '/images/course_image.jpg' elif not course.course_image: # if course_image is empty, use the default image url from settings url = settings.STATIC_URL + settings.DEFAULT_COURSE_ABOUT_IMAGE_URL else: loc = StaticContent.compute_location(course.id, course.course_image) url = StaticContent.serialize_asset_key_with_slash(loc) return url
def course_image_url(course): """Try to look up the image url for the course. If it's not found, log an error and return the dead link""" if course.static_asset_path or modulestore().get_modulestore_type(course.id) == ModuleStoreEnum.Type.xml: # If we are a static course with the course_image attribute # set different than the default, return that path so that # courses can use custom course image paths, otherwise just # return the default static path. url = "/static/" + (course.static_asset_path or getattr(course, "data_dir", "")) if hasattr(course, "course_image") and course.course_image != course.fields["course_image"].default: url += "/" + course.course_image else: url += "/images/course_image.jpg" elif course.course_image == "": # if course_image is empty the url will be blank as location # of the course_image does not exist url = "" else: loc = StaticContent.compute_location(course.id, course.course_image) url = StaticContent.serialize_asset_key_with_slash(loc) return url
def course_image_url(course, image_key='course_image'): """Try to look up the image url for the course. If it's not found, log an error and return the dead link. image_key can be one of the three: 'course_image', 'hero_image', 'thumbnail_image' """ if course.static_asset_path: # If we are a static course with the image_key attribute # set different than the default, return that path so that # courses can use custom course image paths, otherwise just # return the default static path. url = '/static/' + (course.static_asset_path or getattr(course, 'data_dir', '')) if hasattr(course, image_key) and getattr(course, image_key) != course.fields[image_key].default: url += '/' + getattr(course, image_key) else: url += '/images/' + image_key + '.jpg' elif not getattr(course, image_key): # if image_key is empty, use the default image url from settings url = settings.STATIC_URL + settings.DEFAULT_COURSE_ABOUT_IMAGE_URL else: loc = StaticContent.compute_location(course.id, getattr(course, image_key)) url = StaticContent.serialize_asset_key_with_slash(loc) return url
def _parse_video_xml(cls, xml, id_generator=None): """ Parse video fields out of xml_data. The fields are set if they are present in the XML. Arguments: id_generator is used to generate course-specific urls and identifiers """ field_data = {} # Convert between key types for certain attributes -- # necessary for backwards compatibility. conversions = { # example: 'start_time': cls._example_convert_start_time } # Convert between key names for certain attributes -- # necessary for backwards compatibility. compat_keys = { 'from': 'start_time', 'to': 'end_time' } sources = xml.findall('source') if sources: field_data['html5_sources'] = [ele.get('src') for ele in sources] track = xml.find('track') if track is not None: field_data['track'] = track.get('src') handout = xml.find('handout') if handout is not None: field_data['handout'] = handout.get('src') transcripts = xml.findall('transcript') if transcripts: field_data['transcripts'] = {tr.get('language'): tr.get('src') for tr in transcripts} for attr, value in xml.items(): if attr in compat_keys: attr = compat_keys[attr] if attr in cls.metadata_to_strip + ('url_name', 'name'): continue if attr == 'youtube': speeds = cls._parse_youtube(value) for speed, youtube_id in speeds.items(): # should have made these youtube_id_1_00 for # cleanliness, but hindsight doesn't need glasses normalized_speed = speed[:-1] if speed.endswith('0') else speed # If the user has specified html5 sources, make sure we don't use the default video if youtube_id != '' or 'html5_sources' in field_data: field_data['youtube_id_{0}'.format(normalized_speed.replace('.', '_'))] = youtube_id elif attr in conversions: field_data[attr] = conversions[attr](value) elif attr not in cls.fields: field_data.setdefault('xml_attributes', {})[attr] = value else: # We export values with json.dumps (well, except for Strings, but # for about a month we did it for Strings also). field_data[attr] = deserialize_field(cls.fields[attr], value) course_id = getattr(id_generator, 'target_course_id', None) # Update the handout location with current course_id if 'handout' in field_data.keys() and course_id: handout_location = StaticContent.get_location_from_path(field_data['handout']) if isinstance(handout_location, AssetLocator): handout_new_location = StaticContent.compute_location(course_id, handout_location.path) field_data['handout'] = StaticContent.serialize_asset_key_with_slash(handout_new_location) # For backwards compatibility: Add `source` if XML doesn't have `download_video` # attribute. if 'download_video' not in field_data and sources: field_data['source'] = field_data['html5_sources'][0] # For backwards compatibility: if XML doesn't have `download_track` attribute, # it means that it is an old format. So, if `track` has some value, # `download_track` needs to have value `True`. if 'download_track' not in field_data and track is not None: field_data['download_track'] = True video_asset_elem = xml.find('video_asset') if ( edxval_api and video_asset_elem is not None and 'edx_video_id' in field_data ): # Allow ValCannotCreateError to escape edxval_api.import_from_xml( video_asset_elem, field_data['edx_video_id'], course_id=course_id ) # load license if it exists field_data = LicenseMixin.parse_license_from_xml(field_data, xml) return field_data
def course_image_url(course): """Returns the image url for the course.""" loc = StaticContent.compute_location(course.location.course_key, course.course_image) path = StaticContent.serialize_asset_key_with_slash(loc) return path