[ckan-changes] commit/ckan: amercader: [logic] Move package autocomplete

Bitbucket commits-noreply at bitbucket.org
Wed Jul 20 13:39:24 UTC 2011


1 new changeset in ckan:

http://bitbucket.org/okfn/ckan/changeset/5f2ae1a94972/
changeset:   5f2ae1a94972
branch:      feature-1229-db-out-of-controllers
user:        amercader
date:        2011-07-20 13:51:50
summary:     [logic] Move package autocomplete
affected #:  3 files (1.0 KB)

--- a/ckan/controllers/package.py	Tue Jul 19 12:57:39 2011 +0100
+++ b/ckan/controllers/package.py	Wed Jul 20 12:51:50 2011 +0100
@@ -6,7 +6,6 @@
 import re
 
 from sqlalchemy.orm import eagerload_all
-from sqlalchemy import or_
 import genshi
 from pylons import config, cache
 from pylons.i18n import get_lang, _
@@ -739,17 +738,20 @@
         q = unicode(request.params.get('q', ''))
         if not len(q): 
             return ''
+
+        context = {'model': model, 'session': model.Session,
+                   'user': c.user or c.author}
+
+        data_dict = {'query':q}
+
+        packages = get.package_autocomplete(context,data_dict)
+
         pkg_list = []
-        like_q = u"%s%%" % q
-        pkg_query = ckan.authz.Authorizer().authorized_query(c.user, model.Package)
-        pkg_query = pkg_query.filter(or_(model.Package.name.ilike(like_q),
-                                         model.Package.title.ilike(like_q)))
-        pkg_query = pkg_query.limit(10)
-        for pkg in pkg_query:
-            if pkg.name.lower().startswith(q.lower()):
-                pkg_list.append('%s|%s' % (pkg.name, pkg.name))
+        for pkg in packages:
+            if pkg['name'].lower().startswith(q.lower()):
+                pkg_list.append('%s|%s' % (pkg['name'], pkg['name']))
             else:
-                pkg_list.append('%s (%s)|%s' % (pkg.title.replace('|', ' '), pkg.name, pkg.name))
+                pkg_list.append('%s (%s)|%s' % (pkg['title'].replace('|', ' '), pkg['name'], pkg['name']))
         return '\n'.join(pkg_list)
 
     def _render_edit_form(self, fs, params={}, clear_session=False):


--- a/ckan/logic/action/get.py	Tue Jul 19 12:57:39 2011 +0100
+++ b/ckan/logic/action/get.py	Wed Jul 20 12:51:50 2011 +0100
@@ -1,4 +1,6 @@
 from sqlalchemy.sql import select
+from sqlalchemy import or_
+
 from ckan.logic import NotFound, check_access
 from ckan.plugins import (PluginImplementations,
                           IGroupController,
@@ -254,3 +256,25 @@
 
     return group_dict
 
+def package_autocomplete(context, data_dict):
+
+    model = context['model']
+    session = context['session']
+    user = context['user']
+    q = data_dict['query']
+
+    like_q = u"%s%%" % q
+
+    #TODO: Auth
+    pkg_query = ckan.authz.Authorizer().authorized_query(user, model.Package)
+    pkg_query = session.query(model.Package) \
+                    .filter(or_(model.Package.name.ilike(like_q),
+                                model.Package.title.ilike(like_q)))
+    pkg_query = pkg_query.limit(10)
+
+    pkg_list = []
+    for package in pkg_query:
+        result_dict = table_dictize(package, context)
+        pkg_list.append(result_dict)
+
+    return pkg_list


--- a/ckan/tests/functional/api/test_action.py	Tue Jul 19 12:57:39 2011 +0100
+++ b/ckan/tests/functional/api/test_action.py	Wed Jul 20 12:51:50 2011 +0100
@@ -21,7 +21,12 @@
                                         "success": True,
                                         "result": ["annakarenina", "warandpeace"]}
 
-    def test_02_create_update_package(self):
+    def test_02_package_autocomplete(self):
+        query = 'a'
+        res = self.app.get('/package/autocomplete?q=%s' % query)
+        assert res.body == "annakarenina|annakarenina\nA Wonderful Story (warandpeace)|warandpeace"
+
+    def test_03_create_update_package(self):
 
         package = {
             'author': None,
@@ -70,3 +75,4 @@
         package_created.pop('revision_timestamp')
         assert package_updated == package_created#, (pformat(json.loads(res.body)), pformat(package_created['result']))
 
+

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