def test_get_image_url(self): """Test image URL formatting.""" course = CourseFactory.create() self.verify_url( unicode(course.id.make_asset_key('asset', course.course_image)), utils.course_image_url(course) )
def format_course_info_for_json(course): """ format data so as to return json """ location = course.location course_id = '.'.join([location.org, location.course, location.name]) print("===================") print(course.id) print(course_id) print("-----------------------------") locator, course_module = _get_locator_and_course( course_id, 'draft', None, location.name) print(">>>>>>>>>>>>>>>>>>>") img_url = request.get_host() + utils.course_image_url(course_module) pack_course = { "id": course_id, "imgid": ("-".join( [course_id, course.course_image.encode("utf-8").split('.')[0]])), "name": course.display_name, "course_number": course.location.course, "org": course.location.org, "imgurl": img_url.encode("utf-8"), "price": 0, "progress": 20, } return pack_course
def fetch(cls, course_key): """ Fetch the course details for the given course from persistence and return a CourseDetails model. """ descriptor = modulestore().get_course(course_key) course_details = cls(course_key.org, course_key.course, course_key.run) course_details.start_date = descriptor.start course_details.end_date = descriptor.end course_details.enrollment_start = descriptor.enrollment_start course_details.enrollment_end = descriptor.enrollment_end course_details.pre_requisite_courses = descriptor.pre_requisite_courses course_details.course_image_name = descriptor.course_image course_details.course_image_asset_path = course_image_url(descriptor) course_details.language = descriptor.language # Default course license is "All Rights Reserved" course_details.license = getattr(descriptor, "license", "all-rights-reserved") course_details.has_cert_config = has_active_web_certificate(descriptor) for attribute in ABOUT_ATTRIBUTES: value = cls._fetch_about_attribute(course_key, attribute) if value is not None: setattr(course_details, attribute, value) raw_video = cls._fetch_about_attribute(course_key, 'video') if raw_video: course_details.intro_video = CourseDetails.parse_video_tag(raw_video) return course_details
def test_non_ascii_image_name(self): # Verify that non-ascii image names are cleaned course = CourseFactory.create(course_image=u'before_\N{SNOWMAN}_after.jpg') self.assertEquals( utils.course_image_url(course), '/c4x/{org}/{course}/asset/before___after.jpg'.format(org=course.location.org, course=course.location.course) )
def test_spaces_in_image_name(self): # Verify that image names with spaces in them are cleaned course = CourseFactory.create(course_image=u"before after.jpg") self.assertEquals( utils.course_image_url(course), "/c4x/{org}/{course}/asset/before_after.jpg".format(org=course.location.org, course=course.location.course), )
def test_spaces_in_image_name(self): # Verify that image names with spaces in them are cleaned course = CourseFactory.create(course_image=u'before after.jpg') self.assertEquals( utils.course_image_url(course), '/c4x/{org}/{course}/asset/before_after.jpg'.format( org=course.location.org, course=course.location.course))
def fetch(cls, course_key): """ Fetch the course details for the given course from persistence and return a CourseDetails model. """ descriptor = modulestore().get_course(course_key) course_details = cls(course_key.org, course_key.course, course_key.run) course_details.start_date = descriptor.start course_details.end_date = descriptor.end course_details.enrollment_start = descriptor.enrollment_start course_details.enrollment_end = descriptor.enrollment_end course_details.pre_requisite_courses = descriptor.pre_requisite_courses course_details.course_image_name = descriptor.course_image course_details.course_image_asset_path = course_image_url(descriptor) for attribute in ABOUT_ATTRIBUTES: value = cls._fetch_about_attribute(course_key, attribute) if value is not None: setattr(course_details, attribute, value) raw_video = cls._fetch_about_attribute(course_key, 'video') if raw_video: course_details.intro_video = CourseDetails.parse_video_tag(raw_video) return course_details
def fetch(cls, course_key): """ Fetch the course details for the given course from persistence and return a CourseDetails model. """ descriptor = modulestore().get_course(course_key) course_details = cls(course_key.org, course_key.course, course_key.run) course_details.start_date = descriptor.start course_details.end_date = descriptor.end course_details.enrollment_start = descriptor.enrollment_start course_details.enrollment_end = descriptor.enrollment_end course_details.pre_requisite_courses = descriptor.pre_requisite_courses course_details.course_image_name = descriptor.course_image course_details.course_image_asset_path = course_image_url(descriptor) course_details.language = descriptor.language # Default course license is "All Rights Reserved" course_details.license = getattr(descriptor, "license", "all-rights-reserved") course_details.has_cert_config = has_active_web_certificate(descriptor) course_details.self_paced = descriptor.self_paced for attribute in ABOUT_ATTRIBUTES: value = cls._fetch_about_attribute(course_key, attribute) if value is not None: setattr(course_details, attribute, value) raw_video = cls._fetch_about_attribute(course_key, 'video') if raw_video: course_details.intro_video = CourseDetails.parse_video_tag( raw_video) return course_details
def fetch(cls, course_key): """ Fetch the course details for the given course from persistence and return a CourseDetails model. """ descriptor = modulestore().get_course(course_key) course_details = cls(course_key.org, course_key.course, course_key.run) course_details.start_date = descriptor.start course_details.end_date = descriptor.end course_details.enrollment_start = descriptor.enrollment_start course_details.enrollment_end = descriptor.enrollment_end course_details.pre_requisite_courses = descriptor.pre_requisite_courses course_details.course_image_name = descriptor.course_image course_details.course_image_asset_path = course_image_url(descriptor) for attribute in ABOUT_ATTRIBUTES: value = cls._fetch_about_attribute(course_key, attribute) if value is not None: setattr(course_details, attribute, value) raw_video = cls._fetch_about_attribute(course_key, 'video') if raw_video: course_details.intro_video = CourseDetails.parse_video_tag( raw_video) return course_details
def test_get_image_url(self): """Test image URL formatting.""" course = CourseFactory.create() url = utils.course_image_url(course) self.assertEquals( url, unicode(course.id.make_asset_key('asset', course.course_image)))
def settings_handler(request, tag=None, course_id=None, branch=None, version_guid=None, block=None): """ Course settings for dates and about pages GET html: get the page json: get the CourseDetails model PUT json: update the Course and About xblocks through the CourseDetails model """ locator, course_module = _get_locator_and_course(course_id, branch, version_guid, block, request.user) if "text/html" in request.META.get("HTTP_ACCEPT", "") and request.method == "GET": upload_asset_url = locator.url_reverse("assets/") return render_to_response( "settings.html", { "context_course": course_module, "course_locator": locator, "lms_link_for_about_page": utils.get_lms_link_for_about_page(course_module.location), "course_image_url": utils.course_image_url(course_module), "details_url": locator.url_reverse("/settings/details/"), "about_page_editable": not settings.FEATURES.get("ENABLE_MKTG_SITE", False), "upload_asset_url": upload_asset_url, }, ) elif "application/json" in request.META.get("HTTP_ACCEPT", ""): if request.method == "GET": return JsonResponse( CourseDetails.fetch(locator), # encoder serializes dates, old locations, and instances encoder=CourseSettingsEncoder, ) else: # post or put, doesn't matter. return JsonResponse(CourseDetails.update_from_json(locator, request.json), encoder=CourseSettingsEncoder)
def settings_handler(request, tag=None, package_id=None, branch=None, version_guid=None, block=None): """ Course settings for dates and about pages GET html: get the page json: get the CourseDetails model PUT json: update the Course and About xblocks through the CourseDetails model """ locator, course_module = _get_locator_and_course(package_id, branch, version_guid, block, request.user) if 'text/html' in request.META.get('HTTP_ACCEPT', '') and request.method == 'GET': upload_asset_url = locator.url_reverse('assets/') # see if the ORG of this course can be attributed to a 'Microsite'. In that case, the # course about page should be editable in Studio about_page_editable = not microsite.get_value_for_org( course_module.location.org, 'ENABLE_MKTG_SITE', settings.FEATURES.get('ENABLE_MKTG_SITE', False)) short_description_editable = settings.FEATURES.get( 'EDITABLE_SHORT_DESCRIPTION', True) return render_to_response( 'settings.html', { 'context_course': course_module, 'course_locator': locator, 'lms_link_for_about_page': utils.get_lms_link_for_about_page(course_module.location), 'course_image_url': utils.course_image_url(course_module), 'details_url': locator.url_reverse('/settings/details/'), 'about_page_editable': about_page_editable, 'short_description_editable': short_description_editable, 'upload_asset_url': upload_asset_url }) elif 'application/json' in request.META.get('HTTP_ACCEPT', ''): if request.method == 'GET': return JsonResponse( CourseDetails.fetch(locator), # encoder serializes dates, old locations, and instances encoder=CourseSettingsEncoder) else: # post or put, doesn't matter. return JsonResponse(CourseDetails.update_from_json( locator, request.json, request.user), encoder=CourseSettingsEncoder)
def test_non_ascii_image_name(self): """ Verify that non-ascii image names are cleaned """ course_image = u"before_\N{SNOWMAN}_after.jpg" course = CourseFactory.create(course_image=course_image) self.verify_url( unicode(course.id.make_asset_key("asset", course_image.replace(u"\N{SNOWMAN}", "_"))), utils.course_image_url(course), )
def test_spaces_in_image_name(self): """ Verify that image names with spaces in them are cleaned """ course_image = u'before after.jpg' course = CourseFactory.create(course_image=u'before after.jpg') self.verify_url( unicode(course.id.make_asset_key('asset', course_image.replace(" ", "_"))), utils.course_image_url(course) )
def test_non_ascii_image_name(self): """ Verify that non-ascii image names are cleaned """ course_image = u'before_\N{SNOWMAN}_after.jpg' course = CourseFactory.create(course_image=course_image) self.verify_url( unicode(course.id.make_asset_key('asset', course_image.replace(u'\N{SNOWMAN}', '_'))), utils.course_image_url(course) )
def test_non_ascii_image_name(self): """ Verify that non-ascii image names are cleaned """ course_image = u'before_\N{SNOWMAN}_after.jpg' course = CourseFactory.create(course_image=course_image) self.assertEquals( utils.course_image_url(course), unicode(course.id.make_asset_key('asset', course_image.replace(u'\N{SNOWMAN}', '_'))) )
def test_empty_image_name(self, default_store): """ Verify that empty image names are cleaned """ course_image = u'' course = CourseFactory.create(course_image=course_image, default_store=default_store) self.assertEquals( course_image, utils.course_image_url(course), )
def fetch(cls, course_locator): """ Fetch the course details for the given course from persistence and return a CourseDetails model. """ course_old_location = loc_mapper().translate_locator_to_location(course_locator) descriptor = get_modulestore(course_old_location).get_item(course_old_location) course = cls(course_old_location.org, course_old_location.course, course_old_location.name) course.start_date = descriptor.start course.end_date = descriptor.end course.enrollment_start = descriptor.enrollment_start course.enrollment_end = descriptor.enrollment_end course.course_image_name = descriptor.course_image course.course_image_asset_path = course_image_url(descriptor) course.course_price = descriptor.course_price temploc = course_old_location.replace(category='about', name='syllabus') try: course.syllabus = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass temploc = course_old_location.replace(category='about', name='short_description') try: course.short_description = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass temploc = temploc.replace(name='overview') try: course.overview = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass temploc = temploc.replace(name='effort') try: course.effort = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass temploc = temploc.replace(name='video') try: raw_video = get_modulestore(temploc).get_item(temploc).data course.intro_video = CourseDetails.parse_video_tag(raw_video) # course.intro_video = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass temploc = temploc.replace(name='course_price') try: course.course_price = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass return course
def fetch(cls, course_locator): """ Fetch the course details for the given course from persistence and return a CourseDetails model. """ course_old_location = loc_mapper().translate_locator_to_location( course_locator) descriptor = get_modulestore(course_old_location).get_item( course_old_location) course = cls(course_old_location.org, course_old_location.course, course_old_location.name) course.start_date = descriptor.start course.end_date = descriptor.end course.enrollment_start = descriptor.enrollment_start course.enrollment_end = descriptor.enrollment_end course.course_image_name = descriptor.course_image course.course_image_asset_path = course_image_url(descriptor) temploc = course_old_location.replace(category='about', name='syllabus') try: course.syllabus = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass temploc = course_old_location.replace(category='about', name='short_description') try: course.short_description = get_modulestore(temploc).get_item( temploc).data except ItemNotFoundError: pass temploc = temploc.replace(name='overview') try: course.overview = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass temploc = temploc.replace(name='effort') try: course.effort = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass temploc = temploc.replace(name='video') try: raw_video = get_modulestore(temploc).get_item(temploc).data course.intro_video = CourseDetails.parse_video_tag(raw_video) # course.intro_video = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass return course
def fetch(cls, course_location): """ Fetch the course details for the given course from persistence and return a CourseDetails model. """ if not isinstance(course_location, Location): course_location = Location(course_location) course = cls(course_location) descriptor = get_modulestore(course_location).get_item(course_location) course.start_date = descriptor.start course.end_date = descriptor.end course.enrollment_start = descriptor.enrollment_start course.enrollment_end = descriptor.enrollment_end course.course_image_name = descriptor.course_image course.course_image_asset_path = course_image_url(descriptor) temploc = course_location.replace(category='about', name='syllabus') try: course.syllabus = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass temploc = temploc.replace(name='overview') try: course.overview = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass temploc = temploc.replace(name='tags') try: course.tags = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass temploc = temploc.replace(name='effort') try: course.effort = get_modulestore(temploc).get_item(temploc).data except ItemNotFoundError: pass temploc = temploc.replace(name='video') try: raw_video = get_modulestore(temploc).get_item(temploc).data course.intro_video = CourseDetails.parse_video_tag(raw_video) except ItemNotFoundError: pass return course
def fetch(cls, course_key): """ Fetch the course details for the given course from persistence and return a CourseDetails model. """ descriptor = modulestore().get_course(course_key) course_details = cls(course_key.org, course_key.course, course_key.run) course_details.start_date = descriptor.start course_details.end_date = descriptor.end course_details.enrollment_start = descriptor.enrollment_start course_details.enrollment_end = descriptor.enrollment_end course_details.course_image_name = descriptor.course_image course_details.course_image_asset_path = course_image_url(descriptor) temploc = course_key.make_usage_key('about', 'syllabus') try: course_details.syllabus = modulestore().get_item(temploc).data except ItemNotFoundError: pass temploc = course_key.make_usage_key('about', 'short_description') try: course_details.short_description = modulestore().get_item( temploc).data except ItemNotFoundError: pass temploc = course_key.make_usage_key('about', 'overview') try: course_details.overview = modulestore().get_item(temploc).data except ItemNotFoundError: pass temploc = course_key.make_usage_key('about', 'effort') try: course_details.effort = modulestore().get_item(temploc).data except ItemNotFoundError: pass temploc = course_key.make_usage_key('about', 'video') try: raw_video = modulestore().get_item(temploc).data course_details.intro_video = CourseDetails.parse_video_tag( raw_video) except ItemNotFoundError: pass return course_details
def fetch(cls, course_key): """ Fetch the course details for the given course from persistence and return a CourseDetails model. """ descriptor = modulestore().get_course(course_key) course_details = cls(course_key.org, course_key.course, course_key.run) course_details.start_date = descriptor.start course_details.end_date = descriptor.end course_details.enrollment_start = descriptor.enrollment_start course_details.enrollment_end = descriptor.enrollment_end course_details.course_image_name = descriptor.course_image course_details.course_image_asset_path = course_image_url(descriptor) course_details.course_video_name = descriptor.course_video course_details.course_video_asset_path = course_video_url(descriptor) temploc = course_key.make_usage_key('about', 'syllabus') try: course_details.syllabus = modulestore().get_item(temploc).data except ItemNotFoundError: pass temploc = course_key.make_usage_key('about', 'short_description') try: course_details.short_description = modulestore().get_item(temploc).data except ItemNotFoundError: pass temploc = course_key.make_usage_key('about', 'overview') try: course_details.overview = modulestore().get_item(temploc).data except ItemNotFoundError: pass temploc = course_key.make_usage_key('about', 'effort') try: course_details.effort = modulestore().get_item(temploc).data except ItemNotFoundError: pass temploc = course_key.make_usage_key('about', 'video') try: raw_video = modulestore().get_item(temploc).data course_details.intro_video = CourseDetails.parse_video_tag(raw_video) except ItemNotFoundError: pass return course_details
def settings_handler(request, tag=None, course_id=None, branch=None, version_guid=None, block=None): """ Course settings for dates and about pages GET html: get the page json: get the CourseDetails model PUT json: update the Course and About xblocks through the CourseDetails model """ locator, course_module = _get_locator_and_course(course_id, branch, version_guid, block, request.user) if 'text/html' in request.META.get('HTTP_ACCEPT', '') and request.method == 'GET': upload_asset_url = locator.url_reverse('assets/') return render_to_response( 'settings.html', { 'context_course': course_module, 'course_locator': locator, 'lms_link_for_about_page': utils.get_lms_link_for_about_page(course_module.location), 'course_image_url': utils.course_image_url(course_module), 'details_url': locator.url_reverse('/settings/details/'), 'about_page_editable': not settings.FEATURES.get('ENABLE_MKTG_SITE', False), 'upload_asset_url': upload_asset_url }) elif 'application/json' in request.META.get('HTTP_ACCEPT', ''): if request.method == 'GET': return JsonResponse( CourseDetails.fetch(locator), # encoder serializes dates, old locations, and instances encoder=CourseSettingsEncoder) else: # post or put, doesn't matter. return JsonResponse(CourseDetails.update_from_json( locator, request.json), encoder=CourseSettingsEncoder)
def settings_handler(request, tag=None, package_id=None, branch=None, version_guid=None, block=None): """ Course settings for dates and about pages GET html: get the page json: get the CourseDetails model PUT json: update the Course and About xblocks through the CourseDetails model """ locator, course_module = _get_locator_and_course( package_id, branch, version_guid, block, request.user ) if 'text/html' in request.META.get('HTTP_ACCEPT', '') and request.method == 'GET': upload_asset_url = locator.url_reverse('assets/') # see if the ORG of this course can be attributed to a 'Microsite'. In that case, the # course about page should be editable in Studio about_page_editable = not MicrositeConfiguration.get_microsite_configuration_value_for_org( course_module.location.org, 'ENABLE_MKTG_SITE', settings.FEATURES.get('ENABLE_MKTG_SITE', False) ) short_description_editable = settings.FEATURES.get('EDITABLE_SHORT_DESCRIPTION', True) return render_to_response('settings.html', { 'context_course': course_module, 'course_locator': locator, 'lms_link_for_about_page': utils.get_lms_link_for_about_page(course_module.location), 'course_image_url': utils.course_image_url(course_module), 'details_url': locator.url_reverse('/settings/details/'), 'about_page_editable': about_page_editable, 'short_description_editable': short_description_editable, 'upload_asset_url': upload_asset_url }) elif 'application/json' in request.META.get('HTTP_ACCEPT', ''): if request.method == 'GET': return JsonResponse( CourseDetails.fetch(locator), # encoder serializes dates, old locations, and instances encoder=CourseSettingsEncoder ) else: # post or put, doesn't matter. return JsonResponse( CourseDetails.update_from_json(locator, request.json, request.user), encoder=CourseSettingsEncoder )
def settings_handler(request, course_key_string): """ Course settings for dates and about pages GET html: get the page json: get the CourseDetails model PUT json: update the Course and About xblocks through the CourseDetails model """ course_key = CourseKey.from_string(course_key_string) course_module = _get_course_module(course_key, request.user) if "text/html" in request.META.get("HTTP_ACCEPT", "") and request.method == "GET": upload_asset_url = reverse_course_url("assets_handler", course_key) # see if the ORG of this course can be attributed to a 'Microsite'. In that case, the # course about page should be editable in Studio about_page_editable = not microsite.get_value_for_org( course_module.location.org, "ENABLE_MKTG_SITE", settings.FEATURES.get("ENABLE_MKTG_SITE", False) ) short_description_editable = settings.FEATURES.get("EDITABLE_SHORT_DESCRIPTION", True) return render_to_response( "settings.html", { "context_course": course_module, "course_locator": course_key, "lms_link_for_about_page": utils.get_lms_link_for_about_page(course_key), "course_image_url": utils.course_image_url(course_module), "details_url": reverse_course_url("settings_handler", course_key), "about_page_editable": about_page_editable, "short_description_editable": short_description_editable, "upload_asset_url": upload_asset_url, }, ) elif "application/json" in request.META.get("HTTP_ACCEPT", ""): if request.method == "GET": return JsonResponse( CourseDetails.fetch(course_key), # encoder serializes dates, old locations, and instances encoder=CourseSettingsEncoder, ) else: # post or put, doesn't matter. return JsonResponse( CourseDetails.update_from_json(course_key, request.json, request.user), encoder=CourseSettingsEncoder )
def duplicate_course(source, target, user, extra_fields=None, http_protocol='https'): source_course = get_course_by_id(SlashSeparatedCourseKey.from_deprecated_string(source)) target_course = force_create_course(source, target, user, extra_fields) new_course_key = str(target_course.location.course_key) image_url = "{}://{}{}".format(http_protocol, settings.LMS_BASE, course_image_url(source_course)) target_course.course_image = upload_image_from_url(new_course_key, image_url) modulestore().update_item(target_course, user.id) for child in target_course.get_children(): modulestore().delete_item(child.location, user.id) target_locator = target_course.location for child in source_course.get_children(): new_location = _duplicate_item(target_locator, child.location, user=user, display_name=child.display_name) modulestore().publish(new_location, user.id) return target_course
def format_course_info_for_json(course): """ format data so as to return json """ location = course.location course_id = '.'.join([location.org, location.course, location.name]) locator, course_module = _get_locator_and_course(course_id, 'draft', None, location.name) img_url = request.get_host() + utils.course_image_url(course_module) pack_course = { "id": course_id, "imgid": ("-".join([course_id, course.course_image.encode("utf-8").split('.')[0]])), "name": course.display_name, "course_number": course.location.course, "org": course.location.org, "imgurl": img_url.encode("utf-8"), "price": 0, "progress": 20, } return pack_course
def mobi_course_info(request, course_id=None): if request.method != "GET": return HttpResponseBadRequest( "Only support GET request for this restful api") else: try: locator, course_module = _get_locator_and_course( package_id=course_id, branch='draft', version_guid=None, block_id=course_id.split(".")[-1]) except InsufficientSpecificationError: return JsonResponse({ "error": 20000, "errormsg": "unable get the course with id" + course_id }) location = loc_mapper().translate_locator_to_location(locator) course = modulestore('direct').get_item(location) return JsonResponse({ "name": course.display_name, "logo": request.get_host() + utils.course_image_url(course_module), "org": course.location.org, "code": location.course, "startdate": location.name, "peried": 0, "price": 0, "about": "--", "category": course.category })
def settings_handler(request, tag=None, course_id=None, branch=None, version_guid=None, block=None): """ Course settings for dates and about pages GET html: get the page json: get the CourseDetails model PUT json: update the Course and About xblocks through the CourseDetails model """ locator = BlockUsageLocator(course_id=course_id, branch=branch, version_guid=version_guid, usage_id=block) if not has_access(request.user, locator): raise PermissionDenied() if 'text/html' in request.META.get('HTTP_ACCEPT', '') and request.method == 'GET': course_old_location = loc_mapper().translate_locator_to_location(locator) course_module = modulestore().get_item(course_old_location) upload_asset_url = locator.url_reverse('assets/') return render_to_response('settings.html', { 'context_course': course_module, 'course_locator': locator, 'lms_link_for_about_page': utils.get_lms_link_for_about_page(course_old_location), 'course_image_url': utils.course_image_url(course_module), 'details_url': locator.url_reverse('/settings/details/'), 'about_page_editable': not settings.FEATURES.get( 'ENABLE_MKTG_SITE', False ), 'upload_asset_url': upload_asset_url }) elif 'application/json' in request.META.get('HTTP_ACCEPT', ''): if request.method == 'GET': return JsonResponse( CourseDetails.fetch(locator), # encoder serializes dates, old locations, and instances encoder=CourseSettingsEncoder ) else: # post or put, doesn't matter. return JsonResponse( CourseDetails.update_from_json(locator, request.json), encoder=CourseSettingsEncoder )
def format_and_filter_key_for_json(course, keyword=None): """ format data and filter with keywordso as to return json """ if not keyword or (keyword not in course.org and keyword not in course.id and keyword not in course.display_name_with_default): return None try: location = course.location course_id = '.'.join( [location.org, location.course, location.name]) locator, course_module = _get_locator_and_course( course_id, 'draft', None, location.name) img_url = request.get_host() + utils.course_image_url( course_module) return { "id": course_id, "imgid": ("-".join([ course_id, course.course_image.encode("utf-8").split('.')[0] ])), "name": course.display_name, "course_number": course.location.course, "org": course.location.org, "imgurl": img_url.encode("utf-8"), "price": 0, "progress": 20, } except: return None
def mobi_course_info(request, course_id=None): if request.method != "GET": return HttpResponseBadRequest("Only support GET request for this restful api") else: try: locator, course_module = _get_locator_and_course(package_id=course_id, branch='draft', version_guid=None, block_id=course_id.split(".")[-1]) except InsufficientSpecificationError: return JsonResponse({"error": 20000, "errormsg": "unable get the course with id" + course_id}) location = loc_mapper().translate_locator_to_location(locator) course = modulestore('direct').get_item(location) return JsonResponse({ "name": course.display_name, "logo": request.get_host() + utils.course_image_url(course_module), "org": course.location.org, "code": location.course, "startdate": location.name, "peried": 0, "price": 0, "about": "--", "category": course.category })
def format_and_filter_key_for_json(course, keyword=None): """ format data and filter with keywordso as to return json """ if not keyword or (keyword not in course.org and keyword not in course.id and keyword not in course.display_name_with_default): return None try: location = course.location course_id = '.'.join([location.org, location.course, location.name]) locator, course_module = _get_locator_and_course(course_id, 'draft', None, location.name) img_url = request.get_host() + utils.course_image_url(course_module) return { "id": course_id, "imgid": ("-".join([course_id, course.course_image.encode("utf-8").split('.')[0]])), "name": course.display_name, "course_number": course.location.course, "org": course.location.org, "imgurl": img_url.encode("utf-8"), "price": 0, "progress": 20, } except: return None
def index_about_information(cls, modulestore, course): """ Add the given course to the course discovery index Arguments: modulestore - modulestore object to use for operations course - course object from which to take properties, locate about information """ searcher = SearchEngine.get_search_engine(cls.INDEX_NAME) if not searcher: return course_id = unicode(course.id) course_info = { 'id': course_id, 'course': course_id, 'content': {}, 'image_url': course_image_url(course), } # load data for all of the 'about' modules for this course into a dictionary about_dictionary = { item.location.name: item.data for item in modulestore.get_items(course.id, qualifiers={"category": "about"}) } about_context = { "course": course, "about_dictionary": about_dictionary, } for about_information in cls.ABOUT_INFORMATION_TO_INCLUDE: # Broad exception handler so that a single bad property does not scupper the collection of others try: section_content = about_information.get_value(**about_context) except: # pylint: disable=bare-except section_content = None log.warning( "Course discovery could not collect property %s for course %s", about_information.property_name, course_id, exc_info=True, ) if section_content: if about_information.index_flags & AboutInfo.ANALYSE: analyse_content = section_content if isinstance(section_content, basestring): analyse_content = strip_html_content_to_text( section_content) course_info['content'][ about_information.property_name] = analyse_content if about_information.index_flags & AboutInfo.PROPERTY: course_info[ about_information.property_name] = section_content # Broad exception handler to protect around and report problems with indexing try: searcher.index(cls.DISCOVERY_DOCUMENT_TYPE, [course_info]) except: # pylint: disable=bare-except log.exception( "Course discovery indexing error encountered, course discovery index may be out of date %s", course_id, ) raise log.debug("Successfully added %s course to the course discovery index", course_id)
def test_get_image_url(self): """Test image URL formatting.""" course = CourseFactory.create() url = utils.course_image_url(course) self.assertEquals(url, unicode(course.id.make_asset_key('asset', course.course_image)))
def test_get_image_url(self): """Test image URL formatting.""" course = CourseFactory.create(org='edX', course='999') url = utils.course_image_url(course) self.assertEquals(url, '/c4x/edX/999/asset/{0}'.format(course.course_image))
def test_get_image_url(self): """Test image URL formatting.""" course = CourseFactory.create(org="edX", course="999") url = utils.course_image_url(course) self.assertEquals(url, "/c4x/edX/999/asset/{0}".format(course.course_image))
def index_about_information(cls, modulestore, course): """ Add the given course to the course discovery index Arguments: modulestore - modulestore object to use for operations course - course object from which to take properties, locate about information """ searcher = SearchEngine.get_search_engine(cls.INDEX_NAME) if not searcher: return course_id = unicode(course.id) course_info = { 'id': course_id, 'course': course_id, 'content': {}, 'image_url': course_image_url(course), } # load data for all of the 'about' modules for this course into a dictionary about_dictionary = { item.location.name: item.data for item in modulestore.get_items(course.id, qualifiers={"category": "about"}) } about_context = { "course": course, "about_dictionary": about_dictionary, } for about_information in cls.ABOUT_INFORMATION_TO_INCLUDE: # Broad exception handler so that a single bad property does not scupper the collection of others try: section_content = about_information.get_value(**about_context) except: # pylint: disable=bare-except section_content = None log.warning( "Course discovery could not collect property %s for course %s", about_information.property_name, course_id, exc_info=True, ) if section_content: if about_information.index_flags & AboutInfo.ANALYSE: analyse_content = section_content if isinstance(section_content, basestring): analyse_content = strip_html_content_to_text(section_content) course_info['content'][about_information.property_name] = analyse_content if about_information.index_flags & AboutInfo.PROPERTY: course_info[about_information.property_name] = section_content # Broad exception handler to protect around and report problems with indexing try: searcher.index(cls.DISCOVERY_DOCUMENT_TYPE, course_info) except: # pylint: disable=bare-except log.exception( "Course discovery indexing error encountered, course discovery index may be out of date %s", course_id, ) raise log.debug( "Successfully added %s course to the course discovery index", course_id )