From: Marcin Koziej Date: Wed, 11 Jan 2012 11:54:13 +0000 (+0100) Subject: search input field, X-Git-Url: https://git.mdrn.pl/wolnelektury.git/commitdiff_plain/3c12104d064098096eb5fe9c9ee122d0293e10f2?ds=sidebyside search input field, reindex command accepts slugs --- diff --git a/apps/catalogue/fields.py b/apps/catalogue/fields.py index 57ce58189..e19df9d37 100644 --- a/apps/catalogue/fields.py +++ b/apps/catalogue/fields.py @@ -104,16 +104,8 @@ class JQueryAutoCompleteSearchWidget(JQueryAutoCompleteWidget): super(JQueryAutoCompleteSearchWidget, self).__init__(*args, **kwargs) def render_js(self, field_id, options): - return u""" - $('#%s') - .autocomplete($.extend({ - minLength: 0, - select: autocomplete_result_handler, - focus: function (ui, item) { return false; } - }, %s)) - .data("autocomplete")._renderItem = autocomplete_format_item; - """ % (field_id, options) - + return u"" + class JQueryAutoCompleteField(forms.CharField): def __init__(self, source, options={}, *args, **kwargs): @@ -125,9 +117,8 @@ class JQueryAutoCompleteField(forms.CharField): class JQueryAutoCompleteSearchField(forms.CharField): - def __init__(self, source, options={}, *args, **kwargs): + def __init__(self, options={}, *args, **kwargs): if 'widget' not in kwargs: - options['source'] = source kwargs['widget'] = JQueryAutoCompleteSearchWidget(options) super(JQueryAutoCompleteSearchField, self).__init__(*args, **kwargs) diff --git a/apps/catalogue/forms.py b/apps/catalogue/forms.py index b7bf249f8..438df0729 100644 --- a/apps/catalogue/forms.py +++ b/apps/catalogue/forms.py @@ -31,20 +31,15 @@ class BookImportForm(forms.Form): class SearchForm(forms.Form): - q = JQueryAutoCompleteSearchField('/newsearch/hint/') # {'minChars': 2, 'selectFirst': True, 'cacheLength': 50, 'matchContains': "word"}) - tags = forms.CharField(widget=forms.HiddenInput, required=False) + q = JQueryAutoCompleteSearchField() # {'minChars': 2, 'selectFirst': True, 'cacheLength': 50, 'matchContains': "word"}) - 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) + def __init__(self, source, *args, **kwargs): + kwargs['auto_id'] = False super(SearchForm, self).__init__(*args, **kwargs) 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 + self.fields['q'].widget.attrs['autocomplete'] = _('off') + self.fields['q'].widget.attrs['data-source'] = _(source) + self.fields['q'].widget.attrs['id'] = _('search') class UserSetsForm(forms.Form): diff --git a/apps/lessons/urls.py b/apps/lessons/urls.py index 1d256374b..4b6a3e10b 100644 --- a/apps/lessons/urls.py +++ b/apps/lessons/urls.py @@ -9,9 +9,6 @@ from catalogue import forms urlpatterns = patterns('', url(r'^$', 'django.views.generic.simple.direct_to_template', { 'template': 'lessons/document_list.html', - 'extra_context': { - 'form': forms.SearchForm(), - }, }, name='lessons_document_list'), url(r'^(?P[a-zA-Z0-9_-]+)/$', 'lessons.views.document_detail', name='lessons_document_detail'), diff --git a/apps/search/context_processors.py b/apps/search/context_processors.py new file mode 100644 index 000000000..c54525aec --- /dev/null +++ b/apps/search/context_processors.py @@ -0,0 +1,7 @@ + +from catalogue.forms import SearchForm +from django.core.urlresolvers import reverse + + +def search_form(request): + return { 'search_form': SearchForm(reverse('search.views.hint'), request.GET) } diff --git a/apps/search/index.py b/apps/search/index.py index c2dfdee99..cc478ee53 100644 --- a/apps/search/index.py +++ b/apps/search/index.py @@ -610,20 +610,20 @@ class SearchResult(object): # remove duplicate sections sections = {} - + for s in sect: si = s[POSITION][POSITION_INDEX] # skip existing if si in sections: if sections[si]['score'] >= s[SCORE]: continue - + m = {'score': s[SCORE], - 'header_index': s[POSITION][POSITION_INDEX] + 'section_number': s[POSITION][POSITION_INDEX] + 1, } m.update(s[OTHER]) sections[si] = m - + hits = sections.values() for f in frags: @@ -1030,7 +1030,6 @@ class Search(IndexStore): # return None - def get_snippets(self, scoreDoc, query, field='content'): """ Returns a snippet for found scoreDoc. diff --git a/apps/search/management/commands/reindex.py b/apps/search/management/commands/reindex.py index 6d4b4fd56..bce47080d 100755 --- a/apps/search/management/commands/reindex.py +++ b/apps/search/management/commands/reindex.py @@ -9,9 +9,17 @@ class Command(BaseCommand): import search idx = search.ReusableIndex() idx.open() - for b in Book.objects.all(): + + if args: + books = [] + for a in args: + books += Book.objects.filter(slug=a).all() + else: + books = Book.objects.all() + + for b in books: print b.title idx.index_book(b, None) print 'Reindexing tags.' idx.index_tags() - idx.close() \ No newline at end of file + idx.close() diff --git a/apps/search/views.py b/apps/search/views.py index 403d1ae3f..166011cc3 100644 --- a/apps/search/views.py +++ b/apps/search/views.py @@ -59,7 +59,6 @@ def hint(request): # 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) @@ -78,18 +77,6 @@ def hint(request): 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) - - def main(request): results = {} JVM.attachCurrentThread() # where to put this? @@ -158,9 +145,3 @@ def main(request): {'tags': tag_list, 'prefix': query, 'results': results}, 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/settings.py b/wolnelektury/settings.py index 71d5474f9..afd42e96c 100644 --- a/wolnelektury/settings.py +++ b/wolnelektury/settings.py @@ -89,6 +89,7 @@ TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.media', 'django.core.context_processors.request', 'wolnelektury.context_processors.extra_settings', + 'search.context_processors.search_form', ) MIDDLEWARE_CLASSES = [ diff --git a/wolnelektury/templates/base.html b/wolnelektury/templates/base.html index 3ed1984a0..23cb46b56 100644 --- a/wolnelektury/templates/base.html +++ b/wolnelektury/templates/base.html @@ -75,7 +75,8 @@
- + {{search_form.q}} + diff --git a/wolnelektury/templates/catalogue/search_multiple_hits.html b/wolnelektury/templates/catalogue/search_multiple_hits.html index 0d3e70064..40485f578 100644 --- a/wolnelektury/templates/catalogue/search_multiple_hits.html +++ b/wolnelektury/templates/catalogue/search_multiple_hits.html @@ -31,6 +31,7 @@ {% else %} {# it's a section #} + {{hit.header_index}} {% if hit.snippets %} {% for snip in hit.snippets %} {{snip|safe}}