def setUp(self): init_annalist_test_site() self.testsite = Site(TestBaseUri, TestBaseDir) self.testcoll = Collection.create(self.testsite, "testcoll", collection_create_values("testcoll")) self.testtype = RecordType.create( self.testcoll, "testtype", recordtype_create_values("testcoll", "testtype")) self.testtype2 = RecordType.create( self.testcoll, "testtype2", recordtype_create_values("testcoll", "testtype2")) self.testdata = RecordTypeData.create(self.testcoll, "testtype", {}) self.testdata2 = RecordTypeData.create(self.testcoll, "testtype2", {}) e1 = self._create_entity_data("entity1") e2 = self._create_entity_data("entity2") e3 = self._create_entity_data("entity3") e4 = EntityData.create( self.testdata2, "entity4", entitydata_create_values("entity4", type_id="testtype2")) self.type_ids = get_site_types_linked("testcoll") self.type_ids.append( FieldChoice("testtype", label="RecordType testcoll/testtype", link=recordtype_url("testcoll", "testtype"))) self.list_ids = get_site_lists_linked("testcoll") # Login and permissions create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) return
def test_post_edit_type_new_id(self): # Check logic applied when type is renamed (t, d1, e1) = self._create_record_type("edittype1", entity_id="typeentity") self.assertTrue(RecordType.exists(self.testcoll, "edittype1")) self.assertFalse(RecordType.exists(self.testcoll, "edittype2")) self.assertTrue(RecordTypeData.exists(self.testcoll, "edittype1")) self.assertFalse(RecordTypeData.exists(self.testcoll, "edittype2")) self.assertTrue(EntityData.exists(d1, "typeentity")) self._check_record_type_values("edittype1") f = recordtype_entity_view_form_data( type_id="edittype2", orig_id="edittype1", action="edit", update="Updated RecordType" ) u = entitydata_edit_url( "edit", "testcoll", layout.TYPE_TYPEID, entity_id="edittype1", view_id="Type_view" ) r = self.client.post(u, f) self.assertEqual(r.status_code, 302) self.assertEqual(r.reason_phrase, "FOUND") self.assertEqual(r.content, "") self.assertEqual(r['location'], self.continuation_url) # Check that new record type exists and old does not self.assertFalse(RecordType.exists(self.testcoll, "edittype1")) self.assertTrue(RecordType.exists(self.testcoll, "edittype2")) self._check_record_type_values("edittype2", update="Updated RecordType") # Check that type data directory has been renamed self.assertFalse(RecordTypeData.exists(self.testcoll, "edittype1")) self.assertTrue(RecordTypeData.exists(self.testcoll, "edittype2")) self.assertFalse(EntityData.exists(d1, "typeentity")) d2 = RecordTypeData.load(self.testcoll, "edittype2") self.assertTrue(EntityData.exists(d2, "typeentity")) return
def test_post_edit_entity_new_type(self): self._create_entity_data("entityedittype") e1 = self._check_entity_data_values("entityedittype") self.assertFalse(RecordType.exists(self.testcoll, "newtype")) newtype = RecordType.create(self.testcoll, "newtype", recordtype_create_values("newtype")) newtypedata = RecordTypeData(self.testcoll, "newtype") self.assertTrue(RecordType.exists(self.testcoll, "newtype")) self.assertFalse(RecordTypeData.exists(self.testcoll, "newtype")) # Now post edit form submission with new type id f = default_view_form_data( entity_id="entityedittype", orig_id="entityedittype", type_id="newtype", orig_type="testtype", action="edit") u = entitydata_edit_url("edit", "testcoll", "testtype", entity_id="entityedittype") r = self.client.post(u, f) # log.info("***********\n"+r.content) self.assertEqual(r.status_code, 302) self.assertEqual(r.reason_phrase, "Found") self.assertEqual(r.content, b"") self.assertEqual(r['location'], entitydata_list_type_url("testcoll", "testtype")) # Check that new record type data now exists, and that new record exists and old does not self.assertTrue(RecordTypeData.exists(self.testcoll, "newtype")) self.assertFalse(EntityData.exists(self.testdata, "entityedittype")) self.assertTrue(EntityData.exists(newtypedata, "entityedittype")) self._check_entity_data_values("entityedittype", type_id="newtype") return
def coll123_create_data(site): coll1 = Collection.create(site, "coll1", collection_create_values("coll1")) coll2 = Collection.create(site, "coll2", collection_create_values("coll2")) coll3 = Collection.create(site, "coll3", collection_create_values("coll3")) # for coll in [coll1, coll2, coll3]: type1 = RecordType.create( coll, "type1", recordtype_create_values(coll._entityid, "type1")) view1 = RecordView.create( coll, "view1", recordview_create_values(coll._entityid, "view1")) list1 = RecordList.create( coll, "list1", recordlist_create_values(coll._entityid, "list1")) data1 = RecordTypeData.create(coll, "type1", {}) type2 = RecordType.create( coll, "type2", recordtype_create_values(coll._entityid, "type2")) view2 = RecordView.create( coll, "view2", recordview_create_values(coll._entityid, "view2")) list2 = RecordList.create( coll, "list2", recordlist_create_values(coll._entityid, "list2")) data2 = RecordTypeData.create(coll, "type2", {}) # for t, d in [(type1, data1), (type2, data2)]: for eid in ["entity1", "entity2", "entity3"]: e = EntityData.create(d, eid, entitydata_create_values(coll, t, eid)) return
def setUp(self): init_annalist_test_site() self.testsite = Site(TestBaseUri, TestBaseDir) self.testcoll = Collection.create(self.testsite, "testcoll", collection_create_values("testcoll")) self.testtype = RecordType.create(self.testcoll, "testtype", recordtype_create_values("testcoll", "testtype")) self.testtype2 = RecordType.create(self.testcoll, "testtype2", recordtype_create_values("testcoll", "testtype2")) self.testdata = RecordTypeData.create(self.testcoll, "testtype", {}) self.testdata2 = RecordTypeData.create(self.testcoll, "testtype2", {}) e1 = self._create_entity_data("entity1") e2 = self._create_entity_data("entity2") e3 = self._create_entity_data("entity3") e4 = EntityData.create(self.testdata2, "entity4", entitydata_create_values("entity4", type_id="testtype2") ) self.type_ids = get_site_types_linked("testcoll") self.type_ids.append(FieldChoice("_type/testtype", label="RecordType testcoll/_type/testtype", link=recordtype_url("testcoll", "testtype") )) self.type_ids.append(FieldChoice("_type/testtype2", label="RecordType testcoll/_type/testtype2", link=recordtype_url("testcoll", "testtype2") )) self.list_ids = get_site_lists_linked("testcoll") # Login and permissions create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) return
def test_post_delete_type_entity(self): testtypedelete = RecordType.create(self.testcoll, "testtypedelete", recordtype_create_values("testcoll", "testtypedelete")) testdatadelete = RecordTypeData.create(self.testcoll, "testtypedelete", {}) f = entitylist_form_data("delete", entities=["_type/testtypedelete"]) u = entitydata_list_type_url("testcoll", "_type", list_id="Type_list") r = self.client.post(u, f) self.assertEqual(r.status_code, 200) self.assertEqual(r.reason_phrase, "OK") self.assertContains(r, "<h3>Confirm requested action</h3>") self.assertContains(r, "Remove record testtypedelete of type _type in collection testcoll: Are you sure?") self.assertContains(r, 'Click "Confirm" to continue, or "Cancel" to abort operation') self.assertContains(r, '<input type="hidden" name="confirmed_action" value="/testsite/c/testcoll/d/_type/!delete_confirmed"/>', html=True ) self.assertEqual(r.context['action_description'], 'Remove record testtypedelete of type _type in collection testcoll') self.assertEqual(r.context['confirmed_action'], '/testsite/c/testcoll/d/_type/!delete_confirmed') self.assertEqual(r.context['action_params'], confirm_delete_params(button_id="entity_delete", entity_id="testtypedelete", type_id="_type", list_id="Type_list") ) self.assertEqual(r.context['cancel_action'], '/testsite/c/testcoll/l/Type_list/_type/') return
def create_test_coll_inheriting(base_coll_id=None, coll_id="testcoll", type_id="testtype"): """ Similar to init_annalist_test_coll, but collection also inherits from named collection. """ testsite = Site(TestBaseUri, TestBaseDir) basecoll = Collection.load(testsite, base_coll_id) if not basecoll: msg = "Base collection %s not found" % base_coll_id log.warning(msg) assert False, msg testcoll = Collection.create(testsite, coll_id, collection_create_values(coll_id)) testcoll.set_alt_entities(basecoll) testcoll._save() testtype = RecordType.create(testcoll, type_id, recordtype_create_values(coll_id, type_id)) testdata = RecordTypeData.create(testcoll, type_id, {}) teste = EntityData.create( testdata, "entity1", entitydata_create_values(testcoll, testtype, "entity1")) testcoll.generate_coll_jsonld_context() return testcoll
def init_annalist_named_test_coll(base_coll_id=None, coll_id="testcoll", type_id="testtype"): """ Similar to init_annalist_test_coll, but collection also installs and inherits from named collection definitions. """ # @@TODO: DRY: use create_test_coll_inheriting # @@TODO: rename: install_create_test_coll_inheriting log.debug("init_annalist_named_test_coll") testsite = Site(TestBaseUri, TestBaseDir) namedcoll = install_annalist_named_coll(base_coll_id) testcoll = Collection.create(testsite, coll_id, collection_create_values(coll_id)) testcoll.set_alt_entities(namedcoll) testcoll._save() testtype = RecordType.create(testcoll, type_id, recordtype_create_values(coll_id, type_id)) testdata = RecordTypeData.create(testcoll, type_id, {}) teste = EntityData.create( testdata, "entity1", entitydata_create_values(testcoll, testtype, "entity1")) testcoll.generate_coll_jsonld_context() return testcoll
def _create_record_type(self, type_id, entity_id="testentity"): "Helper function creates record type entry with supplied type_id" t = RecordType.create(self.testcoll, type_id, recordtype_create_values(type_id=type_id)) d = RecordTypeData.create(self.testcoll, type_id, {}) e = EntityData.create(d, entity_id, {}) return (t, d, e)
def setUp(self): init_annalist_test_site() self.testsite = Site(TestBaseUri, TestBaseDir) self.testcoll = Collection.create(self.testsite, "testcoll", collection_create_values("testcoll")) self.testtype = RecordType.create(self.testcoll, "testtype", recordtype_create_values("testtype")) self.testdata = RecordTypeData.create(self.testcoll, "testtype", {}) # Create view with duplicate field id self.viewdata = recordview_create_values(view_id="DupField_view") recordview_values_add_field( self.viewdata, field_id="Entity_comment", field_placement="small:0,12" ) recordview_values_add_field( self.viewdata, field_id="Entity_comment", field_property_uri="rdfs:comment_alt", field_placement="small:0,12" ) self.testview = RecordView.create(self.testcoll, "DupField_view", self.viewdata) # Login and permissions create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) return
def coll123_create_data(site): coll1 = Collection.create(site, "coll1", collection_create_values("coll1")) coll2 = Collection.create(site, "coll2", collection_create_values("coll2")) coll3 = Collection.create(site, "coll3", collection_create_values("coll3")) # for coll in [coll1, coll2, coll3]: type1 = RecordType.create(coll, "type1", recordtype_create_values(coll._entityid, "type1")) view1 = RecordView.create(coll, "view1", recordview_create_values(coll._entityid, "view1")) list1 = RecordList.create(coll, "list1", recordlist_create_values(coll._entityid, "list1")) data1 = RecordTypeData.create(coll, "type1", {}) type2 = RecordType.create(coll, "type2", recordtype_create_values(coll._entityid, "type2")) view2 = RecordView.create(coll, "view2", recordview_create_values(coll._entityid, "view2")) list2 = RecordList.create(coll, "list2", recordlist_create_values(coll._entityid, "list2")) data2 = RecordTypeData.create(coll, "type2", {}) # for t,d in [(type1,data1),(type2,data2)]: for eid in ["entity1", "entity2", "entity3"]: e = EntityData.create(d, eid, entitydata_create_values(coll,t,eid)) return
def create_data(self, entity_id): # Create placeholder for testing typedata = RecordTypeData.create(self.testcoll, "Default_type", {}) assert typedata is not None e = EntityData.create( typedata, entity_id, entitydata_create_values(entity_id, coll_id="testcoll", type_id="Default_type")) return e
def setUp(self): init_annalist_test_site() self.testsite = Site(TestBaseUri, TestBaseDir) self.testcoll = Collection(self.testsite, "testcoll") self.testdata = RecordTypeData(self.testcoll, "testtype") # Login and permissions create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) return
def setUp(self): self.testsite = init_annalist_test_site() self.testcoll = init_annalist_named_test_coll(layout.BIBDATA_ID) self.testdata = RecordTypeData.load(self.testcoll, "testtype") self.testtype2 = RecordType.create( self.testcoll, "testtype2", recordtype_create_values("testcoll", "testtype2") ) self.testdata2 = RecordTypeData.create(self.testcoll, "testtype2", {}) create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) e1 = self._create_entity_data("entity1") e2 = self._create_entity_data("entity2") e3 = self._create_entity_data("entity3") e4 = EntityData.create(self.testdata2, "entity4", entitydata_create_values("entity4", type_id="testtype2") ) self.list_ids = get_site_bib_lists_linked("testcoll") return
def setUp(self): self.testsite = init_annalist_test_site() self.testcoll = init_annalist_named_test_coll(layout.BIBDATA_ID) self.testdata = RecordTypeData.load(self.testcoll, "testtype") self.testtype2 = RecordType.create( self.testcoll, "testtype2", recordtype_create_values("testcoll", "testtype2") ) self.testdata2 = RecordTypeData.create(self.testcoll, "testtype2", {}) create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) e1 = self._create_entity_data("entity1") e2 = self._create_entity_data("entity2") e3 = self._create_entity_data("entity3") e4 = EntityData.create(self.testdata2, "entity4", entitydata_create_values("entity4", type_id="testtype2") ) self.list_ids = get_site_bib_lists_linked("testcoll") return
def migrate_collection_dir(coll, prev_dir, curr_dir): """ Migrate (rename) a single directory belonging to the indicated collection. Returns list of errors or empty list. """ errs = [] if not prev_dir: return errs coll_base_dir, coll_meta_file = coll._dir_path() # log.debug( # "collectiondata.migrate_collection_dir %s: %s -> %s"% # (coll_base_dir, prev_dir, curr_dir) # ) expand_prev_dir = os.path.join(coll_base_dir, prev_dir) expand_curr_dir = os.path.join(coll_base_dir, curr_dir) # log.debug(" prev %s"%(expand_prev_dir,)) # log.debug(" curr %s"%(expand_curr_dir,)) if (curr_dir != prev_dir) and os.path.isdir(expand_prev_dir): log.info("migrate_coll_base_dir: %s"%coll_base_dir) log.info(" rename: %s -> %s"%(prev_dir, curr_dir)) # print "@@ rename %s -> %s"%(expand_prev_dir, expand_curr_dir) try: os.rename(expand_prev_dir, expand_curr_dir) except Exception as e: msg = (message.COLL_MIGRATE_DIR_FAILED% { "id": coll.get_id() , "old_path": prev_dir, "new_path": curr_dir , "exc": e } ) log.error("migrate_collection_dir: "+msg) errs.append(msg) # Create type data container for site types (so it can be enumerated later) if curr_dir in TYPE_CLASS_MAP: if not RecordTypeData.exists(coll, curr_dir): log.info("Create RecordTypeData for %s"%curr_dir) typedata = RecordTypeData.create(coll, curr_dir, {}) return errs
def setUp(self): init_annalist_test_site() self.testsite = Site(TestBaseUri, TestBaseDir) self.testcoll = Collection.create(self.testsite, "testcoll", collection_create_values("testcoll")) self.testtype = RecordType.create(self.testcoll, "testtype", recordtype_create_values("testcoll", "testtype")) self.testtype2 = RecordType.create(self.testcoll, "testtype2", recordtype_create_values("testcoll", "testtype2")) self.testdata = RecordTypeData.create(self.testcoll, "testtype", {}) self.testdata2 = RecordTypeData.create(self.testcoll, "testtype2", {}) # self.user = User.objects.create_user('testuser', '*****@*****.**', 'testpassword') # self.user.save() create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) e1 = self._create_entity_data("entity1") e2 = self._create_entity_data("entity2") e3 = self._create_entity_data("entity3") e4 = EntityData.create(self.testdata2, "entity4", entitydata_create_values("entity4", type_id="testtype2") ) self.list_ids = get_site_lists_linked("testcoll") return
def setUp(self): # Set up basic site data init_annalist_test_site() self.testsite = Site(TestBaseUri, TestBaseDir) self.testcoll = Collection.create(self.testsite, "testcoll", collection_create_values("testcoll")) self.testtype = RecordType.create(self.testcoll, "testtype", recordtype_create_values("testtype")) self.testdata = RecordTypeData.create(self.testcoll, "testtype", {}) # Login and permissions create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) return
def setUp(self): # Set up basic site data init_annalist_test_site() self.testsite = Site(TestBaseUri, TestBaseDir) self.testcoll = Collection.create(self.testsite, "testcoll", collection_create_values("testcoll")) self.testtype = RecordType.create(self.testcoll, "testtype", recordtype_create_values("testtype")) self.testdata = RecordTypeData.create(self.testcoll, "testtype", {}) # Login and permissions create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) return
def setUp(self): init_annalist_test_site() self.testsite = Site(TestBaseUri, TestBaseDir) self.testcoll = Collection.create(self.testsite, "testcoll", collection_create_values("testcoll")) self.testtype = RecordType.create(self.testcoll, "testtype", recordtype_create_values("testtype")) self.testdata = RecordTypeData.create(self.testcoll, "testtype", {}) self.type_ids = get_site_types_sorted() + ['testtype'] self.no_options = ['(no options)'] # Login and permissions create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) return
def setUp(self): init_annalist_test_site() init_annalist_test_coll() self.testsite = Site(TestBaseUri, TestBaseDir) self.testcoll = Collection.create(self.testsite, "testcoll", collection_create_values("testcoll")) self.testtype = RecordType.create(self.testcoll, "testtype", recordtype_create_values("testtype")) self.testdata = RecordTypeData.create(self.testcoll, "testtype", {}) self.no_options = [ FieldChoice('', label="(no options)") ] self.no_view_id = [ FieldChoice('', label="(view id)") ] self.no_list_id = [ FieldChoice('', label="(list id)") ] self.view_options = get_site_views_linked("testcoll") self.list_options = get_site_lists_linked("testcoll") # Login and permissions create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) return
def _check_entity_data_values(self, entity_id, type_id="testtype", type_uri=None, update="Entity", parent=None, update_dict=None): "Helper function checks content of form-updated record type entry with supplied entity_id" recorddata = RecordTypeData.load(self.testcoll, type_id) self.assertTrue(EntityData.exists(recorddata, entity_id)) e = EntityData.load(recorddata, entity_id) self.assertEqual(e.get_id(), entity_id) self.assertEqual(e.get_url(""), TestHostUri + entity_url("testcoll", type_id, entity_id)) v = entitydata_values(entity_id, type_id=type_id, type_uri=type_uri, update=update) if update_dict: v.update(update_dict) for k in update_dict: if update_dict[k] is None: v.pop(k, None) self.assertDictionaryMatch(e.get_values(), v) return e
def test_CreateTestSiteData(self): copySitedata( settings.SITE_SRC_ROOT + "/sampledata/init/" + test_layout.SITE_DIR, settings.SITE_SRC_ROOT + "/annalist/sitedata", TestBaseDir) testsite = Site(TestBaseUri, TestBaseDir) coll123_create_data(testsite) # testcoll = Collection.create(testsite, "testcoll", collection_create_values("testcoll")) testtype = RecordType.create(testcoll, "testtype", recordtype_create_values("testtype")) # testview = RecordView.create(testcoll, "testview", recordview_create_values("testview")) # testlist = RecordList.create(testcoll, "testlist", recordlist_create_values("testlist")) testdata = RecordTypeData.create(testcoll, "testtype", {}) teste = EntityData.create( testdata, "entity1", entitydata_create_values(testcoll, testtype, "entity1")) return
def init_annalist_test_coll(coll_id="testcoll", type_id="testtype"): log.debug("init_annalist_test_coll") testsite = Site(TestBaseUri, TestBaseDir) testcoll = Collection.create(testsite, coll_id, collection_create_values(coll_id)) testtype = RecordType.create(testcoll, type_id, recordtype_create_values(coll_id, type_id)) testdata = RecordTypeData.create(testcoll, type_id, {}) teste = EntityData.create( testdata, "entity1", entitydata_create_values(testcoll,testtype,"entity1") ) testcoll.generate_coll_jsonld_context() # Reset id generator counters EntityData._last_id = 0 RecordType._last_id = 0 RecordView._last_id = 0 RecordList._last_id = 0 RecordField._last_id = 0 AnnalistUser._last_id = 0 return testcoll
def init_annalist_test_coll(coll_id="testcoll", type_id="testtype"): log.debug("init_annalist_test_coll") testsite = Site(TestBaseUri, TestBaseDir) testcoll = Collection.create(testsite, coll_id, collection_create_values(coll_id)) testtype = RecordType.create(testcoll, type_id, recordtype_create_values(coll_id, type_id)) testdata = RecordTypeData.create(testcoll, type_id, {}) teste = EntityData.create( testdata, "entity1", entitydata_create_values(testcoll, testtype, "entity1")) testcoll.generate_coll_jsonld_context() # Reset id generator counters EntityData._last_id = 0 RecordType._last_id = 0 RecordView._last_id = 0 RecordList._last_id = 0 RecordField._last_id = 0 AnnalistUser._last_id = 0 return testcoll
def setUp(self): self.testsite = init_annalist_test_site() self.testcoll = Collection.create(self.testsite, "testcoll", collection_create_values("testcoll")) # Create test type self.testtypes = RecordType.create( self.testcoll, "testtype", recordtype_create_values(coll_id="testcoll", type_id="testtype", type_uri="test:testtype", supertype_uris=[])) # Create test type data parents self.testdata = RecordTypeData.create(self.testcoll, "testtype", {}) # Login and permissions create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) return
def setUp(self): self.testsite = init_annalist_test_site() self.testcoll = Collection.create( self.testsite, "testcoll", collection_create_values("testcoll") ) # Create test type self.testtypes = RecordType.create( self.testcoll, "testtype", recordtype_create_values( coll_id="testcoll", type_id="testtype", type_uri="test:testtype", supertype_uris=[] ) ) # Create test type data parents self.testdata = RecordTypeData.create(self.testcoll, "testtype", {}) # Login and permissions create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) return
def setUp(self): self.testsite = init_annalist_test_site() self.testcoll = init_annalist_named_test_coll(layout.BIBDATA_ID) # Create BibEntry record (BibEntry_type defines field alias) self.testdata = RecordTypeData.create(self.testcoll, "BibEntry_type", {}) self.bibentity1_data = ( { "@type": [ "bib:BibEntry" , ANNAL.CURIE.EntityData ] , ANNAL.CURIE.type: "bib:BibEntry" , ANNAL.CURIE.type_id: "BibEntry_type" , "bib:type": "article" , "bib:title": "bib:title for bibentity1" , "bib:note": "Sample bibliographic entry with field aliasing" , "bib:month": "09" , "bib:year": "2014" , "bib:author": [ { "bib:id": "author_id" , "bib:name": "Author, J. H." , "bib:alternate": "Joe H. Author" , "bib:firstname": "Joe" , "bib:lastname": "Author" }] , "bib:identifier": [] , "bib:journal": [] , "bib:editor": [] , "bib:publication_details": [] , "bib:license": [] , "bib:bookentry": [] }) self.testbib1 = EntityData.create(self.testdata, "bibentity1", self.bibentity1_data) # Login and permissions create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) return
def init_annalist_named_test_coll( base_coll_id=None, coll_id="testcoll", type_id="testtype"): """ Similar to init_annalist_test_coll, but collection also installs and inherits from named collection definitions. """ # @@TODO: DRY: use create_test_coll_inheriting # @@TODO: rename: install_create_test_coll_inheriting log.debug("init_annalist_named_test_coll") testsite = Site(TestBaseUri, TestBaseDir) namedcoll = install_annalist_named_coll(base_coll_id) testcoll = Collection.create(testsite, coll_id, collection_create_values(coll_id)) testcoll.set_alt_entities(namedcoll) testcoll._save() testtype = RecordType.create(testcoll, type_id, recordtype_create_values(coll_id, type_id)) testdata = RecordTypeData.create(testcoll, type_id, {}) teste = EntityData.create( testdata, "entity1", entitydata_create_values(testcoll, testtype, "entity1") ) testcoll.generate_coll_jsonld_context() return testcoll
def create_test_coll_inheriting( base_coll_id=None, coll_id="testcoll", type_id="testtype"): """ Similar to init_annalist_test_coll, but collection also inherits from named collection. """ testsite = Site(TestBaseUri, TestBaseDir) basecoll = Collection.load(testsite, base_coll_id) if not basecoll: msg = "Base collection %s not found"%base_coll_id log.warning(msg) assert False, msg testcoll = Collection.create(testsite, coll_id, collection_create_values(coll_id)) testcoll.set_alt_entities(basecoll) testcoll._save() testtype = RecordType.create(testcoll, type_id, recordtype_create_values(coll_id, type_id)) testdata = RecordTypeData.create(testcoll, type_id, {}) teste = EntityData.create( testdata, "entity1", entitydata_create_values(testcoll, testtype, "entity1") ) testcoll.generate_coll_jsonld_context() return testcoll
def setUp(self): init_annalist_test_site() self.testsite = Site(TestBaseUri, TestBaseDir) self.testcoll = Collection.create( self.testsite, "testcoll", collection_create_values("testcoll") ) # Create test types self.testtypes = RecordType.create( self.testcoll, "testtypes", recordtype_create_values( coll_id="testcoll", type_id="testtypes", type_uri="test:testtypes", supertype_uris=[] ) ) self.testtype1 = RecordType.create( self.testcoll, "testtype1", recordtype_create_values( coll_id="testcoll", type_id="testtype1", type_uri="test:testtype1", supertype_uris=["test:testtypes"] ) ) self.testtype2 = RecordType.create( self.testcoll, "testtype2", recordtype_create_values( coll_id="testcoll", type_id="testtype2", type_uri="test:testtype2", supertype_uris=["test:testtypes"] ) ) self.ref_type = RecordType.create( self.testcoll, "ref_type", recordtype_create_values( coll_id="testcoll", type_id="ref_type", type_uri="test:ref_type", supertype_uris=[] ) ) # Create test type data parents self.testdatas = RecordTypeData.create(self.testcoll, "testtypes", {}) self.testdata1 = RecordTypeData.create(self.testcoll, "testtype1", {}) self.testdata2 = RecordTypeData.create(self.testcoll, "testtype2", {}) self.ref_data = RecordTypeData.create(self.testcoll, "ref_type", {}) # Create test type data es = EntityData.create(self.testdatas, "entitys", entitydata_create_values( "entitys", type_id="testtypes", extra_fields={"test:turi": "test:testtypes"} ) ) e1 = EntityData.create(self.testdata1, "entity1", entitydata_create_values( "entity1", type_id="testtype1", extra_fields={"test:turi": "test:testtype1"} ) ) e2 = EntityData.create(self.testdata2, "entity2", entitydata_create_values( "entity2", type_id="testtype2", extra_fields={"test:turi": "test:testtype2"} ) ) # Login and permissions create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) return
def post(self, request, coll_id=None, type_id=None, entity_id=None, view_id=None, action=None): """ Handle response from generic entity editing form. """ log.info( "views.entityedit.post: coll_id %s, type_id %s, entity_id %s, view_id %s, action %s" % (coll_id, type_id, entity_id, view_id, action)) log.log(settings.TRACE_FIELD_VALUE, "views.entityedit.post %s" % (self.get_request_path())) log.log( settings.TRACE_FIELD_VALUE, " coll_id %s, type_id %s, entity_id %s, view_id %s, action %s" % (coll_id, type_id, entity_id, view_id, action)) log.debug(" form data %r" % (request.POST)) action = request.POST.get('action', action) viewinfo = self.view_setup(action, coll_id, type_id, view_id, entity_id) if viewinfo.http_response: return viewinfo.http_response # Get key form data values # Except for entity_id, use values from URI when form does not supply a value entity_id = request.POST.get('entity_id', None) orig_entity_id = request.POST.get('orig_id', entity_id) entity_type_id = request.POST.get('entity_type', type_id) orig_entity_type_id = request.POST.get('orig_type', type_id) continuation_url = (request.POST.get('continuation_url', None) or self.view_uri('AnnalistEntityDefaultListType', coll_id=coll_id, type_id=type_id)) view_id = request.POST.get('view_id', view_id) # log.info( # " coll_id %s, type_id %s, entity_id %s, view_id %s, action %s"% # (coll_id, type_id, entity_id, view_id, action) # ) # log.info("continuation_url %s, type_id %s"%(continuation_url, type_id)) typeinfo = viewinfo.entitytypeinfo context_extra_values = ({ 'site_title': viewinfo.sitedata["title"], 'title': viewinfo.collection[RDFS.CURIE.label], 'action': action, 'edit_add_field': viewinfo.recordview.get(ANNAL.CURIE.add_field, "yes"), 'continuation_url': continuation_url, 'request_url': self.get_request_path(), 'coll_id': coll_id, 'coll_label': viewinfo.collection[RDFS.CURIE.label], 'type_id': type_id, 'view_choices': self.get_view_choices_field(viewinfo), 'orig_id': orig_entity_id, 'orig_type': orig_entity_type_id, 'view_id': view_id }) message_vals = { 'id': entity_id, 'type_id': type_id, 'coll_id': coll_id } messages = ({ 'parent_heading': typeinfo.entitymessages['parent_heading'] % message_vals, 'parent_missing': typeinfo.entitymessages['parent_missing'] % message_vals, 'entity_heading': typeinfo.entitymessages['entity_heading'] % message_vals, 'entity_invalid_id': typeinfo.entitymessages['entity_invalid_id'] % message_vals, 'entity_exists': typeinfo.entitymessages['entity_exists'] % message_vals, 'entity_not_exists': typeinfo.entitymessages['entity_not_exists'] % message_vals, 'entity_type_heading': typeinfo.entitymessages['entity_type_heading'] % message_vals, 'entity_type_invalid': typeinfo.entitymessages['entity_type_invalid'] % message_vals, 'remove_field_error': message.REMOVE_FIELD_ERROR, 'no_field_selected': message.NO_FIELD_SELECTED }) # Process form response and respond accordingly #@@ TODO: this should be redundant - create as-needed, not before # as of 2014-11-07, removing this causes test failures if not typeinfo.entityparent._exists(): # Create RecordTypeData when not already exists RecordTypeData.create(viewinfo.collection, typeinfo.entityparent.get_id(), {}) #@@ return self.form_response(viewinfo, entity_id, orig_entity_id, entity_type_id, orig_entity_type_id, messages, context_extra_values)
def rename_entity_type(self, viewinfo, old_typeinfo, old_type_id, new_typeinfo, new_type_id, type_data): """ Save a renamed type entity. This involves renaming all of the instances of the type to the new type (with new type id and in new location). Returns None if the operation succeeds, or error message details to be displayed as a pair of values for the message heading and the message body. """ # NOTE: old RecordData instance is not removed. # Don't allow type-rename to or from a type value if old_typeinfo.type_id != new_typeinfo.type_id: log.warning( "EntityEdit.rename_entity_type: attempt to change type of type record" ) return (message.INVALID_OPERATION_ATTEMPTED, message.INVALID_TYPE_CHANGE) # Don't allow renaming built-in type builtin_types = get_built_in_type_ids() if (new_type_id in builtin_types) or (old_type_id in builtin_types): log.warning( "EntityEdit.rename_entity_type: attempt to rename or define a built-in type" ) return (message.INVALID_OPERATION_ATTEMPTED, message.INVALID_TYPE_RENAME) # Create new type record new_typeinfo.create_entity(new_type_id, type_data) # Update instances of type src_typeinfo = EntityTypeInfo(viewinfo.site, viewinfo.collection, old_type_id) dst_typeinfo = EntityTypeInfo(viewinfo.site, viewinfo.collection, new_type_id, create_typedata=True) if new_typeinfo.entity_exists(new_type_id): # Enumerate type instance records and move to new type remove_OK = True for d in src_typeinfo.enum_entities(): data_id = d.get_id() data_vals = d.get_values() data_vals[ANNAL.CURIE.type_id] = new_type_id data_vals[ ANNAL.CURIE.type] = dst_typeinfo.entityclass._entitytype if self.rename_entity(src_typeinfo, data_id, dst_typeinfo, data_id, data_vals): remove_OK = False # Finally, remove old type record: if remove_OK: # Precautionary new_typeinfo.remove_entity(old_type_id) RecordTypeData.remove(new_typeinfo.entitycoll, old_type_id) else: log.warning("Failed to rename type %s to type %s" % (old_type_id, new_type_id)) return (message.SYSTEM_ERROR, message.RENAME_TYPE_FAILED % (old_type_id, new_type_id)) return None
def _create_record_type(self, type_id, entity_id="testentity"): "Helper function creates record type entry with supplied type_id" t = RecordType.create(self.testcoll, type_id, recordtype_create_values(type_id=type_id)) d = RecordTypeData.create(self.testcoll, type_id, {}) e = EntityData.create(d, entity_id, {}) return (t, d, e)
def setUp(self): init_annalist_test_site() self.testsite = Site(TestBaseUri, TestBaseDir) self.testcoll = Collection.create(self.testsite, "testcoll", collection_create_values("testcoll")) # Create test types self.testtypes = RecordType.create( self.testcoll, "testtypes", recordtype_create_values(coll_id="testcoll", type_id="testtypes", type_uri="test:testtypes", supertype_uris=[])) self.testtype1 = RecordType.create( self.testcoll, "testtype1", recordtype_create_values(coll_id="testcoll", type_id="testtype1", type_uri="test:testtype1", supertype_uris=["test:testtypes"])) self.testtype2 = RecordType.create( self.testcoll, "testtype2", recordtype_create_values(coll_id="testcoll", type_id="testtype2", type_uri="test:testtype2", supertype_uris=["test:testtypes"])) self.ref_type = RecordType.create( self.testcoll, "ref_type", recordtype_create_values(coll_id="testcoll", type_id="ref_type", type_uri="test:ref_type", supertype_uris=[])) # Create test type data parents self.testdatas = RecordTypeData.create(self.testcoll, "testtypes", {}) self.testdata1 = RecordTypeData.create(self.testcoll, "testtype1", {}) self.testdata2 = RecordTypeData.create(self.testcoll, "testtype2", {}) self.ref_data = RecordTypeData.create(self.testcoll, "ref_type", {}) # Create test type data es = EntityData.create( self.testdatas, "entitys", entitydata_create_values( "entitys", type_id="testtypes", extra_fields={"test:turi": "test:testtypes"})) e1 = EntityData.create( self.testdata1, "entity1", entitydata_create_values( "entity1", type_id="testtype1", extra_fields={"test:turi": "test:testtype1"})) e2 = EntityData.create( self.testdata2, "entity2", entitydata_create_values( "entity2", type_id="testtype2", extra_fields={"test:turi": "test:testtype2"})) # Login and permissions create_test_user(self.testcoll, "testuser", "testpassword") self.client = Client(HTTP_HOST=TestHost) loggedin = self.client.login(username="******", password="******") self.assertTrue(loggedin) return
class EntityTypeInfo(object): """ Check a supplied type identifier, and access values for: Entity class Entity parent Entity alternative parent for site-wide values Type-dependent messages """ def __init__(self, site, coll, type_id, create_typedata=False): """ Set up type attribute values. site current site object coll collection object in which type is used type_id entity type id, which is a collection-defined value, or one of a number of special site-wide built-in types. create_typedata if true, requests that a RecordTypeData entity be created and saved on disk for user-defined types if it does not already exist. (Creating a RecordTypeData entity ensures that the corresponding data storage location is available for saving entity data.) Attributes of type information object are: recordtype type object describing the identified type entityparent Parent enbtity for entities of this type, or None if the type is not defined for the collection entityaltparent Alternative (site-wide) parent entity for built-in types, or None entityclass Python class object for entity entitymessages a table of message strings for diagnostics relating to operations on this type. and other values as initialized here. """ self.entitysite = site self.entitycoll = coll self.recordtype = None self.entityparent = None self.coll_id = coll.get_id() self.type_id = type_id self.permissions_map = None if type_id in TYPE_CLASS_MAP: self.recordtype = RecordType.load(coll, type_id, site) self.entityparent = coll self.entityaltparent = site self.entityclass = TYPE_CLASS_MAP[type_id] self.entitymessages = TYPE_MESSAGE_MAP[type_id] self.permissions_map = TYPE_PERMISSIONS_MAP[type_id] else: if RecordType.exists(coll, type_id, site): self.recordtype = RecordType.load(coll, type_id, site) if create_typedata and not RecordTypeData.exists(coll, type_id): self.entityparent = RecordTypeData.create(coll, type_id, {}) else: self.entityparent = RecordTypeData(coll, type_id) self.entityaltparent = None self.entityclass = EntityData self.entitymessages = ENTITY_MESSAGES self.permissions_map = ENTITY_PERMISSIONS if not self.recordtype: # .recordtype is used by views.displayinfo to locate the default # view and/or list id for examining records of a particular type. # # Also used in entityedit for getting @type URI/CURIE values. # # Used in render_utils to get link to type record log.warning("EntityTypeInfo.__init__: RecordType %s not found"%type_id) return def parent_exists(self): """ Test for existence of parent entity for the current type. """ return self.entityparent._exists() def entity_exists(self, entity_id, use_altparent=False): """ Test for existence of identified entity of the current type. """ altparent = self.entityaltparent if use_altparent else None return self.entityclass.exists(self.entityparent, entity_id, altparent=altparent) def create_entity(self, entity_id, entity_values): """ Creates and returns an entity for the current type, with the supplied values. """ log.debug( "create_entity id %s, parent %s, values %r"% (entity_id, self.entityparent, entity_values) ) # Set type URI for entity; previous types are not carried forwards # If type does not define a URI, use type URL typeuri = None if self.recordtype: if ANNAL.CURIE.uri in self.recordtype: typeuri = self.recordtype[ANNAL.CURIE.uri] if not typeuri: typeuri = self.recordtype[ANNAL.CURIE.url] entity_values['@type'] = typeuri # NOTE: previous type not carried forward # Don't save entity URI if same as URL if entity_values.get(ANNAL.CURIE.uri) == entity_values.get(ANNAL.CURIE.url): entity_values.pop(ANNAL.CURIE.uri, None) return self.entityclass.create(self.entityparent, entity_id, entity_values) def remove_entity(self, entity_id): """ Remove identified entity for the current type. """ log.debug( "remove_entity id %s, parent %s"% (entity_id, self.entityparent) ) return self.entityclass.remove(self.entityparent, entity_id) def get_entity(self, entity_id, action="view"): """ Loads and returns an entity for the current type, or returns None if the entity does not exist. If `action` is "new" then a new entity is initialized (but not saved). """ log.debug( "get_entity id %s, parent %s, altparent %s, action %s"% (entity_id, self.entityparent, self.entityaltparent, action) ) entity = None if util.valid_id(entity_id): if action == "new": entity = self.entityclass(self.entityparent, entity_id) entity_initial_values = self.get_initial_entity_values(entity_id) entity.set_values(entity_initial_values) elif self.entityclass.exists( self.entityparent, entity_id, altparent=self.entityaltparent ): entity = self.entityclass.load(self.entityparent, entity_id, altparent=self.entityaltparent) return entity def get_entity_with_aliases(self, entity_id, action="view"): """ Loads and returns an entity for the current type, or returns None if the entity does not exist. If `action` is "new" then a new entity is initialized (but not saved). Field aliases defined in the associated record type are populated in the value returned. """ entity = self.get_entity(entity_id, action=action) # Fill in field aliases if entity and ANNAL.CURIE.field_aliases in self.recordtype: for alias in self.recordtype[ANNAL.CURIE.field_aliases]: tgt = alias[ANNAL.CURIE.alias_target] src = alias[ANNAL.CURIE.alias_source] if entity.get(tgt, None) in [None, ""]: entity[tgt] = entity.get(src, "") return entity def enum_entity_ids(self, usealtparent=False): """ Iterate over entity identifiers in collection with current type. usealtparent is True if site-wide entities are to be included. """ altparent = self.entityaltparent if usealtparent else None if self.entityparent: for eid in self.entityparent.child_entity_ids( self.entityclass, altparent=altparent): yield eid else: log.warning("EntityTypeInfo.enum_entity_ids: missing entityparent; type_id %s"%(self.type_id)) return def enum_entities(self, user_perms=None, usealtparent=False): """ Iterate over entities in collection with current type. Returns entities with alias fields instantiated. usealtparent is True if site-wide entities are to be included. """ if (not user_perms or self.permissions_map['list'] in user_perms[ANNAL.CURIE.user_permissions]): altparent = self.entityaltparent if usealtparent else None if self.entityparent: for eid in self.entityparent.child_entity_ids( self.entityclass, altparent=altparent): yield self.get_entity_with_aliases(eid) else: log.warning("EntityTypeInfo.enum_entities: missing entityparent; type_id %s"%(self.type_id)) return def get_initial_entity_values(self, entity_id): """ Returns an initial value dictionary for the indicated entity. Attempts to read initial values from the type parent directory. Failing that, returns system-wide default values. """ values = ( { '@type': [ANNAL.CURIE.EntityData] , ANNAL.CURIE.type_id: self.type_id , RDFS.CURIE.label: "" , RDFS.CURIE.comment: "" }) init_entity = self.get_entity("_initial_values") if init_entity: values = init_entity.get_values() values.pop("@id", None) values.pop(ANNAL.CURIE.id, None) values.pop(ANNAL.CURIE.url, None) values[ANNAL.CURIE.id] = entity_id return values def get_default_view_id(self): """ Returns the default view id for the current record type """ view_id = None if self.recordtype: view_id = self.recordtype.get(ANNAL.CURIE.type_view, None) else: log.warning("EntityTypeInfo.get_default_view_id: no type data for %s"%(self.type_id)) return view_id or "Default_view"
def __init__(self, site, coll, type_id, create_typedata=False): """ Set up type attribute values. site current site object coll collection object in which type is used type_id entity type id, which is a collection-defined value, or one of a number of special site-wide built-in types. create_typedata if true, requests that a RecordTypeData entity be created and saved on disk for user-defined types if it does not already exist. (Creating a RecordTypeData entity ensures that the corresponding data storage location is available for saving entity data.) Attributes of type information object are: recordtype type object describing the identified type entityparent Parent enbtity for entities of this type, or None if the type is not defined for the collection entityaltparent Alternative (site-wide) parent entity for built-in types, or None entityclass Python class object for entity entitymessages a table of message strings for diagnostics relating to operations on this type. and other values as initialized here. """ self.entitysite = site self.entitycoll = coll self.recordtype = None self.entityparent = None self.coll_id = coll.get_id() self.type_id = type_id self.permissions_map = None if type_id in TYPE_CLASS_MAP: self.recordtype = RecordType.load(coll, type_id, site) self.entityparent = coll self.entityaltparent = site self.entityclass = TYPE_CLASS_MAP[type_id] self.entitymessages = TYPE_MESSAGE_MAP[type_id] self.permissions_map = TYPE_PERMISSIONS_MAP[type_id] else: if RecordType.exists(coll, type_id, site): self.recordtype = RecordType.load(coll, type_id, site) if create_typedata and not RecordTypeData.exists(coll, type_id): self.entityparent = RecordTypeData.create(coll, type_id, {}) else: self.entityparent = RecordTypeData(coll, type_id) self.entityaltparent = None self.entityclass = EntityData self.entitymessages = ENTITY_MESSAGES self.permissions_map = ENTITY_PERMISSIONS if not self.recordtype: # .recordtype is used by views.displayinfo to locate the default # view and/or list id for examining records of a particular type. # # Also used in entityedit for getting @type URI/CURIE values. # # Used in render_utils to get link to type record log.warning("EntityTypeInfo.__init__: RecordType %s not found"%type_id) return