Permalink
Browse files

change searchbox js to allow to query for term not in dropdown and ad…

…d super simple results page for those queries
  • Loading branch information...
cguardia committed Jan 14, 2013
1 parent 4b87002 commit f004a41fb204eeea321ea1e73309ec58e50952b3
@@ -67,6 +67,20 @@ $(document).ready(function () {
/* We have the URL as the data-value, get the label */
var label = server_data[item];
return $('<span>' + label + '</span>');
+ },
+ sorter:function (items) {
+ /*
+
+ First item is auto selected, which forces the search
+ to be exact. Adding the query at the top of the
+ results gets around this problem, so we can return a
+ result page when there's no exact match
+
+ */
+ item_url = sb_url + '?results=1&query=' + this.query;
+ server_data[item_url] = this.query;
+ items.unshift(sb_url + '?results=1&query=' + this.query);
+ return items;
}
});
-});
+});
@@ -6,16 +6,9 @@ def __init__(self, context, request):
self.context = context
self.request = request
- @mgmt_view(
- name='search',
- permission='sdi.sdi.manage-contents',
- tab_condition=False,
- renderer='json'
- )
- def search(self):
+ def _query_results(self, query):
request = self.request
context = self.context
- query = request.params['query']
query = query + '*'
catalog = find_catalog(context, 'system')
@@ -25,7 +18,7 @@ def search(self):
q = (allowed.allows(request, 'sdi.view') & text.eq(query))
resultset = q.execute()
- resultset = resultset.sort(name, limit=10)
+ resultset = resultset.sort(name)
results = []
for res_id in resultset.ids:
@@ -36,3 +29,27 @@ def search(self):
results.append(result)
return results
+
+ @mgmt_view(
+ name='search',
+ permission='sdi.sdi.manage-contents',
+ tab_condition=False,
+ renderer='json'
+ )
+ def search(self):
+ request = self.request
+ query = request.params['query']
+ return self._query_results(query)
+
+ @mgmt_view(
+ name='search',
+ permission='sdi.sdi.manage-contents',
+ tab_condition=False,
+ request_param='results=1',
+ renderer='templates/search_results.pt'
+ )
+ def search_results(self):
+ request = self.request
+ query = request.params['query']
+ return {'results': self._query_results(query),
+ 'query': query}
@@ -0,0 +1,23 @@
+<div metal:use-macro="request.sdiapi.main_template">
+
+ <div metal:fill-slot="main">
+
+ <h2>Search Results</h2>
+
+ <h4>Search term: ${query}</h4>
+
+ <ol tal:condition="results|None" class="table table-striped">
+
+ <li tal:repeat="result results">
+ <a href="${result['url']}">${result['label']}</a>
+ </li>
+
+ </ol>
+
+ <p tal:condition="not:results">
+ There were no results for this search term.
+ </p>
+
+ </div>
+
+</div>
@@ -30,7 +30,7 @@ def test_search_no_results(self):
results = inst.search()
self.assertEqual(results, [])
- def test_search_results_resource_with_title(self):
+ def test_search_resource_with_title(self):
from substanced.interfaces import IFolder
context = testing.DummyResource(__provides__=IFolder)
request = self._makeRequest()
@@ -47,7 +47,7 @@ def test_search_results_resource_with_title(self):
self.assertEqual(item['label'], 'Some Resource')
self.assertEqual(item['url'], '/mgmt_path')
- def test_search_results_resource_no_title(self):
+ def test_search_resource_no_title(self):
from substanced.interfaces import IFolder
context = testing.DummyResource(__provides__=IFolder)
request = self._makeRequest()
@@ -64,6 +64,27 @@ def test_search_results_resource_no_title(self):
self.assertEqual(item['label'], 'abcde')
self.assertEqual(item['url'], '/mgmt_path')
+ def test_search_results(self):
+ from substanced.interfaces import IFolder
+ context = testing.DummyResource(__provides__=IFolder)
+ request = self._makeRequest()
+ request.params['query'] = 'abc'
+ request.params['results'] = '1'
+ result = testing.DummyResource()
+ result.__name__ = 'abcde'
+ context.__objectmap__ = DummyObjectMap(result)
+ context['catalogs'] = self._makeCatalogs(oids=[1],
+ resources=(result,))
+ inst = self._makeOne(context, request)
+ response = inst.search_results()
+ results = response['results']
+ self.assertEqual(len(results), 1)
+ item = results[0]
+ self.assertEqual(item['label'], 'abcde')
+ self.assertEqual(item['url'], '/mgmt_path')
+ term = response['query']
+ self.assertEqual(term, 'abc')
+
class DummyCatalogs(testing.DummyResource):
__is_service__ = True

0 comments on commit f004a41

Please sign in to comment.