1 # -*- coding: utf-8 -*-
3 from django.shortcuts import render_to_response, get_object_or_404
4 from django.template import RequestContext
5 from django.contrib.auth.decorators import login_required
6 from django.views.decorators import cache
7 from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect
9 from catalogue.utils import get_random_hash
10 from catalogue.models import Book, Tag, Fragment, TAG_CATEGORIES
11 from catalogue.fields import dumps
12 from catalogue.views import JSONResponse
13 from catalogue import forms
14 from search import MultiSearch, JVM, SearchResult
15 from lucene import StringReader
16 from suggest.forms import PublishingSuggestForm
20 dictionary = enchant.Dict('pl_PL')
23 def did_you_mean(query, tokens):
25 # sprawdzić, czy słowo nie jest aby autorem - proste szukanie termu w author!
27 print("%s ok? %s, sug: %s" %(t, dictionary.check(t), dictionary.suggest(t)))
28 if not dictionary.check(t):
30 change[t] = dictionary.suggest(t)[0]
37 for frm, to in change.items():
38 query = query.replace(frm, to)
43 def category_name(category):
45 return filter(lambda c: c[0] == category, TAG_CATEGORIES)[0][1].encode('utf-8')
47 raise KeyError("No category %s" % category)
51 prefix = request.GET.get('term', '')
53 return JSONResponse(dumps(None))
54 JVM.attachCurrentThread()
59 tags = request.GET.get('tags', '')
60 hint.tags(Tag.get_tag_list(tags))
64 # tagi beda ograniczac tutaj
65 # ale tagi moga byc na ksiazce i na fragmentach
66 # jezeli tagi dot tylko ksiazki, to wazne zeby te nowe byly w tej samej ksiazce
67 # jesli zas dotycza themes, to wazne, zeby byly w tym samym fragmencie.
69 tags = s.hint_tags(prefix)
70 books = s.hint_books(prefix)
76 'category': category_name(t.category),
78 'url': t.get_absolute_url()}
81 'category': category_name('book'),
83 'url': b.get_absolute_url()}
87 def foo(s, q, tag_list=None):
90 tag_list = Tag.get_tag_list(tag_list)
101 JVM.attachCurrentThread() # where to put this?
108 if 'q' in request.GET:
109 tags = request.GET.get('tags', '')
110 query = request.GET['q']
111 book_id = request.get('book', None)
113 if book_id is not None:
114 book = get_object_or_404(Book, id=book_id)
118 tag_list = Tag.get_tag_list(tags)
123 return render_to_response('catalogue/search_too_short.html', {'tags': tag_list, 'prefix': query},
124 context_instance=RequestContext(request))
129 toks = StringReader(query)
130 fuzzy = 'fuzzy' in request.GET
134 results = SearchResult.aggregate(srch.search_perfect_book(toks, fuzzy=fuzzy, hint=hint),
135 srch.search_perfect_parts(toks, fuzzy=fuzzy, hint=hint),
136 srch.search_everywhere(toks, fuzzy=fuzzy, hint=hint))
137 results.sort(reverse=True)
142 if len(results) == 1:
143 if len(results[0].hits) == 0:
144 return HttpResponseRedirect(results[0].book.get_absolute_url())
145 elif len(results[0].hits) == 1 and results[0].hits[0] is not None:
146 frag = Fragment.objects.get(anchor=results[0].hits[0])
147 return HttpResponseRedirect(frag.get_absolute_url())
148 elif len(results) == 0:
149 form = PublishingSuggestForm(initial={"books": query + ", "})
150 return render_to_response('catalogue/search_no_hits.html',
151 {'tags': tag_list, 'prefix': query, "pubsuggest_form": form,
152 'form': forms.SearchForm()},
153 context_instance=RequestContext(request))
155 return render_to_response('catalogue/search_multiple_hits.html',
156 {'tags': tag_list, 'prefix': query,
157 'results': results, 'from': forms.SearchForm()},
158 context_instance=RequestContext(request))
160 # return render_to_response('newsearch/search.html', {'results': results,
161 # 'did_you_mean': (query is not None) and
162 # did_you_mean(query, srch.get_tokens(query, field='SIMPLE')),
164 # context_instance=RequestContext(request))