def delete(self): """need this because the JSONClientValidated cannot deal with the magic identifier""" magic_identifier = unquote(self.get_id().identifier) orig_identifier = magic_identifier.split('?')[0] collection = JSONClientValidated('assessment_authoring', collection='AssessmentPart', runtime=self.my_osid_object._runtime) collection.delete_one({'_id': ObjectId(orig_identifier)})
def has_provenance_children(self): collection = JSONClientValidated('repository', collection='Composition', runtime=self.my_osid_object._runtime) if collection.find({ 'provenanceId': self.my_osid_object.object_map['id'] }).count() > 0: return True else: return False
def get_provenance_children(self): if self.has_provenance_children(): collection = JSONClientValidated( 'repository', collection='Composition', runtime=self.my_osid_object._runtime) result = collection.find( {'provenanceId': self.my_osid_object.object_map['id']}) return AssetList(result, runtime=self.my_osid_object._runtime, proxy=self.my_osid_object._proxy) raise IllegalState('No provenance children.')
def has_provenance_children(self): """stub""" collection = JSONClientValidated('assessment', collection='AssessmentTaken', runtime=self.my_osid_object._runtime) if (collection.find({ 'provenanceId': self.my_osid_object.object_map['id'] }).count() > 0): return True else: return False
def get_provenance_children(self): """stub""" if self.has_provenance_children(): collection = JSONClientValidated( 'assessment', collection='AssessmentTaken', runtime=self.my_osid_object._runtime) result = collection.find( {'provenanceId': self.my_osid_object.object_map['id']}) return AssessmentTakenList(result, runtime=self.my_osid_object._runtime, proxy=self.my_osid_object._proxy) raise IllegalState('No provenance children.')
def get_provenance_parent(self): if self.has_provenance(): collection = JSONClientValidated( 'repository', collection='Composition', runtime=self.my_osid_object._runtime) result = collection.find_one({ '_id': ObjectId(Id(self.get_provenance_id()).get_identifier()) }) return Asset(osid_object_map=result, runtime=self.my_osid_object._runtime, proxy=self.my_osid_object._proxy) raise IllegalState("Composition has no provenance parent.")
def has_provenance_children(self): """stub""" collection = JSONClientValidated('assessment', collection='Item', runtime=self.my_osid_object._runtime) if (collection.find({ 'provenanceId': self.my_osid_object.object_map['id'] }).count() > 0 or collection.find( # for backwards compatibility { 'provenanceItemId': self.my_osid_object.object_map['id'] }).count() > 0): return True else: return False
def get_provenance_parent(self): """stub""" if self.has_provenance(): collection = JSONClientValidated( 'assessment', collection='Item', runtime=self.my_osid_object._runtime) result = collection.find_one({ '_id': ObjectId(Id(self.get_provenance_id()).get_identifier()) }) return Item(osid_object_map=result, runtime=self.my_osid_object._runtime, proxy=self.my_osid_object._proxy) raise IllegalState("Item has no provenance parent.")
def setUpClass(cls): cls.mgr = get_assessment_manager() cls.db = 'testing' cls.collection = 'json_client' cls.client = JSONClientValidated( cls.db, collection=cls.collection, runtime=cls.mgr._provider_manager._runtime)
def get_provenance_children(self): """stub""" if self.has_provenance_children(): collection = JSONClientValidated( 'assessment', collection='Item', runtime=self.my_osid_object._runtime) try: result = collection.find( {'provenanceId': self.my_osid_object.object_map['id']}) if result.count() == 0: raise KeyError except KeyError: # For deprecated mecqbank data result = collection.find( {'provenanceItemId': self.my_osid_object.object_map['id']}) return ItemList(result, runtime=self.my_osid_object._runtime, proxy=self.my_osid_object._proxy) raise IllegalState('No provenance children.')
def test_can_configure_mongo_indexes(self): import dlkit.runtime.configs from dlkit.runtime.configs import impl_key_dict,\ DLKIT_AUTHORITY, MONGO_HOST_URI, FILESYSTEM_ASSET_CONTENT_TYPE from dlkit.json_.utilities import JSONClientValidated index_to_test = 'genusTypeId' # assumes this will never be set in the config test_collection = JSONClientValidated( 'assessment', collection='Bank', runtime=self._bank._osid_object._runtime) current_indexes = test_collection.raw().index_information() index_keys = [v['key'][0][0] for k, v in current_indexes.items()] self.assertFalse(index_to_test in index_keys) dlkit.runtime.configs.TEST_JSON_1 = { 'id': 'mongo_configuration_1', 'displayName': 'Mongo Configuration', 'description': 'Configuration for Mongo Implementation', 'parameters': { 'implKey': impl_key_dict('json'), 'mongoDBNamePrefix': { 'syntax': 'STRING', 'displayName': 'Mongo DB Name Prefix', 'description': 'Prefix for naming mongo databases.', 'values': [{ 'value': 'test_dlkit_', 'priority': 1 }] }, 'authority': { 'syntax': 'STRING', 'displayName': 'Mongo Authority', 'description': 'Authority.', 'values': [{ 'value': DLKIT_AUTHORITY, 'priority': 1 }] }, 'mongoHostURI': { 'syntax': 'STRING', 'displayName': 'Mongo Host URI', 'description': 'URI for setting the MongoClient host.', 'values': [{ 'value': MONGO_HOST_URI, 'priority': 1 }] }, 'recordsRegistry': { 'syntax': 'STRING', 'displayName': 'Python path to the extension records registry file', 'description': 'dot-separated path to the extension records registry file', 'values': [{ 'value': 'dlkit.records.registry', 'priority': 1 }] }, 'repositoryProviderImpl': { 'syntax': 'STRING', 'displayName': 'Repository Provider Implementation', 'description': 'Implementation for repository service provider', 'values': [{ 'value': 'TEST_FILESYSTEM_ADAPTER_1', 'priority': 1 }] }, 'assetContentRecordTypeForFiles': { 'syntax': 'TYPE', 'displayName': 'Asset Content Type for Files', 'description': 'Asset Content Type for Records that store Files in a repository', 'values': [{ 'value': FILESYSTEM_ASSET_CONTENT_TYPE, 'priority': 1 }] }, 'magicItemLookupSessions': { 'syntax': 'STRING', 'displayName': 'Which magic item lookup sessions to try', 'description': 'To handle magic IDs.', 'values': [{ 'value': 'dlkit.records.adaptive.multi_choice_questions.randomized_questions.RandomizedMCItemLookupSession', 'priority': 1 }] }, 'magicAssessmentPartLookupSessions': { 'syntax': 'STRING', 'displayName': 'Which magic assessment part lookup sessions to try', 'description': 'To handle magic IDs.', 'values': [{ 'value': 'dlkit.records.adaptive.magic_parts.assessment_part_records.MagicAssessmentPartLookupSession', 'priority': 1 }] }, 'indexes': { 'syntax': 'OBJECT', 'displayName': 'Mongo DB Indexes', 'description': 'Indexes to set in MongoDB', 'values': [{ 'value': { 'assessment.Bank': [index_to_test] }, 'priority': 1 }] }, } } new_bank = self.create_new_bank() test_collection = JSONClientValidated( 'assessment', collection='Bank', runtime=new_bank._osid_object._runtime) current_indexes = test_collection.raw().index_information() index_keys = [v['key'][0][0] for k, v in current_indexes.items()] self.assertTrue(index_to_test in index_keys)
def load_item_for_objective(self): """if this is the first time for this magic part, find an LO linked item""" mgr = self.my_osid_object._get_provider_manager('ASSESSMENT', local=True) if self.my_osid_object._my_map['itemBankId']: item_query_session = mgr.get_item_query_session_for_bank( Id(self.my_osid_object._my_map['itemBankId']), proxy=self.my_osid_object._proxy) else: item_query_session = mgr.get_item_query_session( proxy=self.my_osid_object._proxy) item_query_session.use_federated_bank_view() item_query = item_query_session.get_item_query() for objective_id_str in self.my_osid_object._my_map[ 'learningObjectiveIds']: item_query.match_learning_objective_id(Id(objective_id_str), True) item_list = list(item_query_session.get_items_by_query(item_query)) # Let's query all takens and their children sections for questions, to # remove seen ones taking_agent_id = self._assessment_section._assessment_taken.taking_agent_id atqs = mgr.get_assessment_taken_query_session( proxy=self.my_osid_object._proxy) atqs.use_federated_bank_view() querier = atqs.get_assessment_taken_query() querier.match_taking_agent_id(taking_agent_id, match=True) # let's seed this with the current section's questions seen_items = [ item_id for item_id in self._assessment_section._item_id_list ] taken_ids = [ str(t.ident) for t in atqs.get_assessments_taken_by_query(querier) ] # Try to find the questions directly via Mongo query -- don't do # for section in taken._get_assessment_sections(): # seen_items += [question['itemId'] for question in section._my_map['questions']] # because standing up all the sections is wasteful collection = JSONClientValidated('assessment', collection='AssessmentSection', runtime=self.my_osid_object._runtime) results = collection.find({"assessmentTakenId": {"$in": taken_ids}}) for section in results: if 'questions' in section: seen_items += [ question['itemId'] for question in section['questions'] ] unseen_item_id = None # need to randomly shuffle this item_list shuffle(item_list) for item in item_list: if str(item.ident) not in seen_items: unseen_item_id = item.get_id() break if unseen_item_id is not None: self.my_osid_object._my_map['itemIds'] = [str(unseen_item_id)] elif self.my_osid_object._my_map['allowRepeatItems']: if len(item_list) > 0: self.my_osid_object._my_map['itemIds'] = [ str(item_list[0].ident) ] else: self.my_osid_object._my_map['itemIds'] = [ ] # don't put '' here, it will break when it tries to find an item with id '' else: self.my_osid_object._my_map['itemIds'] = [ ] # don't put '' here, it will break when it tries to find an item with id ''