[ckan-changes] commit/ckan: 4 new changesets

Bitbucket commits-noreply at bitbucket.org
Wed Oct 19 10:27:41 UTC 2011


4 new changesets in ckan:

http://bitbucket.org/okfn/ckan/changeset/727eb7269dcc/
changeset:   727eb7269dcc
branch:      feature-1381-manage-groups-via-package-api
user:        amercader
date:        2011-10-18 17:28:07
summary:     [logic] Add missing import
affected #:  1 file (-1 bytes)

--- a/ckan/logic/auth/create.py	Tue Oct 18 14:22:39 2011 +0100
+++ b/ckan/logic/auth/create.py	Tue Oct 18 16:28:07 2011 +0100
@@ -1,4 +1,4 @@
-from ckan.logic import check_access_old
+from ckan.logic import check_access_old, NotFound
 from ckan.authz import Authorizer
 from ckan.lib.base import _
 


http://bitbucket.org/okfn/ckan/changeset/2a3b56df3227/
changeset:   2a3b56df3227
branch:      feature-1381-manage-groups-via-package-api
user:        amercader
date:        2011-10-18 19:12:30
summary:     [api, tests] Make sure groups can be defined via name or id depending on the API version
affected #:  2 files (-1 bytes)

--- a/ckan/lib/dictization/model_save.py	Tue Oct 18 16:28:07 2011 +0100
+++ b/ckan/lib/dictization/model_save.py	Tue Oct 18 18:12:30 2011 +0100
@@ -332,7 +332,7 @@
 def package_api_to_dict(api1_dict, context):
 
     package = context.get("package")
-
+    api_version = context.get('api_version') or '1'
     dictized = {}
 
     for key, value in api1_dict.iteritems():
@@ -358,7 +358,10 @@
                     new_value.append({"key": extras_key,
                                       "value": None})
         if key == 'groups':
-             new_value = [{"name": item} for item in value] 
+            if api_version == '1':
+                new_value = [{'name': item} for item in value]
+            else:
+                new_value = [{'id': item} for item in value]
 
         dictized[key] = new_value
 


--- a/ckan/tests/functional/api/model/test_package.py	Tue Oct 18 16:28:07 2011 +0100
+++ b/ckan/tests/functional/api/model/test_package.py	Tue Oct 18 18:12:30 2011 +0100
@@ -108,15 +108,20 @@
     def test_register_post_with_group(self):
         assert not self.get_package_by_name(self.package_fixture_data['name'])
         offset = self.package_offset()
+        
+        test_groups = [u'david']
+        user = model.User.by_name(u'russianfan')
 
-        groups = [u'david']
-        user = model.User.by_name(u'russianfan')
-        for grp in groups:
+        groups = []
+        for grp in test_groups:
             group = model.Group.get(grp)
+            if self.api_version == '1':
+                groups.append(group.name)
+            else:
+                groups.append(group.id)
             model.setup_default_user_roles(group, [user])
 
 
-
         package_fixture_data = self.package_fixture_data
         package_fixture_data['groups'] = groups
         data = self.dumps(package_fixture_data)
@@ -127,14 +132,25 @@
         self.remove()
         package = self.get_package_by_name(self.package_fixture_data['name'])
         assert package
-        self.assert_equal([g.name for g in package.groups], groups)
+        if self.api_version == '1':
+            self.assert_equal([g.name for g in package.groups], groups)
+        else:
+            self.assert_equal([g.id for g in package.groups], groups)
         del package_fixture_data['groups']
 
     def test_register_post_with_group_not_authorized(self):
         assert not self.get_package_by_name(self.package_fixture_data['name'])
         offset = self.package_offset()
 
-        groups = [u'david']
+        test_groups = [u'david']
+
+        groups = []
+        for grp in test_groups:
+            group = model.Group.get(grp)
+            if self.api_version == '1':
+                groups.append(group.name)
+            else:
+                groups.append(group.id)
 
         package_fixture_data = self.package_fixture_data
         package_fixture_data['groups'] = groups
@@ -147,7 +163,15 @@
         assert not self.get_package_by_name(self.package_fixture_data['name'])
         offset = self.package_offset()
         user = model.User.by_name(u'testsysadmin')
-        groups = [u'david']
+        test_groups = [u'david']
+
+        groups = []
+        for grp in test_groups:
+            group = model.Group.get(grp)
+            if self.api_version == '1':
+                groups.append(group.name)
+            else:
+                groups.append(group.id)
 
         package_fixture_data = self.package_fixture_data
         package_fixture_data['groups'] = groups
