def setup_class(cls): if not tests.is_search_supported(): raise tests.SkipTest("Search not supported") tests.setup_test_search_index() ctd.CreateTestData.create() model.repo.new_revision() cls.vocab = model.Vocabulary(TEST_VOCAB_NAME) model.Session.add(cls.vocab) model.Session.commit() vocab_tag_1 = model.Tag('tag1', cls.vocab.id) vocab_tag_2 = model.Tag('tag2', cls.vocab.id) model.Session.add(vocab_tag_1) model.Session.add(vocab_tag_2) pkg = model.Package.get('warandpeace') pkg_tag1 = model.PackageTag(pkg, vocab_tag_1) pkg_tag2 = model.PackageTag(pkg, vocab_tag_2) model.Session.add(pkg_tag1) model.Session.add(pkg_tag2) model.Session.commit() model.Session.remove()
def test_create_package_with_tags(self): model.repo.new_revision() pkg = model.Package(name=u'test-package') # method 1 tag1 = model.Tag(name=u'science') package_tag1 = model.PackageTag(package=pkg, tag=tag1) pkg.package_tag_all[:] = [package_tag1] # method 2 tag2 = model.Tag(name=u'geology') package_tag2 = model.PackageTag(package=pkg, tag=tag2) pkg.add_tag(tag2) # method 3 pkg.add_tag_by_name(u'energy') model.Session.add_all([pkg, package_tag1, package_tag2]) model.Session.commit() model.Session.remove() pkg = model.Package.by_name(u'test-package') assert_equal(set([tag.name for tag in pkg.get_tags()]), set([u'science', u'geology', u'energy']))
def setup_class(cls): cls.vocab = model.Vocabulary(TEST_VOCAB_NAME) model.Session.add(cls.vocab) model.Session.commit() vocab_tag_1 = model.Tag('tag1', cls.vocab.id) vocab_tag_2 = model.Tag('tag2', cls.vocab.id) model.Session.add(vocab_tag_1) model.Session.add(vocab_tag_2) model.Session.commit() model.Session.remove()
def initial_data(self, clean_db): self.vocab = model.Vocabulary(TEST_VOCAB_NAME) model.Session.add(self.vocab) model.Session.commit() vocab_tag_1 = model.Tag("tag1", self.vocab.id) vocab_tag_2 = model.Tag("tag2", self.vocab.id) model.Session.add(vocab_tag_1) model.Session.add(vocab_tag_2) model.Session.commit() model.Session.remove()
def setup_class(self): model.repo.init_db() rev1 = model.repo.new_revision() self.tagname = u'testtagm2m' self.tagname2 = u'testtagm2m2' self.tagname3 = u'testtag3' self.pkgname = u'testpkgm2m' pkg = model.Package(name=self.pkgname) self.tag = model.Tag(name=self.tagname) self.tag2 = model.Tag(name=self.tagname2) pkg2tag = model.PackageTag(package=pkg, tag=self.tag) pkg.tags.append(self.tag2) model.Session.add_all([pkg,self.tag,self.tag2,pkg2tag]) model.Session.commit() self.pkg2tag_id = pkg2tag.id self.rev = rev1
def initial_data(self, clean_db): CreateTestData.create() # tag whose association will get deleted tag3 = model.Tag(name=self.tagname) pkg = model.Package.by_name(u"annakarenina") pkg.add_tag(tag3) model.repo.commit_and_remove() pkg = model.Package.by_name(u"annakarenina") pkg.remove_tag(tag3) # now do a tag for ordering tagordered = model.Tag(name=self.orderedfirst) wap = model.Package.by_name(u"warandpeace") # do them the wrong way round wap.add_tag(tagordered) pkg.add_tag(tagordered) model.repo.commit_and_remove()
def _create_test_data(cls): CreateTestData.create() # Remove visitor and logged in roles roles = [] q = model.Session.query(model.UserObjectRole).\ filter(model.UserObjectRole.user==model.User.by_name(u"visitor")) roles.extend(q.all()) q = model.Session.query(model.UserObjectRole).\ filter(model.UserObjectRole.user==model.User.by_name(u"logged_in")) roles.extend(q.all()) for role in roles: model.Session.delete(role) rev = model.repo.new_revision() model.Session.add_all([ model.User(name=u'pkggroupadmin'), model.User(name=u'site_reader'), model.User(name=u'outcast'), model.Package(name=cls.ENTITY_NAME), model.Package(name=u'deleted'), model.Group(name=cls.ENTITY_NAME), model.Group(name=u'deleted'), model.Tag(name=cls.ENTITY_NAME), model.RoleAction(role=cls.TRUSTED_ROLE, context=u'', action=model.Action.SITE_READ), model.RoleAction(role=cls.TRUSTED_ROLE, context=u'', action=model.Action.READ), ]) model.repo.commit_and_remove() # testsysadmin is sysadmin # annafan is package admin for annakarenina rev = model.repo.new_revision() site_reader = model.User.by_name(u'site_reader') pkggroupadmin = model.User.by_name(u'pkggroupadmin') pkg = model.Package.by_name(cls.ENTITY_NAME) group = model.Group.by_name(cls.ENTITY_NAME) tag = model.Tag.by_name(cls.ENTITY_NAME) pkg.add_tag(tag) model.add_user_to_role(site_reader, cls.TRUSTED_ROLE, model.System()) model.add_user_to_role(site_reader, cls.TRUSTED_ROLE, pkg) model.add_user_to_role(site_reader, cls.TRUSTED_ROLE, group) model.add_user_to_role(pkggroupadmin, model.Role.ADMIN, pkg) model.add_user_to_role(pkggroupadmin, model.Role.ADMIN, group) model.Package.by_name(u'deleted').delete() model.Group.by_name(u'deleted').delete() model.repo.commit_and_remove() cls.testsysadmin = model.User.by_name(u'testsysadmin') cls.pkggroupadmin = model.User.by_name(u'pkggroupadmin') cls.site_reader = model.User.by_name(u'site_reader') cls.outcast = model.User.by_name(u'outcast')
def setup_class(self): CreateTestData.create() self.orderedfirst = u'000-zzz' # tag whose association will get deleted self.tagname = u'russian-tag-we-will-delete' tag3 = model.Tag(name=self.tagname) pkg = model.Package.by_name(u'annakarenina') pkg.add_tag(tag3) model.repo.commit_and_remove() pkg = model.Package.by_name(u'annakarenina') pkg.remove_tag(tag3) # now do a tag for ordering tagordered = model.Tag(name=self.orderedfirst) wap = model.Package.by_name(u'warandpeace') # do them the wrong way round wap.add_tag(tagordered) pkg.add_tag(tagordered) model.repo.commit_and_remove()
def test_create_package_with_tags(self): pkg = model.Package(name=factories.Dataset.stub().name) # method 1 tag1 = model.Tag(name=factories.Tag.stub().name) package_tag1 = model.PackageTag(package=pkg, tag=tag1) pkg.package_tags[:] = [package_tag1] # method 2 tag2 = model.Tag(name=factories.Tag.stub().name) package_tag2 = model.PackageTag(package=pkg, tag=tag2) pkg.add_tag(tag2) # method 3 pkg.add_tag_by_name(u"energy") model.Session.add_all([pkg, package_tag1, package_tag2]) model.Session.commit() model.Session.remove() pkg = model.Package.by_name(pkg.name) assert set([tag.name for tag in pkg.get_tags() ]) == set([tag1.name, tag2.name, u"energy"])
def test_create_package_with_tags(self): pkg = model.Package(name=u"test-package") # method 1 tag1 = model.Tag(name=u"science") package_tag1 = model.PackageTag(package=pkg, tag=tag1) pkg.package_tag_all[:] = [package_tag1] # method 2 tag2 = model.Tag(name=u"geology") package_tag2 = model.PackageTag(package=pkg, tag=tag2) pkg.add_tag(tag2) # method 3 pkg.add_tag_by_name(u"energy") model.Session.add_all([pkg, package_tag1, package_tag2]) model.Session.commit() model.Session.remove() pkg = model.Package.by_name(u"test-package") assert set([tag.name for tag in pkg.get_tags() ]) == set([u"science", u"geology", u"energy"])
def initial_data(self, clean_db, clean_index): if not tests.is_search_supported(): raise tests.SkipTest("Search not supported") ctd.CreateTestData.create() self.vocab = model.Vocabulary(TEST_VOCAB_NAME) model.Session.add(self.vocab) model.Session.commit() vocab_tag_1 = model.Tag("tag1", self.vocab.id) vocab_tag_2 = model.Tag("tag2", self.vocab.id) model.Session.add(vocab_tag_1) model.Session.add(vocab_tag_2) pkg = model.Package.get("warandpeace") pkg_tag1 = model.PackageTag(pkg, vocab_tag_1) pkg_tag2 = model.PackageTag(pkg, vocab_tag_2) model.Session.add(pkg_tag1) model.Session.add(pkg_tag2) model.Session.commit() model.Session.remove()
def setup_class(self): CreateTestData.create() model.repo.new_revision() self.orderedfirst = u'000-zzz' # tag whose association will get deleted self.tagname = u'russian-tag-we-will-delete' tag3 = model.Tag(name=self.tagname) pkg = model.Package.by_name(u'annakarenina') pkg.tags.append(tag3) model.repo.commit_and_remove() model.repo.new_revision() pkg = model.Package.by_name(u'annakarenina') # we aren't guaranteed it is last ... idx = [ t.name for t in pkg.tags].index(self.tagname) del pkg.tags[idx] # now do a tag for ordering tagordered = model.Tag(name=self.orderedfirst) wap = model.Package.by_name(u'warandpeace') # do them the wrong way round tagordered.packages.append(wap) tagordered.packages.append(pkg) model.repo.commit_and_remove()
def test_group_list_dictize_including_tags(self): factories.Group() # group tags aren't in the group_create schema, so its slightly more # convoluted way to create them group_obj = model.Session.query(model.Group).first() tag = model.Tag(name='t1') model.Session.add(tag) model.Session.commit() tag = model.Session.query(model.Tag).first() group_obj = model.Session.query(model.Group).first() member = model.Member(group=group_obj, table_id=tag.id, table_name='tag') model.Session.add(member) model.repo.new_revision() model.Session.commit() group_list = model.Session.query(model.Group).filter_by().all() context = {'model': model, 'session': model.Session} group_dicts = model_dictize.group_list_dictize(group_list, context, include_tags=True) assert_equal(group_dicts[0]['tags'][0]['name'], 't1')
def test_group_list_dictize_including_tags(self): factories.Group() # group tags aren't in the group_create schema, so its slightly more # convoluted way to create them group_obj = model.Session.query(model.Group).first() tag = model.Tag(name="t1") model.Session.add(tag) model.Session.commit() tag = model.Session.query(model.Tag).first() group_obj = model.Session.query(model.Group).first() member = model.Member(group=group_obj, table_id=tag.id, table_name="tag") model.Session.add(member) model.Session.commit() group_list = model.Session.query(model.Group).filter_by().all() context = {"model": model, "session": model.Session} group_dicts = model_dictize.group_list_dictize(group_list, context, include_tags=True) assert group_dicts[0]["tags"][0]["name"] == "t1"
def make_some_vocab_tags(cls): model.repo.new_revision() # Create a couple of vocabularies. genre_vocab = model.Vocabulary(u'genre') model.Session.add(genre_vocab) composers_vocab = model.Vocabulary(u'composers') model.Session.add(composers_vocab) # Create some additional free tags for tag search tests. tolkien_tag = model.Tag(name="tolkien") model.Session.add(tolkien_tag) toledo_tag = model.Tag(name="toledo") model.Session.add(toledo_tag) tolerance_tag = model.Tag(name="tolerance") model.Session.add(tolerance_tag) tollbooth_tag = model.Tag(name="tollbooth") model.Session.add(tollbooth_tag) # We have to add free tags to a package or they won't show up in tag results. model.Package.get('warandpeace').add_tags( (tolkien_tag, toledo_tag, tolerance_tag, tollbooth_tag)) # Create some tags that belong to vocabularies. sonata_tag = model.Tag(name=u'sonata', vocabulary_id=genre_vocab.id) model.Session.add(sonata_tag) bach_tag = model.Tag(name=u'Bach', vocabulary_id=composers_vocab.id) model.Session.add(bach_tag) neoclassical_tag = model.Tag(name='neoclassical', vocabulary_id=genre_vocab.id) model.Session.add(neoclassical_tag) neofolk_tag = model.Tag(name='neofolk', vocabulary_id=genre_vocab.id) model.Session.add(neofolk_tag) neomedieval_tag = model.Tag(name='neomedieval', vocabulary_id=genre_vocab.id) model.Session.add(neomedieval_tag) neoprog_tag = model.Tag(name='neoprog', vocabulary_id=genre_vocab.id) model.Session.add(neoprog_tag) neopsychedelia_tag = model.Tag(name='neopsychedelia', vocabulary_id=genre_vocab.id) model.Session.add(neopsychedelia_tag) neosoul_tag = model.Tag(name='neosoul', vocabulary_id=genre_vocab.id) model.Session.add(neosoul_tag) nerdcore_tag = model.Tag(name='nerdcore', vocabulary_id=genre_vocab.id) model.Session.add(nerdcore_tag) model.Package.get('warandpeace').add_tag(bach_tag) model.Package.get('annakarenina').add_tag(sonata_tag) model.Session.commit()
def create(cls, auth_profile="", package_type=None): model.Session.remove() rev = model.repo.new_revision() # same name as user we create below rev.author = cls.author rev.message = u'''Creating test data. * Package: annakarenina * Package: warandpeace * Associated tags, etc etc ''' if auth_profile == "publisher": organization_group = model.Group(name=u"organization_group", type="organization") cls.pkg_names = [u'annakarenina', u'warandpeace'] pkg1 = model.Package(name=cls.pkg_names[0], type=package_type) if auth_profile == "publisher": pkg1.group = organization_group model.Session.add(pkg1) pkg1.title = u'A Novel By Tolstoy' pkg1.version = u'0.7a' pkg1.url = u'http://datahub.io' # put an & in the url string to test escaping if 'alt_url' in model.Resource.get_extra_columns(): configured_extras = ({ 'alt_url': u'alt123' }, { 'alt_url': u'alt345' }) else: configured_extras = ({}, {}) pr1 = model.Resource( url=u'http://datahub.io/download/x=1&y=2', format=u'plain text', description=u'Full text. Needs escaping: " Umlaut: \xfc', hash=u'abc123', extras={'size_extra': u'123'}, **configured_extras[0]) pr2 = model.Resource(url=u'http://datahub.io/index.json', format=u'JSON', description=u'Index of the novel', hash=u'def456', extras={'size_extra': u'345'}, **configured_extras[1]) model.Session.add(pr1) model.Session.add(pr2) pkg1.resources_all.append(pr1) pkg1.resources_all.append(pr2) pkg1.notes = u'''Some test notes ### A 3rd level heading **Some bolded text.** *Some italicized text.* Foreign characters: u with umlaut \xfc 66-style quote \u201c foreign word: th\xfcmb Needs escaping: left arrow < <http://ckan.net/> ''' pkg2 = model.Package(name=cls.pkg_names[1], type=package_type) tag1 = model.Tag(name=u'russian') tag2 = model.Tag(name=u'tolstoy') if auth_profile == "publisher": pkg2.group = organization_group # Flexible tag, allows spaces, upper-case, # and all punctuation except commas tag3 = model.Tag(name=u'Flexible \u30a1') for obj in [pkg2, tag1, tag2, tag3]: model.Session.add(obj) pkg1.add_tags([tag1, tag2, tag3]) pkg2.add_tags([tag1, tag3]) cls.tag_names = [t.name for t in (tag1, tag2, tag3)] pkg1.license_id = u'other-open' pkg2.license_id = u'cc-nc' # closed license pkg2.title = u'A Wonderful Story' pkg1.extras = {u'genre': 'romantic novel', u'original media': 'book'} # group david = model.Group(name=u'david', title=u'Dave\'s books', description=u'These are books that David likes.', type=auth_profile or 'group') roger = model.Group(name=u'roger', title=u'Roger\'s books', description=u'Roger likes these books.', type=auth_profile or 'group') for obj in [david, roger]: model.Session.add(obj) cls.group_names.add(u'david') cls.group_names.add(u'roger') model.Session.flush() model.Session.add( model.Member(table_id=pkg1.id, table_name='package', group=david)) model.Session.add( model.Member(table_id=pkg2.id, table_name='package', group=david)) model.Session.add( model.Member(table_id=pkg1.id, table_name='package', group=roger)) # authz sysadmin = model.User(name=u'testsysadmin', password=u'testsysadmin') sysadmin.sysadmin = True model.Session.add_all([ model.User(name=u'tester', apikey=u'tester', password=u'tester'), model.User(name=u'joeadmin', password=u'joeadmin'), model.User( name=u'annafan', about= u'I love reading Annakarenina. My site: http://datahub.io', password=u'annafan'), model.User(name=u'russianfan', password=u'russianfan'), sysadmin, ]) cls.user_refs.extend([ u'tester', u'joeadmin', u'annafan', u'russianfan', u'testsysadmin' ]) model.repo.commit_and_remove()
def create_arbitrary(cls, package_dicts, relationships=[], extra_user_names=[], extra_group_names=[]): '''Creates packages and a few extra objects as well at the same time if required. @param package_dicts - a list of dictionaries with the package properties. Extra keys allowed: @param extra_group_names - a list of group names to create. No properties get set though. ''' assert isinstance(relationships, (list, tuple)) assert isinstance(extra_user_names, (list, tuple)) assert isinstance(extra_group_names, (list, tuple)) model.Session.remove() new_user_names = extra_user_names new_group_names = set() new_groups = {} if package_dicts: if isinstance(package_dicts, dict): package_dicts = [package_dicts] for item in package_dicts: rev = model.repo.new_revision() rev.author = cls.author rev.message = u'Creating test packages.' pkg_dict = {} for field in cls.pkg_core_fields: if item.has_key(field): pkg_dict[field] = unicode(item[field]) if model.Package.by_name(pkg_dict['name']): log.warning('Cannot create package "%s" as it already exists.' % \ (pkg_dict['name'])) continue pkg = model.Package(**pkg_dict) model.Session.add(pkg) for attr, val in item.items(): if isinstance(val, str): val = unicode(val) if attr == 'name': continue if attr in cls.pkg_core_fields: pass elif attr == 'download_url': pkg.add_resource(unicode(val)) elif attr == 'resources': assert isinstance(val, (list, tuple)) for res_dict in val: non_extras = {} for k, v in res_dict.items(): if k != 'extras': if not isinstance(v, datetime.datetime): v = unicode(v) non_extras[str(k)] = v extras = { str(k): unicode(v) for k, v in res_dict.get('extras', {}).items() } pkg.add_resource(extras=extras, **non_extras) elif attr == 'tags': if isinstance(val, (str, unicode)): tags = val.split() elif isinstance(val, list): tags = val else: raise NotImplementedError for tag_name in tags: tag_name = unicode(tag_name) tag = model.Tag.by_name(tag_name) if not tag: tag = model.Tag(name=tag_name) cls.tag_names.append(tag_name) model.Session.add(tag) pkg.add_tag(tag) model.Session.flush() elif attr == 'groups': model.Session.flush() if isinstance(val, (str, unicode)): group_names = val.split() elif isinstance(val, list): group_names = val else: raise NotImplementedError for group_name in group_names: group = model.Group.by_name(unicode(group_name)) if not group: if not group_name in new_groups: group = model.Group( name=unicode(group_name)) model.Session.add(group) new_group_names.add(group_name) new_groups[group_name] = group else: # If adding multiple packages with the same # group name, model.Group.by_name will not # find the group as the session has not yet # been committed at this point. Fetch from # the new_groups dict instead. group = new_groups[group_name] capacity = 'organization' if group.is_organization\ else 'public' member = model.Member(group=group, table_id=pkg.id, table_name='package', capacity=capacity) model.Session.add(member) if group.is_organization: pkg.owner_org = group.id elif attr == 'license': pkg.license_id = val elif attr == 'license_id': pkg.license_id = val elif attr == 'extras': pkg.extras = val elif attr == 'admins': assert 0, 'Deprecated param "admins"' else: raise NotImplementedError(attr) cls.pkg_names.append(item['name']) model.repo.commit_and_remove() needs_commit = False rev = model.repo.new_revision() for group_name in extra_group_names: group = model.Group(name=unicode(group_name)) model.Session.add(group) new_group_names.add(group_name) needs_commit = True if needs_commit: model.repo.commit_and_remove() needs_commit = False # create users that have been identified as being needed for user_name in new_user_names: if not model.User.by_name(unicode(user_name)): user = model.User(name=unicode(user_name)) model.Session.add(user) cls.user_refs.append(user_name) needs_commit = True if needs_commit: model.repo.commit_and_remove() needs_commit = False # setup authz for groups just created for group_name in new_group_names: group = model.Group.by_name(unicode(group_name)) cls.group_names.add(group_name) needs_commit = True if needs_commit: model.repo.commit_and_remove() needs_commit = False if relationships: rev = model.repo.new_revision() rev.author = cls.author rev.message = u'Creating package relationships.' def pkg(pkg_name): return model.Package.by_name(unicode(pkg_name)) for subject_name, relationship, object_name in relationships: pkg(subject_name).add_relationship(unicode(relationship), pkg(object_name)) needs_commit = True model.repo.commit_and_remove()
def _create_test_data(cls): # Entities (Packages/Groups) are named after what roles (permissions) # are assigned to them: # First letter is the role for logged in users # Second letter is the role for visitors # Where: # r = Allowed to read # w = Allowed to read/write # x = Not allowed either model.repo.init_db() rev = model.repo.new_revision() cls.roles = ('xx', 'rx', 'wx', 'rr', 'wr', 'ww', 'deleted') tag = model.Tag("test") model.Session.add(tag) for mode in cls.roles: pkg = model.Package(name=unicode(mode)) model.Session.add(pkg) pkg.add_tag(tag) model.Session.add(model.Group(name=unicode(mode))) entities_to_test_deleting = [] for interface in cls.INTERFACES: for action in ('purge', 'delete'): for user in ('visitor', 'user', 'admin', 'mrloggedin', 'testsysadmin', 'pkggroupadmin'): for entity_type in cls.DEFAULT_ENTITY_TYPES: entity_class = cls.ENTITY_CLASS_MAP[entity_type] entity_name = u'%s_%s_%s' % (action, user, interface) model.Session.add(entity_class(name=entity_name)) entities_to_test_deleting.append( (entity_name, entity_class)) model.Session.add(model.User(name=u'testsysadmin')) model.Session.add(model.User(name=u'pkggroupadmin')) model.Session.add(model.User(name=u'pkgeditor')) model.Session.add(model.User(name=u'pkgreader')) model.Session.add(model.User(name=u'mrloggedin')) model.Session.add(model.User(name=u'pkgadminfriend')) model.Session.add(model.User(name=u'groupadmin')) model.Session.add(model.User(name=u'groupeditor')) model.Session.add(model.User(name=u'groupreader')) visitor_name = '123.12.12.123' model.repo.commit_and_remove() rev = model.repo.new_revision() model.Package.by_name(u'ww').add_relationship( u'depends_on', model.Package.by_name(u'xx')) model.Package.by_name(u'ww').add_relationship( u'links_to', model.Package.by_name(u'wr')) model.repo.commit_and_remove() testsysadmin = model.User.by_name(u'testsysadmin') pkggroupadmin = model.User.by_name(u'pkggroupadmin') pkgeditor = model.User.by_name(u'pkgeditor') pkgreader = model.User.by_name(u'pkgreader') groupadmin = model.User.by_name(u'groupadmin') groupeditor = model.User.by_name(u'groupeditor') groupreader = model.User.by_name(u'groupreader') mrloggedin = model.User.by_name(name=u'mrloggedin') visitor = model.User.by_name(name=model.PSEUDO_USER__VISITOR) for mode in cls.roles: pkg = model.Package.by_name(unicode(mode)) model.add_user_to_role(pkggroupadmin, model.Role.ADMIN, pkg) model.add_user_to_role(pkgeditor, model.Role.EDITOR, pkg) model.add_user_to_role(pkgreader, model.Role.READER, pkg) group = model.Group.by_name(unicode(mode)) group.packages = model.Session.query(model.Package).all() model.add_user_to_role(pkggroupadmin, model.Role.ADMIN, group) model.add_user_to_role(groupadmin, model.Role.ADMIN, group) model.add_user_to_role(groupeditor, model.Role.EDITOR, group) model.add_user_to_role(groupreader, model.Role.READER, group) if mode == u'deleted': rev = model.repo.new_revision() pkg = model.Package.by_name(unicode(mode)) pkg.state = model.State.DELETED group = model.Package.by_name(unicode(mode)) group.state = model.State.DELETED model.repo.commit_and_remove() else: if mode[0] == u'r': model.add_user_to_role(mrloggedin, model.Role.READER, pkg) model.add_user_to_role(mrloggedin, model.Role.READER, group) if mode[0] == u'w': model.add_user_to_role(mrloggedin, model.Role.EDITOR, pkg) model.add_user_to_role(mrloggedin, model.Role.EDITOR, group) if mode[1] == u'r': model.add_user_to_role(visitor, model.Role.READER, pkg) model.add_user_to_role(visitor, model.Role.READER, group) if mode[1] == u'w': model.add_user_to_role(visitor, model.Role.EDITOR, pkg) model.add_user_to_role(visitor, model.Role.EDITOR, group) model.add_user_to_role(testsysadmin, model.Role.ADMIN, model.System()) for entity_name, entity_class in entities_to_test_deleting: entity = entity_class.by_name(entity_name) model.add_user_to_role(visitor, model.Role.EDITOR, entity) model.add_user_to_role(mrloggedin, model.Role.EDITOR, entity) model.add_user_to_role(visitor, model.Role.READER, entity) model.add_user_to_role(mrloggedin, model.Role.READER, entity) model.add_user_to_role(pkggroupadmin, model.Role.ADMIN, entity) model.repo.commit_and_remove() assert model.Package.by_name(u'deleted').state == model.State.DELETED cls.testsysadmin = model.User.by_name(u'testsysadmin') cls.pkggroupadmin = model.User.by_name(u'pkggroupadmin') cls.pkgadminfriend = model.User.by_name(u'pkgadminfriend') cls.pkgeditor = model.User.by_name(u'pkgeditor') cls.pkgreader = model.User.by_name(u'pkgreader') cls.groupadmin = model.User.by_name(u'groupadmin') cls.groupeditor = model.User.by_name(u'groupeditor') cls.groupreader = model.User.by_name(u'groupreader') cls.mrloggedin = model.User.by_name(name=u'mrloggedin') cls.visitor = model.User.by_name(name=model.PSEUDO_USER__VISITOR)
def create_arbitrary(cls, package_dicts, relationships=[], extra_user_names=[], extra_group_names=[], admins=[]): '''Creates packages and a few extra objects as well at the same time if required. @param package_dicts - a list of dictionaries with the package properties. Extra keys allowed: "admins" - list of user names to make admin for this package. @param extra_group_names - a list of group names to create. No properties get set though. @param admins - a list of user names to make admins of all the packages created. ''' assert isinstance(relationships, (list, tuple)) assert isinstance(extra_user_names, (list, tuple)) assert isinstance(extra_group_names, (list, tuple)) import ckan.model as model model.Session.remove() new_user_names = extra_user_names new_group_names = set() new_groups = {} rev = model.repo.new_revision() rev.author = cls.author rev.message = u'Creating test packages.' admins_list = defaultdict(list) # package_name: admin_names if package_dicts: if isinstance(package_dicts, dict): package_dicts = [package_dicts] for item in package_dicts: pkg_dict = {} for field in cls.pkg_core_fields: if item.has_key(field): pkg_dict[field] = unicode(item[field]) pkg = model.Package(**pkg_dict) model.Session.add(pkg) for attr, val in item.items(): if isinstance(val, str): val = unicode(val) if attr=='name': continue if attr in cls.pkg_core_fields: pass elif attr == 'download_url': pkg.add_resource(unicode(val)) elif attr == 'resources': assert isinstance(val, (list, tuple)) for res_dict in val: non_extras = {} for k, v in res_dict.items(): if k != 'extras': if not isinstance(v, datetime.datetime): v = unicode(v) non_extras[str(k)] = v extras = dict([(str(k), unicode(v)) for k, v in res_dict.get('extras', {}).items()]) pkg.add_resource(extras=extras, **non_extras) elif attr == 'tags': if isinstance(val, (str, unicode)): tags = val.split() elif isinstance(val, list): tags = val else: raise NotImplementedError for tag_name in tags: tag_name = unicode(tag_name) tag = model.Tag.by_name(tag_name) if not tag: tag = model.Tag(name=tag_name) cls.tag_names.append(tag_name) model.Session.add(tag) pkg.add_tag(tag) model.Session.flush() elif attr == 'groups': model.Session.flush() if isinstance(val, (str, unicode)): group_names = val.split() elif isinstance(val, list): group_names = val else: raise NotImplementedError for group_name in group_names: group = model.Group.by_name(unicode(group_name)) if not group: if not group_name in new_groups: group = model.Group(name=unicode(group_name)) model.Session.add(group) new_group_names.add(group_name) new_groups[group_name] = group else: # If adding multiple packages with the same group name, # model.Group.by_name will not find the group as the # session has not yet been committed at this point. # Fetch from the new_groups dict instead. group = new_groups[group_name] member = model.Member(group=group, table_id=pkg.id, table_name='package') model.Session.add(member) elif attr == 'license': pkg.license_id = val elif attr == 'license_id': pkg.license_id = val elif attr == 'extras': pkg.extras = val elif attr == 'admins': assert isinstance(val, list) admins_list[item['name']].extend(val) for user_name in val: if user_name not in new_user_names: new_user_names.append(user_name) else: raise NotImplementedError(attr) cls.pkg_names.append(item['name']) model.setup_default_user_roles(pkg, admins=[]) for admin in admins: admins_list[item['name']].append(admin) model.repo.commit_and_remove() needs_commit = False rev = model.repo.new_revision() for group_name in extra_group_names: group = model.Group(name=unicode(group_name)) model.Session.add(group) new_group_names.add(group_name) needs_commit = True if needs_commit: model.repo.commit_and_remove() needs_commit = False # create users that have been identified as being needed for user_name in new_user_names: if not model.User.by_name(unicode(user_name)): user = model.User(name=unicode(user_name)) model.Session.add(user) cls.user_refs.append(user_name) needs_commit = True if needs_commit: model.repo.commit_and_remove() needs_commit = False # setup authz for admins for pkg_name, admins in admins_list.items(): pkg = model.Package.by_name(unicode(pkg_name)) admins_obj_list = [] for admin in admins: if isinstance(admin, model.User): admin_obj = admin else: admin_obj = model.User.by_name(unicode(admin)) assert admin_obj, admin admins_obj_list.append(admin_obj) model.setup_default_user_roles(pkg, admins_obj_list) needs_commit = True # setup authz for groups just created for group_name in new_group_names: group = model.Group.by_name(unicode(group_name)) model.setup_default_user_roles(group) cls.group_names.add(group_name) needs_commit = True if needs_commit: model.repo.commit_and_remove() needs_commit = False if relationships: rev = model.repo.new_revision() rev.author = cls.author rev.message = u'Creating package relationships.' def pkg(pkg_name): return model.Package.by_name(unicode(pkg_name)) for subject_name, relationship, object_name in relationships: pkg(subject_name).add_relationship( unicode(relationship), pkg(object_name)) needs_commit = True model.repo.commit_and_remove()
def create(cls, auth_profile="", package_type=None): import ckan.model as model model.Session.remove() rev = model.repo.new_revision() # same name as user we create below rev.author = cls.author rev.message = u'''Creating test data. * Package: annakarenina * Package: warandpeace * Associated tags, etc etc ''' if auth_profile == "publisher": publisher_group = model.Group(name=u"publisher_group", type="publisher") cls.pkg_names = [u'annakarenina', u'warandpeace'] pkg1 = model.Package(name=cls.pkg_names[0], type=package_type) if auth_profile == "publisher": pkg1.group = publisher_group model.Session.add(pkg1) pkg1.title = u'A Novel By Tolstoy' pkg1.version = u'0.7a' pkg1.url = u'http://www.annakarenina.com' # put an & in the url string to test escaping if 'alt_url' in model.Resource.get_extra_columns(): configured_extras = ({'alt_url': u'alt123'}, {'alt_url': u'alt345'}) else: configured_extras = ({}, {}) pr1 = model.Resource( url=u'http://www.annakarenina.com/download/x=1&y=2', format=u'plain text', description=u'Full text. Needs escaping: " Umlaut: \xfc', hash=u'abc123', extras={'size_extra': u'123'}, **configured_extras[0] ) pr2 = model.Resource( url=u'http://www.annakarenina.com/index.json', format=u'json', description=u'Index of the novel', hash=u'def456', extras={'size_extra': u'345'}, **configured_extras[1] ) model.Session.add(pr1) model.Session.add(pr2) pkg1.resources.append(pr1) pkg1.resources.append(pr2) pkg1.notes = u'''Some test notes ### A 3rd level heading **Some bolded text.** *Some italicized text.* Foreign characters: u with umlaut \xfc 66-style quote \u201c foreign word: th\xfcmb Needs escaping: left arrow < <http://ckan.net/> ''' pkg2 = model.Package(name=cls.pkg_names[1], type=package_type) tag1 = model.Tag(name=u'russian') tag2 = model.Tag(name=u'tolstoy') if auth_profile == "publisher": pkg2.group = publisher_group # Flexible tag, allows spaces, upper-case, # and all punctuation except commas tag3 = model.Tag(name=u'Flexible \u30a1') for obj in [pkg2, tag1, tag2, tag3]: model.Session.add(obj) pkg1.add_tags([tag1, tag2, tag3]) pkg2.add_tags([ tag1, tag3 ]) cls.tag_names = [ t.name for t in (tag1, tag2, tag3) ] pkg1.license_id = u'other-open' pkg2.license_id = u'cc-nc' # closed license pkg2.title = u'A Wonderful Story' pkg1.extras = {u'genre':'romantic novel', u'original media':'book'} # group david = model.Group(name=u'david', title=u'Dave\'s books', description=u'These are books that David likes.', type=auth_profile or 'group') roger = model.Group(name=u'roger', title=u'Roger\'s books', description=u'Roger likes these books.', type=auth_profile or 'group') for obj in [david, roger]: model.Session.add(obj) cls.group_names.add(u'david') cls.group_names.add(u'roger') model.Session.flush() model.Session.add(model.Member(table_id=pkg1.id, table_name='package', group=david)) model.Session.add(model.Member(table_id=pkg2.id, table_name='package', group=david)) model.Session.add(model.Member(table_id=pkg1.id, table_name='package', group=roger)) # authz model.Session.add_all([ model.User(name=u'tester', apikey=u'tester', password=u'tester'), model.User(name=u'joeadmin', password=u'joeadmin'), model.User(name=u'annafan', about=u'I love reading Annakarenina. My site: <a href="http://anna.com">anna.com</a>', password=u'annafan'), model.User(name=u'russianfan', password=u'russianfan'), model.User(name=u'testsysadmin', password=u'testsysadmin'), ]) cls.user_refs.extend([u'tester', u'joeadmin', u'annafan', u'russianfan', u'testsysadmin']) model.repo.commit_and_remove() visitor = model.User.by_name(model.PSEUDO_USER__VISITOR) anna = model.Package.by_name(u'annakarenina') war = model.Package.by_name(u'warandpeace') annafan = model.User.by_name(u'annafan') russianfan = model.User.by_name(u'russianfan') model.setup_default_user_roles(anna, [annafan]) model.setup_default_user_roles(war, [russianfan]) model.add_user_to_role(visitor, model.Role.ADMIN, war) david = model.Group.by_name(u'david') roger = model.Group.by_name(u'roger') model.setup_default_user_roles(david, [russianfan]) model.setup_default_user_roles(roger, [russianfan]) model.add_user_to_role(visitor, model.Role.ADMIN, roger) testsysadmin = model.User.by_name(u'testsysadmin') model.add_user_to_role(testsysadmin, model.Role.ADMIN, model.System()) model.repo.commit_and_remove() # Create a couple of authorization groups for ag_name in [u'anauthzgroup', u'anotherauthzgroup']: ag=model.AuthorizationGroup.by_name(ag_name) if not ag: #may already exist, if not create ag=model.AuthorizationGroup(name=ag_name) model.Session.add(ag) model.repo.commit_and_remove() # and give them a range of roles on various things ag = model.AuthorizationGroup.by_name(u'anauthzgroup') aag = model.AuthorizationGroup.by_name(u'anotherauthzgroup') pkg = model.Package.by_name(u'warandpeace') g = model.Group.by_name('david') model.add_authorization_group_to_role(ag, u'editor', model.System()) model.add_authorization_group_to_role(ag, u'reader', pkg) model.add_authorization_group_to_role(ag, u'admin', aag) model.add_authorization_group_to_role(aag, u'editor', ag) model.add_authorization_group_to_role(ag, u'editor', g) model.repo.commit_and_remove()