From 6f7b965d39534bfaa80e7ad3f638d8b4b46010e1 Mon Sep 17 00:00:00 2001 From: Sean Hammond Date: Wed, 8 Feb 2012 16:24:59 +0100 Subject: [PATCH] [#1775,#1776] Add tag_create() logic action function. Lets you create either a free tag or a tag that belongs to a vocab. Add tag_dict_save() to model_save.py. Add tag_create() to logic/action/create.py and logic/auth/create.py. Add default_create_tags_schema() to schema.py. Add a simple functional test to test_vocabulary.py. More tests needed. --- ckan/lib/dictization/model_save.py | 8 +++++ ckan/logic/action/create.py | 25 +++++++++++++- ckan/logic/auth/create.py | 4 +++ ckan/logic/schema.py | 4 +++ .../functional/api/model/test_vocabulary.py | 34 +++++++++++++++++++ 5 files changed, 74 insertions(+), 1 deletion(-) diff --git a/ckan/lib/dictization/model_save.py b/ckan/lib/dictization/model_save.py index 407ea4cfbda..cc72a08a0b9 100644 --- a/ckan/lib/dictization/model_save.py +++ b/ckan/lib/dictization/model_save.py @@ -489,3 +489,11 @@ def vocabulary_dict_update(vocabulary_dict, context): vocabulary_obj.name = vocabulary_dict['name'] return vocabulary_obj + +def tag_dict_save(tag_dict, context): + model = context['model'] + tag = context.get('tag') + if tag: + tag_dict['id'] = tag.id + tag = table_dict_save(tag_dict, model.Tag, context) + return tag diff --git a/ckan/logic/action/create.py b/ckan/logic/action/create.py index 5309ace0c71..f30f3f59002 100644 --- a/ckan/logic/action/create.py +++ b/ckan/logic/action/create.py @@ -19,19 +19,22 @@ package_dict_save, user_dict_save, vocabulary_dict_save, + tag_dict_save, activity_dict_save) from ckan.lib.dictization.model_dictize import (group_dictize, package_dictize, user_dictize, vocabulary_dictize, + tag_dictize, activity_dictize) from ckan.logic.schema import (default_create_package_schema, default_resource_schema, default_create_relationship_schema, default_create_vocabulary_schema, - default_create_activity_schema) + default_create_activity_schema, + default_create_tag_schema) from ckan.logic.schema import default_group_schema, default_user_schema from ckan.lib.navl.dictization_functions import validate @@ -409,3 +412,23 @@ def package_relationship_create_rest(context, data_dict): relationship_dict = package_relationship_create(context, data_dict) return relationship_dict + +def tag_create(context, tag_dict): + '''Create a new tag and return a dictionary representation of it.''' + + model = context['model'] + + check_access('tag_create', context, tag_dict) + + schema = context.get('schema') or default_create_tag_schema() + data, errors = validate(tag_dict, schema, context) + if errors: + raise ValidationError(errors) + + tag = tag_dict_save(tag_dict, context) + + if not context.get('defer_commit'): + model.repo.commit() + + log.debug("Created tag '%s' " % tag) + return tag_dictize(tag, context) diff --git a/ckan/logic/auth/create.py b/ckan/logic/auth/create.py index 270af74c6e6..eaf845e0c06 100644 --- a/ckan/logic/auth/create.py +++ b/ckan/logic/auth/create.py @@ -137,3 +137,7 @@ def vocabulary_create(context, data_dict): def activity_create(context, data_dict): user = context['user'] return {'success': Authorizer.is_sysadmin(user)} + +def tag_create(context, data_dict): + user = context['user'] + return {'success': Authorizer.is_sysadmin(user)} diff --git a/ckan/logic/schema.py b/ckan/logic/schema.py index 6012c108153..03c5575eaaa 100644 --- a/ckan/logic/schema.py +++ b/ckan/logic/schema.py @@ -86,6 +86,10 @@ def default_tags_schema(): } return schema +def default_create_tag_schema(): + schema = default_tags_schema() + return schema + def default_package_schema(): schema = { diff --git a/ckan/tests/functional/api/model/test_vocabulary.py b/ckan/tests/functional/api/model/test_vocabulary.py index cd61e21ae06..a4d036d95a7 100644 --- a/ckan/tests/functional/api/model/test_vocabulary.py +++ b/ckan/tests/functional/api/model/test_vocabulary.py @@ -139,6 +139,30 @@ def _delete_vocabulary(self, vocab_id, user=None): status=404) assert response.json['success'] == False + def _list_tags(self, vocabulary, user=None): + params = {'vocabulary_name': vocabulary['name']} + if user: + extra_environ = {'Authorization' : str(user.apikey)} + else: + extra_environ = None + response = self._post('/api/action/tag_list', params=params, + extra_environ=extra_environ) + assert response['success'] == True + return response['result'] + + def _create_tag(self, user, tag_name, vocabulary=None): + tag_dict = {'name': tag_name} + if vocabulary: + tag_dict['vocabulary_id'] = vocabulary['id'] + if user: + extra_environ = {'Authorization' : str(user.apikey)} + else: + extra_environ = None + response = self._post('/api/action/tag_create', params=tag_dict, + extra_environ=extra_environ) + assert response['success'] == True + return response['result'] + def test_vocabulary_create(self): '''Test adding a new vocabulary to a CKAN instance via the action API. @@ -311,3 +335,13 @@ def test_vocabulary_delete_not_authorized(self): str(self.normal_user.apikey)}, status=403) assert response.json['success'] == False + + def test_add_tag_to_vocab(self): + vocab = self._create_vocabulary(vocab_name="Musical Genres", + user=self.sysadmin_user) + tags_before = self._list_tags(vocab) + assert len(tags_before) == 0, tags_before + tag_created = self._create_tag(self.sysadmin_user, 'noise', vocab) + tags_after = self._list_tags(vocab) + assert len(tags_after) == 1 + assert tag_created['name'] in tags_after