diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2012-12-21 22:07:45 +0100 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2012-12-21 22:07:45 +0100 |
commit | e745764fc55043f6107f68986f82845310537fee (patch) | |
tree | ca8923d2b98f304759afa5799eebd25f129165d2 | |
parent | 1134dc1efa6a4326424cf7b1271cb4465afb712d (diff) |
Added feedback and limits to searches.
-rwxr-xr-x | app.py | 13 | ||||
-rw-r--r-- | db.py | 9 | ||||
-rw-r--r-- | static/index.html | 1 | ||||
-rw-r--r-- | static/init.js | 11 |
4 files changed, 29 insertions, 5 deletions
@@ -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) @@ -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 @@ <input type="text" id="search_box" /> <span> <input type="button" id="search-add" value="Add selected" /> + <span id="search-info"></span> </span> <table class="track-table"> <thead> 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('<tr><td colspan="3"><img src="/static/icons/loading.gif" alt="Searching..." /> Searching...</td></tr>'); 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) { |