@@ -158,7 +182,11 @@
         self.remove()
         package = self.get_package_by_name(self.package_fixture_data['name'])
         assert package
-        self.assert_equal([g.name for g in package.groups], groups)
+        if self.api_version == '1':
+            self.assert_equal([g.name for g in package.groups], groups)
+        else:
+            self.assert_equal([g.id for g in package.groups], groups)
+
         del package_fixture_data['groups']
 
     def test_register_post_json(self):


http://bitbucket.org/okfn/ckan/changeset/e8a4257b855f/
changeset:   e8a4257b855f
branch:      feature-1381-manage-groups-via-package-api
user:        amercader
date:        2011-10-19 12:00:14
summary:     [authz, tests, api] Changes in the authz checks to support groups via the API.

It now returns a 404 if the group was not found
affected #:  4 files (-1 bytes)

--- a/ckan/controllers/api.py	Tue Oct 18 18:12:30 2011 +0100
+++ b/ckan/controllers/api.py	Wed Oct 19 11:00:14 2011 +0100
@@ -283,6 +283,9 @@
                                    resource_location=location)
         except NotAuthorized:
             return self._finish_not_authz()
+        except NotFound, e:
+            extra_msg = e.extra_msg
+            return self._finish_not_found(extra_msg)
         except ValidationError, e:
             log.error('Validation error: %r' % str(e.error_dict))
             return self._finish(409, e.error_dict, content_type='json')


--- a/ckan/lib/dictization/model_save.py	Tue Oct 18 18:12:30 2011 +0100
+++ b/ckan/lib/dictization/model_save.py	Wed Oct 19 11:00:14 2011 +0100
@@ -357,7 +357,7 @@
                 else:
                     new_value.append({"key": extras_key,
                                       "value": None})
-        if key == 'groups':
+        if key == 'groups' and len(value):
             if api_version == '1':
                 new_value = [{'name': item} for item in value]
             else:


--- a/ckan/logic/auth/create.py	Tue Oct 18 18:12:30 2011 +0100
+++ b/ckan/logic/auth/create.py	Wed Oct 19 11:00:14 2011 +0100
@@ -80,15 +80,16 @@
     model = context['model']
     pkg = context.get("package")
 
-    ## hack as api does not allow groups
-    if context.get("allow_partial_update"):
-        return True
+    api_version = context.get('api_version') or '1'
 
     group_dicts = data_dict.get("groups", [])
     groups = set()
     for group_dict in group_dicts:
         if isinstance(group_dict,dict):
-            id = group_dict.get('id')
+            if api_version == '1':
+                id = group_dict.get('name')
+            else:
+                id = group_dict.get('id')
             if not id:
                 continue
         else:


--- a/ckan/tests/functional/api/model/test_package.py	Tue Oct 18 18:12:30 2011 +0100
+++ b/ckan/tests/functional/api/model/test_package.py	Wed Oct 19 11:00:14 2011 +0100
@@ -26,6 +26,19 @@
         super(PackagesTestCase, self).teardown()
         model.Session.connection().invalidate()
 
+    def get_groups_identifiers(self, test_groups, users=[]):
+        groups = []
+        for grp in test_groups:
+            group = model.Group.get(grp)
+            if self.get_expected_api_version() == '1':
+                groups.append(group.name)
+            else:
+                groups.append(group.id)
+
+            if users:
+                model.setup_default_user_roles(group, users)
+        return groups
+
     def test_register_get_ok(self):
         offset = self.package_offset()
         res = self.app.get(offset, status=self.STATUS_200_OK)
@@ -112,15 +125,7 @@
         test_groups = [u'david']
         user = model.User.by_name(u'russianfan')
 
-        groups = []
-        for grp in test_groups:
-            group = model.Group.get(grp)
-            if self.api_version == '1':
-                groups.append(group.name)
-            else:
-                groups.append(group.id)
-            model.setup_default_user_roles(group, [user])
-
+        groups = self.get_groups_identifiers(test_groups,[user])
 
         package_fixture_data = self.package_fixture_data
         package_fixture_data['groups'] = groups
@@ -132,7 +137,7 @@
         self.remove()
         package = self.get_package_by_name(self.package_fixture_data['name'])
         assert package
-        if self.api_version == '1':
+        if self.get_expected_api_version() == '1':
             self.assert_equal([g.name for g in package.groups], groups)
         else:
             self.assert_equal([g.id for g in package.groups], groups)
@@ -143,14 +148,7 @@
         offset = self.package_offset()
 
         test_groups = [u'david']
