本文整理汇总了Python中models.settings.course_metadata.CourseMetadata类的典型用法代码示例。如果您正苦于以下问题:Python CourseMetadata类的具体用法?Python CourseMetadata怎么用?Python CourseMetadata使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CourseMetadata类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_update_from_json
def test_update_from_json(self):
test_model = CourseMetadata.update_from_json(
self.course,
{
"advertised_start": {"value": "start A"},
"days_early_for_beta": {"value": 2},
},
user=self.user
)
self.update_check(test_model)
# try fresh fetch to ensure persistence
fresh = modulestore().get_course(self.course.id)
test_model = CourseMetadata.fetch(fresh)
self.update_check(test_model)
# now change some of the existing metadata
test_model = CourseMetadata.update_from_json(
fresh,
{
"advertised_start": {"value": "start B"},
"display_name": {"value": "jolly roger"},
},
user=self.user
)
self.assertIn('display_name', test_model, 'Missing editable metadata field')
self.assertEqual(test_model['display_name']['value'], 'jolly roger', "not expected value")
self.assertIn('advertised_start', test_model, 'Missing revised advertised_start metadata field')
self.assertEqual(test_model['advertised_start']['value'], 'start B', "advertised_start not expected value")
示例2: calendar_settings_handler
def calendar_settings_handler(request, package_id=None, branch=None, version_guid=None, block=None, tag=None):
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':
return render_to_response('settings_calendar.html', {
'package_id': package_id,
'context_course': course_module,
'advanced_dict': json.dumps(CourseMetadata.fetch(course_module)),
'advanced_settings_url': locator.url_reverse('settings/calendar')
})
elif 'application/json' in request.META.get('HTTP_ACCEPT', ''):
if request.method == 'GET':
return JsonResponse(CourseMetadata.fetch(course_module))
else:
# Whether or not to filter the tabs key out of the settings metadata
filter_tabs = _config_course_advanced_components(request, course_module)
try:
return JsonResponse(CourseMetadata.update_from_json(
course_module,
request.json,
filter_tabs=filter_tabs,
user=request.user,
))
except (TypeError, ValueError) as err:
return HttpResponseBadRequest(
"Incorrect setting format. {}".format(err),
content_type="text/plain"
)
示例3: test_contentstore_views_entrance_exam_get_bogus_exam
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: advanced_settings_handler
def advanced_settings_handler(request, package_id=None, branch=None, version_guid=None, block=None, tag=None):
"""
Course settings configuration
GET
html: get the page
json: get the model
PUT, POST
json: update the Course's settings. The payload is a json rep of the
metadata dicts. The dict can include a "unsetKeys" entry which is a list
of keys whose values to unset: i.e., revert to default
"""
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":
return render_to_response(
"settings_advanced.html",
{
"context_course": course_module,
"advanced_dict": json.dumps(CourseMetadata.fetch(course_module)),
"advanced_settings_url": locator.url_reverse("settings/advanced"),
},
)
elif "application/json" in request.META.get("HTTP_ACCEPT", ""):
if request.method == "GET":
return JsonResponse(CourseMetadata.fetch(course_module))
else:
# Whether or not to filter the tabs key out of the settings metadata
filter_tabs = _config_course_advanced_components(request, course_module)
try:
return JsonResponse(
CourseMetadata.update_from_json(course_module, request.json, filter_tabs=filter_tabs)
)
except (TypeError, ValueError) as err:
return HttpResponseBadRequest("Incorrect setting format. {}".format(err), content_type="text/plain")
示例5: course_audit_api
def course_audit_api(request, course_id, operation):
re_json = {"success": False}
request_method = request.method
if request_method != "POST":
return JsonResponse(re_json)
# get course location and module infomation
try:
course_location_info = course_id.split('.')
locator = BlockUsageLocator(package_id=course_id, branch='draft', version_guid=None, block_id=course_location_info[-1])
course_location = loc_mapper().translate_locator_to_location(locator)
course_module = get_modulestore(course_location).get_item(course_location)
instructors = CourseInstructorRole(locator).users_with_role()
if len(instructors) <= 0:
return JsonResponse(re_json)
user = instructors[0]
meta_json = {}
if operation == "pass":
meta_json["course_audit"] = 1
elif operation == "offline":
meta_json["course_audit"] = 0
else:
return JsonResponse(re_json)
re_json["success"] = True
CourseMetadata.update_from_json(course_module, meta_json, True, user)
return JsonResponse(re_json)
except:
return JsonResponse(re_json)
示例6: _delete_entrance_exam
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)
示例7: _delete_entrance_exam
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)
示例8: test_validate_and_update_from_json_wrong_inputs
def test_validate_and_update_from_json_wrong_inputs(self):
# input incorrectly formatted data
is_valid, errors, test_model = CourseMetadata.validate_and_update_from_json(
self.course,
{
"advertised_start": {"value": 1, "display_name": "Course Advertised Start Date", },
"days_early_for_beta": {"value": "supposed to be an integer",
"display_name": "Days Early for Beta Users", },
"advanced_modules": {"value": 1, "display_name": "Advanced Module List", },
},
user=self.user
)
# Check valid results from validate_and_update_from_json
self.assertFalse(is_valid)
self.assertEqual(len(errors), 3)
self.assertFalse(test_model)
error_keys = set([error_obj['model']['display_name'] for error_obj in errors])
test_keys = set(['Advanced Module List', 'Course Advertised Start Date', 'Days Early for Beta Users'])
self.assertEqual(error_keys, test_keys)
# try fresh fetch to ensure no update happened
fresh = modulestore().get_course(self.course.id)
test_model = CourseMetadata.fetch(fresh)
self.assertNotEqual(test_model['advertised_start']['value'], 1, 'advertised_start should not be updated to a wrong value')
self.assertNotEqual(test_model['days_early_for_beta']['value'], "supposed to be an integer",
'days_early_for beta should not be updated to a wrong value')
示例9: update_entrance_exam
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)
示例10: _create_entrance_exam
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)
示例11: test_fetch_initial_fields
def test_fetch_initial_fields(self):
test_model = CourseMetadata.fetch(self.course.location)
self.assertIn("display_name", test_model, "Missing editable metadata field")
self.assertEqual(test_model["display_name"], "Robot Super Course", "not expected value")
test_model = CourseMetadata.fetch(self.fullcourse_location)
self.assertNotIn("graceperiod", test_model, "blacklisted field leaked in")
self.assertIn("display_name", test_model, "full missing editable metadata field")
self.assertEqual(test_model["display_name"], "Robot Super Course", "not expected value")
self.assertIn("rerandomize", test_model, "Missing rerandomize metadata field")
self.assertIn("showanswer", test_model, "showanswer field ")
self.assertIn("xqa_key", test_model, "xqa_key field ")
示例12: test_fetch_initial_fields
def test_fetch_initial_fields(self):
test_model = CourseMetadata.fetch(self.course)
self.assertIn('display_name', test_model, 'Missing editable metadata field')
self.assertEqual(test_model['display_name']['value'], self.course.display_name)
test_model = CourseMetadata.fetch(self.fullcourse)
self.assertNotIn('graceperiod', test_model, 'blacklisted field leaked in')
self.assertIn('display_name', test_model, 'full missing editable metadata field')
self.assertEqual(test_model['display_name']['value'], self.fullcourse.display_name)
self.assertIn('rerandomize', test_model, 'Missing rerandomize metadata field')
self.assertIn('showanswer', test_model, 'showanswer field ')
self.assertIn('xqa_key', test_model, 'xqa_key field ')
示例13: _create_entrance_exam
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)
示例14: test_import_delete_pre_exiting_entrance_exam
def test_import_delete_pre_exiting_entrance_exam(self):
"""
Check that pre existed entrance exam content should be overwrite with the imported course.
"""
exam_url = '/course/{}/entrance_exam/'.format(unicode(self.course.id))
resp = self.client.post(exam_url, {'entrance_exam_minimum_score_pct': 0.5}, http_accept='application/json')
self.assertEqual(resp.status_code, 201)
# Reload the test course now that the exam module has been added
self.course = modulestore().get_course(self.course.id)
metadata = CourseMetadata.fetch_all(self.course)
self.assertTrue(metadata['entrance_exam_enabled'])
self.assertIsNotNone(metadata['entrance_exam_minimum_score_pct'])
self.assertEqual(metadata['entrance_exam_minimum_score_pct']['value'], 0.5)
self.assertTrue(len(milestones_helpers.get_course_milestones(unicode(self.course.id))))
content_milestones = milestones_helpers.get_course_content_milestones(
unicode(self.course.id),
metadata['entrance_exam_id']['value'],
milestones_helpers.get_milestone_relationship_types()['FULFILLS']
)
self.assertTrue(len(content_milestones))
# Now import entrance exam course
with open(self.entrance_exam_tar) as gtar:
args = {"name": self.entrance_exam_tar, "course-data": [gtar]}
resp = self.client.post(self.url, args)
self.assertEquals(resp.status_code, 200)
course = self.store.get_course(self.course.id)
self.assertIsNotNone(course)
self.assertEquals(course.entrance_exam_enabled, True)
self.assertEquals(course.entrance_exam_minimum_score_pct, 0.7)
示例15: test_update_from_json
def test_update_from_json(self):
test_model = CourseMetadata.update_from_json(
self.course.location,
{"advertised_start": "start A", "testcenter_info": {"c": "test"}, "days_early_for_beta": 2},
)
self.update_check(test_model)
# try fresh fetch to ensure persistence
test_model = CourseMetadata.fetch(self.course.location)
self.update_check(test_model)
# now change some of the existing metadata
test_model = CourseMetadata.update_from_json(
self.course.location, {"advertised_start": "start B", "display_name": "jolly roger"}
)
self.assertIn("display_name", test_model, "Missing editable metadata field")
self.assertEqual(test_model["display_name"], "jolly roger", "not expected value")
self.assertIn("advertised_start", test_model, "Missing revised advertised_start metadata field")
self.assertEqual(test_model["advertised_start"], "start B", "advertised_start not expected value")