[ckan-changes] commit/ckan: John Glover: [api] Add resource format autocomplete api
Bitbucket
commits-noreply at bitbucket.org
Thu Jul 28 17:25:15 UTC 2011
1 new changeset in ckan:
http://bitbucket.org/okfn/ckan/changeset/1697dfa2552c/
changeset: 1697dfa2552c
user: John Glover
date: 2011-07-28 19:25:03
summary: [api] Add resource format autocomplete api
affected #: 4 files (1.7 KB)
--- a/ckan/config/routing.py Thu Jul 28 17:20:40 2011 +0100
+++ b/ckan/config/routing.py Thu Jul 28 18:25:03 2011 +0100
@@ -156,6 +156,8 @@
conditions=dict(method=['GET']))
map.connect('/api/2/util/tag/autocomplete', controller='api', action='tag_autocomplete',
conditions=dict(method=['GET']))
+ map.connect('/api/2/util/resource/format_autocomplete', controller='api', action='format_autocomplete',
+ conditions=dict(method=['GET']))
map.connect('/api/2/util/authorizationgroup/autocomplete', controller='api',
action='authorizationgroup_autocomplete')
--- a/ckan/controllers/api.py Thu Jul 28 17:20:40 2011 +0100
+++ b/ckan/controllers/api.py Thu Jul 28 18:25:03 2011 +0100
@@ -551,4 +551,19 @@
}
return self._finish_ok(resultSet)
+ def format_autocomplete(self):
+ q = request.params.get('incomplete', '')
+ limit = request.params.get('limit', 5)
+ formats = []
+ if q:
+ context = {'model': model, 'session': model.Session,
+ 'user': c.user or c.author}
+ data_dict = {'q': q, 'limit': limit}
+ formats = get.format_autocomplete(context, data_dict)
+ resultSet = {
+ 'ResultSet': {
+ 'Result': [{'Format': format} for format in formats]
+ }
+ }
+ return self._finish_ok(resultSet)
--- a/ckan/logic/action/get.py Thu Jul 28 17:20:40 2011 +0100
+++ b/ckan/logic/action/get.py Thu Jul 28 18:25:03 2011 +0100
@@ -1,5 +1,5 @@
from sqlalchemy.sql import select
-from sqlalchemy import or_, func, desc
+from sqlalchemy import or_, and_, func, desc
from ckan.logic import NotFound, check_access
from ckan.plugins import (PluginImplementations,
@@ -457,6 +457,36 @@
return [tag.name for tag in query.results]
+def format_autocomplete(context, data_dict):
+ '''Returns formats containing the provided string'''
+ model = context['model']
+ session = context['session']
+ user = context['user']
+
+ q = data_dict.get('q', None)
+ if not q:
+ return []
+
+ limit = data_dict.get('limit', 5)
+ like_q = u'%' + q + u'%'
+ query = session.query(model.ResourceRevision.format)\
+ .filter(model.ResourceRevision.format.ilike(like_q))\
+ .limit(limit)\
+ .distinct()
+
+ query = session.query(model.ResourceRevision.format,
+ func.count(model.ResourceRevision.format).label('total'))\
+ .filter(and_(
+ model.ResourceRevision.state == 'active',
+ model.ResourceRevision.current == True
+ ))\
+ .filter(model.ResourceRevision.format.ilike(like_q))\
+ .group_by(model.ResourceRevision.format)\
+ .order_by('total DESC')\
+ .limit(limit)
+
+ return [resource.format for resource in query]
+
def user_autocomplete(context, data_dict):
'''Returns users containing the provided string'''
model = context['model']
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