-
-        groups = []
-        for grp in test_groups:
-            group = model.Group.get(grp)
-            if self.api_version == '1':
-                groups.append(group.name)
-            else:
-                groups.append(group.id)
+        groups = self.get_groups_identifiers(test_groups)
 
         package_fixture_data = self.package_fixture_data
         package_fixture_data['groups'] = groups
@@ -159,19 +157,26 @@
                              extra_environ=self.extra_environ)
         del package_fixture_data['groups']
 
+    def test_register_post_with_group_not_found(self):
+        assert not self.get_package_by_name(self.package_fixture_data['name'])
+        offset = self.package_offset()
+
+        test_groups = [u'this-group-does-not-exist']
+        groups = test_groups
+
+        package_fixture_data = self.package_fixture_data
+        package_fixture_data['groups'] = groups
+        data = self.dumps(package_fixture_data)
+        res = self.post_json(offset, data, status=self.STATUS_404_NOT_FOUND,
+                             extra_environ=self.extra_environ)
+        del package_fixture_data['groups']
+
     def test_register_post_with_group_sysadmin(self):
         assert not self.get_package_by_name(self.package_fixture_data['name'])
         offset = self.package_offset()
         user = model.User.by_name(u'testsysadmin')
         test_groups = [u'david']
-
-        groups = []
-        for grp in test_groups:
-            group = model.Group.get(grp)
-            if self.api_version == '1':
-                groups.append(group.name)
-            else:
-                groups.append(group.id)
+        groups = self.get_groups_identifiers(test_groups)
 
         package_fixture_data = self.package_fixture_data
         package_fixture_data['groups'] = groups
@@ -182,7 +187,7 @@
         self.remove()
         package = self.get_package_by_name(self.package_fixture_data['name'])
         assert package
-        if self.api_version == '1':
+        if self.get_expected_api_version() == '1':
             self.assert_equal([g.name for g in package.groups], groups)
         else:
             self.assert_equal([g.id for g in package.groups], groups)


http://bitbucket.org/okfn/ckan/changeset/6227142b0460/
changeset:   6227142b0460
user:        amercader
date:        2011-10-19 12:15:58
summary:     [merge] from feature-1381-manage-groups-via-package-api
affected #:  6 files (-1 bytes)

--- a/ckan/controllers/api.py	Mon Oct 17 15:07:58 2011 +0100
+++ b/ckan/controllers/api.py	Wed Oct 19 11:15:58 2011 +0100
@@ -283,6 +283,9 @@
                                    resource_location=location)
         except NotAuthorized:
             return self._finish_not_authz()
+        except NotFound, e:
+            extra_msg = e.extra_msg
+            return self._finish_not_found(extra_msg)
         except ValidationError, e:
             log.error('Validation error: %r' % str(e.error_dict))
             return self._finish(409, e.error_dict, content_type='json')


--- a/ckan/lib/dictization/model_save.py	Mon Oct 17 15:07:58 2011 +0100
+++ b/ckan/lib/dictization/model_save.py	Wed Oct 19 11:15:58 2011 +0100
@@ -332,7 +332,7 @@
 def package_api_to_dict(api1_dict, context):
 
     package = context.get("package")
-
+    api_version = context.get('api_version') or '1'
     dictized = {}
 
     for key, value in api1_dict.iteritems():
@@ -357,10 +357,14 @@
                 else:
                     new_value.append({"key": extras_key,
                                       "value": None})
+        if key == 'groups' and len(value):
+            if api_version == '1':
+                new_value = [{'name': item} for item in value]
+            else:
+                new_value = [{'id': item} for item in value]
 
         dictized[key] = new_value
 
-    groups = dictized.pop('groups', None)
     download_url = dictized.pop('download_url', None)
     if download_url and not dictized.get('resources'):
         dictized["resources"] = [{'url': download_url}]


--- a/ckan/logic/auth/create.py	Mon Oct 17 15:07:58 2011 +0100
+++ b/ckan/logic/auth/create.py	Wed Oct 19 11:15:58 2011 +0100
@@ -1,4 +1,4 @@
-from ckan.logic import check_access_old
+from ckan.logic import check_access_old, NotFound
 from ckan.authz import Authorizer
 from ckan.lib.base import _
 
@@ -80,16 +80,20 @@
     model = context['model']
     pkg = context.get("package")
 
-    ## hack as api does not allow groups
-    if context.get("allow_partial_update"):
-        return True
+    api_version = context.get('api_version') or '1'
 
     group_dicts = data_dict.get("groups", [])
     groups = set()
     for group_dict in group_dicts:
