changeset 1742:8f18745faf4a

In +index view, use a multivalue instead of multiple booleans for contentype filter.
author Cheer Xiao <xiaqqaix@gmail.com>
date Sat, 25 Aug 2012 22:10:16 +0800
parents bf840fab4737
children fbe32ae2dd3d
files MoinMoin/apps/frontend/views.py MoinMoin/constants/forms.py MoinMoin/forms.py MoinMoin/templates/forms.html MoinMoin/templates/index.html
diffstat 5 files changed, 45 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/apps/frontend/views.py	Sat Aug 25 22:07:39 2012 +0800
+++ b/MoinMoin/apps/frontend/views.py	Sat Aug 25 22:10:16 2012 +0800
@@ -31,7 +31,7 @@
 from flaskext.babel import format_date
 from flaskext.themes import get_themes_list
 
-from flatland import Form
+from flatland import Form, Enum
 from flatland.validation import Validator
 
 from jinja2 import Markup
@@ -47,7 +47,7 @@
 from MoinMoin.i18n import _, L_, N_
 from MoinMoin.themes import render_template, get_editor_info, contenttype_to_class
 from MoinMoin.apps.frontend import frontend
-from MoinMoin.forms import OptionalText, RequiredText, URL, YourOpenID, YourEmail, RequiredPassword, Checkbox, InlineCheckbox, Select, Tags, Natural, Submit, Hidden
+from MoinMoin.forms import OptionalText, RequiredText, URL, YourOpenID, YourEmail, RequiredPassword, Checkbox, InlineCheckbox, Select, Tags, Natural, Submit, Hidden, MultiSelect
 from MoinMoin.items import BaseChangeForm, Item, NonExistent
 from MoinMoin import config, user, util
 from MoinMoin.config import CONTENTTYPE_GROUPS
@@ -726,22 +726,17 @@
         abort(403)
 
 
-class ContenttypeFilterForm(Form):
-    markup_text_items = InlineCheckbox.using(label=L_('markup text'))
-    other_text_items = InlineCheckbox.using(label=L_('other text'))
-    image_items = InlineCheckbox.using(label=L_('image'))
-    audio_items = InlineCheckbox.using(label=L_('audio'))
-    video_items = InlineCheckbox.using(label=L_('video'))
-    other_items = InlineCheckbox.using(label=L_('other'))
-    unknown_items = InlineCheckbox.using(label=L_('unknown'))
+contenttype_groups = []
+contenttype_group_descriptions = {}
+for gname, contenttypes in CONTENTTYPE_GROUPS:
+    contenttype_groups.append(gname)
+    contenttype_group_descriptions[gname] = ', '.join([ctlabel for ctname, ctlabel in contenttypes])
+contenttype_groups.append('unknown items')
 
-for gname, contenttypes in CONTENTTYPE_GROUPS:
-    filter_ = ContenttypeFilterForm.field_schema_mapping.get(gname.replace(' ', '_'))
-    if filter_:
-        filter_.properties['helper'] = ", ".join([ctlabel for ctname, ctlabel in contenttypes])
+ContenttypeGroup = MultiSelect.of(Enum.using(valid_values=contenttype_groups).with_properties(descriptions=contenttype_group_descriptions)).using(optional=True)
 
 class IndexForm(Form):
-    contenttype = ContenttypeFilterForm
+    contenttype = ContenttypeGroup
     submit = Submit.using(default=L_('Filter'))
 
 @frontend.route('/+index/', defaults=dict(item_name=''), methods=['GET', 'POST'])
@@ -752,11 +747,17 @@
     except AccessDenied:
         abort(403)
 
-    # XXX retain default values when some arg missing from request.args?
-    form = IndexForm.from_flat(request.args)
+    # request.args is a MultiDict instance, which degenerates into a normal
+    # single-valued dict on most occasions (making the first value the *only*
+    # value for a specific key) unless explicitly told to expose multiple
+    # values, eg. calling items with multi=True. See Werkzeug documentation for
+    # more.
+    form = IndexForm.from_flat(request.args.items(multi=True))
+    form['submit'].set_default() # XXX from_flat() kills all values
+    if not form['contenttype']:
+        form['contenttype'].set(contenttype_groups)
 
