def clean_id(_id): """ Django seems to un-url-safe the IDs passed in to the rest framework views, so we need to url-safe them, then convert them to OSID IDs """ if _id.find('@') >= 0: return Id(quote(_id)) else: return Id(_id)
def create_asset_with_content(self): form = self._repo.get_asset_form_for_create([]) form.display_name = 'My new asset' form.description = 'Asset container for' new_asset = self._repo.create_asset(form) asset_content_types = [] try: config = self._repo._osid_object._runtime.get_configuration() parameter_id = Id('parameter:assetContentRecordTypeForFiles@mongo') asset_content_types.append( config.get_value_by_parameter(parameter_id).get_type_value()) except AttributeError: pass content_form = self._repo.get_asset_content_form_for_create( new_asset.ident, asset_content_types) blob = DataInputStream(self.test_file1) content_form.set_data(blob) self._repo.create_asset_content(content_form) asset = self._repo.get_asset(new_asset.ident) return asset
def setup_composition(self, repository_id): repo = self.get_repo(repository_id) asset_form = repo.get_asset_form_for_create([]) asset_form.display_name = 'test' asset_form.description = 'ing' new_asset = repo.create_asset(asset_form) # now add the new data asset_content_type_list = [] try: config = repo._osid_object._runtime.get_configuration() parameter_id = Id('parameter:assetContentRecordTypeForFiles@mongo') asset_content_type_list.append( config.get_value_by_parameter(parameter_id).get_type_value()) except AttributeError: pass asset_content_form = repo.get_asset_content_form_for_create( new_asset.ident, asset_content_type_list) asset_content_form.set_data(DataInputStream(self.test_file1)) repo.create_asset_content(asset_content_form) form = repo.get_composition_form_for_create([]) form.display_name = 'my test composition' form.description = 'foobar' form.set_children([new_asset.ident]) composition = repo.create_composition(form) return composition.object_map
def POST(self): """ Create a new log, if authorized """ try: logm = logutils.get_logging_manager() if 'bankId' not in self.data(): utilities.verify_keys_present(self.data(), ['name', 'description']) form = logm.get_log_form_for_create([]) finalize_method = logm.create_log else: log = logm.get_log(Id(self.data()['bankId'])) form = logm.get_log_form_for_update(log.ident) finalize_method = logm.update_log if 'name' in self.data(): form.display_name = self.data()['name'] if 'description' in self.data(): form.description = self.data()['description'] if 'genusTypeId' in self.data(): form.set_genus_type(Type(self.data()['genusTypeId'])) new_log = utilities.convert_dl_object(finalize_method(form)) return new_log except Exception as ex: utilities.handle_exceptions(ex)
def setup_asset(self, repository_id): test_file = '/functional/files/test_file_1.txt' repo = self.get_repo(repository_id) asset_form = repo.get_asset_form_for_create([]) asset_form.display_name = 'test' asset_form.description = 'ing' new_asset = repo.create_asset(asset_form) # now add the new data asset_content_type_list = [] try: config = repo._runtime.get_configuration() parameter_id = Id('parameter:assetContentRecordTypeForFiles@json') asset_content_type_list.append( config.get_value_by_parameter(parameter_id).get_type_value()) except AttributeError: pass asset_content_form = repo.get_asset_content_form_for_create( new_asset.ident, asset_content_type_list) self.default_asset_file = ABS_PATH + test_file with open(self.default_asset_file, 'r') as file_: asset_content_form.set_data(DataInputStream(file_)) repo.create_asset_content(asset_content_form) new_asset = repo.get_asset(new_asset.ident) return new_asset.object_map
def test_can_add_comment_to_response(self): first_section = self._bank.get_first_assessment_section(self._taken.ident) response = self._bank.get_response(first_section.ident, self._item.ident) comment_type = [FILE_COMMENT_RECORD_TYPE] book = self.get_book(self._bank.ident) form = book.get_comment_form_for_create(response.ident, comment_type) form.set_text('Good job') form.set_file(DataInputStream(self.test_file1), asset_name='my comments') new_comment = book.create_comment(form) data = new_comment.object_map self.assertEqual( data['text']['text'], 'Good job' ) self.assertIn( 'fileId', data ) repo = self.get_repo(book.ident) asset = repo.get_asset(Id(data['fileId']['assetId'])) self.assertIn( '/stream', asset.object_map['assetContents'][0]['url'] )
def test_clear_match_composition_descendants(self): rm = get_repository_manager() form = rm.get_repository_form_for_create([]) form.display_name = 'test repo' repo = rm.create_repository(form) composition_id = Id( 'package.Resource%3A000000000000000000000000%40ODL.MIT.EDU') query = QueryWrapper(runtime=rm._provider_manager._runtime) composition_query = EdXCompositionQueryRecord(query) composition_query.match_composition_descendants( composition_id, repo.ident, True) assert '_id' in composition_query._my_osid_query._query_terms assert composition_query._my_osid_query._query_terms['_id'] == { '$in': [ObjectId('000000000000000000000000')] } composition_query.clear_match_composition_descendants() assert '_id' not in composition_query._my_osid_query._query_terms rm.delete_repository(repo.ident)
def test_confirm_runtime_matches_test_config(self): from dlkit.runtime.configs import TEST_JSON_1 authority_param_id = Id('parameter:authority@json') computed_authority = self._bank._osid_object._runtime.get_configuration( ).get_value_by_parameter(authority_param_id).get_string_value() self.assertEqual( computed_authority, TEST_JSON_1['parameters']['authority']['values'][0]['value'])
def test_can_create_composition_with_asset_child(self): composition_map = self.setup_composition(self._repo.ident) asset_child_id = composition_map['childIds'][0] asset = self._repo.get_asset(Id(asset_child_id)) self.assertEqual(asset.get_asset_contents().available(), 1) self.is_streamable_url(asset.get_asset_contents().next().get_url())
def get_repo(self, repo_id): rm = get_manager(self.req, 'repository') if isinstance(repo_id, str): repo_id = Id(repo_id) repo = rm.get_repository(repo_id) add_user_authz_to_settings('instructor', self.username, catalog_id=repo.ident) add_user_authz_to_settings('student', self.student_name, catalog_id=repo.ident) return repo
def get_book(self, book_id): cm = get_manager(self.req, 'commenting') if is_string(book_id): book_id = Id(book_id) book = cm.get_book(book_id) add_user_authz_to_settings('instructor', self.username, catalog_id=book.ident) add_user_authz_to_settings('student', self.student_name, catalog_id=book.ident) return book
def test_can_create_orchestrated_log_with_default_attributes(self): self.num_logs(0) payload = {'bankId': str(self.assessment_bank.ident)} req = self.app.post(self.url, params=json.dumps(payload), headers={'content-type': 'application/json'}) self.ok(req) log = self.json(req) self.assertEqual(log['displayName']['text'], 'Orchestrated assessment Log') self.assertEqual(log['description']['text'], 'Orchestrated assessment Log') self.assertEqual(self.assessment_bank.ident.identifier, Id(log['id']).identifier) self.num_logs(1)
def test_can_create_orchestrated_log_and_set_attributes(self): payload = { 'bankId': str(self.assessment_bank.ident), 'name': 'my new orchestra', 'description': 'for my assessment bank' } req = self.app.post(self.url, params=json.dumps(payload), headers={'content-type': 'application/json'}) self.ok(req) log = self.json(req) self.assertEqual(log['displayName']['text'], payload['name']) self.assertEqual(log['description']['text'], payload['description']) self.assertEqual(self.assessment_bank.ident.identifier, Id(log['id']).identifier)
def add_item(self, bank): form = bank.get_item_form_for_create([]) form.display_name = 'a test item!' form.description = 'for testing with' form.set_learning_objectives([Id(self._lo)]) new_item = bank.create_item(form) question_form = bank.get_question_form_for_create(new_item.ident, []) question_form.display_name = 'Question for ' + new_item.display_name.text question_form.description = '' new_question = bank.create_question(question_form) answer_form = bank.get_answer_form_for_create(new_item.ident, []) answer_form.display_name = 'Answer for ' + new_item.display_name.text new_answer = bank.create_answer(answer_form) item = bank.get_item(new_item.ident) return item
def post(self, request, format=None): """ Create a new bin, if authorized """ try: if 'bankId' not in self.data: gutils.verify_keys_present(self.data, ['displayName', 'description']) form = self.gm.get_gradebook_form_for_create([]) finalize_method = self.gm.create_gradebook else: gradebook = self.gm.get_gradebook(Id(self.data['bankId'])) form = self.gm.get_gradebook_form_for_update(gradebook.ident) finalize_method = self.gm.update_gradebook form = gutils.set_form_basics(form, self.data) new_gradebook = gutils.convert_dl_object(finalize_method(form)) return gutils.CreatedResponse(new_gradebook) except (PermissionDenied, InvalidArgument, NotFound, KeyError) as ex: gutils.handle_exceptions(ex)
def create_item(self, bank, with_lo=False): form = bank.get_item_form_for_create([]) form.display_name = 'a test item!' form.description = 'for testing with' if with_lo: self._lo = 'mc3-objective%3A9729%40MIT-OEIT' form.set_learning_objectives([Id(self._lo)]) new_item = bank.create_item(form) question_form = bank.get_question_form_for_create(new_item.ident, []) question_form.display_name = 'Question for ' + new_item.display_name.text question_form.description = '' bank.create_question(question_form) answer_form = bank.get_answer_form_for_create(new_item.ident, []) answer_form.display_name = 'Answer for ' + new_item.display_name.text bank.create_answer(answer_form) item = bank.get_item(new_item.ident) return item
def get_bank(self, bank_id): if not isinstance(bank_id, Id): bank_id = Id(bank_id) am = gutils.get_session_data(self.req, 'am') return am.get_bank(bank_id)
def convert_to_osid_id(id): if isinstance(id, basestring): return Id(id) else: return id
def extract_items(request, a_list, bank=None, section=None): from .assessment import get_question_status # import here to prevent circular imports results = { '_links': { 'self' : build_safe_uri(request) }, 'data' : [] } if (not isinstance(a_list, list) and not isinstance(a_list, abc_type_objects.TypeList) and not isinstance(a_list, abc_assessment_objects.AssessmentList) and not isinstance(a_list, abc_assessment_objects.BankList) and not isinstance(a_list, abc_assessment_objects.ItemList) and not isinstance(a_list, abc_assessment_objects.AnswerList) and not isinstance(a_list, abc_assessment_objects.QuestionList) and not isinstance(a_list, abc_assessment_objects.AssessmentOfferedList) and not isinstance(a_list, abc_assessment_objects.AssessmentTakenList) and not isinstance(a_list, abc_assessment_objects.ResponseList) and not isinstance(a_list, abc_repository_objects.RepositoryList) and not isinstance(a_list, abc_repository_objects.AssetList) and not isinstance(a_list, abc_repository_objects.CompositionList) and not isinstance(a_list, abc_resource_objects.BinList) and not isinstance(a_list, abc_resource_objects.ResourceList) and not isinstance(a_list, abc_grading_objects.GradebookList) and not isinstance(a_list, abc_grading_objects.GradeSystemList) and not isinstance(a_list, abc_grading_objects.GradebookColumnList) and not isinstance(a_list, abc_grading_objects.GradeEntryList) and not isinstance(a_list, abc_learning_objects.ObjectiveList) and not isinstance(a_list, abc_learning_objects.ObjectiveBankList)): a_list = [a_list] try: list_len = a_list.available() except AttributeError: list_len = len(a_list) if list_len > 0: paginated = paginate(list(a_list), request) #for item in a_list: results.update({ 'data': paginated }) for index, item in enumerate(paginated['results']): # for questions, need to add in their status if (isinstance(item, abc_assessment_objects.Question) or (isinstance(item, dict) and 'Question' in item['type'])): if isinstance(item, dict): item_id = Id(item['id']) else: item_id = item.ident status = get_question_status(bank, section, item_id) # item_json.update(status) results['data']['results'][index].update(status) # results['data'].append(item_json) root_url_base = append_slash( request.build_absolute_uri().split('?')[0].replace('/query', '')) root_url_offered_or_taken = append_slash( request.build_absolute_uri().split('?page')[0]) #for item in serialized_data['results']: for index, item in enumerate(paginated['results']): item_id = item['id'] # make assessment offerings point two levels back, to just # <bank_id>/offerings/<offering_id> if (isinstance(item, abc_assessment_objects.AssessmentOffered) or item['type'] == 'AssessmentOffered'): results['data']['results'][index]['_link'] = root_url_offered_or_taken + \ '../../../assessmentsoffered/' + \ my_unquote(item_id) + '/' elif (isinstance(item, abc_assessment_objects.AssessmentTaken) or item['type'] == 'AssessmentTaken'): results['data']['results'][index]['_link'] = root_url_offered_or_taken + \ '../../../assessmentstaken/' + \ my_unquote(item_id) + '/' elif ((isinstance(item, abc_repository_objects.Asset) or item['type'] == 'Asset') and '/compositions/' in root_url_base): results['data']['results'][index]['_link'] = root_url_base + '../../../assets/' + \ my_unquote(item_id) + '/' elif ((isinstance(item, abc_grading_objects.GradeEntry) or item['type'] == 'GradeEntry') and '/columns/' in root_url_base): results['data']['results'][index]['_link'] = '{0}../../../entries/{1}/'.format(root_url_base, my_unquote(item_id)) else: results['data']['results'][index]['_link'] = root_url_base + my_unquote(item_id) + '/' else: results['data'] = {'count': 0, 'next': None, 'results': [], 'previous': None} return results
def test_can_get_null_results_of_asset_contents_by_ids(self): asset_contents = self._repo.get_asset_contents_by_ids( [Id('foo%3A111111111111111111111111%40ODL.MIT.EDU')]) self.assertEqual(asset_contents.available(), 0)
def create_agent_id(username, authority='MIT-ODL'): return Id(identifier=username, namespace='osid.agent.Agent', authority=authority)
def construct_qti_id(qti_id, namespace='assessment.Item'): return Id(identifier=qti_id, namespace=namespace, authority='QTI.IMS.COM')