-        id = group_dict.get('id')
-        if not id:
-            continue
+        if isinstance(group_dict,dict):
+            if api_version == '1':
+                id = group_dict.get('name')
+            else:
+                id = group_dict.get('id')
+            if not id:
+                continue
+        else:
+            id = group_dict
         grp = model.Group.get(id)
         if grp is None:
             raise NotFound(_('Group was not found.'))


--- a/ckan/logic/schema.py	Mon Oct 17 15:07:58 2011 +0100
+++ b/ckan/logic/schema.py	Wed Oct 19 11:15:58 2011 +0100
@@ -104,6 +104,7 @@
         'relationships_as_subject': default_relationship_schema(),
         'groups': {
             'id': [ignore_missing, unicode],
+            'name': [ignore_missing, unicode],
             '__extras': [ignore],
         }
     }


--- a/ckan/tests/functional/api/model/test_package.py	Mon Oct 17 15:07:58 2011 +0100
+++ b/ckan/tests/functional/api/model/test_package.py	Wed Oct 19 11:15:58 2011 +0100
@@ -3,9 +3,9 @@
 from nose.tools import assert_equal, assert_raises
 
 from ckan.tests.functional.api.base import BaseModelApiTestCase
-from ckan.tests.functional.api.base import Api1TestCase as Version1TestCase 
-from ckan.tests.functional.api.base import Api2TestCase as Version2TestCase 
-from ckan.tests.functional.api.base import ApiUnversionedTestCase as UnversionedTestCase 
+from ckan.tests.functional.api.base import Api1TestCase as Version1TestCase
+from ckan.tests.functional.api.base import Api2TestCase as Version2TestCase
+from ckan.tests.functional.api.base import ApiUnversionedTestCase as UnversionedTestCase
 from ckan import plugins
 import ckan.lib.search as search
 
@@ -26,6 +26,19 @@
         super(PackagesTestCase, self).teardown()
         model.Session.connection().invalidate()
 
+    def get_groups_identifiers(self, test_groups, users=[]):
+        groups = []
+        for grp in test_groups:
+            group = model.Group.get(grp)
+            if self.get_expected_api_version() == '1':
+                groups.append(group.name)
+            else:
+                groups.append(group.id)
+
+            if users:
+                model.setup_default_user_roles(group, users)
+        return groups
+
     def test_register_get_ok(self):
         offset = self.package_offset()
         res = self.app.get(offset, status=self.STATUS_200_OK)
@@ -39,7 +52,7 @@
         res = self.app.post(offset, params=postparams,
                             status=self.STATUS_201_CREATED,
                             extra_environ=self.extra_environ)
-        
+
         # Check the returned package is as expected
         pkg = self.loads(res.body)
         assert_equal(pkg['name'], self.package_fixture_data['name'])
@@ -50,7 +63,7 @@
 
         # Check the value of the Location header.
         location = res.header('Location')
-        
+
         assert offset in location
         res = self.app.get(location, status=self.STATUS_200_OK)
         # Check the database record.
@@ -95,15 +108,91 @@
         assert '"extras": {' in res, res
         for key, value in self.package_fixture_data['extras'].items():
             assert '"%s": "%s"' % (key, value) in res, res
-        
+
         self.remove()
-        
+
         # Test Packages Register Post 409 (conflict - create duplicate package).
         offset = self.package_offset()
         postparams = '%s=1' % self.dumps(self.package_fixture_data)
         res = self.app.post(offset, params=postparams, status=self.STATUS_409_CONFLICT,
                 extra_environ=self.extra_environ)
