1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-10 04:02:17 +00:00

Converting api:search

This commit is contained in:
spl0k 2014-04-05 22:23:23 +02:00
parent fd8a99e362
commit f9cd584a65

View File

@ -19,7 +19,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from flask import request from flask import request
from web import app from storm.info import ClassAlias
from web import app, store
from db import Folder, Track, Artist, Album from db import Folder, Track, Artist, Album
@app.route('/rest/search.view', methods = [ 'GET', 'POST' ]) @app.route('/rest/search.view', methods = [ 'GET', 'POST' ])
@ -33,19 +34,20 @@ def old_search():
return request.error_formatter(0, 'Invalid parameter') return request.error_formatter(0, 'Invalid parameter')
if artist: if artist:
query = Folder.query.filter(~ Folder.tracks.any(), Folder.name.contains(artist)) parent = ClassAlias(Folder)
query = store.find(parent, Folder.parent_id == parent.id, Track.folder_id == Folder.id, parent.name.contains_string(artist)).config(distinct = True)
elif album: elif album:
query = Folder.query.filter(Folder.tracks.any(), Folder.name.contains(album)) query = store.find(Folder, Track.folder_id == Folder.id, Folder.name.contains_string(album)).config(distinct = True)
elif title: elif title:
query = Track.query.filter(Track.title.contains(title)) query = store.find(Track, Track.title.contains_string(title))
elif anyf: elif anyf:
folders = Folder.query.filter(Folder.name.contains(anyf)) folders = store.find(Folder, Folder.name.contains_string(anyf))
tracks = Track.query.filter(Track.title.contains(anyf)) tracks = store.find(Track, Track.title.contains_string(anyf))
res = folders.slice(offset, offset + count).all() res = list(folders[offset : offset + count])
if offset + count > folders.count(): if offset + count > folders.count():
toff = max(0, offset - folders.count()) toff = max(0, offset - folders.count())
tend = offset + count - folders.count() tend = offset + count - folders.count()
res += tracks.slice(toff, tend).all() res += list(tracks[toff : tend])
return request.formatter({ 'searchResult': { return request.formatter({ 'searchResult': {
'totalHits': folders.count() + tracks.count(), 'totalHits': folders.count() + tracks.count(),
@ -58,7 +60,7 @@ def old_search():
return request.formatter({ 'searchResult': { return request.formatter({ 'searchResult': {
'totalHits': query.count(), 'totalHits': query.count(),
'offset': offset, 'offset': offset,
'match': [ r.as_subsonic_child(request.user) for r in query.slice(offset, offset + count) ] 'match': [ r.as_subsonic_child(request.user) for r in query[offset : offset + count] ]
}}) }})
@app.route('/rest/search2.view', methods = [ 'GET', 'POST' ]) @app.route('/rest/search2.view', methods = [ 'GET', 'POST' ])
@ -79,9 +81,10 @@ def new_search():
if not query: if not query:
return request.error_formatter(10, 'Missing query parameter') return request.error_formatter(10, 'Missing query parameter')
artist_query = Folder.query.filter(~ Folder.tracks.any(), Folder.name.contains(query)).slice(artist_offset, artist_offset + artist_count) parent = ClassAlias(Folder)
album_query = Folder.query.filter(Folder.tracks.any(), Folder.name.contains(query)).slice(album_offset, album_offset + album_count) artist_query = store.find(parent, Folder.parent_id == parent.id, Track.folder_id == Folder.id, parent.name.contains_string(query)).config(distinct = True, offset = artist_offset, limit = artist_count)
song_query = Track.query.filter(Track.title.contains(query)).slice(song_offset, song_offset + song_count) album_query = store.find(Folder, Track.folder_id == Folder.id, Folder.name.contains_string(query)).config(distinct = True, offset = album_offset, limit = album_count)
song_query = store.find(Track, Track.title.contains_string(query))[song_offset : song_offset + song_count]
return request.formatter({ 'searchResult2': { return request.formatter({ 'searchResult2': {
'artist': [ { 'id': str(a.id), 'name': a.name } for a in artist_query ], 'artist': [ { 'id': str(a.id), 'name': a.name } for a in artist_query ],
@ -107,9 +110,9 @@ def search_id3():
if not query: if not query:
return request.error_formatter(10, 'Missing query parameter') return request.error_formatter(10, 'Missing query parameter')
artist_query = Artist.query.filter(Artist.name.contains(query)).slice(artist_offset, artist_offset + artist_count) artist_query = store.find(Artist, Artist.name.contains_string(query))[artist_offset : artist_offset + artist_count]
album_query = Album.query.filter(Album.name.contains(query)).slice(album_offset, album_offset + album_count) album_query = store.find(Album, Album.name.contains_string(query))[album_offset : album_offset + album_count]
song_query = Track.query.filter(Track.title.contains(query)).slice(song_offset, song_offset + song_count) song_query = store.find(Track, Track.title.contains_string(query))[song_offset : song_offset + song_count]
return request.formatter({ 'searchResult2': { return request.formatter({ 'searchResult2': {
'artist': [ a.as_subsonic_artist(request.user) for a in artist_query ], 'artist': [ a.as_subsonic_artist(request.user) for a in artist_query ],