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

Bitbucket commits-noreply at bitbucket.org
Tue Jul 26 17:02:00 UTC 2011


3 new changesets in ckan:

http://bitbucket.org/okfn/ckan/changeset/f093777305c3/
changeset:   f093777305c3
branch:      feature-1229-db-out-of-controllers
user:        amercader
date:        2011-07-26 17:01:12
summary:     [logic] Move group index
affected #:  7 files (2.3 KB)

--- a/ckan/controllers/group.py	Tue Jul 26 15:03:17 2011 +0100
+++ b/ckan/controllers/group.py	Tue Jul 26 16:01:12 2011 +0100
@@ -45,15 +45,19 @@
         self.extensions = PluginImplementations(IGroupController)
     
     def index(self):
-        
+
         if not self.authorizer.am_authorized(c, model.Action.SITE_READ, model.System):
             abort(401, _('Not authorized to see this page'))
-        
-        query = authz.Authorizer().authorized_query(c.user, model.Group)
-        query = query.order_by(model.Group.name.asc())
-        query = query.order_by(model.Group.title.asc())
+
+        context = {'model': model, 'session': model.Session,
+                   'user': c.user or c.author}
+
+        data_dict = {'all_fields': True}
+               
+        results = get.group_list(context,data_dict)
+
         c.page = Page(
-            collection=query,
+            collection=results,
             page=request.params.get('page', 1),
             items_per_page=20
         )


--- a/ckan/lib/dictization/model_dictize.py	Tue Jul 26 15:03:17 2011 +0100
+++ b/ckan/lib/dictization/model_dictize.py	Tue Jul 26 16:01:12 2011 +0100
@@ -11,7 +11,7 @@
 
 ## package save
 
-def group_list_dictize(obj_list, context, sort_key=lambda x:x):
+def group_list_dictize(obj_list, context, sort_key=lambda x:x['display_name']):
 
     active = context.get('active', True)
 
@@ -23,6 +23,10 @@
         if active and obj.state not in ('active', 'pending'):
             continue
 
+        group_dict['display_name'] = obj.display_name
+
+        group_dict['packages'] = len(obj.packages)
+
         result_list.append(group_dict)
     return sorted(result_list, key=sort_key)
 
@@ -148,11 +152,13 @@
 def group_dictize(group, context):
 
     result_dict = table_dictize(group, context)
+    
+    result_dict['display_name'] = group.display_name
 
