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
8 from django.utils.translation import ugettext as _
10 from catalogue.utils import get_random_hash
11 from catalogue.models import Book, Tag, Fragment, TAG_CATEGORIES
12 from catalogue.fields import dumps
13 from catalogue.views import JSONResponse
14 from catalogue import forms
15 from search import Search, JVM, SearchResult
16 from lucene import StringReader
17 from suggest.forms import PublishingSuggestForm
21 dictionary = enchant.Dict('pl_PL')
24 def did_you_mean(query, tokens):
26 # sprawdzić, czy słowo nie jest aby autorem - proste szukanie termu w author!
28 print("%s ok? %s, sug: %s" %(t, dictionary.check(t), dictionary.suggest(t)))
29 if not dictionary.check(t):
31 change[t] = dictionary.suggest(t)[0]
38 for frm, to in change.items():
39 query = query.replace(frm, to)
45 prefix = request.GET.get('term', '')
47 return JSONResponse([])
48 JVM.attachCurrentThread()
53 tags = request.GET.get('tags', '')
54 hint.tags(Tag.get_tag_list(tags))
58 # tagi beda ograniczac tutaj
59 # ale tagi moga byc na ksiazce i na fragmentach
60 # jezeli tagi dot tylko ksiazki, to wazne zeby te nowe byly w tej samej ksiazce
61 # jesli zas dotycza themes, to wazne, zeby byly w tym samym fragmencie.
64 tags = s.hint_tags(prefix)
65 books = s.hint_books(prefix)
71 'category': _(t.category),
73 'url': t.get_absolute_url()}
76 'category': _('book'),
78 'url': b.get_absolute_url()}
82 def foo(s, q, tag_list=None):
85 tag_list = Tag.get_tag_list(tag_list)
96 JVM.attachCurrentThread() # where to put this?
103 if 'q' in request.GET:
104 tags = request.GET.get('tags', '')
105 query = request.GET['q']
106 book_id = request.GET.get('book', None)
108 if book_id is not None:
109 book = get_object_or_404(Book, id=book_id)
113 tag_list = Tag.get_tag_list(tags)
118 return render_to_response('catalogue/search_too_short.html', {'tags': tag_list, 'prefix': query},
119 context_instance=RequestContext(request))
125 toks = StringReader(query)
126 fuzzy = 'fuzzy' in request.GET
130 results = SearchResult.aggregate(srch.search_perfect_book(toks, fuzzy=fuzzy, hint=hint),
131 srch.search_perfect_parts(toks, fuzzy=fuzzy, hint=hint),
132 srch.search_everywhere(toks, fuzzy=fuzzy, hint=hint))
133 results.sort(reverse=True)
138 if len(results) == 1:
139 if len(results[0].hits) == 0:
140 return HttpResponseRedirect(results[0].book.get_absolute_url())
141 elif len(results[0].hits) == 1 and results[0].hits[0] is not None:
142 frag = Fragment.objects.get(anchor=results[0].hits[0])
143 return HttpResponseRedirect(frag.get_absolute_url())
144 elif len(results) == 0:
145 form = PublishingSuggestForm(initial={"books": query + ", "})
146 return render_to_response('catalogue/search_no_hits.html',
147 {'tags': tag_list, 'prefix': query, "pubsuggest_form": form,
148 'form': forms.SearchForm()},
149 context_instance=RequestContext(request))
151 return render_to_response('catalogue/search_multiple_hits.html',
152 {'tags': tag_list, 'prefix': query,
153 'results': results, 'from': forms.SearchForm()},
154 context_instance=RequestContext(request))
156 # return render_to_response('newsearch/search.html', {'results': results,
157 # 'did_you_mean': (query is not None) and
158 # did_you_mean(query, srch.get_tokens(query, field='SIMPLE')),
160 # context_instance=RequestContext(request))