From b80f646690239d9a90d708d57780f328328b26d5 Mon Sep 17 00:00:00 2001 From: Marcin Koziej Date: Thu, 24 Nov 2011 12:28:33 +0100 Subject: [PATCH] more integrated to templates --- apps/catalogue/fields.py | 3 +- apps/catalogue/forms.py | 7 ++- apps/catalogue/urls.py | 2 +- apps/search/index.py | 8 ++-- apps/search/urls.py | 2 +- apps/search/views.py | 44 ++++++++++++++++--- .../catalogue/search_multiple_hits.html | 44 +++++++++++++++++-- wolnelektury/templates/newsearch/search.html | 4 +- 8 files changed, 95 insertions(+), 19 deletions(-) diff --git a/apps/catalogue/fields.py b/apps/catalogue/fields.py index 853a321ca..57ce58189 100644 --- a/apps/catalogue/fields.py +++ b/apps/catalogue/fields.py @@ -108,7 +108,8 @@ class JQueryAutoCompleteSearchWidget(JQueryAutoCompleteWidget): $('#%s') .autocomplete($.extend({ minLength: 0, - select: autocomplete_result_handler + select: autocomplete_result_handler, + focus: function (ui, item) { return false; } }, %s)) .data("autocomplete")._renderItem = autocomplete_format_item; """ % (field_id, options) diff --git a/apps/catalogue/forms.py b/apps/catalogue/forms.py index 71412e1a2..92f50edcb 100644 --- a/apps/catalogue/forms.py +++ b/apps/catalogue/forms.py @@ -33,12 +33,17 @@ class SearchForm(forms.Form): q = JQueryAutoCompleteSearchField('/newsearch/hint/') # {'minChars': 2, 'selectFirst': True, 'cacheLength': 50, 'matchContains': "word"}) tags = forms.CharField(widget=forms.HiddenInput, required=False) + book = forms.IntegerField(widget=forms.HiddenInput, min_value=0, required=False) + def __init__(self, *args, **kwargs): tags = kwargs.pop('tags', []) + book = kwargs.pop('book', None) super(SearchForm, self).__init__(*args, **kwargs) - self.fields['q'].widget.attrs['title'] = _('title, author, theme/topic, epoch, kind, genre') + self.fields['q'].widget.attrs['title'] = _('title, author, theme/topic, epoch, kind, genre, phrase') #self.fields['q'].widget.attrs['style'] = '' self.fields['tags'].initial = '/'.join(tag.url_chunk for tag in Tag.get_tag_list(tags)) + if book is not None: + self.fields['book'].initial = book.id class UserSetsForm(forms.Form): diff --git a/apps/catalogue/urls.py b/apps/catalogue/urls.py index c770892d9..5d623fa92 100644 --- a/apps/catalogue/urls.py +++ b/apps/catalogue/urls.py @@ -20,7 +20,7 @@ urlpatterns = patterns('catalogue.views', url(r'^polki/nowa/$', 'new_set', name='new_set'), url(r'^tags/$', 'tags_starting_with', name='hint'), url(r'^jtags/$', 'json_tags_starting_with', name='jhint'), - url(r'^szukaj/$', 'search', name='search'), + url(r'^szukaj/$', 'search', name='old_search'), # zip #url(r'^zip/pdf\.zip$', 'download_zip', {'format': 'pdf', 'slug': None}, 'download_zip_pdf'), diff --git a/apps/search/index.py b/apps/search/index.py index a3a62de18..7fffb57dc 100644 --- a/apps/search/index.py +++ b/apps/search/index.py @@ -639,10 +639,12 @@ class Hint(object): return None def part_filter(self): + fs = [] if self.part_tags: - return self.tag_filter(self.part_tags, field='themes') - else: - return None + fs.append(self.tag_filter(self.part_tags, field='themes')) + if self.book is not None: + bf = TermsFilter() + bf.addTerm # TODO def should_search_for_book(self): return self.book is None diff --git a/apps/search/urls.py b/apps/search/urls.py index 3ac5e49ff..607f094cd 100644 --- a/apps/search/urls.py +++ b/apps/search/urls.py @@ -5,7 +5,7 @@ from django.conf.urls.defaults import * urlpatterns = patterns('search.views', - url(r'^$', 'main', name='newsearch'), + url(r'^$', 'main', name='search'), url(r'^hint/$', 'hint'), ) diff --git a/apps/search/views.py b/apps/search/views.py index ff831f06b..438989c01 100644 --- a/apps/search/views.py +++ b/apps/search/views.py @@ -4,14 +4,16 @@ from django.shortcuts import render_to_response, get_object_or_404 from django.template import RequestContext from django.contrib.auth.decorators import login_required from django.views.decorators import cache +from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponsePermanentRedirect from catalogue.utils import get_random_hash -from catalogue.models import Book, Tag, TAG_CATEGORIES +from catalogue.models import Book, Tag, Fragment, TAG_CATEGORIES from catalogue.fields import dumps from catalogue.views import JSONResponse from catalogue import forms from search import MultiSearch, JVM, SearchResult from lucene import StringReader +from suggest.forms import PublishingSuggestForm import enchant @@ -105,15 +107,25 @@ def main(request): if 'q' in request.GET: tags = request.GET.get('tags', '') + query = request.GET['q'] + book_id = request.get('book', None) + book = None + if book_id is not None: + book = get_object_or_404(Book, id=book_id) + hint = srch.hint() try: tag_list = Tag.get_tag_list(tags) except: tag_list = [] + if len(query) < 2: + return render_to_response('catalogue/search_too_short.html', {'tags': tag_list, 'prefix': query}, + context_instance=RequestContext(request)) + hint.tags(tag_list) + hint.book(book) - query = request.GET['q'] toks = StringReader(query) fuzzy = 'fuzzy' in request.GET if fuzzy: @@ -127,8 +139,26 @@ def main(request): for r in results: print r.hits - return render_to_response('newsearch/search.html', {'results': results, - 'did_you_mean': (query is not None) and - did_you_mean(query, srch.get_tokens(query, field='SIMPLE')), - 'fuzzy': fuzzy}, - context_instance=RequestContext(request)) + if len(results) == 1: + if len(results[0].hits) == 0: + return HttpResponseRedirect(results[0].book.get_absolute_url()) + elif len(results[0].hits) == 1 and results[0].hits[0] is not None: + frag = Fragment.objects.get(anchor=results[0].hits[0]) + return HttpResponseRedirect(frag.get_absolute_url()) + elif len(results) == 0: + form = PublishingSuggestForm(initial={"books": query + ", "}) + return render_to_response('catalogue/search_no_hits.html', + {'tags': tag_list, 'prefix': query, "pubsuggest_form": form, + 'form': forms.SearchForm()}, + context_instance=RequestContext(request)) + + return render_to_response('catalogue/search_multiple_hits.html', + {'tags': tag_list, 'prefix': query, + 'results': results, 'from': forms.SearchForm()}, + context_instance=RequestContext(request)) + + # return render_to_response('newsearch/search.html', {'results': results, + # 'did_you_mean': (query is not None) and + # did_you_mean(query, srch.get_tokens(query, field='SIMPLE')), + # 'fuzzy': fuzzy}, + # context_instance=RequestContext(request)) diff --git a/wolnelektury/templates/catalogue/search_multiple_hits.html b/wolnelektury/templates/catalogue/search_multiple_hits.html index b569e7dce..23d1a59d4 100644 --- a/wolnelektury/templates/catalogue/search_multiple_hits.html +++ b/wolnelektury/templates/catalogue/search_multiple_hits.html @@ -7,9 +7,46 @@ {% block bodyid %}tagged-object-list{% endblock %} {% block body %} -

{% title_from_tags tags %}

- {% breadcrumbs tags %} +
+

{{ form.q }} {% trans "or" %} {% trans "return to main page" %}

+
+
+
    + {% for result in results %} +
  1. +

    {{result.book.pretty_title}} (id: {{result.book_id}}, score: {{result.score}})

    +
      + {% for hit in result.hits %} +
    • + {% for snip in hit.3.snippets %} + {{snip|safe}}
      + {% endfor %} +
    • + {% endfor %} + + {% for part in result.parts %} + {% if part.header %} +
    • W {{part.header}} nr {{part.position}}
    • + {% else %} + {% if part.fragment %} +
    • +
      Tagi/Motywy: {% for tag in part.fragment.tags %}{{tag.name}} {% endfor %}
      + {{part.fragment.short_html|safe}} +
    • + {% endif %} + {% endif %} + {% endfor %} +
    +
  2. + {% empty %} +

    No results.

    + {% endfor %} +
+
+ + +{% comment %}

{% trans "More than one result matching the criteria found." %}

+{% endcomment %}
@@ -31,4 +69,4 @@

* {% trans "Loading" %}

-{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/wolnelektury/templates/newsearch/search.html b/wolnelektury/templates/newsearch/search.html index ad7fd153b..c494ca602 100644 --- a/wolnelektury/templates/newsearch/search.html +++ b/wolnelektury/templates/newsearch/search.html @@ -10,7 +10,7 @@ {% block body %}

Search

-
+

@@ -32,7 +32,7 @@ {% for hit in result.hits %}

  • {% for snip in hit.3.snippets %} - {{snip}}
    + {{snip|safe}}
    {% endfor %}
  • {% endfor %} -- 2.20.1