From e745764fc55043f6107f68986f82845310537fee Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Fri, 21 Dec 2012 22:07:45 +0100 Subject: Added feedback and limits to searches. --- app.py | 13 +++++++++++-- db.py | 9 +++++++-- static/index.html | 1 + static/init.js | 11 ++++++++++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 8aecdf8..eb3e7da 100755 --- a/app.py +++ b/app.py @@ -59,6 +59,10 @@ class JSONApplication(object): def search(self, environ, start_response, path): args = cgi.FieldStorage(environ = environ) query = args.getvalue('q') + if not (query or '').strip(): + start_response('200 OK', []) + return json.dumps({'total': 0, 'results': []}) + r = self.re_search.findall(query) d = {} l = [] @@ -69,16 +73,21 @@ class JSONApplication(object): else: l.append(v) + total_results = 0 results = [] try: session = db.Session() r = db.Track.search(session, *l, **d) - results = [self.format_track(x) for x in r] + total_results = r.count() + results = [self.format_track(x) for x in r.limit(100)] finally: session.close() start_response('200 OK', []) - return json.dumps(results) + return json.dumps({ + 'total': total_results, + 'results': results, + }) def albums(self, environ, start_response, path): page = int(path[1] if len(path) > 1 else 0) diff --git a/db.py b/db.py index e680448..ed53fdb 100644 --- a/db.py +++ b/db.py @@ -177,11 +177,16 @@ class Track(Base): r = r.join(f) if n in kwargs: r = r.filter(f.name.ilike('%{0}%'.format(kwargs[n]))) + s_and = [] for i in args: - s_or.append(f.name.ilike('%{0}%'.format(i))) + s_and.append(f.name.ilike('%{0}%'.format(i))) + s_or.append(and_(*s_and)) if len(s_or): r = r.filter(or_(*s_or)) - return r.all() + r = r#.limit(100) + #.all() + #print r + return r def get_path(self): return os.path.join(self.directory.path, self.filename) diff --git a/static/index.html b/static/index.html index b320435..0fc0023 100644 --- a/static/index.html +++ b/static/index.html @@ -52,6 +52,7 @@ + diff --git a/static/init.js b/static/init.js index 117e8d9..c4f6c2c 100644 --- a/static/init.js +++ b/static/init.js @@ -236,8 +236,17 @@ $(document).ready(function() { }); $('#search_box').keypress(function(event) { if(event.keyCode == 13) { + $('#search-results').empty(); + $('#search-results').append(''); var val = $(this).val(); - $.get('/json/search?q=' + encodeURIComponent(val), set_tracks($('#search-results'), $('#search-add')), 'json'); + $.get('/json/search?q=' + encodeURIComponent(val), function(data) { + var s = 'Showing ' + data.results.length; + if(data.total > data.results.length) + s += ' of ' + data.total; + s += ' results.'; + $('#search-info').text(s); + set_tracks($('#search-results'), $('#search-add'))(data.results); + }, 'json'); } }); $('#search-add').click(function(event) { -- cgit v1.2.3
Searching... Searching...