本文整理汇总了Python中models.settings.course_metadata.CourseMetadata.update_from_dict方法的典型用法代码示例。如果您正苦于以下问题:Python CourseMetadata.update_from_dict方法的具体用法?Python CourseMetadata.update_from_dict怎么用?Python CourseMetadata.update_from_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.settings.course_metadata.CourseMetadata
的用法示例。
在下文中一共展示了CourseMetadata.update_from_dict方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _delete_entrance_exam
# 需要导入模块: from models.settings.course_metadata import CourseMetadata [as 别名]
# 或者: from models.settings.course_metadata.CourseMetadata import update_from_dict [as 别名]
def _delete_entrance_exam(request, course_key):
"""
Internal workflow operation to remove an entrance exam
"""
store = modulestore()
course = store.get_course(course_key)
if course is None:
return HttpResponse(status=400)
remove_entrance_exam_milestone_reference(request, course_key)
# Reset the entrance exam flags on the course
# Reload the course so we have the latest state
course = store.get_course(course_key)
if course.entrance_exam_id:
metadata = {
'entrance_exam_enabled': False,
'entrance_exam_minimum_score_pct': None,
'entrance_exam_id': None,
}
CourseMetadata.update_from_dict(metadata, course, request.user)
# Clean up any pre-existing entrance exam graders
remove_entrance_exam_graders(course_key, request.user)
return HttpResponse(status=204)
示例2: _delete_entrance_exam
# 需要导入模块: from models.settings.course_metadata import CourseMetadata [as 别名]
# 或者: from models.settings.course_metadata.CourseMetadata import update_from_dict [as 别名]
def _delete_entrance_exam(request, course_key):
"""
Internal workflow operation to remove an entrance exam
"""
store = modulestore()
course = store.get_course(course_key)
if course is None:
return HttpResponse(status=400)
course_children = store.get_items(
course_key,
qualifiers={'category': 'chapter'}
)
for course_child in course_children:
if course_child.is_entrance_exam:
delete_item(request, course_child.scope_ids.usage_id)
milestones_helpers.remove_content_references(unicode(course_child.scope_ids.usage_id))
# Reset the entrance exam flags on the course
# Reload the course so we have the latest state
course = store.get_course(course_key)
if getattr(course, 'entrance_exam_id'):
metadata = {
'entrance_exam_enabled': False,
'entrance_exam_minimum_score_pct': None,
'entrance_exam_id': None,
}
CourseMetadata.update_from_dict(metadata, course, request.user)
# Clean up any pre-existing entrance exam graders
remove_entrance_exam_graders(course_key, request.user)
return HttpResponse(status=204)
示例3: test_contentstore_views_entrance_exam_get_bogus_exam
# 需要导入模块: from models.settings.course_metadata import CourseMetadata [as 别名]
# 或者: from models.settings.course_metadata.CourseMetadata import update_from_dict [as 别名]
def test_contentstore_views_entrance_exam_get_bogus_exam(self):
"""
Unit Test: test_contentstore_views_entrance_exam_get_bogus_exam
"""
resp = self.client.post(
self.exam_url,
{'entrance_exam_minimum_score_pct': '50'},
http_accept='application/json'
)
self.assertEqual(resp.status_code, 201)
resp = self.client.get(self.exam_url)
self.assertEqual(resp.status_code, 200)
self.course = modulestore().get_course(self.course.id)
# Should raise an ItemNotFoundError and return a 404
updated_metadata = {'entrance_exam_id': 'i4x://org.4/course_4/chapter/ed7c4c6a4d68409998e2c8554c4629d1'}
CourseMetadata.update_from_dict(
updated_metadata,
self.course,
self.user,
)
self.course = modulestore().get_course(self.course.id)
resp = self.client.get(self.exam_url)
self.assertEqual(resp.status_code, 404)
# Should raise an InvalidKeyError and return a 404
updated_metadata = {'entrance_exam_id': '123afsdfsad90f87'}
CourseMetadata.update_from_dict(
updated_metadata,
self.course,
self.user,
)
self.course = modulestore().get_course(self.course.id)
resp = self.client.get(self.exam_url)
self.assertEqual(resp.status_code, 404)
示例4: update_entrance_exam
# 需要导入模块: from models.settings.course_metadata import CourseMetadata [as 别名]
# 或者: from models.settings.course_metadata.CourseMetadata import update_from_dict [as 别名]
def update_entrance_exam(request, course_key, exam_data):
"""
Operation to update course fields pertaining to entrance exams
The update operation is not currently exposed directly via the API
Because the operation is not exposed directly, we do not return a 200 response
But we do return a 400 in the error case because the workflow is executed in a request context
"""
course = modulestore().get_course(course_key)
if course:
metadata = exam_data
CourseMetadata.update_from_dict(metadata, course, request.user)
示例5: _create_entrance_exam
# 需要导入模块: from models.settings.course_metadata import CourseMetadata [as 别名]
# 或者: from models.settings.course_metadata.CourseMetadata import update_from_dict [as 别名]
def _create_entrance_exam(request, course_key, entrance_exam_minimum_score_pct=None):
"""
Internal workflow operation to create an entrance exam
"""
# Provide a default value for the minimum score percent if nothing specified
if entrance_exam_minimum_score_pct is None:
entrance_exam_minimum_score_pct = float(settings.ENTRANCE_EXAM_MIN_SCORE_PCT)
# Confirm the course exists
course = modulestore().get_course(course_key)
if course is None:
return HttpResponse(status=400)
# Create the entrance exam item (currently it's just a chapter)
payload = {
"category": "chapter",
"display_name": "Entrance Exam",
"parent_locator": unicode(course.location),
"is_entrance_exam": True,
"in_entrance_exam": True,
}
factory = RequestFactory()
internal_request = factory.post("/", json.dumps(payload), content_type="application/json")
internal_request.user = request.user
created_item = json.loads(create_item(internal_request).content)
# Set the entrance exam metadata flags for this course
# Reload the course so we don't overwrite the new child reference
course = modulestore().get_course(course_key)
metadata = {
"entrance_exam_enabled": True,
"entrance_exam_minimum_score_pct": entrance_exam_minimum_score_pct / 100,
"entrance_exam_id": created_item["locator"],
}
CourseMetadata.update_from_dict(metadata, course, request.user)
# Add an entrance exam milestone if one does not already exist
milestone_namespace = generate_milestone_namespace(NAMESPACE_CHOICES["ENTRANCE_EXAM"], course_key)
milestones = milestones_api.get_milestones(milestone_namespace)
if len(milestones):
milestone = milestones[0]
else:
description = "Autogenerated during {} entrance exam creation.".format(unicode(course.id))
milestone = milestones_api.add_milestone(
{"name": "Completed Course Entrance Exam", "namespace": milestone_namespace, "description": description}
)
relationship_types = milestones_api.get_milestone_relationship_types()
milestones_api.add_course_milestone(unicode(course.id), relationship_types["REQUIRES"], milestone)
milestones_api.add_course_content_milestone(
unicode(course.id), created_item["locator"], relationship_types["FULFILLS"], milestone
)
return HttpResponse(status=201)
示例6: _create_entrance_exam
# 需要导入模块: from models.settings.course_metadata import CourseMetadata [as 别名]
# 或者: from models.settings.course_metadata.CourseMetadata import update_from_dict [as 别名]
def _create_entrance_exam(request, course_key, entrance_exam_minimum_score_pct=None):
"""
Internal workflow operation to create an entrance exam
"""
# Provide a default value for the minimum score percent if nothing specified
if entrance_exam_minimum_score_pct is None:
entrance_exam_minimum_score_pct = _get_default_entrance_exam_minimum_pct()
# Confirm the course exists
course = modulestore().get_course(course_key)
if course is None:
return HttpResponse(status=400)
# Create the entrance exam item (currently it's just a chapter)
payload = {
'category': "chapter",
'display_name': _("Entrance Exam"),
'parent_locator': unicode(course.location),
'is_entrance_exam': True,
'in_entrance_exam': True,
}
parent_locator = unicode(course.location)
created_block = create_xblock(
parent_locator=parent_locator,
user=request.user,
category='chapter',
display_name=_('Entrance Exam'),
is_entrance_exam=True
)
# Set the entrance exam metadata flags for this course
# Reload the course so we don't overwrite the new child reference
course = modulestore().get_course(course_key)
metadata = {
'entrance_exam_enabled': True,
'entrance_exam_minimum_score_pct': unicode(entrance_exam_minimum_score_pct),
'entrance_exam_id': unicode(created_block.location),
}
CourseMetadata.update_from_dict(metadata, course, request.user)
# Create the entrance exam section item.
create_xblock(
parent_locator=unicode(created_block.location),
user=request.user,
category='sequential',
display_name=_('Entrance Exam - Subsection')
)
add_entrance_exam_milestone(course.id, created_block)
return HttpResponse(status=201)
示例7: _delete_entrance_exam
# 需要导入模块: from models.settings.course_metadata import CourseMetadata [as 别名]
# 或者: from models.settings.course_metadata.CourseMetadata import update_from_dict [as 别名]
def _delete_entrance_exam(request, course_key):
"""
Internal workflow operation to remove an entrance exam
"""
store = modulestore()
course = store.get_course(course_key)
if course is None:
return HttpResponse(status=400)
course_children = store.get_items(course_key, qualifiers={"category": "chapter"})
for course_child in course_children:
if course_child.is_entrance_exam:
delete_item(request, course_child.scope_ids.usage_id)
milestones_api.remove_content_references(unicode(course_child.scope_ids.usage_id))
# Reset the entrance exam flags on the course
# Reload the course so we have the latest state
course = store.get_course(course_key)
if getattr(course, "entrance_exam_id"):
metadata = {"entrance_exam_enabled": False, "entrance_exam_minimum_score_pct": None, "entrance_exam_id": None}
CourseMetadata.update_from_dict(metadata, course, request.user)
return HttpResponse(status=204)
示例8: _import_handler
# 需要导入模块: from models.settings.course_metadata import CourseMetadata [as 别名]
# 或者: from models.settings.course_metadata.CourseMetadata import update_from_dict [as 别名]
#.........这里部分代码省略.........
for dirpath, _dirnames, filenames in os.walk(directory):
for filename in filenames:
yield (filename, dirpath)
def get_dir_for_fname(directory, filename):
"""
Returns the dirpath for the first file found in the directory
with the given name. If there is no file in the directory with
the specified name, return None.
"""
for fname, dirpath in get_all_files(directory):
if fname == filename:
return dirpath
return None
dirpath = get_dir_for_fname(course_dir, root_name)
if not dirpath:
_save_request_status(request, courselike_string, -2)
return JsonResponse(
{
'ErrMsg': _('Could not find the {0} file in the package.').format(root_name),
'Stage': -2
},
status=415
)
dirpath = os.path.relpath(dirpath, data_root)
logging.debug('found %s at %s', root_name, dirpath)
log.info("Course import %s: Extracted file verified", courselike_key)
_save_request_status(request, courselike_string, 3)
with dog_stats_api.timer(
'courselike_import.time',
tags=[u"courselike:{}".format(courselike_key)]
):
courselike_items = import_func(
modulestore(), request.user.id,
settings.GITHUB_REPO_ROOT, [dirpath],
load_error_modules=False,
static_content_store=contentstore(),
target_id=courselike_key
)
new_location = courselike_items[0].location
logging.debug('new course at %s', new_location)
log.info("Course import %s: Course import successful", courselike_key)
_save_request_status(request, courselike_string, 4)
# Send errors to client with stage at which error occurred.
except Exception as exception: # pylint: disable=broad-except
log.exception(
"error importing course"
)
return JsonResponse(
{
'ErrMsg': str(exception),
'Stage': -session_status[courselike_string]
},
status=400
)
finally:
if course_dir.isdir():
shutil.rmtree(course_dir)
log.info("Course import %s: Temp data cleared", courselike_key)
# set failed stage number with negative sign in case of unsuccessful import
if session_status[courselike_string] != 4:
_save_request_status(request, courselike_string, -abs(session_status[courselike_string]))
# status == 4 represents that course has been imported successfully.
if session_status[courselike_string] == 4 and root_name == COURSE_ROOT:
# Reload the course so we have the latest state
course = modulestore().get_course(courselike_key)
if course.entrance_exam_enabled:
entrance_exam_chapter = modulestore().get_items(
course.id,
qualifiers={'category': 'chapter'},
settings={'is_entrance_exam': True}
)[0]
metadata = {'entrance_exam_id': unicode(entrance_exam_chapter.location)}
CourseMetadata.update_from_dict(metadata, course, request.user)
add_entrance_exam_milestone(course.id, entrance_exam_chapter)
log.info("Course %s Entrance exam imported", course.id)
return JsonResponse({'Status': 'OK'})
elif request.method == 'GET': # assume html
status_url = reverse_course_url(
"import_status_handler", courselike_key, kwargs={'filename': "fillerName"}
)
return render_to_response('import.html', {
context_name: courselike_module,
'successful_import_redirect_url': successful_url,
'import_status_url': status_url,
'library': isinstance(courselike_key, LibraryLocator)
})
else:
return HttpResponseNotFound()
示例9: _create_entrance_exam
# 需要导入模块: from models.settings.course_metadata import CourseMetadata [as 别名]
# 或者: from models.settings.course_metadata.CourseMetadata import update_from_dict [as 别名]
def _create_entrance_exam(request, course_key, entrance_exam_minimum_score_pct=None):
"""
Internal workflow operation to create an entrance exam
"""
# Provide a default value for the minimum score percent if nothing specified
if entrance_exam_minimum_score_pct is None:
entrance_exam_minimum_score_pct = _get_default_entrance_exam_minimum_pct()
# Confirm the course exists
course = modulestore().get_course(course_key)
if course is None:
return HttpResponse(status=400)
# Create the entrance exam item (currently it's just a chapter)
payload = {
'category': "chapter",
'display_name': _("Entrance Exam"),
'parent_locator': unicode(course.location),
'is_entrance_exam': True,
'in_entrance_exam': True,
}
parent_locator = unicode(course.location)
created_block = create_xblock(
parent_locator=parent_locator,
user=request.user,
category='chapter',
display_name=_('Entrance Exam'),
is_entrance_exam=True
)
# Set the entrance exam metadata flags for this course
# Reload the course so we don't overwrite the new child reference
course = modulestore().get_course(course_key)
metadata = {
'entrance_exam_enabled': True,
'entrance_exam_minimum_score_pct': unicode(entrance_exam_minimum_score_pct),
'entrance_exam_id': unicode(created_block.location),
}
CourseMetadata.update_from_dict(metadata, course, request.user)
# Create the entrance exam section item.
create_xblock(
parent_locator=unicode(created_block.location),
user=request.user,
category='sequential',
display_name=_('Entrance Exam - Subsection')
)
# Add an entrance exam milestone if one does not already exist
namespace_choices = milestones_helpers.get_namespace_choices()
milestone_namespace = milestones_helpers.generate_milestone_namespace(
namespace_choices.get('ENTRANCE_EXAM'),
course_key
)
milestones = milestones_helpers.get_milestones(milestone_namespace)
if len(milestones):
milestone = milestones[0]
else:
description = 'Autogenerated during {} entrance exam creation.'.format(unicode(course.id))
milestone = milestones_helpers.add_milestone({
'name': _('Completed Course Entrance Exam'),
'namespace': milestone_namespace,
'description': description
})
relationship_types = milestones_helpers.get_milestone_relationship_types()
milestones_helpers.add_course_milestone(
unicode(course.id),
relationship_types['REQUIRES'],
milestone
)
milestones_helpers.add_course_content_milestone(
unicode(course.id),
unicode(created_block.location),
relationship_types['FULFILLS'],
milestone
)
return HttpResponse(status=201)
示例10: import_olx
# 需要导入模块: from models.settings.course_metadata import CourseMetadata [as 别名]
# 或者: from models.settings.course_metadata.CourseMetadata import update_from_dict [as 别名]
#.........这里部分代码省略.........
# Send errors to client with stage at which error occurred.
except Exception as exception: # pylint: disable=broad-except
if course_dir.isdir(): # pylint: disable=no-value-for-parameter
shutil.rmtree(course_dir)
LOGGER.info(u'Course import %s: Temp data cleared', courselike_key)
LOGGER.exception(u'Error importing course %s', courselike_key, exc_info=True)
self.status.fail(text_type(exception))
return
# try-finally block for proper clean up after receiving file.
try:
tar_file = tarfile.open(temp_filepath)
try:
safetar_extractall(tar_file, (course_dir + u'/').encode(u'utf-8'))
except SuspiciousOperation as exc:
LOGGER.info(u'Course import %s: Unsafe tar file - %s', courselike_key, exc.args[0])
with respect_language(language):
self.status.fail(_(u'Unsafe tar file. Aborting import.'))
return
finally:
tar_file.close()
LOGGER.info(u'Course import %s: Uploaded file extracted', courselike_key)
self.status.set_state(u'Verifying')
self.status.increment_completed_steps()
# find the 'course.xml' file
def get_all_files(directory):
"""
For each file in the directory, yield a 2-tuple of (file-name,
directory-path)
"""
for directory_path, _dirnames, filenames in os.walk(directory):
for filename in filenames:
yield (filename, directory_path)
def get_dir_for_filename(directory, filename):
"""
Returns the directory path for the first file found in the directory
with the given name. If there is no file in the directory with
the specified name, return None.
"""
for name, directory_path in get_all_files(directory):
if name == filename:
return directory_path
return None
dirpath = get_dir_for_filename(course_dir, root_name)
if not dirpath:
with respect_language(language):
self.status.fail(_(u'Could not find the {0} file in the package.').format(root_name))
return
dirpath = os.path.relpath(dirpath, data_root)
LOGGER.debug(u'found %s at %s', root_name, dirpath)
LOGGER.info(u'Course import %s: Extracted file verified', courselike_key)
self.status.set_state(u'Updating')
self.status.increment_completed_steps()
with dog_stats_api.timer(
u'courselike_import.time',
tags=[u"courselike:{}".format(courselike_key)]
):
courselike_items = import_func(
modulestore(), user.id,
settings.GITHUB_REPO_ROOT, [dirpath],
load_error_modules=False,
static_content_store=contentstore(),
target_id=courselike_key
)
new_location = courselike_items[0].location
LOGGER.debug(u'new course at %s', new_location)
LOGGER.info(u'Course import %s: Course import successful', courselike_key)
except Exception as exception: # pylint: disable=broad-except
LOGGER.exception(u'error importing course', exc_info=True)
self.status.fail(text_type(exception))
finally:
if course_dir.isdir(): # pylint: disable=no-value-for-parameter
shutil.rmtree(course_dir)
LOGGER.info(u'Course import %s: Temp data cleared', courselike_key)
if self.status.state == u'Updating' and is_course:
# Reload the course so we have the latest state
course = modulestore().get_course(courselike_key)
if course.entrance_exam_enabled:
entrance_exam_chapter = modulestore().get_items(
course.id,
qualifiers={u'category': u'chapter'},
settings={u'is_entrance_exam': True}
)[0]
metadata = {u'entrance_exam_id': text_type(entrance_exam_chapter.location)}
CourseMetadata.update_from_dict(metadata, course, user)
from contentstore.views.entrance_exam import add_entrance_exam_milestone
add_entrance_exam_milestone(course.id, entrance_exam_chapter)
LOGGER.info(u'Course %s Entrance exam imported', course.id)