diff --git a/ckan/logic/action/create.py b/ckan/logic/action/create.py index ab0e9cd8cd3..874abc88685 100644 --- a/ckan/logic/action/create.py +++ b/ckan/logic/action/create.py @@ -178,16 +178,14 @@ def package_create_validate(context, data_dict): return data def resource_create(context, data_dict): - '''Add a resource to a dataset. + '''Appends a new resource to a datasets list of resources. - TODO: This function doesn't actually do anything yet. - - :param id: (optional) - :type id: string + :param package_id: id of package that the resource needs should be added to. + :type package_id: string + :param url: url of resource + :type url: string :param revision_id: (optional) :type revisiion_id: string - :param url: (optional) - :type url: string :param description: (optional) :type description: string :param format: (optional) @@ -224,9 +222,24 @@ def resource_create(context, data_dict): model = context['model'] user = context['user'] - data, errors = _validate(data_dict, - ckan.logic.schema.default_resource_schema(), - context) + package_id = _get_or_bust(data_dict, 'package_id') + data_dict.pop('package_id') + + pkg_dict = _get_action('package_show')(context, {'id': package_id}) + + _check_access('resource_create', context, data_dict) + + if not 'resources' in pkg_dict: + pkg_dict['resources'] = [] + pkg_dict['resources'].append(data_dict) + + try: + pkg_dict = _get_action('package_update')(context, pkg_dict) + except ValidationError, e: + errors = e.error_dict['resources'][-1] + raise ValidationError(errors, _error_summary(errors)) + + return pkg_dict['resources'][-1] def related_create(context, data_dict): diff --git a/ckan/logic/auth/create.py b/ckan/logic/auth/create.py index 546a1b872ac..7bb24f40916 100644 --- a/ckan/logic/auth/create.py +++ b/ckan/logic/auth/create.py @@ -29,7 +29,9 @@ def related_create(context, data_dict=None): def resource_create(context, data_dict): - return {'success': False, 'msg': 'Not implemented yet in the auth refactor'} + # resource create runs through package_update, so no need to repeat check. Only here + # if extensions need to override this seperately. + return {'success': True} def package_relationship_create(context, data_dict): model = context['model'] diff --git a/ckan/logic/auth/publisher/create.py b/ckan/logic/auth/publisher/create.py index d1bece7ebaf..95ee79b66be 100644 --- a/ckan/logic/auth/publisher/create.py +++ b/ckan/logic/auth/publisher/create.py @@ -32,7 +32,9 @@ def related_create(context, data_dict=None): def resource_create(context, data_dict): - return {'success': False, 'msg': 'Not implemented yet in the auth refactor'} + # resource create runs through package_update, so no need to repeat check. Only here + # if extensions need to override this seperately. + return {'success': True} def package_relationship_create(context, data_dict): """ diff --git a/ckan/tests/logic/test_action.py b/ckan/tests/logic/test_action.py index 934e49efd4e..f22858ee1ab 100644 --- a/ckan/tests/logic/test_action.py +++ b/ckan/tests/logic/test_action.py @@ -189,6 +189,33 @@ def test_18_create_package_not_authorized(self): res = self.app.post('/api/action/package_create', params=postparams, status=StatusCodes.STATUS_403_ACCESS_DENIED) + def test_41_create_resource(self): + + anna_id = model.Package.by_name(u'annakarenina').id + resource = {'package_id': anna_id, 'url': 'new_url'} + + postparams = '%s=1' % json.dumps(resource) + res = self.app.post('/api/action/resource_create', params=postparams, + extra_environ={'Authorization': 'tester'}) + + resource = json.loads(res.body)['result'] + + assert resource['url'] == 'new_url' + + def test_42_create_resource_with_error(self): + + anna_id = model.Package.by_name(u'annakarenina').id + resource = {'package_id': anna_id, 'url': 'new_url', 'created': 'bad_date'} + + postparams = '%s=1' % json.dumps(resource) + res = self.app.post('/api/action/resource_create', params=postparams, + extra_environ={'Authorization': 'tester'}, + status=StatusCodes.STATUS_409_CONFLICT) + + assert json.loads(res.body)['error'] == {"__type": "Validation Error", "created": ["Date format incorrect"]} + + + def test_04_user_list(self): postparams = '%s=1' % json.dumps({}) res = self.app.post('/api/action/user_list', params=postparams)