changeset 802:919ae0e7ef1c pytest2

add a separate +search view, remove incomplete search paging support, cleanup .../+search -> get a search form to fill out .../+search?q=foo -> search for "foo" use "search" view url as action for search forms rename "search_in_all" to "history" (clearer, shorter for URL in GET requests) remove most search related code from "show" view (except feeding the empty search form to the template) fix css so the small theme searchform is right-aligned again
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 29 Aug 2011 19:33:49 +0200
parents 72adfe303975
children e592d2c9313d
files MoinMoin/apps/frontend/views.py MoinMoin/templates/layout.html MoinMoin/templates/search_results.html MoinMoin/themes/modernized/static/css/common.css
diffstat 4 files changed, 36 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/apps/frontend/views.py	Mon Aug 29 18:11:42 2011 +0200
+++ b/MoinMoin/apps/frontend/views.py	Mon Aug 29 19:33:49 2011 +0200
@@ -107,6 +107,7 @@
 Disallow: /+diffsince/
 Disallow: /+diff/
 Disallow: /+diffraw/
+Disallow: /+search
 Disallow: /+dispatch/
 Disallow: /+admin/
 Allow: /
@@ -134,40 +135,43 @@
         return True
 
 class SearchForm(Form):
-    q = String.using(optional=False).with_properties(autofocus=True, placeholder=L_("Search Query"))
+    q = String.using(optional=False, default=u'').with_properties(autofocus=True, placeholder=L_("Search Query"))
+    history = Boolean.using(label=L_('search also in non-current revisions'), optional=True)
     submit = String.using(default=L_('Search'), optional=True)
-    pagelen = String.using(optional=False)
-    search_in_all = Boolean.using(label=L_('search also in non-current revisions'), optional=True)
 
     validators = [ValidSearch()]
 
 
-def _search(search_form, item_name):
+@frontend.route('/+search', methods=['GET', 'POST'])
+def search():
+    search_form = SearchForm.from_flat(request.values)
+    valid = search_form.validate()
+    search_form['submit'].set_default() # XXX from_flat() kills all values
     query = search_form['q'].value
-    pagenum = 1  # We start from first page
-    pagelen = int(search_form['pagelen'].value)
-    all_revs = bool(request.values.get('search_in_all'))
-    qp = flaskg.storage.query_parser(["name_exact", "name", "content"], all_revs=all_revs)
-    q = qp.parse(query)
-    with flaskg.storage.searcher(all_revs) as searcher:
-        results = searcher.search_page(q, pagenum, pagelen)
+    if valid:
+        history = bool(request.values.get('history'))
+        qp = flaskg.storage.query_parser(["name_exact", "name", "content"], all_revs=history)
+        q = qp.parse(query)
+        with flaskg.storage.searcher(all_revs=history) as searcher:
+            results = searcher.search(q, limit=100)
+            return render_template('search_results.html',
+                                   results=results,
+                                   query=query,
+                                   medium_search_form=search_form,
+                                   item_name='+search', # XXX
+                                  )
+    else:
         return render_template('search_results.html',
-                               results=results,
                                query=query,
                                medium_search_form=search_form,
-                               item_name=item_name,
+                               item_name='+search', # XXX
                               )
 
 
-@frontend.route('/<itemname:item_name>', defaults=dict(rev=-1), methods=['GET', 'POST'])
-@frontend.route('/+show/<int:rev>/<itemname:item_name>', methods=['GET', 'POST'])
+
+@frontend.route('/<itemname:item_name>', defaults=dict(rev=-1), methods=['GET'])
+@frontend.route('/+show/<int:rev>/<itemname:item_name>', methods=['GET'])
 def show_item(item_name, rev):
-    # first check whether we have a valid search query:
-    search_form = SearchForm.from_flat(request.values)
-    if search_form.validate():
-        return _search(search_form, item_name)
-    search_form['submit'].set_default() # XXX from_flat() kills all values
-
     flaskg.user.addTrail(item_name)
     item_displayed.send(app._get_current_object(),
                         item_name=item_name)
