diff --git a/ckan/lib/cli.py b/ckan/lib/cli.py index 389ad17a11b..fadf096cf78 100644 --- a/ckan/lib/cli.py +++ b/ckan/lib/cli.py @@ -1088,3 +1088,59 @@ def function_info(self, functions): for bit in bits: output.append(' %s' % bit) return ('\n').join(output) + + +class CreateTestDataCommand(CkanCommand): + '''Create test data in the database. + Tests can also delete the created objects easily with the delete() method. + + create-test-data - annakarenina and warandpeace + create-test-data search - realistic data to test search + create-test-data gov - government style data + create-test-data family - package relationships data + create-test-data user - create a user 'tester' with api key 'tester' + create-test-data translations - annakarenina, warandpeace, and some test + translations of terms + create-test-data vocabs - annakerenina, warandpeace, and some test + vocabularies + + ''' + summary = __doc__.split('\n')[0] + usage = __doc__ + max_args = 1 + min_args = 0 + + def command(self): + self._load_config() + self._setup_app() + from ckan import plugins + plugins.load('synchronous_search') # so packages get indexed + from create_test_data import CreateTestData + + if self.args: + cmd = self.args[0] + else: + cmd = 'basic' + if self.verbose: + print 'Creating %s test data' % cmd + if cmd == 'basic': + CreateTestData.create_basic_test_data() + elif cmd == 'user': + CreateTestData.create_test_user() + print 'Created user %r with password %r and apikey %r' % ('tester', + 'tester', 'tester') + elif cmd == 'search': + CreateTestData.create_search_test_data() + elif cmd == 'gov': + CreateTestData.create_gov_test_data() + elif cmd == 'family': + CreateTestData.create_family_test_data() + elif cmd == 'translations': + CreateTestData.create_translations_test_data() + elif cmd == 'vocabs': + CreateTestData.create_vocabs_test_data() + else: + print 'Command %s not recognized' % cmd + raise NotImplementedError + if self.verbose: + print 'Creating %s test data: Complete!' % cmd diff --git a/ckan/lib/create_test_data.py b/ckan/lib/create_test_data.py index a3121aa9365..fbf18d39265 100644 --- a/ckan/lib/create_test_data.py +++ b/ckan/lib/create_test_data.py @@ -1,77 +1,26 @@ -import cli +import logging from collections import defaultdict import datetime -#NB Don't import here - do it in the methods. This is because all logging gets -# disabled if you don't _load_config first. - -class CreateTestData(cli.CkanCommand): - '''Create test data in the database. - Tests can also delete the created objects easily with the delete() method. - - create-test-data - annakarenina and warandpeace - create-test-data search - realistic data to test search - create-test-data gov - government style data - create-test-data family - package relationships data - create-test-data user - create a user 'tester' with api key 'tester' - create-test-data translations - annakarenina, warandpeace, and some test - translations of terms - create-test-data vocabs - annakerenina, warandpeace, and some test - vocabularies - - ''' - summary = __doc__.split('\n')[0] - usage = __doc__ - max_args = 1 - min_args = 0 - author = u'tester' +import ckan.model as model +import authztool + +log = logging.getLogger(__name__) +class CreateTestData(object): + # keep track of the objects created by this class so that + # tests can easy call delete() method to delete them all again. pkg_names = [] tag_names = [] group_names = set() user_refs = [] + author = u'tester' + pkg_core_fields = ['name', 'title', 'version', 'url', 'notes', 'author', 'author_email', 'maintainer', 'maintainer_email', ] - - def command(self): - self._load_config() - self._setup_app() - from ckan import plugins - plugins.load('synchronous_search') # so packages get indexed - import logging - CreateTestData.log = logging.getLogger(__name__) - - if self.args: - cmd = self.args[0] - else: - cmd = 'basic' - if self.verbose: - print 'Creating %s test data' % cmd - if cmd == 'basic': - self.create_basic_test_data() - elif cmd == 'user': - self.create_test_user() - print 'Created user %r with password %r and apikey %r' % ('tester', - 'tester', 'tester') - elif cmd == 'search': - self.create_search_test_data() - elif cmd == 'gov': - self.create_gov_test_data() - elif cmd == 'family': - self.create_family_test_data() - elif cmd == 'translations': - self.create_translations_test_data() - elif cmd == 'vocabs': - self.create_vocabs_test_data() - else: - print 'Command %s not recognized' % cmd - raise NotImplementedError - if self.verbose: - print 'Creating %s test data: Complete!' % cmd - @classmethod def create_basic_test_data(cls): cls.create() @@ -92,7 +41,6 @@ def create_family_test_data(cls, extra_users=[]): @classmethod def create_test_user(cls): - import ckan.model as model tester = model.User.by_name(u'tester') if tester is None: tester = model.User(name=u'tester', apikey=u'tester', @@ -196,7 +144,6 @@ def create_arbitrary(cls, package_dicts, relationships=[], 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() @@ -216,7 +163,7 @@ def create_arbitrary(cls, package_dicts, relationships=[], if item.has_key(field): pkg_dict[field] = unicode(item[field]) if model.Package.by_name(pkg_dict['name']): - cls.log.warning('Cannot create package "%s" as it already exists.' % \ + log.warning('Cannot create package "%s" as it already exists.' % \ (pkg_dict['name'])) continue pkg = model.Package(**pkg_dict) @@ -371,7 +318,6 @@ def create_groups(cls, group_dicts, admin_user_name=None, auth_profile=""): '''A more featured interface for creating groups. All group fields can be filled, packages added and they can have an admin user.''' - import ckan.model as model rev = model.repo.new_revision() # same name as user we create below rev.author = cls.author @@ -383,7 +329,7 @@ def create_groups(cls, group_dicts, admin_user_name=None, auth_profile=""): group_attributes = set(('name', 'title', 'description', 'parent_id')) for group_dict in group_dicts: if model.Group.by_name(group_dict['name']): - cls.log.warning('Cannot create group "%s" as it already exists.' % \ + log.warning('Cannot create group "%s" as it already exists.' % \ (group_dict['name'])) continue group = model.Group(name=unicode(group_dict['name'])) @@ -407,7 +353,6 @@ def create_groups(cls, group_dicts, admin_user_name=None, auth_profile=""): @classmethod 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 @@ -578,9 +523,8 @@ def create_users(cls, user_dicts): @classmethod def _create_user_without_commit(cls, name='', **user_dict): - import ckan.model as model - if model.User.by_name(name or user_dict.get('name') or user_dict.get('openid')): - cls.log.warning('Cannot create user "%s" as it already exists.' % \ + if model.User.by_name(name) or model.User.by_openid(user_dict.get('openid')): + log.warning('Cannot create user "%s" as it already exists.' % \ (name or user_dict['name'])) return # User objects are not revisioned so no need to create a revision @@ -596,7 +540,6 @@ def _create_user_without_commit(cls, name='', **user_dict): @classmethod def create_user(cls, name='', **kwargs): - import ckan.model as model cls._create_user_without_commit(name, **kwargs) model.Session.commit() @@ -606,8 +549,6 @@ def create_roles(cls, roles): There is clever searching going on to find the objects of any type, by name or ID. You can also use the subject_name='system'. ''' - import authztool - import ckan.model as model for role_tuple in roles: object_name, role, subject_name = role_tuple authztool.RightsTool.make_or_remove_roles('make', object_name, role, subject_name, @@ -631,7 +572,6 @@ def flag_for_deletion(cls, pkg_names=[], tag_names=[], group_names=[], @classmethod def delete(cls): '''Purges packages etc. that were created by this class.''' - import ckan.model as model for pkg_name in cls.pkg_names: model.Session().autoflush = False pkg = model.Package.by_name(unicode(pkg_name)) @@ -674,7 +614,6 @@ def get_all_data(cls): @classmethod def make_some_vocab_tags(cls): - import ckan.model as model model.repo.new_revision() # Create a couple of vocabularies. diff --git a/setup.py b/setup.py index d1e857d9802..0aec4622e29 100644 --- a/setup.py +++ b/setup.py @@ -60,7 +60,7 @@ [paste.paster_command] db = ckan.lib.cli:ManageDb - create-test-data = ckan.lib.create_test_data:CreateTestData + create-test-data = ckan.lib.cli:CreateTestDataCommand sysadmin = ckan.lib.cli:Sysadmin user = ckan.lib.cli:UserCmd dataset = ckan.lib.cli:DatasetCmd