changeset 250:bc16cd1f04e0

integrate the simple search into show view (makes it easier to show search query validation errors)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 02 Jun 2011 19:48:12 +0200
parents 20c18292bd02
children 4654343b5abd
files MoinMoin/apps/frontend/views.py MoinMoin/templates/layout.html
diffstat 2 files changed, 15 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/apps/frontend/views.py	Thu Jun 02 19:21:11 2011 +0200
+++ b/MoinMoin/apps/frontend/views.py	Thu Jun 02 19:48:12 2011 +0200
@@ -113,6 +113,9 @@
     too_short_query_msg = L_('Search query too short.')
 
     def validate(self, element, state):
+        if element['q'].value is None:
+            # no query, nothing to search for
+            return False
         if len(element['q'].value) < 2:
             return self.note_error(element, state, 'too_short_query_msg')
         return True
@@ -123,23 +126,21 @@
 
     validators = [ValidSearch()]
 
-@frontend.route('/+search', methods=['GET', 'POST'])
-def search():
-    form = SearchForm.from_flat(request.values)
-    if form.validate():
-        query = form['q'].value
-        return _search(query)
-    # XXX show this on the UI in some pretty way:
-    return "search form did not validate: %r" % form.errors
-
 
 def _search(query):
     return "searching not implemented yet, query: %r" % query
 
 
-@frontend.route('/<itemname:item_name>', defaults=dict(rev=-1))
-@frontend.route('/+show/<int:rev>/<itemname:item_name>')
+@frontend.route('/<itemname:item_name>', defaults=dict(rev=-1), methods=['GET', 'POST'])
+@frontend.route('/+show/<int:rev>/<itemname:item_name>', methods=['GET', 'POST'])
 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():
+        query = search_form['q'].value
+        return _search(query)
+    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)
@@ -169,7 +170,7 @@
                               data_rendered=Markup(item._render_data()),
                               show_revision=show_revision,
                               show_navigation=show_navigation,
-                              search_form=SearchForm.from_defaults(),
+                              search_form=search_form,
                              )
     return Response(content, status)
 
--- a/MoinMoin/templates/layout.html	Thu Jun 02 19:21:11 2011 +0200
+++ b/MoinMoin/templates/layout.html	Thu Jun 02 19:48:12 2011 +0200
@@ -19,10 +19,11 @@
 <div id="moin-header">
 {% block header %}
     {% if search_form %} 
-    {{ gen.form.open(search_form, id='moin-searchform', method='get', action=url_for('frontend.search')) }}
+    {{ gen.form.open(search_form, id='moin-searchform', method='get', action=url_for('frontend.show_item', item_name=item_name)) }}
         <div>
             {{ gen.input(search_form['q'], type='search', id='moin-search-query', size='30') }}
             {{ gen.input(search_form['submit'], type='submit') }}
+            {{ forms.render_errors(search_form) }}
         </div>
     {{ gen.form.close() }}
     {% endif %}