Device view: Allow searching the tags of books that match books in the calibre library by prefix the search term with tags:

This commit is contained in:
Kovid Goyal 2014-01-11 18:47:34 +05:30
commit de49923947

View File

@ -1104,7 +1104,8 @@ class OnDeviceSearch(SearchQueryParser): # {{{
'format', 'format',
'formats', 'formats',
'title', 'title',
'inlibrary' 'inlibrary',
'tags'
] ]
def __init__(self, model): def __init__(self, model):
@ -1135,14 +1136,15 @@ class OnDeviceSearch(SearchQueryParser): # {{{
if location not in self.USABLE_LOCATIONS: if location not in self.USABLE_LOCATIONS:
return set([]) return set([])
matches = set([]) matches = set([])
all_locs = set(self.USABLE_LOCATIONS) - set(['all']) all_locs = set(self.USABLE_LOCATIONS) - set(['all', 'tags'])
locations = all_locs if location == 'all' else [location] locations = all_locs if location == 'all' else [location]
q = { q = {
'title' : lambda x : getattr(x, 'title').lower(), 'title' : lambda x : getattr(x, 'title').lower(),
'author': lambda x: ' & '.join(getattr(x, 'authors')).lower(), 'author': lambda x: ' & '.join(getattr(x, 'authors')).lower(),
'collections':lambda x: ','.join(getattr(x, 'device_collections')).lower(), 'collections':lambda x: ','.join(getattr(x, 'device_collections')).lower(),
'format':lambda x: os.path.splitext(x.path)[1].lower(), 'format':lambda x: os.path.splitext(x.path)[1].lower(),
'inlibrary':lambda x : getattr(x, 'in_library') 'inlibrary':lambda x : getattr(x, 'in_library'),
'tags':lambda x : getattr(x, 'tags', [])
} }
for x in ('author', 'format'): for x in ('author', 'format'):
q[x+'s'] = q[x] q[x+'s'] = q[x]
@ -1169,10 +1171,11 @@ class OnDeviceSearch(SearchQueryParser): # {{{
else: else:
m = matchkind m = matchkind
if locvalue == 'collections': vals = accessor(row)
vals = accessor(row).split(',') if vals is None:
else: vals = ''
vals = [accessor(row)] if isinstance(vals, basestring):
vals = vals.split(',') if locvalue == 'collections' else [vals]
if _match(query, vals, m, use_primary_find_in_search=upf): if _match(query, vals, m, use_primary_find_in_search=upf):
matches.add(index) matches.add(index)
break break