Skip to content

Commit

Permalink
[#1251] Add resource_view_update
Browse files Browse the repository at this point in the history
  • Loading branch information
johnglover committed Sep 25, 2013
1 parent b4eef9e commit feff4ef
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 0 deletions.
39 changes: 39 additions & 0 deletions ckan/logic/action/update.py
Expand Up @@ -229,6 +229,45 @@ def resource_update(context, data_dict):
return pkg_dict['resources'][n]


def resource_view_update(context, data_dict):
'''Update a resource view.
To update a resource_view you must be authorized to update the resource
that the resource_view belongs to.
For further parameters see ``resource_view_create()``.
:param id: the id of the resource_view to update
:type id: string
:returns: the updated resource_view
:rtype: string
'''
model = context['model']
id = _get_or_bust(data_dict, "id")
schema = (context.get('schema') or
ckan.logic.schema.default_update_resource_view_schema())

data, errors = _validate(data_dict, schema, context)
if errors:
model.Session.rollback()
raise ValidationError(errors)

resource_view = model.ResourceView.get(id)
if not resource_view:
raise NotFound

context["resource_view"] = resource_view
context['resource'] = model.Resource.get(resource_view.resource_id)
_check_access('resource_view_update', context, data_dict)

resource_view = model_save.resource_view_dict_save(data, context)
if not context.get('defer_commit'):
model.repo.commit()
return model_dictize.resource_view_dictize(resource_view, context)


def package_update(context, data_dict):
'''Update a dataset (package).
Expand Down
4 changes: 4 additions & 0 deletions ckan/logic/auth/update.py
Expand Up @@ -69,6 +69,10 @@ def resource_update(context, data_dict):
return {'success': True}


def resource_view_update(context, data_dict):
return resource_update(context, data_dict)


def package_relationship_update(context, data_dict):
return new_authz.is_authorized('package_relationship_create',
context,
Expand Down
6 changes: 6 additions & 0 deletions ckan/logic/schema.py
Expand Up @@ -576,3 +576,9 @@ def default_resource_view_schema():
'config': [ignore_missing]
}
return schema


def default_update_resource_view_schema():
schema = default_resource_view_schema()
schema.update({'id': [not_missing, not_empty, unicode]})
return schema
119 changes: 119 additions & 0 deletions ckan/tests/logic/test_action.py
Expand Up @@ -1242,6 +1242,24 @@ def test_resource_view_create(self):
assert resource_view[field] == resource_view_created[field], \
(field, resource_view[field], resource_view_created[field])

def test_resource_view_create_missing_required_fields(self):
resource_id = model.Package.by_name(u'annakarenina').resources[0].id
resource_view = {'resource_id': resource_id,
'view_type': u'test'}
postparams = '%s=1' % json.dumps(resource_view)
self.app.post(
'/api/action/resource_view_create', params=postparams,
extra_environ={'Authorization': str(self.normal_user.apikey)},
status=409)

resource_view = {'resource_id': resource_id,
'view_number': 2}
postparams = '%s=1' % json.dumps(resource_view)
self.app.post(
'/api/action/resource_view_create', params=postparams,
extra_environ={'Authorization': str(self.normal_user.apikey)},
status=409)

def test_resource_view_create_invalid_resource_id(self):
resource_view = {'resource_id': u'bad-resource-id',
'view_type': u'test',
Expand Down Expand Up @@ -1293,6 +1311,107 @@ def test_resource_view_show_invalid_resource_view_id(self):
self.app.post('/api/action/resource_view_show',
params=postparams, status=404)

def test_resource_view_update(self):
resource_id = model.Package.by_name(u'annakarenina').resources[0].id
resource_view = {'resource_id': resource_id,
'view_type': u'test',
'view_number': 1,
'config': {'config-key': u'test'}}
postparams = '%s=1' % json.dumps(resource_view)
res = self.app.post(
'/api/action/resource_view_create', params=postparams,
extra_environ={'Authorization': str(self.normal_user.apikey)})
resource_view_created = json.loads(res.body)['result']

resource_view.update({'id': resource_view_created['id'],
'view_type': u'new_view_type',
'view_number': 2,
'config': {'config-key': u'new-config-value'}})
postparams = '%s=1' % json.dumps(resource_view)
res = self.app.post(
'/api/action/resource_view_update', params=postparams,
extra_environ={'Authorization': str(self.normal_user.apikey)})
result = json.loads(res.body)['result']

assert result == resource_view

def test_resource_view_update_missing_required_fields(self):
resource_id = model.Package.by_name(u'annakarenina').resources[0].id
resource_view = {'resource_id': resource_id,
'view_type': u'test',
'view_number': 1,
'config': {'config-key': u'test'}}
postparams = '%s=1' % json.dumps(resource_view)
res = self.app.post(
'/api/action/resource_view_create', params=postparams,
extra_environ={'Authorization': str(self.normal_user.apikey)})
resource_view_created = json.loads(res.body)['result']

postparams = '%s=1' % json.dumps({
'id': resource_view_created['id'],
'resource_id': resource_id,
'view_number': 0
})
self.app.post(
'/api/action/resource_view_update', params=postparams,
extra_environ={'Authorization': str(self.normal_user.apikey)},
status=409)

postparams = '%s=1' % json.dumps({
'id': resource_view_created['id'],
'resource_id': resource_id,
'view_type': u'test'
})
self.app.post(
'/api/action/resource_view_update', params=postparams,
extra_environ={'Authorization': str(self.normal_user.apikey)},
status=409)

def test_resource_view_update_missing_resource_view_id(self):
postparams = '%s=1' % json.dumps({})
self.app.post(
'/api/action/resource_view_update', params=postparams,
extra_environ={'Authorization': str(self.normal_user.apikey)},
status=409)

def test_resource_view_update_invalid_resource_view_id(self):
resource_id = model.Package.by_name(u'annakarenina').resources[0].id
resource_view = {'resource_id': resource_id,
'view_type': u'test',
'view_number': 1,
'config': {'config-key': u'test'}}
postparams = '%s=1' % json.dumps(resource_view)
self.app.post(
'/api/action/resource_view_create', params=postparams,
extra_environ={'Authorization': str(self.normal_user.apikey)})

resource_view.update({'id': u'bad-resource-view-id'})
postparams = '%s=1' % json.dumps(resource_view)
self.app.post(
'/api/action/resource_view_update', params=postparams,
extra_environ={'Authorization': str(self.normal_user.apikey)},
status=404)

def test_resource_view_update_invalid_auth(self):
resource_id = model.Package.by_name(u'annakarenina').resources[0].id
resource_view = {'resource_id': resource_id,
'view_type': u'test',
'view_number': 1,
'config': {'config-key': u'test'}}
postparams = '%s=1' % json.dumps(resource_view)
res = self.app.post(
'/api/action/resource_view_create', params=postparams,
extra_environ={'Authorization': str(self.normal_user.apikey)})
resource_view_created = json.loads(res.body)['result']

resource_view.update({'id': resource_view_created['id'],
'view_type': u'new_view_type',
'view_number': 2,
'config': {'config-key': u'new-config-value'}})
postparams = '%s=1' % json.dumps(resource_view)
self.app.post('/api/action/resource_view_update', params=postparams,
status=403)


class TestActionTermTranslation(WsgiAppCase):

Expand Down

0 comments on commit feff4ef

Please sign in to comment.