update search view
[mygpo.git] / mygpo / directory / search.py
blob4faabc8a976b93393f60944df6c1b1f9317c9fce
1 from mygpo.core.models import Podcast, PodcastGroup
2 from mygpo.utils import is_url
3 from mygpo.data.feeddownloader import update_podcasts
4 from mygpo.api.sanitizing import sanitize_url
7 def search_wrapper(result):
8 doc = result['doc']
9 if doc['doc_type'] == 'Podcast':
10 p = Podcast.wrap(doc)
11 elif doc['doc_type'] == 'PodcastGroup':
12 p = PodcastGroup.wrap(doc)
13 p._id = result['id']
14 return p
17 def search_podcasts(q, limit=20, skip=0):
19 if is_url(q):
20 url = sanitize_url(q)
22 podcast = Podcast.for_url(url, create=True)
24 if not podcast.title:
25 update_podcasts([podcast])
27 podcast = Podcast.for_url(url)
29 return [podcast], 1
32 db = Podcast.get_db()
34 #FIXME current couchdbkit can't parse responses for multi-query searches
35 q = q.replace(',', '')
37 res = db.search('podcasts/search', wrapper=search_wrapper,
38 include_docs=True, limit=limit, skip=skip, q=q,
39 sort='\\subscribers<int>')
41 #FIXME: return empty results in case of search backend error
42 try:
43 return list(res), res.total_rows
44 except:
45 return [], 0