+def category_name(category):
+ try:
+ return filter(lambda c: c[0] == category, TAG_CATEGORIES)[0][1].encode('utf-8')
+ except IndexError:
+ raise KeyError("No category %s" % category)
+
+
+def hint(request):
+ prefix = request.GET.get('term', '')
+ if len(prefix) < 2:
+ return JSONResponse(dumps(None))
+ JVM.attachCurrentThread()
+ s = MultiSearch()
+
+ hint = s.hint()
+ try:
+ tags = request.GET.get('tags', '')
+ hint.tags(Tag.get_tag_list(tags))
+ except:
+ pass
+
+ # tagi beda ograniczac tutaj
+ # ale tagi moga byc na ksiazce i na fragmentach
+ # jezeli tagi dot tylko ksiazki, to wazne zeby te nowe byly w tej samej ksiazce
+ # jesli zas dotycza themes, to wazne, zeby byly w tym samym fragmencie.
+
+ tags = s.hint_tags(prefix)
+ books = s.hint_books(prefix)
+
+ # TODO DODAC TU HINTY
+
+ return JSONResponse(
+ [{'label': t.name,
+ 'category': category_name(t.category),
+ 'id': t.id,
+ 'url': t.get_absolute_url()}
+ for t in tags] + \
+ [{'label': b.title,
+ 'category': category_name('book'),
+ 'id': b.id,
+ 'url': b.get_absolute_url()}
+ for b in books])
+
+
+def foo(s, q, tag_list=None):
+ hint = s.hint()
+ try:
+ tag_list = Tag.get_tag_list(tag_list)
+ hint.tags(tag_list)
+ except:
+ tag_list = None
+
+ q = StringReader(q)
+ return (q, hint)
+
+