@@ -197,7 +201,7 @@
                               data_rendered=Markup(item._render_data()),
                               show_revision=show_revision,
                               show_navigation=show_navigation,
-                              search_form=search_form,
+                              search_form=SearchForm.from_defaults(),
                              )
     return Response(content, status)
 
--- a/MoinMoin/templates/layout.html	Mon Aug 29 18:11:42 2011 +0200
+++ b/MoinMoin/templates/layout.html	Mon Aug 29 19:33:49 2011 +0200
@@ -19,11 +19,10 @@
 <div id="moin-header">
 {% block header %}
     {% if search_form %} 
-    {{ gen.form.open(search_form, id='moin-searchform', method='get', action=url_for('frontend.show_item', item_name=item_name)) }}
+    {{ gen.form.open(search_form, id='moin-searchform', method='get', action=url_for('frontend.search')) }}
         <div>
             {{ gen.input(search_form['q'], type='search', id='moin-search-query', size='30') }}
             {{ gen.input(search_form['submit'], type='submit') }}
-            {{ gen.input(search_form['pagelen'], type='hidden', value='25') }}
             {{ forms.render_errors(search_form) }}
         </div>
     {{ gen.form.close() }}
--- a/MoinMoin/templates/search_results.html	Mon Aug 29 18:11:42 2011 +0200
+++ b/MoinMoin/templates/search_results.html	Mon Aug 29 19:33:49 2011 +0200
@@ -1,29 +1,27 @@
 {% extends theme("layout.html") %}
 {% import "utils.html" as utils %}
 {% block content %}
+    {% if results is defined %}
     <p class="searchstats">
         {% if results %}
-        {{ _("Result: Page %(start_page)d of %(end_page)d.
-              Showing results %(start_result)d - %(end_result)d of %(result_len)d (%(runtime).3f secs).",
-              start_page=results.pagenum, end_page=results.pagecount,
-              start_result=results.offset + 1, end_result=results.offset + results.pagelen,
-              result_len=results|length, runtime=results.results.runtime
+        {{ _("%(result_len)d results found (%(runtime).3f secs).",
+              result_len=results|length, runtime=results.runtime
             )
         }}
         {% else %}
-        {{ _("No results found (%(runtime).3f secs).", runtime=results.results.runtime) }}
+        {{ _("No results found (%(runtime).3f secs).", runtime=results.runtime) }}
         {% endif %}
     </p>
-    {{ gen.form.open(medium_search_form, id='moin-long-searchform', method='get', action=url_for('frontend.show_item', item_name=item_name)) }}
+    {% endif %}
+    {{ gen.form.open(medium_search_form, id='moin-long-searchform', method='get', action=url_for('frontend.search')) }}
         <div>
             {{ gen.input(medium_search_form['q'], type='search', id='moin-search-query') }}
             {{ gen.input(medium_search_form['submit'], type='submit') }}
-            {{ gen.input(medium_search_form['pagelen'], type='hidden', value='25') }}
-            {{ forms.render_field_without_markup(gen, medium_search_form['search_in_all'], 'checkbox') }}
+            {{ forms.render_field_without_markup(gen, medium_search_form['history'], 'checkbox') }}
             {{ forms.render_errors(medium_search_form) }}
         </div>
     {{ gen.form.close() }}
-    {% if results %}
+    {% if results is defined %}
     <div class="searchresults">
         <table>
             {% for result in results %}
--- a/MoinMoin/themes/modernized/static/css/common.css	Mon Aug 29 18:11:42 2011 +0200
+++ b/MoinMoin/themes/modernized/static/css/common.css	Mon Aug 29 19:33:49 2011 +0200
@@ -571,7 +571,7 @@
             line-height: 1.12em; }
 
 /* moin-header searchform */
-#moin-searchform { margin: 8px .5em; padding: 0; font-size: 0.82em; float: right;  text-align: left; }
+#moin-searchform { margin: 8px .5em; padding: 0; font-size: 0.82em; float: right;  text-align: right; }
 #moin-searchform input { font-size: 100%; vertical-align: middle;
             background-color: #F3F7FD; /* same as body bg col */
             border: 1px solid #A4B9DF; }