コード例 #1
0
ファイル: patch.py プロジェクト: CIOIL/DataGovIL
def resource_patch(context, data_dict):
    '''Patch a resource

    :param id: the id of the resource
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict
    '''
    _check_access('resource_patch', context, data_dict)

    show_context = {
        'model': context['model'],
        'session': context['session'],
        'user': context['user'],
        'auth_user_obj': context['auth_user_obj'],
        }

    resource_dict = _get_action('resource_show')(
        show_context,
        {'id': _get_or_bust(data_dict, 'id')})

    patched = dict(resource_dict)
    patched.update(data_dict)
    return _update.resource_update(context, patched)
コード例 #2
0
def resource_patch(context, data_dict):
    '''Patch a resource

    :param id: the id of the resource
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict
    '''
    _check_access('resource_patch', context, data_dict)

    show_context = {
        'model': context['model'],
        'session': context['session'],
        'user': context['user'],
        'auth_user_obj': context['auth_user_obj'],
    }

    resource_dict = _get_action('resource_show')(
        show_context, {
            'id': _get_or_bust(data_dict, 'id')
        })

    patched = dict(resource_dict)
    patched.update(data_dict)
    return _update.resource_update(context, patched)
コード例 #3
0
def res_update(context, data_dict=None):
    ret_val = resource_update(context, data_dict)
    
    dataset_obj = context['package']
    data_dict = {'id': dataset_obj.id,
                 'name': dataset_obj.name}
    if not dataset_obj.private: # dataset is public
        log.debug("resource_update sync dataset")
        queue.put(data_dict)
    
    return ret_val
コード例 #4
0
 def resource_update(self, context, data_dict):
     resource = core_update.resource_update(context, data_dict)
     try:
         context = {'model': model, 'ignore_auth': True}
         if resource.get('format','').lower() in self.datapusher_formats:
             p.toolkit.get_action('datapusher_submit')(context, {
                 'resource_id': resource['id']
             })
     except p.toolkit.ValidationError, e:
         # If datapusher is offline want to catch error instead
         # of raising otherwise resource save will fail with 500
         log.critical(e)
         pass
コード例 #5
0
 def resource_update(self, context, data_dict):
     resource = core_update.resource_update(context, data_dict)
     try:
         context = {'model': model, 'ignore_auth': True}
         if resource.get('format', '').lower() in self.datapusher_formats:
             p.toolkit.get_action('datapusher_submit')(
                 context, {
                     'resource_id': resource['id']
                 })
     except p.toolkit.ValidationError, e:
         # If datapusher is offline want to catch error instead
         # of raising otherwise resource save will fail with 500
         log.critical(e)
         pass
コード例 #6
0
def resource_patch(context, data_dict):
    '''
    Cloned from core. It adds a 'no_compute_extra_hdx_show_properties' in contexts to
    make the update faster (less computation in the custom package_show)
    Also used to parse validation parameters (SKIP_VALIDATION) for special cases.

    Patch a resource

    :param id: the id of the resource
    :type id: string

    The difference between the update and patch methods is that the patch will
    perform an update of the provided parameters, while leaving all other
    parameters unchanged, whereas the update methods deletes all parameters
    not explicitly provided in the data_dict
    '''
    _check_access('resource_patch', context, data_dict)

    context['no_compute_extra_hdx_show_properties'] = True
    process_batch_mode(context, data_dict)
    process_skip_validation(context, data_dict)

    show_context = {
        'model':
        context['model'],
        'session':
        context['session'],
        'user':
        context['user'],
        'auth_user_obj':
        context['auth_user_obj'],
        'no_compute_extra_hdx_show_properties':
        context.get('no_compute_extra_hdx_show_properties')
    }

    resource_dict = _get_action('resource_show')(
        show_context, {
            'id': _get_or_bust(data_dict, 'id')
        })

    patched = dict(resource_dict)
    patched.update(data_dict)
    return _update.resource_update(context, patched)
コード例 #7
0
def resource_update(context, data_dict):
    log.debug(data_dict)
    model = context['model']
    resource = Resource.get(data_dict['id'])
    extras = resource.extras
    if resource is not None and 'url' not in data_dict:
        url = resource.url
        data_dict['url'] = url
    resource_dict = update.resource_update(context, data_dict)
    resource = Resource.get(data_dict['id'])
    if len(resource.extras) <= 0:
        resource.extras = extras
    model.repo.commit()
    for key in resource.extras:
        resource_dict[key] = resource.extras[key]

    send_resource_log(context, resource_dict, 'Resource metadata updated',
                      'ResourceMetadataUpdated')

    return resource_dict
コード例 #8
0
def resource_update(context, data_dict):
    if not tk.asbool(
            config.get('ckan.cloud_storage_enable')) or data_dict.get('url'):
        return origin.resource_update(context, data_dict)

    model = context['model']
    user = context['user']
    id = _get_or_bust(data_dict, "id")

    resource = model.Resource.get(id)
    context["resource"] = resource

    if not resource:
        log.error('Could not find resource ' + id)
        raise NotFound(_('Resource was not found.'))

    _check_access('resource_update', context, data_dict)
    del context["resource"]

    package_id = resource.resource_group.package.id
    pkg_dict = _get_action('package_show')(context, {'id': package_id})

    for n, p in enumerate(pkg_dict['resources']):
        if p['id'] == id:
            break
    else:
        log.error('Could not find resource ' + id)
        raise NotFound(_('Resource was not found.'))

    upload = uploader.S3Upload(data_dict)

    pkg_dict['resources'][n] = data_dict

    try:
        context['defer_commit'] = True
        context['use_cache'] = False
        pkg_dict = _get_action('package_update')(context, pkg_dict)
        context.pop('defer_commit')
    except ValidationError, e:
        errors = e.error_dict['resources'][n]
        raise ValidationError(errors)
