Skip to content

Commit

Permalink
[2571] append only resource create
Browse files Browse the repository at this point in the history
  • Loading branch information
kindly committed Jun 20, 2012
1 parent 2542cb3 commit d376196
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
33 changes: 23 additions & 10 deletions ckan/logic/action/create.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion ckan/logic/auth/create.py
Expand Up @@ -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']
Expand Down
4 changes: 3 additions & 1 deletion ckan/logic/auth/publisher/create.py
Expand Up @@ -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):
"""
Expand Down
27 changes: 27 additions & 0 deletions ckan/tests/logic/test_action.py
Expand Up @@ -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)
Expand Down

0 comments on commit d376196

Please sign in to comment.