-    selected_groups = [k.replace("_", " ") for k, v in form['contenttype'].iteritems() if v]
-
+    selected_groups = form['contenttype'].value
     startswith = request.values.get("startswith")
 
     initials = item.name_initial(item.get_subitem_revs())
--- a/MoinMoin/constants/forms.py	Sat Aug 25 22:07:39 2012 +0800
+++ b/MoinMoin/constants/forms.py	Sat Aug 25 22:10:16 2012 +0800
@@ -24,6 +24,7 @@
 WIDGET_HIDDEN = u'hidden'
 
 WIDGET_SELECT = u'select'
+WIDGET_MULTI_SELECT = u'multi_select'
 
 WIDGET_READONLY_STRING_LIST = u'readonly_string_list'
 WIDGET_READONLY_ITEM_LINK_LIST = u'readonly_item_link_list'
--- a/MoinMoin/forms.py	Sat Aug 25 22:07:39 2012 +0800
+++ b/MoinMoin/forms.py	Sat Aug 25 22:10:16 2012 +0800
@@ -12,7 +12,7 @@
 import re, datetime
 import json
 
-from flatland import Element, Form, String, Integer, Boolean, Enum, Dict, JoinedString, List, DateTime as _DateTime
+from flatland import Element, Form, String, Integer, Boolean, Enum, Dict, JoinedString, List, Array, DateTime as _DateTime
 from flatland.util import class_cloner, Unspecified
 from flatland.validation import Validator, Present, IsEmail, ValueBetween, URLValidator, Converted, ValueAtLeast
 from flatland.exc import AdaptationError
@@ -200,3 +200,6 @@
     def set(self, query, **query_args):
         revs = flaskg.storage.search(query, **query_args)
         super(BackReference, self).set([rev.meta[NAME] for rev in revs])
+
+
+MultiSelect = Array.with_properties(widget=WIDGET_MULTI_SELECT)
--- a/MoinMoin/templates/forms.html	Sat Aug 25 22:07:39 2012 +0800
+++ b/MoinMoin/templates/forms.html	Sat Aug 25 22:10:16 2012 +0800
@@ -55,6 +55,7 @@
       'submit': raw_input,
       'hidden': raw_input,
       'select': select,
+      'multi_select': multi_select,
       'readonly_string_list': readonly_string_list,
       'readonly_item_link_list': readonly_item_link_list,
   }[field.properties.widget] or stub -%}
@@ -164,6 +165,24 @@
   </dd>
 {% endmacro %}
 
+{% macro multi_select(field) %}
+  {% set valid_values = field.member_schema.valid_values %}
+  {% set labels = field.member_schema.properties.get('labels', {}) %}
+  {% set descriptions = field.member_schema.properties.get('helpers', {}) %}
+  {% for value in valid_values %}
+    <li>
+      {{ raw_input(field, 'checkbox', value=value) }}
+      {{ _valued_label(field, value, labels.get(value, value), class='moin-inline-label') }}
+      {% if descriptions[value] is defined %}
+        <span class="helper-text">
+          {{ descriptions[value] }}
+        </span>
+      {% endif %}
+    </li>
+  {% endfor %}
+  {{ render_errors(field) }}
+{% endmacro %}
+
 {% macro readonly_string_list(field) %}
   <dt>
     {{ gen.label(field) }}
--- a/MoinMoin/templates/index.html	Sat Aug 25 22:07:39 2012 +0800
+++ b/MoinMoin/templates/index.html	Sat Aug 25 22:10:16 2012 +0800
@@ -56,17 +56,7 @@
                         <a href="#" class="filter-toggle">&raquo; {{ _("Toggle") }}</a>
                         <a href="#" class="filter-more">&raquo; {{ _("More") }}</a>
                     </li>
-                    {% for e in [
-                        'markup_text_items',
-                        'other_text_items',
-                        'image_items',
-                        'audio_items',
-                        'video_items',
-                        'other_items',
-                        'unknown_items',
-                        ] %}
-                        <li>{{ forms.render(form['contenttype'][e]) }}</li>
-                    {% endfor %}
+                    {{ forms.render(form['contenttype']) }}
                 </ul>
                 {{ forms.render(form['submit']) }}
                 {{ gen.form.close() }}