コード例 #9
0
ファイル: update.py プロジェクト: DataShades/ckan-galleries
def resource_update(context, data_dict):
    if not config.get('ckan.cloud_storage_enable') or data_dict.get('url'):
        return origin.resource_update(context, data_dict)

    model = context['model']
    user = context['user']
    id = _get_or_bust(data_dict, "id")

    resource = model.Resource.get(id)
    context["resource"] = resource

    if not resource:
        log.error('Could not find resource ' + id)
        raise NotFound(_('Resource was not found.'))

    _check_access('resource_update', context, data_dict)
    del context["resource"]

    package_id = resource.resource_group.package.id
    pkg_dict = _get_action('package_show')(context, {'id': package_id})

    for n, p in enumerate(pkg_dict['resources']):
        if p['id'] == id:
            break
    else:
        log.error('Could not find resource ' + id)
        raise NotFound(_('Resource was not found.'))

    upload = uploader.S3Upload(data_dict)

    pkg_dict['resources'][n] = data_dict

    try:
        context['defer_commit'] = True
        context['use_cache'] = False
        pkg_dict = _get_action('package_update')(context, pkg_dict)
        context.pop('defer_commit')
    except ValidationError, e:
        errors = e.error_dict['resources'][n]
        raise ValidationError(errors)
コード例 #10
0
def resource_update(context, data_dict):
    '''
    This runs the 'resource_update' action from core ckan's update.py
    It allows us to do some minor changes and wrap it.
    '''

    process_batch_mode(context, data_dict)
    process_skip_validation(context, data_dict)

    # make the update faster (less computation in the custom package_show)
    context['no_compute_extra_hdx_show_properties'] = True

    if data_dict.get('resource_type', '') != 'file.upload':
        #If this isn't an upload, it is a link so make sure we update
        #the url_type otherwise solr will screw everything up
        data_dict['url_type'] = 'api'
    if data_dict.get('datastore_active', 'false') in ('false', 'False'):
        data_dict['datastore_active'] = False
    else:
        if data_dict.get('datastore_active', 'true') in ('true', 'True'):
            data_dict['datastore_active'] = True
    result_dict = core_update.resource_update(context, data_dict)

    return result_dict
コード例 #11
0
def resource_update(context, data_dict):
    _clear_get_resource_data_cache()
    return update_core.resource_update(context, data_dict)
コード例 #12
0
    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">')
コード例 #13
0
ファイル: test_activity.py プロジェクト: zydio/ckan
    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
コード例 #14
0
ファイル: test_activity.py プロジェクト: srkunze/ckan
    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
コード例 #15
0
def resource_update(context, data_dict):
    '''
    This runs the 'resource_update' action from core ckan's update.py
    It allows us to do some minor changes and wrap it.
    '''

    process_batch_mode(context, data_dict)
    flag_if_file_uploaded(context, data_dict)
    process_skip_validation(context, data_dict)

    # make the update faster (less computation in the custom package_show)
    context['no_compute_extra_hdx_show_properties'] = True

    prev_resource_dict = _fetch_prev_resource_info(context['model'], data_dict)
    prev_resource_is_upload = prev_resource_dict.get('url_type') == 'upload'
    new_file_uploaded = bool(data_dict.get('upload'))

    if data_dict.get('resource_type', '') != 'file.upload':
        #If this isn't an upload, it is a link so make sure we update
        #the url_type otherwise solr will screw everything up
        data_dict['url_type'] = 'api'

        # we need to overwrite size field (not just setting it to None or pop) otherwise
        # ckan.lib.dictization.model_save.resource_dict_save() keeps the old value
        data_dict['size'] = 0
    else:
        try:
            if len(request.files) > 0:
                data_dict['size'] = request.content_length
                data_dict['mimetype'] = request.files['upload'].mimetype
        except RuntimeError as re:
            log.debug(
                'This usually happens for tests when there is no HTTP request: '
                + unicode(re))

    if data_dict.get('datastore_active', 'false') in ('false', 'False'):
        data_dict['datastore_active'] = False
    else:
        if data_dict.get('datastore_active', 'true') in ('true', 'True'):
            data_dict['datastore_active'] = True
    result_dict = core_update.resource_update(context, data_dict)

    new_resource_is_api = result_dict.get('url_type') == 'api'
    filename = find_filename_in_url(result_dict.get('url', ''))
    munged_current_filename = munge.munge_filename(filename)
    munged_prev_filename = munge.munge_filename(prev_resource_dict['url'])
    new_file_has_same_name = munged_current_filename == munged_prev_filename
    if prev_resource_is_upload and (
        (new_file_uploaded and not new_file_has_same_name)
            or new_resource_is_api):
        log.info('Deleting resource {}/{}'.format(prev_resource_dict['id'],
                                                  prev_resource_dict['name']))
        file_remove(prev_resource_dict['id'], prev_resource_dict['url'],
                    prev_resource_dict['url_type'])
    else:
        log.info(
            'Not deleting resource: prev_resource_is_upload {} / new_file_uploaded {}'
            '/ new_file_has_same_name {} / new_resource_is_api {}'.format(
                prev_resource_is_upload, new_file_uploaded,
                new_file_has_same_name, new_resource_is_api))

    return result_dict