-        self.remove()        
+        self.remove()
+
+    def test_register_post_with_group(self):
+        assert not self.get_package_by_name(self.package_fixture_data['name'])
+        offset = self.package_offset()
+        
+        test_groups = [u'david']
+        user = model.User.by_name(u'russianfan')
+
+        groups = self.get_groups_identifiers(test_groups,[user])
+
+        package_fixture_data = self.package_fixture_data
+        package_fixture_data['groups'] = groups
+        data = self.dumps(package_fixture_data)
+        res = self.post_json(offset, data, status=self.STATUS_201_CREATED,
+                             extra_environ={'Authorization':str(user.apikey)})
+
+        # Check the database record.
+        self.remove()
+        package = self.get_package_by_name(self.package_fixture_data['name'])
+        assert package
+        if self.get_expected_api_version() == '1':
+            self.assert_equal([g.name for g in package.groups], groups)
+        else:
+            self.assert_equal([g.id for g in package.groups], groups)
+        del package_fixture_data['groups']
+
+    def test_register_post_with_group_not_authorized(self):
+        assert not self.get_package_by_name(self.package_fixture_data['name'])
+        offset = self.package_offset()
+
+        test_groups = [u'david']
+        groups = self.get_groups_identifiers(test_groups)
+
+        package_fixture_data = self.package_fixture_data
+        package_fixture_data['groups'] = groups
+        data = self.dumps(package_fixture_data)
+        res = self.post_json(offset, data, status=self.STATUS_403_ACCESS_DENIED,
+                             extra_environ=self.extra_environ)
+        del package_fixture_data['groups']
+
+    def test_register_post_with_group_not_found(self):
+        assert not self.get_package_by_name(self.package_fixture_data['name'])
+        offset = self.package_offset()
+
+        test_groups = [u'this-group-does-not-exist']
+        groups = test_groups
+
+        package_fixture_data = self.package_fixture_data
+        package_fixture_data['groups'] = groups
+        data = self.dumps(package_fixture_data)
+        res = self.post_json(offset, data, status=self.STATUS_404_NOT_FOUND,
+                             extra_environ=self.extra_environ)
+        del package_fixture_data['groups']
+
+    def test_register_post_with_group_sysadmin(self):
+        assert not self.get_package_by_name(self.package_fixture_data['name'])
+        offset = self.package_offset()
+        user = model.User.by_name(u'testsysadmin')
+        test_groups = [u'david']
+        groups = self.get_groups_identifiers(test_groups)
+
+        package_fixture_data = self.package_fixture_data
+        package_fixture_data['groups'] = groups
+        data = self.dumps(package_fixture_data)
+        res = self.post_json(offset, data, status=self.STATUS_201_CREATED,
+                              extra_environ={'Authorization':str(user.apikey)})
+        # Check the database record.
+        self.remove()
+        package = self.get_package_by_name(self.package_fixture_data['name'])
+        assert package
+        if self.get_expected_api_version() == '1':
+            self.assert_equal([g.name for g in package.groups], groups)
+        else:
+            self.assert_equal([g.id for g in package.groups], groups)
+
+        del package_fixture_data['groups']
 
     def test_register_post_json(self):
         assert not self.get_package_by_name(self.package_fixture_data['name'])
@@ -116,7 +205,7 @@
         package = self.get_package_by_name(self.package_fixture_data['name'])
         assert package
         self.assert_equal(package.title, self.package_fixture_data['title'])
-        
+
     def test_register_post_bad_content_type(self):
         assert not self.get_package_by_name(self.package_fixture_data['name'])
         offset = self.package_offset()
@@ -134,7 +223,7 @@
             # Check there is no database record.
             assert not package
         else:
-            assert package        
+            assert package
 
     def test_register_post_bad_request(self):
         test_params = {
@@ -282,7 +371,7 @@
                 u'size_extra':u'400',
             }],
             'extras': {
-                u'key3': u'val3', 
+                u'key3': u'val3',
                 u'key4': u'',
                 u'key2': None,
                 u'key7': ['a','b'],
@@ -377,7 +466,7 @@
         new_fixture_data = {
             'name':u'somethingnew',
             'extras': {
-                u'key1': None, 
+                u'key1': None,
                 },
         }
         self.create_package_roles_revision(old_fixture_data)
@@ -572,7 +661,7 @@
             }
         offset = self.package_offset()
         postparams = '%s=1' % self.dumps(test_params)
-        res = self.app.post(offset, params=postparams, 
+        res = self.app.post(offset, params=postparams,
                             extra_environ=self.extra_environ)
         model.Session.remove()
         pkg = self.get_package_by_name(test_params['name'])


--- a/ckan/tests/lib/test_dictization_schema.py	Mon Oct 17 15:07:58 2011 +0100
+++ b/ckan/tests/lib/test_dictization_schema.py	Wed Oct 19 11:15:58 2011 +0100
@@ -68,7 +68,7 @@
         pprint(errors)
         assert converted_data == {'extras': [{'key': u'genre', 'value': u'"romantic novel"'},
                                             {'key': u'original media', 'value': u'"book"'}],
-#                                 'groups': [{'name': u'david'}, {'name': u'roger'}],
+                                 'groups': [{'name': u'david'}, {'name': u'roger'}],
                                  'license_id': u'other-open',
                                  'name': u'anna2',
                                  'notes': u'Some test notes\n\n### A 3rd level heading\n\n**Some bolded text.**\n\n*Some italicized text.*\n\nForeign characters:\nu with umlaut \xfc\n66-style quote \u201c\nforeign word: th\xfcmb\n \nNeeds escaping:\nleft arrow <\n\n<http://ckan.net/>\n\n',

Repository URL: https://bitbucket.org/okfn/ckan/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.




More information about the ckan-changes mailing list