search input field,
authorMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Wed, 11 Jan 2012 11:54:13 +0000 (12:54 +0100)
committerMarcin Koziej <marcin.koziej@nowoczesnapolska.org.pl>
Wed, 11 Jan 2012 12:15:50 +0000 (13:15 +0100)
reindex command accepts slugs

apps/catalogue/fields.py
apps/catalogue/forms.py
apps/lessons/urls.py
apps/search/context_processors.py [new file with mode: 0644]
apps/search/index.py
apps/search/management/commands/reindex.py
apps/search/views.py
wolnelektury/settings.py
wolnelektury/templates/base.html
wolnelektury/templates/catalogue/search_multiple_hits.html

index 57ce581..e19df9d 100644 (file)
@@ -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)
index b7bf249..438df07 100644 (file)
@@ -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):
index 1d25637..4b6a3e1 100644 (file)
@@ -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<slug>[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 (file)
index 0000000..c54525a
--- /dev/null
@@ -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) }
index c2dfdee..cc478ee 100644 (file)
@@ -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.
index 6d4b4fd..bce4708 100755 (executable)
@@ -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()
index 403d1ae..166011c 100644 (file)
@@ -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))
index 71d5474..afd42e9 100644 (file)
@@ -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 = [
index 3ed1984..23cb46b 100644 (file)
@@ -75,7 +75,8 @@
             <form id="search-area" action="/fullsearch/">
                 
                 <span id="search-field" class="grid-line">
-                    <input id="search" title="np. Leśmian" name="q" autocomplete="off" data-source="/fullsearch/hint/">
+                 {{search_form.q}}
+<!--                    <input title="np. Leśmian" name="q" autocomplete="off" data-source="/fullsearch/hint/">-->
                 </span><span id="search-button">
                     <button type='submit'><span class="mono">{% trans "Search" %}</span></button>
                 </span>
index 0d3e700..40485f5 100644 (file)
@@ -31,6 +31,7 @@
            
            {% else %}
            {# it's a section #}
+           <a href="{% url book_text result.book.slug %}#f{{hit.section_number}}">{{hit.header_index}}</a>
             {% if hit.snippets %}
              {% for snip in hit.snippets %}
                {{snip|safe}}<br/>