-    result_dict["extras"] = extras_dict_dictize(
+    result_dict['extras'] = extras_dict_dictize(
         group._extras, context)
 
-    result_dict["packages"] = obj_list_dictize(
+    result_dict['packages'] = obj_list_dictize(
         group.packages, context)
 
     return result_dict


--- a/ckan/logic/action/get.py	Tue Jul 26 15:03:17 2011 +0100
+++ b/ckan/logic/action/get.py	Tue Jul 26 16:01:12 2011 +0100
@@ -11,6 +11,7 @@
 from ckan.lib.dictization.model_dictize import (package_dictize,
                                                 resource_list_dictize,
                                                 group_dictize,
+                                                group_list_dictize,
                                                 tag_dictize,
                                                 user_dictize)
 
@@ -87,14 +88,27 @@
     return revision_dicts
 
 def group_list(context, data_dict):
-    model = context["model"]
-    user = context["user"]
+    '''Returns a list of groups'''
+
+    model = context['model']
+    user = context['user']
     api = context.get('api_version') or '1'
     ref_group_by = 'id' if api == '2' else 'name';
 
+    all_fields = data_dict.get('all_fields',None)
+
     query = ckan.authz.Authorizer().authorized_query(user, model.Group)
-    groups = query.all() 
-    return [getattr(p, ref_group_by) for p in groups]
+    query = query.order_by(model.Group.name.asc())
+    query = query.order_by(model.Group.title.asc())
+
+    groups = query.all()
+
+    if not all_fields:
+        group_list = [getattr(p, ref_group_by) for p in groups]
+    else:
+        group_list = group_list_dictize(groups,context)
+    
+    return group_list
 
 def group_list_authz(context, data_dict):
     model = context['model']


--- a/ckan/templates/_util.html	Tue Jul 26 15:03:17 2011 +0100
+++ b/ckan/templates/_util.html	Tue Jul 26 16:01:12 2011 +0100
@@ -161,7 +161,20 @@
     </tr></py:for></table>
-  
+
+  <!--! List of data package groups: pass in a collection of data package groups 
+        and this renders the standard group listing. Same as the above, but using dictionaries -->
+  <table py:def="group_list_from_dict(groups)" class="groups">
+    <tr><th>Title</th><th>Number of packages</th><th>Description</th></tr>
+    <py:for each="group in groups">
+    <tr>
+      <td><a href="${h.url_for(controller='group', action='read', id=group['name'])}">${group['display_name']}</a></td>
+      <td>${group['packages']}</td>
+      <td>${h.truncate(group['description'], length=80, whole_word=True)}</td>
+    </tr>
+    </py:for>
+  </table>
+ 
   <!--! List of authorization groups: pass in a collection of authorization groups and 
         this renders the standard group listing --><table py:def="authorization_group_list(authorization_groups)" class="authorization_groups">


--- a/ckan/templates/group/index.html	Tue Jul 26 15:03:17 2011 +0100
+++ b/ckan/templates/group/index.html	Tue Jul 26 16:01:12 2011 +0100
@@ -11,7 +11,7 @@
     <p i18n:msg="item_count">There are <strong>${c.page.item_count}</strong> groups.</p>
 
     ${c.page.pager()}
-    ${group_list(c.page.items)}
+    ${group_list_from_dict(c.page.items)}
     ${c.page.pager()}
     
     <py:choose test="">


--- a/ckan/tests/functional/api/test_action.py	Tue Jul 26 15:03:17 2011 +0100
+++ b/ckan/tests/functional/api/test_action.py	Tue Jul 26 16:01:12 2011 +0100
@@ -278,3 +278,30 @@
             'success': False
         }
 
+    def test_13_group_list(self):
+        postparams = '%s=1' % json.dumps({})
+        res = self.app.post('/api/action/group_list', params=postparams)
+        res_obj = json.loads(res.body)
+        assert res_obj == {
+            'result': [
+                'david',
+                'roger'
+            ],
+            'help': 'Returns a list of groups',
+            'success': True
+        }
+        
+        #Get all fields
+        postparams = '%s=1' % json.dumps({'all_fields':True})
+        res = self.app.post('/api/action/group_list', params=postparams)
+        res_obj = json.loads(res.body)
+
+        assert res_obj['success'] == True
+        assert res_obj['result'][0]['name'] == 'david'
+        assert res_obj['result'][0]['display_name'] == 'Dave\'s books'
+        assert res_obj['result'][0]['packages'] == 2
+        assert res_obj['result'][1]['name'] == 'roger'
+        assert res_obj['result'][1]['packages'] == 1
+        assert 'id' in res_obj['result'][0]
+        assert 'revision_id' in res_obj['result'][0]
+        assert 'state' in res_obj['result'][0]


--- a/ckan/tests/lib/test_dictization.py	Tue Jul 26 15:03:17 2011 +0100
+++ b/ckan/tests/lib/test_dictization.py	Tue Jul 26 16:01:12 2011 +0100
@@ -771,6 +771,7 @@
                     'extras': [{'key': u'genre', 'state': u'active', 'value': u'"horror"'},
                                {'key': u'media', 'state': u'active', 'value': u'"dvd"'}],
                     'name': u'help',
+                    'display_name': u'help',
                     'packages': [{'author': None,
                                   'author_email': None,
                                   'license_id': u'other-open',


http://bitbucket.org/okfn/ckan/changeset/ba79edd4da23/
changeset:   ba79edd4da23
branch:      feature-1229-db-out-of-controllers
user:        amercader
date:        2011-07-26 18:45:46
summary:     [logic] Move group read. Note that extension calls are now handled in the logic
layer
affected #:  3 files (1.2 KB)

--- a/ckan/controllers/group.py	Tue Jul 26 16:01:12 2011 +0100
+++ b/ckan/controllers/group.py	Tue Jul 26 17:45:46 2011 +0100
@@ -39,10 +39,6 @@
                 c, model.Action.CHANGE_STATE, group)
 
     ## end hooks
-
-    def __init__(self):
-        BaseController.__init__(self)
-        self.extensions = PluginImplementations(IGroupController)
     
     def index(self):
 
@@ -65,17 +61,21 @@
 
 
     def read(self, id):
-        c.group = model.Group.get(id)
-        if c.group is None:
-            abort(404)
-        auth_for_read = self.authorizer.am_authorized(c, model.Action.READ, c.group)
-        if not auth_for_read:
-            abort(401, _('Not authorized to read %s') % id.encode('utf8'))
+        context = {'model': model, 'session': model.Session,
+                   'user': c.user or c.author,
+                   'schema': self._form_to_db_schema()}
+        data_dict = {'id': id}
+        try:
+            c.group_dict = get.group_show(context, data_dict)
+            c.group = context['group']
+        except NotFound:
+            abort(404, _('Group not found'))
+        except NotAuthorized:
+            abort(401, _('Unauthorized to read group %s') % id)
         
-        import ckan.misc
-        format = ckan.misc.MarkdownFormat()
-        desc_formatted = format.to_html(c.group.description)
-        try: 
+        try:
+ 
+            desc_formatted = ckan.misc.MarkdownFormat().to_html(c.group.description)
             desc_formatted = genshi.HTML(desc_formatted)
         except genshi.ParseError, e:
             log.error('Could not print group description: %r Error: %r', c.group.description, e)
@@ -88,8 +88,7 @@
             page=request.params.get('page', 1),
             items_per_page=50
         )
-        for extension in self.extensions:
-            extension.read(c.group)
+
         return render('group/read.html')
 
     def new(self, data=None, errors=None, error_summary=None):


--- a/ckan/logic/action/get.py	Tue Jul 26 16:01:12 2011 +0100
+++ b/ckan/logic/action/get.py	Tue Jul 26 17:45:46 2011 +0100
@@ -267,6 +267,8 @@
     return rev_dict
 
 def group_show(context, data_dict):
+    '''Shows group details'''
+
     model = context['model']
     id = data_dict['id']
     api = context.get('api_version') or '1'
@@ -277,6 +279,7 @@
 
     if group is None:
         raise NotFound
+
     check_access(group, model.Action.READ, context)
 
     group_dict = group_dictize(group, context)


--- a/ckan/tests/functional/api/test_action.py	Tue Jul 26 16:01:12 2011 +0100
+++ b/ckan/tests/functional/api/test_action.py	Tue Jul 26 17:45:46 2011 +0100
@@ -305,3 +305,35 @@
         assert 'id' in res_obj['result'][0]
         assert 'revision_id' in res_obj['result'][0]
         assert 'state' in res_obj['result'][0]
+
+    def test_14_group_show(self):
+        postparams = '%s=1' % json.dumps({'id':'david'})
+        res = self.app.post('/api/action/group_show', params=postparams)
+        res_obj = json.loads(res.body)
+        assert res_obj['help'] == 'Shows group details'
+        assert res_obj['success'] == True
+        result = res_obj['result']
+        assert result['name'] == 'david'
+        assert result['title'] == result['display_name'] == 'Dave\'s books'
+        assert result['state'] == 'active'
+        assert 'id' in result
+        assert 'revision_id' in result
+        assert len(result['packages']) == 2
+
+        #Group not found
+        postparams = '%s=1' % json.dumps({'id':'not_present_in_the_db'})
+        res = self.app.post('/api/action/group_show', params=postparams,
+                            status=self.STATUS_404_NOT_FOUND)
+
+        res_obj = json.loads(res.body)
+        pprint(res_obj)
+        assert res_obj == {
+            'error': {
+                '__type': 'Not Found Error',
+                'message': 'Not found'
+            },
+            'help': 'Shows group details',
+            'success': False
+        }
+
+


http://bitbucket.org/okfn/ckan/changeset/0fe6fc561805/
changeset:   0fe6fc561805
branch:      feature-1229-db-out-of-controllers
user:        amercader
date:        2011-07-26 18:46:37
summary:     [api] Return an error message if object was not found
affected #:  1 file (267 bytes)

--- a/ckan/controllers/api.py	Tue Jul 26 17:45:46 2011 +0100
+++ b/ckan/controllers/api.py	Tue Jul 26 17:46:37 2011 +0100
@@ -179,6 +179,11 @@
                                     'message': _('Access denied')}
             return_dict['success'] = False
             return self._finish(403, return_dict, content_type='json')
+        except NotFound:
+            return_dict['error'] = {'__type': 'Not Found Error',
+                                    'message': _('Not found')}
+            return_dict['success'] = False
+            return self._finish(404, return_dict, content_type='json')
         except ValidationError, e:
             error_dict = e.error_dict 
             error_dict['__type'] = 'Validation Error'

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