def test_1_basic(self): schema = default_create_package_schema() context = { 'model': model, 'session': Session, 'user': '******', 'extras_as_string': True, 'schema': schema, 'api_version': 2 } package_dict_1 = package_create(context, self.package_fixture_data_1) del context['package'] package_dict_2 = package_create(context, self.package_fixture_data_2) postparams = '%s=1' % json.dumps( { 'q': 'test', 'facet.field': ('groups', 'tags', 'res_format', 'license'), 'rows': 20, 'start': 0, 'extras': { 'ext_bbox': '%s,%s,%s,%s' % (10, 10, 40, 40) } }) res = self.app.post('/api/action/package_search', params=postparams) res = json.loads(res.body) result = res['result'] # Only one dataset returned assert_equal(res['success'], True) assert_equal(result['count'], 1) assert_equal(result['results'][0]['name'], 'test-spatial-dataset-search-point-2')
def package_create(context, data_dict): try: toolkit.check_access('sysadmin', context, data_dict) return create_core.package_create(context, data_dict) except toolkit.NotAuthorized: data_dict['private'] = True return create_core.package_create(context, data_dict)
def package_create_rest_minimal(context, data_dict): setup() package = '' fulltext = '' old_fulltext = '' categories = [] if all(isinstance(n, basestring) for n in data_dict['groups']): groups = data_dict['groups'] for g in groups: categories.append({'name': g}) data_dict['groups'] = categories if all(isinstance(n, basestring) for n in data_dict['tags']): tags = [] for tag in data_dict['tags']: tags.append({'name': tag}) data_dict['tags'] = tags if data_dict.has_key('extras'): if 'full_text_search' in data_dict['extras'].keys(): fulltext = data_dict['extras']['full_text_search'] data_dict = _del_extra_field_from_list(data_dict, 'full_text_search') data_dict['extras'] = _get_extras_dict(data_dict['extras']) package = create.package_create(context, data_dict) old_fulltext = None if package.has_key('id'): old_fulltext = Session.query(PackageFulltext) \ .filter(PackageFulltext.package_id==package['id']) \ .first() fulltext_dict_save(fulltext, old_fulltext, package, context) else: data_dict['extras'] = _get_extras_dict(data_dict['extras']) package = create.package_create(context, data_dict) else: data_dict['extras'] = _get_extras_dict(data_dict['extras']) package = create.package_create(context, data_dict) if check_logged_in(context): fulltext = _get_fulltext(package['id']) if fulltext: package['extras']['full_text_search'] = fulltext.text return package minimal_package = _del_extra_field_from_list(package) minimal_package = _del_main_field_from_dict(minimal_package) return minimal_package
def package_create_rest_minimal(context, data_dict): setup() package= '' fulltext = '' old_fulltext = '' categories = [] if all(isinstance(n, basestring) for n in data_dict['groups']): groups = data_dict['groups'] for g in groups: categories.append({'name': g}) data_dict['groups']= categories if all(isinstance(n, basestring) for n in data_dict['tags']): tags = [] for tag in data_dict['tags']: tags.append({'name': tag}) data_dict['tags'] = tags if data_dict.has_key('extras'): if 'full_text_search' in data_dict['extras'].keys(): fulltext = data_dict['extras']['full_text_search'] data_dict = _del_extra_field_from_list(data_dict, 'full_text_search') data_dict['extras'] = _get_extras_dict(data_dict['extras']) package = create.package_create(context, data_dict) old_fulltext = None if package.has_key('id'): old_fulltext = Session.query(PackageFulltext) \ .filter(PackageFulltext.package_id==package['id']) \ .first() fulltext_dict_save(fulltext, old_fulltext, package, context) else: data_dict['extras'] = _get_extras_dict(data_dict['extras']) package = create.package_create(context, data_dict) else: data_dict['extras'] = _get_extras_dict(data_dict['extras']) package = create.package_create(context, data_dict) if check_logged_in(context): fulltext = _get_fulltext(package['id']) if fulltext: package['extras']['full_text_search'] = fulltext.text return package minimal_package = _del_extra_field_from_list(package) minimal_package = _del_main_field_from_dict(minimal_package) return minimal_package
def package_create(context, data_dict): # The only thing we do here is remove some extras that are always # inherited from the dataset publisher, to avoid duplicating them _remove_extras_from_data_dict(data_dict) return create_core.package_create(context, data_dict)
def test_basic_query(self): schema = default_create_package_schema() context = {'model':model,'session':Session,'user':'******','extras_as_string':True,'schema':schema,'api_version':2} package_dict = package_create(context,self.package_fixture_data) package_id = context.get('id') # Point inside bbox offset = self._offset_with_bbox() res = self.app.get(offset, status=200) res_dict = self.data_from_res(res) assert res_dict['count'] == 1 assert res_dict['results'][0] == package_id # Point outside bbox offset = self._offset_with_bbox(-10,10,-20,20) res = self.app.get(offset, status=200) res_dict = self.data_from_res(res) assert res_dict['count'] == 0 assert res_dict['results'] == [] # Delete the package and ensure it does not come up on # search results package_delete(context,{'id':package_id}) offset = self._offset_with_bbox() res = self.app.get(offset, status=200) res_dict = self.data_from_res(res) assert res_dict['count'] == 0 assert res_dict['results'] == []
def pkg_create(context, data_dict): log.debug('my very own package_create() called') # 'return_minimal' will only get the user information and not any dataset associated with the user # without return_minimal' the context object will change and point to some other dataset # which will get overwritten context['return_minimal'] = True user = p.toolkit.get_action('user_show')(context, {'id': context['user']}) data_dict['sub_name'] = user['fullname'] data_dict['sub_email'] = user['email'] data_dict['creator_organization'] = '' data_dict['creator_address'] = '' data_dict['creator_phone'] = '' data_dict['version'] = u'1.0' data_dict['license_id'] = u'cc-by' data_dict['citation'] = u'' #if organization is iutah iutahorg = p.toolkit.get_action('organization_show')(context, {'id': 'iutah'}) if data_dict['owner_org'] == iutahorg['id']: data_dict['private'] = True # remove if there any deleted repeatable elements from the data_dict _remove_deleted_repeatable_elements(data_dict, 'creators') _remove_deleted_repeatable_elements(data_dict, 'contributors') _remove_deleted_repeatable_elements(data_dict, 'variables') # remove any invalid variables _remove_invalid_variables(data_dict) p.toolkit.check_access('package_create',context, data_dict) pkg = package_create(context, data_dict) return pkg
def dataset_create(context, data_dict=None): ret_val = package_create(context, data_dict) if not context.get('defer_commit') and not ret_val['private']: log.debug("package_create sync dataset") queue.put(ret_val) return ret_val
def package_create(context, data_dict): deployment_mode = toolkit.asbool(config.get('ckan.ab_scheming.deployment', False)) # need to change data_dict if import from ckanapi if deployment_mode: data_dict = change_pkg_dict_for_import_deployment(data_dict, 'create') if data_dict['type'] in ['publications', 'opendata']: context['defer_commit'] = True return create.package_create(context, data_dict)
def drupal_package_create(self, context, data_dict): session = context['model'].Session context['nid'] = data_dict.pop('nid') context['extras_as_string'] = True package_create = create.package_create(context, data_dict) package_create['nid'] = context['nid'] package_create['revision_message'] = '%s-%s'%(session.revision.id,session.revision.message) return package_create
def create_package(cls, **package_dict): context = {'model': model, 'session': model.Session, 'user': '******', 'extras_as_string': True, 'schema': default_create_package_schema(), 'api_version': 2} package_dict = package_create(context, package_dict) return context.get('id')
def create_package(cls, **package_dict): user = plugins.toolkit.get_action('get_site_user')({'model': model, 'ignore_auth': True}, {}) context = {'model': model, 'session': model.Session, 'user': user['name'], 'extras_as_string': True, 'api_version': 2} package_dict = package_create(context, package_dict) return context.get('id')
def drupal_package_create(self, context, data_dict): session = context['model'].Session context['nid'] = data_dict.pop('nid') context['extras_as_string'] = True package_create = create.package_create(context, data_dict) package_create['nid'] = context['nid'] package_create['revision_message'] = '%s-%s' % ( session.revision.id, session.revision.message) return package_create
def setup(self): """Prepare each test""" # Setup a dummy datastore. self.dataset = package_create(TestViewCreated.context, {'name': 'map-test-dataset'}) self.resource = datastore_create(TestViewCreated.context, { 'resource': { 'package_id': self.dataset['id'] }, 'fields': [ {'id': 'id', 'type': 'integer'}, {'id': 'latitude', 'type': 'double precision'}, {'id': 'longitude', 'type': 'double precision'}, {'id': 'long2', 'type': 'double precision'}, {'id': 'text', 'type': 'text'}, {'id': 'big', 'type': 'double precision'}, ], 'primary_key': 'id' }) # Add some data. datastore_upsert(TestViewCreated.context, { 'resource_id': self.resource['resource_id'], 'method': 'upsert', 'records': [{ 'id': 1, 'latitude': -11, 'longitude': -15, 'long2': 22, 'text': 'hello', 'big': '-1000' }, { 'id': 2, 'latitude': 23, 'longitude': 48, 'long2': -12, 'text': 'hello', 'big': '199' }] }) # Base dict for view creation/update methods self.base_data_dict = { 'description': u'', 'title': u'test', 'resource_id': self.resource['resource_id'], 'plot_marker_color': u'#EE0000', 'enable_plot_map': u'True', 'overlapping_records_view': u'', 'longitude_field': u'longitude', 'heat_intensity': u'0.1', 'view_type': u'tiledmap', 'utf_grid_title': u'_id', 'plot_marker_line_color': u'#FFFFFF', 'latitude_field': u'latitude', 'enable_utf_grid': u'True', 'grid_base_color': u'#F02323' }
def create_package(cls, **package_dict): user = plugins.toolkit.get_action("get_site_user")({"model": model, "ignore_auth": True}, {}) context = { "model": model, "session": model.Session, "user": user["name"], "extras_as_string": True, "api_version": 2, "ignore_auth": True, } package_dict = package_create(context, package_dict) return context.get("id")
def setup(self): """Prepare each test""" # Setup a dummy datastore. self.dataset = package_create(TestMapActions.context, {'name': 'map-test-dataset'}) self.resource = datastore_create(TestMapActions.context, { 'resource': { 'package_id': self.dataset['id'] }, 'fields': [ {'id': 'id', 'type': 'integer'}, {'id': 'latitude', 'type': 'double precision'}, {'id': 'longitude', 'type': 'double precision'}, {'id': 'skip', 'type': 'text'}, {'id': 'lat2', 'type': 'double precision'}, {'id': 'long2', 'type': 'double precision'} ], 'primary_key': 'id' }) # Add some data. datastore_upsert(TestMapActions.context, { 'resource_id': self.resource['resource_id'], 'method': 'upsert', 'records': [{ 'id': 1, 'latitude': -11, 'longitude': -15, 'skip': 'no', 'lat2': 1, 'long2': 1 }, { 'id': 2, 'latitude': 23, 'longitude': 48, 'skip': 'no', 'lat2': 2, 'long2': 2 }, { 'id': 3, 'latitude': None, 'longitude': None, 'skip': 'yes', 'lat2': None, 'long2': None }, { 'id': 4, 'latitude': 1234, 'longitude': 1234, 'skip': 'yes', 'lat2': None, 'long2': None }] })
def create_package(cls, **package_dict): user = plugins.toolkit.get_action('get_site_user')({ 'model': model, 'ignore_auth': True }, {}) context = { 'model': model, 'session': model.Session, 'user': user['name'], 'extras_as_string': True, 'api_version': 2 } package_dict = package_create(context, package_dict) return context.get('id')
def create_package(**package_dict): user = plugins.toolkit.get_action("get_site_user")({ "model": model, "ignore_auth": True }, {}) context = { "model": model, "session": model.Session, "user": user["name"], "extras_as_string": True, "api_version": 2, "ignore_auth": True, } package_dict = package_create(context, package_dict) return context.get("id")
def test_1_basic(self): schema = default_create_package_schema() context = {'model':model,'session':Session,'user':'******','extras_as_string':True,'schema':schema,'api_version':2} package_dict_1 = package_create(context,self.package_fixture_data_1) del context['package'] package_dict_2 = package_create(context,self.package_fixture_data_2) postparams = '%s=1' % json.dumps({ 'q': 'test', 'facet.field': ('groups', 'tags', 'res_format', 'license'), 'rows': 20, 'start': 0, 'extras': { 'ext_bbox': '%s,%s,%s,%s' % (10,10,40,40) } }) res = self.app.post('/api/action/package_search', params=postparams) res = json.loads(res.body) result = res['result'] # Only one dataset returned assert_equal(res['success'], True) assert_equal(result['count'], 1) assert_equal(result['results'][0]['name'], 'test-spatial-dataset-search-point-2')
def package_create(self, context, data_dict): preview = context.get('preview', False) schema = context.get('schema') or default_create_package_schema() if preview: return session = context['model'].Session url = urlparse.urljoin(self.base_url, 'services/package.json') data_dict['body'] = data_dict.get('notes', '') ## run through validate to make sure tags are in correct place data, errors = validate(data_dict, schema, context) terms = {} for num, tag in enumerate(data.get('tags', [])): terms[str(num)] = tag['name'] data_dict['terms'] = terms data = json.dumps({'data': data_dict}) req = urllib2.Request(url, data, {'Content-type': 'application/json'}) ##XXX think about error conditions a bit more f = urllib2.urlopen(req, None, 3) try: drupal_info = json.loads(f.read()) finally: f.close() nid = drupal_info['nid'] context['nid'] = nid try: package_create = create.package_create(context, data_dict) except: url = urlparse.urljoin(self.base_url, 'services/package/%s.json' % (nid)) req = urllib2.Request(url) req.get_method = lambda: 'DELETE' f = urllib2.urlopen(req, None, 3) try: drupal_info = f.read() finally: f.close() raise package_create['nid'] = context['nid'] package_create['revision_message'] = '%s-%s' % ( session.revision.id, session.revision.message) return package_create
def package_create(self, context, data_dict): preview = context.get('preview', False) schema = context.get('schema') or default_create_package_schema() if preview: return session = context['model'].Session url = urlparse.urljoin(self.base_url, 'services/package.json') data_dict['body'] = data_dict.get('notes', '') ## run through validate to make sure tags are in correct place data, errors = validate(data_dict, schema, context) terms = {} for num, tag in enumerate(data.get('tags', [])): terms[str(num)] = tag['name'] data_dict['terms'] = terms data = json.dumps({'data': data_dict}) req = urllib2.Request(url, data, {'Content-type': 'application/json'}) ##XXX think about error conditions a bit more f = urllib2.urlopen(req, None, 3) try: drupal_info = json.loads(f.read()) finally: f.close() nid = drupal_info['nid'] context['nid'] = nid try: package_create = create.package_create(context, data_dict) except: url = urlparse.urljoin(self.base_url, 'services/package/%s.json' % (nid)) req = urllib2.Request(url) req.get_method = lambda: 'DELETE' f = urllib2.urlopen(req, None, 3) try: drupal_info = f.read() finally: f.close() raise package_create['nid'] = context['nid'] package_create['revision_message'] = '%s-%s'%(session.revision.id,session.revision.message) return package_create
def test_basic_query(self): schema = default_create_package_schema() context = { 'model': model, 'session': Session, 'user': '******', 'extras_as_string': True, 'schema': schema, 'api_version': 2 } package_dict = package_create(context, self.package_fixture_data) package_id = context.get('id') # Point inside bbox offset = self._offset_with_bbox() res = self.app.get(offset, status=200) res_dict = self.data_from_res(res) assert res_dict['count'] == 1 assert res_dict['results'][0] == package_id # Point outside bbox offset = self._offset_with_bbox(-10, 10, -20, 20) res = self.app.get(offset, status=200) res_dict = self.data_from_res(res) assert res_dict['count'] == 0 assert res_dict['results'] == [] # Delete the package and ensure it does not come up on # search results package_delete(context, {'id': package_id}) offset = self._offset_with_bbox() res = self.app.get(offset, status=200) res_dict = self.data_from_res(res) assert res_dict['count'] == 0 assert res_dict['results'] == []
def package_create(context, data_dict): # The only thing we do here is remove some extras that are always # inherited from the dataset publisher, to avoid duplicating them _remove_extras_from_data_dict(data_dict) created_package = create_core.package_create(context, data_dict) # Part of first publisher date patch - after package create patch the organization if 'owner_org' in data_dict.keys(): hlp.first_published_date_patch(created_package.get('owner_org')) if created_package.get('state') != "deleted" and not created_package.get( "private"): package_title = created_package.get('title') or created_package.get( 'name') send_data_published_notification(context, created_package.get('owner_org', ''), package_title) # Data is published send an email return created_package
def package_create(context, data_dict): if type(data_dict) is dict: log.debug('Creating mapping...') mapped_resources = [] if 'resources' in data_dict: for resource in data_dict['resources']: mapped_resource = generate_mapping(context, resource) mapped_resources.append(mapped_resource) data_dict['resources'] = mapped_resources package_dict = create.package_create(context, data_dict) package = None if type(package_dict) is not dict: package = get.package_show(context, {'id': package_dict}) else: package = get.package_show(context, {'id': package_dict['id']}) if package is not None: if package['type'] == 'dataset': send_dataset_log(context, package, 'Dataset created', 'DatasetPublished') update_de(package) return package_dict
def showcase_create(context, data_dict): '''Upload the image and continue with package creation.''' # force type to 'showcase' data_dict['type'] = 'showcase' # If get_uploader is available (introduced for IUploader in CKAN 2.5), use # it, otherwise use the default uploader. # https://github.com/ckan/ckan/pull/2510 try: upload = uploader.get_uploader('showcase') except AttributeError: upload = uploader.Upload('showcase') upload.update_data_dict(data_dict, 'image_url', 'image_upload', 'clear_upload') upload.upload(uploader.get_max_image_size()) pkg = package_create(context, data_dict) #pkg = toolkit.get_action('package_create')(context, data_dict) return pkg
def package_create_minimal(context, data_dict): '''Create a new dataset (package). You must be authorized to create new datasets. If you specify any groups for the new dataset, you must also be authorized to edit these groups. Plugins may change the parameters of this function depending on the value of the ``type`` parameter, see the :py:class:`~ckan.plugins.interfaces.IDatasetForm` plugin interface. :param name: the name of the new dataset, must be between 2 and 100 characters long and contain only lowercase alphanumeric characters, ``-`` and ``_``, e.g. ``'warandpeace'`` :type name: string :param title: the title of the dataset (optional, default: same as ``name``) :type title: string :param author: the name of the dataset's author (optional) :type author: string :param author_email: the email address of the dataset's author (optional) :type author_email: string :param maintainer: the name of the dataset's maintainer (optional) :type maintainer: string :param maintainer_email: the email address of the dataset's maintainer (optional) :type maintainer_email: string :param license_id: the id of the dataset's license, see :py:func:`~ckan.logic.action.get.license_list` for available values (optional) :type license_id: license id string :param notes: a description of the dataset (optional) :type notes: string :param url: a URL for the dataset's source (optional) :type url: string :param version: (optional) :type version: string, no longer than 100 characters :param state: the current state of the dataset, e.g. ``'active'`` or ``'deleted'``, only active datasets show up in search results and other lists of datasets, this parameter will be ignored if you are not authorized to change the state of the dataset (optional, default: ``'active'``) :type state: string :param type: the type of the dataset (optional), :py:class:`~ckan.plugins.interfaces.IDatasetForm` plugins associate themselves with different dataset types and provide custom dataset handling behaviour for these types :type type: string :param resources: the dataset's resources, see :py:func:`resource_create` for the format of resource dictionaries (optional) :type resources: list of resource dictionaries :param tags: the dataset's tags, see :py:func:`tag_create` for the format of tag dictionaries (optional) :type tags: list of tag dictionaries :param extras: the dataset's extras (optional), extras are arbitrary (key: value) metadata items that can be added to datasets, each extra dictionary should have keys ``'key'`` (a string), ``'value'`` (a string) :type extras: list of dataset extra dictionaries :param relationships_as_object: see :py:func:`package_relationship_create` for the format of relationship dictionaries (optional) :type relationships_as_object: list of relationship dictionaries :param relationships_as_subject: see :py:func:`package_relationship_create` for the format of relationship dictionaries (optional) :type relationships_as_subject: list of relationship dictionaries :param groups: the groups to which the dataset belongs (optional), each group dictionary should have one or more of the following keys which identify an existing group: ``'id'`` (the id of the group, string), ``'name'`` (the name of the group, string), ``'title'`` (the title of the group, string), to see which groups exist call :py:func:`~ckan.logic.action.get.group_list` :type groups: list of dictionaries :param owner_org: the id of the dataset's owning organization, see :py:func:`~ckan.logic.action.get.organization_list` or :py:func:`~ckan.logic.action.get.organization_list_for_user` for available values (optional) :type owner_org: string :returns: the newly created dataset (unless 'return_id_only' is set to True in the context, in which case just the dataset id will be returned) :rtype: dictionary ''' setup() package= '' fulltext = '' old_fulltext = '' if data_dict.has_key('extras'): contains = _contains_key(data_dict['extras'], 'full_text_search') if(contains): fulltext = contains data_dict = _del_extra_field_from_dict(data_dict, 'full_text_search') package = create.package_create(context, data_dict) old_fulltext = None if package.has_key('id'): old_fulltext = Session.query(PackageFulltext) \ .filter(PackageFulltext.package_id==package['id']) \ .first() fulltext_dict_save(fulltext, old_fulltext, package, context) else: package = create.package_create(context, data_dict) else: package = create.package_create(context, data_dict) if check_logged_in(context): if fulltext: # why fulltext.text? Left it for compatibility if isinstance(fulltext,unicode): valueFulltext = fulltext else: valueFulltext = fulltext.text fulltext_dict = { 'key': 'full_text_search', 'value': valueFulltext } package['extras'].append(fulltext_dict) return package minimal_package = _del_extra_field_from_dict(package) minimal_package = _del_main_field_from_dict(minimal_package) return minimal_package
def setup_class(cls, mock_flash): """Prepare the test""" # We need datastore for these tests. if not tests.is_datastore_supported(): raise nose.SkipTest("Datastore not supported") # Setup a test app wsgiapp = middleware.make_app(config['global_conf'], **config) cls.app = paste.fixture.TestApp(wsgiapp) ctd.CreateTestData.create() cls.context = {'model': ckan.model, 'session': ckan.model.Session, 'user': ckan.model.User.get('testsysadmin').name} # Load plugins p.load('tiledmap') p.load('datastore') # Set windshaft host/port as these settings do not have a default. # TODO: Test that calls fail if not set tm_config.update({ 'tiledmap.windshaft.host': '127.0.0.1', 'tiledmap.windshaft.port': '4000' }) # Copy tiledmap settings cls.config = dict(tm_config.items()) # Setup a dummy datastore. cls.dataset = package_create(cls.context, {'name': 'map-test-dataset'}) cls.resource = datastore_create(cls.context, { 'resource': { 'package_id': cls.dataset['id'] }, 'fields': [ {'id': 'id', 'type': 'integer'}, {'id': 'latitude', 'type': 'numeric'}, {'id': 'longitude', 'type': 'numeric'}, {'id': 'some_field_1', 'type': 'text'}, {'id': 'some_field_2', 'type': 'text'} ], 'primary_key': 'id' }) # Add some data. We add 4 records such that: # - The first three records have 'some_field_1' set to 'hello' ; # - The third record does not have a geom ; # - The fourth record has a geom, but 'some_field_1' is set to something elese. datastore_upsert(cls.context, { 'resource_id': cls.resource['resource_id'], 'method': 'upsert', 'records': [{ 'id': '1', 'latitude': -15, 'longitude': -11, 'some_field_1': 'hello', 'some_field_2': 'world' }, { 'id': 2, 'latitude': 48, 'longitude': 23, 'some_field_1': 'hello', 'some_field_2': 'again' }, { 'id': 3, 'latitude': None, 'longitude': None, 'some_field_1': 'hello', 'some_field_2': 'hello' }, { 'id': 4, 'latitude': 80, 'longitude': 80, 'some_field_1': 'all your bases', 'some_field_2': 'are belong to us' }] }) # Create a tiledmap resource view. This process itself is fully tested in test_view_create.py. # This will also generate the geometry column - that part of the process is fully tested in test_actions data_dict = { 'description': u'', 'title': u'test', 'resource_id': cls.resource['resource_id'], 'plot_marker_color': u'#EE0000', 'enable_plot_map': u'True', 'overlapping_records_view': u'', 'longitude_field': u'longitude', 'heat_intensity': u'0.1', 'view_type': u'tiledmap', 'utf_grid_title': u'_id', 'plot_marker_line_color': u'#FFFFFF', 'latitude_field': u'latitude', 'enable_utf_grid': u'True', 'utf_grid_fields' : ['some_field_1', 'some_field_2'], 'grid_base_color': u'#F02323', 'enable_heat_map': u'True', 'enable_grid_map': u'True' } resource_view_create = p.toolkit.get_action('resource_view_create') cls.resource_view = resource_view_create(cls.context, data_dict) # Create a resource that does not have spatial fields cls.non_spatial_resource = datastore_create(cls.context, { 'resource': { 'package_id': cls.dataset['id'] }, 'fields': [ {'id': 'id', 'type': 'integer'}, {'id': 'some_field', 'type': 'text'} ], 'primary_key': 'id' })
def package_create(context, data_dict): if data_dict.get('type') == 'dataset': data_dict = _package_create_or_update(data_dict) return create_core.package_create(context, data_dict)
def SaerischemaPlugin_package_create(context, data_dict): data_dict = SaerischemaPlugin_add_group_to_package_based_on_topic_category( context, data_dict) return package_create(context, data_dict)
def package_create_minimal(context, data_dict): '''Create a new dataset (package). You must be authorized to create new datasets. If you specify any groups for the new dataset, you must also be authorized to edit these groups. Plugins may change the parameters of this function depending on the value of the ``type`` parameter, see the :py:class:`~ckan.plugins.interfaces.IDatasetForm` plugin interface. :param name: the name of the new dataset, must be between 2 and 100 characters long and contain only lowercase alphanumeric characters, ``-`` and ``_``, e.g. ``'warandpeace'`` :type name: string :param title: the title of the dataset (optional, default: same as ``name``) :type title: string :param author: the name of the dataset's author (optional) :type author: string :param author_email: the email address of the dataset's author (optional) :type author_email: string :param maintainer: the name of the dataset's maintainer (optional) :type maintainer: string :param maintainer_email: the email address of the dataset's maintainer (optional) :type maintainer_email: string :param license_id: the id of the dataset's license, see :py:func:`~ckan.logic.action.get.license_list` for available values (optional) :type license_id: license id string :param notes: a description of the dataset (optional) :type notes: string :param url: a URL for the dataset's source (optional) :type url: string :param version: (optional) :type version: string, no longer than 100 characters :param state: the current state of the dataset, e.g. ``'active'`` or ``'deleted'``, only active datasets show up in search results and other lists of datasets, this parameter will be ignored if you are not authorized to change the state of the dataset (optional, default: ``'active'``) :type state: string :param type: the type of the dataset (optional), :py:class:`~ckan.plugins.interfaces.IDatasetForm` plugins associate themselves with different dataset types and provide custom dataset handling behaviour for these types :type type: string :param resources: the dataset's resources, see :py:func:`resource_create` for the format of resource dictionaries (optional) :type resources: list of resource dictionaries :param tags: the dataset's tags, see :py:func:`tag_create` for the format of tag dictionaries (optional) :type tags: list of tag dictionaries :param extras: the dataset's extras (optional), extras are arbitrary (key: value) metadata items that can be added to datasets, each extra dictionary should have keys ``'key'`` (a string), ``'value'`` (a string) :type extras: list of dataset extra dictionaries :param relationships_as_object: see :py:func:`package_relationship_create` for the format of relationship dictionaries (optional) :type relationships_as_object: list of relationship dictionaries :param relationships_as_subject: see :py:func:`package_relationship_create` for the format of relationship dictionaries (optional) :type relationships_as_subject: list of relationship dictionaries :param groups: the groups to which the dataset belongs (optional), each group dictionary should have one or more of the following keys which identify an existing group: ``'id'`` (the id of the group, string), ``'name'`` (the name of the group, string), ``'title'`` (the title of the group, string), to see which groups exist call :py:func:`~ckan.logic.action.get.group_list` :type groups: list of dictionaries :param owner_org: the id of the dataset's owning organization, see :py:func:`~ckan.logic.action.get.organization_list` or :py:func:`~ckan.logic.action.get.organization_list_for_user` for available values (optional) :type owner_org: string :returns: the newly created dataset (unless 'return_id_only' is set to True in the context, in which case just the dataset id will be returned) :rtype: dictionary ''' setup() package = '' fulltext = '' old_fulltext = '' if data_dict.has_key('extras'): contains = _contains_key(data_dict['extras'], 'full_text_search') if (contains): fulltext = contains data_dict = _del_extra_field_from_dict(data_dict, 'full_text_search') package = create.package_create(context, data_dict) old_fulltext = None if package.has_key('id'): old_fulltext = Session.query(PackageFulltext) \ .filter(PackageFulltext.package_id==package['id']) \ .first() fulltext_dict_save(fulltext, old_fulltext, package, context) else: package = create.package_create(context, data_dict) else: package = create.package_create(context, data_dict) if check_logged_in(context): if fulltext: # why fulltext.text? Left it for compatibility if isinstance(fulltext, unicode): valueFulltext = fulltext else: valueFulltext = fulltext.text fulltext_dict = {'key': 'full_text_search', 'value': valueFulltext} package['extras'].append(fulltext_dict) return package minimal_package = _del_extra_field_from_dict(package) minimal_package = _del_main_field_from_dict(minimal_package) return minimal_package
def test_activity(self): """Test activity streams HTML rendering.""" # Register a new user. user_dict = {'name': 'billybeane', 'fullname': 'Billy Beane', 'about': 'General Manager, Oakland Athletics', 'email': '*****@*****.**', 'password': '******'} context = { 'model': ckan.model, 'session': ckan.model.Session, 'user': self.sysadmin_user.name, 'allow_partial_update': True, 'extras_as_string': True, } user = user_create(context, user_dict) offset = url_for(controller='user', action='read', id=user['id']) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s signed up.' % user['fullname'] in stripped, stripped # Create a new package. package = { 'name' : 'baseball_stats', 'title' : "Billy's Stats about Baseball Players", } context['user'] = user['name'] package = package_create(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s created the dataset %s ' % ( user['fullname'], package['title']) in stripped, stripped # Add a resource to the package. resource = { 'url': 'http://www.example.com', 'description': "Chad Bradford's OBP Stats`", 'format': 'cvs', 'name': 'Chad Bradford Stats', } package['resources'].append(resource) request_data = { 'id': package['id'], 'resources': package['resources'], } package = package_update(context, request_data) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s added the resource %s to the dataset %s' % \ (user['fullname'], resource['name'], package['title']) \ in stripped, stripped # Update the package. package['title'] = "Billy's Updated Stats about Baseball Players" package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated the dataset %s' \ % (user['fullname'], package['title']) \ in stripped, stripped # Update the resource. resource = package['resources'][0] resource['name'] = 'Chad Bradford Updated Stats' resource = resource_update(context, resource) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated the resource %s in the dataset %s' \ % (user['fullname'], resource['name'], package['title']) \ in stripped, stripped # Delete the resource. context['allow_partial_update'] = False package['resources'] = [] package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the resource %s from the dataset %s' % \ (user['fullname'], resource['name'], package['title']) \ in stripped, stripped # Create a new group. group = { 'name': 'baseball-stats-group', 'title': 'A Group for Datasets about Baseball' } context['allow_partial_update'] = True group = group_create(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s created the group %s' % (user['fullname'], group['name']) \ in stripped, stripped # Update the group. group['title'] = 'updated' group = group_update(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated the group %s' % (user['fullname'], group['name']) \ in stripped, stripped # Delete the group. group['state'] = 'deleted' group_update(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the group %s' % (user['fullname'], group['name']) \ in stripped, stripped # Add a new tag to the package. tag = {'name': 'baseball'} package['tags'].append(tag) package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s added the tag %s to the dataset %s' % \ (user['fullname'], tag['name'], package['title']) \ in stripped, stripped # Remove the tag from the package. package['tags'] = [] context['allow_partial_update'] = False package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s removed the tag %s from the dataset %s' % \ (user['fullname'], tag['name'], package['title']) \ in stripped, stripped # Add an extra to the package. package['extras'].append({'key': 'quality', 'value': '10000'}) package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s added the extra "%s" to the dataset %s' % \ (user['fullname'], 'quality', package['title']) \ in stripped, stripped # Update the extra. package['extras'][0]['value'] = 'updated' package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s changed the extra "%s" of the dataset %s' % \ (user['fullname'], 'quality', package['title']) \ in stripped, stripped # Delete the extra. del package['extras'][0] package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the extra "%s" from the dataset %s' % \ (user['fullname'], 'quality', package['title']) \ in stripped, stripped # Delete the package. package_delete(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the dataset %s' % \ (user['fullname'], package['title']) \ in stripped, stripped # Update the user's profile. user['about'] = '' user_update(context, user) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated their profile.' % user['fullname'] \ in stripped, stripped # By now we've created >15 activities, but only the latest 15 should # appear on the page. result = self.app.get(offset, status=200) assert result.body.count('<div class="activity">') \ == 15
def test_user_activity(self): """Test user activity streams HTML rendering.""" # Register a new user. user_dict = {'name': 'billybeane', 'fullname': 'Billy Beane', 'about': 'General Manager, Oakland Athletics', 'email': '*****@*****.**', 'password': '******'} context = { 'model': ckan.model, 'session': ckan.model.Session, 'user': self.sysadmin_user.name, 'allow_partial_update': True, } user = user_create(context, user_dict) offset = url_for('user.activity', id=user['id']) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s signed up' % user['fullname'] in stripped, stripped # Create a new package. package = { 'name' : 'baseball_stats', 'title' : "Billy's Stats about Baseball Players", } context['user'] = user['name'] # FIXME This test use an old way to get at the schema to # recreate this we need to pretend to be using the api. We # should not be calling package_create like this we should be # going via the api or package controllers context['api_version'] = 3 context['ignore_auth'] = True package = package_create(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s created the dataset %s ' % ( user['fullname'], package['title']) in stripped, stripped # Add a resource to the package. resource = { 'url': 'http://www.example.com', 'description': "Chad Bradford's OBP Stats`", 'format': 'cvs', 'name': 'Chad Bradford Stats', } package['resources'].append(resource) request_data = { 'id': package['id'], 'resources': package['resources'], } package = package_update(context, request_data) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s added the resource %s to the dataset %s' % \ (user['fullname'], resource['name'], package['title']) \ in stripped, stripped # Update the package. package['title'] = "Billy's Updated Stats about Baseball Players" package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated the dataset %s' \ % (user['fullname'], package['title']) \ in stripped, stripped # Update the resource. resource = package['resources'][0] resource['name'] = 'Chad Bradford Updated Stats' resource = resource_update(context, resource) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated the resource %s in the dataset %s' \ % (user['fullname'], resource['name'], package['title']) \ in stripped, stripped # Delete the resource. context['allow_partial_update'] = False package['resources'] = [] package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the resource %s from the dataset %s' % \ (user['fullname'], resource['name'], package['title']) \ in stripped, stripped # Follow the package. follow_dataset(context, {'id': package['id']}) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s started following %s' % (user['fullname'], package['title']) not in stripped, stripped # Follow another user. follow_user(context, {'id': 'joeadmin'}) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s started following %s' % (user['fullname'], 'joeadmin') not in stripped, stripped # Create a new group. group = { 'name': 'baseball-stats-group', 'title': 'A Group for Datasets about Baseball' } context['allow_partial_update'] = True group = group_create(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s created the group %s' % (user['fullname'], group['title']) \ in stripped, stripped # Update the group. group['title'] = 'updated' group = group_update(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated the group %s' % (user['fullname'], group['title']) \ in stripped, stripped # Delete the group. group['state'] = 'deleted' group_update(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the group %s' % (user['fullname'], group['title']) \ in stripped, stripped # Add a new tag to the package. tag = {'name': 'baseball'} package['tags'].append(tag) package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s added the tag %s to the dataset %s' % \ (user['fullname'], tag['name'], package['title']) \ in stripped, stripped # Remove the tag from the package. package['tags'] = [] context['allow_partial_update'] = False package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s removed the tag %s from the dataset %s' % \ (user['fullname'], tag['name'], package['title']) \ in stripped, stripped # Add an extra to the package. package['extras'].append({'key': 'quality', 'value': '10000'}) package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s added the extra "%s" to the dataset %s' % \ (user['fullname'], 'quality', package['title']) \ in stripped, stripped # Update the extra. package['extras'][0]['value'] = 'updated' package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s changed the extra "%s" of the dataset %s' % \ (user['fullname'], 'quality', package['title']) \ in stripped, stripped # Delete the extra. del package['extras'][0] package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the extra "%s" from the dataset %s' % \ (user['fullname'], 'quality', package['title']) \ in stripped, stripped # Delete the package. # we need to get round the delete permission context['ignore_auth'] = True package_delete(context, package) del context['ignore_auth'] result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s deleted the dataset %s' % \ (user['fullname'], package['title']) \ in stripped, stripped # Update the user's profile. user['about'] = '' user_update(context, user) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert '%s updated their profile' % user['fullname'] \ in stripped, stripped # By now we've created >15 activities, but only the latest 15 should # appear on the page. result = self.app.get(offset, status=200) assert result.body.count('<span class="actor">') \ == 15, result.body.count('<span class="actor">') # The user's dashboard page should load successfully and have the # latest 15 activities on it. offset = url_for('dashboard.index') extra_environ = {'Authorization': str(ckan.model.User.get('billybeane').apikey)} result = self.app.get(offset, extra_environ=extra_environ, status=200) assert result.body.count('<span class="actor">') == 15, \ result.body.count('<span class="actor">')
def test_user_activity(self): """Test user activity streams HTML rendering.""" # Register a new user. user_dict = { "name": "billybeane", "fullname": "Billy Beane", "about": "General Manager, Oakland Athletics", "email": "*****@*****.**", "password": "******", } context = { "model": ckan.model, "session": ckan.model.Session, "user": self.sysadmin_user.name, "allow_partial_update": True, "extras_as_string": True, } user = user_create(context, user_dict) offset = url_for(controller="user", action="read", id=user["id"]) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s signed up" % user["fullname"] in stripped, stripped # Create a new package. package = {"name": "baseball_stats", "title": "Billy's Stats about Baseball Players"} context["user"] = user["name"] # FIXME This test use an old way to get at the schema to # recreate this we need to pretend to be using the api. We # should not be calling package_create like this we should be # going via the api or package controllers context["api_version"] = 3 context["ignore_auth"] = True package = package_create(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s created the dataset %s " % (user["fullname"], package["title"]) in stripped, stripped # Add a resource to the package. resource = { "url": "http://www.example.com", "description": "Chad Bradford's OBP Stats`", "format": "cvs", "name": "Chad Bradford Stats", } package["resources"].append(resource) request_data = {"id": package["id"], "resources": package["resources"]} package = package_update(context, request_data) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( "%s added the resource %s to the dataset %s" % (user["fullname"], resource["name"], package["title"]) in stripped ), stripped # Update the package. package["title"] = "Billy's Updated Stats about Baseball Players" package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s updated the dataset %s" % (user["fullname"], package["title"]) in stripped, stripped # Update the resource. resource = package["resources"][0] resource["name"] = "Chad Bradford Updated Stats" resource = resource_update(context, resource) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( "%s updated the resource %s in the dataset %s" % (user["fullname"], resource["name"], package["title"]) in stripped ), stripped # Delete the resource. context["allow_partial_update"] = False package["resources"] = [] package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( "%s deleted the resource %s from the dataset %s" % (user["fullname"], resource["name"], package["title"]) in stripped ), stripped # Follow the package. follow_dataset(context, {"id": package["id"]}) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s started following %s" % (user["fullname"], package["title"]) not in stripped, stripped # Follow another user. follow_user(context, {"id": "joeadmin"}) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s started following %s" % (user["fullname"], "joeadmin") not in stripped, stripped # Create a new group. group = {"name": "baseball-stats-group", "title": "A Group for Datasets about Baseball"} context["allow_partial_update"] = True group = group_create(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s created the group %s" % (user["fullname"], group["title"]) in stripped, stripped # Update the group. group["title"] = "updated" group = group_update(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s updated the group %s" % (user["fullname"], group["title"]) in stripped, stripped # Delete the group. group["state"] = "deleted" group_update(context, group) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s deleted the group %s" % (user["fullname"], group["title"]) in stripped, stripped # Add a new tag to the package. tag = {"name": "baseball"} package["tags"].append(tag) package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( "%s added the tag %s to the dataset %s" % (user["fullname"], tag["name"], package["title"]) in stripped ), stripped # Remove the tag from the package. package["tags"] = [] context["allow_partial_update"] = False package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( "%s removed the tag %s from the dataset %s" % (user["fullname"], tag["name"], package["title"]) in stripped ), stripped # Add an extra to the package. package["extras"].append({"key": "quality", "value": "10000"}) package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( '%s added the extra "%s" to the dataset %s' % (user["fullname"], "quality", package["title"]) in stripped ), stripped # Update the extra. package["extras"][0]["value"] = "updated" package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( '%s changed the extra "%s" of the dataset %s' % (user["fullname"], "quality", package["title"]) in stripped ), stripped # Delete the extra. del package["extras"][0] package = package_update(context, package) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert ( '%s deleted the extra "%s" from the dataset %s' % (user["fullname"], "quality", package["title"]) in stripped ), stripped # Delete the package. # we need to get round the delete permission context["ignore_auth"] = True package_delete(context, package) del context["ignore_auth"] result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s deleted the dataset %s" % (user["fullname"], package["title"]) in stripped, stripped # Update the user's profile. user["about"] = "" user_update(context, user) result = self.app.get(offset, status=200) stripped = self.strip_tags(result) assert "%s updated their profile" % user["fullname"] in stripped, stripped # By now we've created >15 activities, but only the latest 15 should # appear on the page. result = self.app.get(offset, status=200) assert result.body.count('<div class="activity">') == 15 # The user's dashboard page should load successfully and have the # latest 15 activities on it. offset = url_for(controller="user", action="dashboard") extra_environ = {"Authorization": str(ckan.model.User.get("billybeane").apikey)} result = self.app.post(offset, extra_environ=extra_environ, status=200) assert result.body.count('<div class="activity">') == 15