changeset 3113:3489709aa99f

AdvancedSearch: make multipe categories/languages/mimetype selections possible, thanks to James Sun (crptone@gmail.com, ported from 1.6)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 25 Feb 2008 21:31:05 +0100
parents dc182f4936b1
children 338ad6aee6da
files MoinMoin/action/fullsearch.py MoinMoin/macro/AdvancedSearch.py MoinMoin/search/queryparser.py MoinMoin/util/web.py
diffstat 4 files changed, 54 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/fullsearch.py	Mon Feb 25 21:07:30 2008 +0100
+++ b/MoinMoin/action/fullsearch.py	Mon Feb 25 21:31:05 2008 +0100
@@ -89,10 +89,10 @@
         or_terms = request.form.get('or_terms', [''])[0].strip()
         not_terms = request.form.get('not_terms', [''])[0].strip()
         #xor_terms = request.form.get('xor_terms', [''])[0].strip()
-        categories = request.form.get('categories', [''])[0].strip()
+        categories = request.form.get('categories', [''])
         timeframe = request.form.get('time', [''])[0].strip()
-        language = request.form.get('language', [''])[0]
-        mimetype = request.form.get('mimetype', [0])[0]
+        language = request.form.get('language', [''])
+        mimetype = request.form.get('mimetype', [0])
         excludeunderlay = request.form.get('excludeunderlay', [0])[0]
         nosystemitems = request.form.get('nosystemitems', [0])[0]
         historysearch = request.form.get('historysearch', [0])[0]
@@ -141,17 +141,16 @@
 
         word_re = re.compile(r'(\"[\w\s]+"|\w+)')
         needle = ''
-        if language:
-            needle += 'language:%s ' % language
-        if mimetype:
-            needle += 'mimetype:%s ' % mimetype
+        if categories[0]:
+            needle += 'category:%s ' % ','.join(categories)
+        if language[0]:
+            needle += 'language:%s ' % ','.join(language)
+        if mimetype[0]:
+            needle += 'mimetype:%s ' % ','.join(mimetype)
         if excludeunderlay:
             needle += '-domain:underlay '
         if nosystemitems:
             needle += '-domain:system '
-        if categories:
-            needle += '(%s) ' % ' or '.join(['category:%s' % cat
-                for cat in word_re.findall(categories)])
         if and_terms:
             needle += '(%s) ' % and_terms
         if not_terms:
--- a/MoinMoin/macro/AdvancedSearch.py	Mon Feb 25 21:07:30 2008 +0100
+++ b/MoinMoin/macro/AdvancedSearch.py	Mon Feb 25 21:31:05 2008 +0100
@@ -17,6 +17,26 @@
 
 Dependencies = ['pages']
 
+def getMimetypes():
+    # The types will be listed here, instead of parsing mimetypes.types_map
+    types = [
+        'application',
+        'audio',
+        'image',
+        'message',
+        'text',
+        'video',
+        ]
+    return types
+
+
+def getCategories(request):
+    # This will return all pages with "Category" in the title
+    cat_filter = request.cfg.cache.page_category_regex.search
+    pages = request.rootpage.getPageList(filter=cat_filter)
+    pages.sort()
+    return pages
+
 
 def form_get(request, name, default=''):
     """ Fetches a form field
@@ -42,7 +62,7 @@
 
     search_boxes = ''.join([
         f.table_row(1),
-        f.table_cell(1, attrs={'rowspan': '6', 'class': 'searchfor'}),
+        f.table_cell(1, attrs={'rowspan': '5', 'class': 'searchfor'}),
         f.text(_('Search for items')),
         f.table_cell(0),
         ''.join([''.join([
@@ -68,31 +88,36 @@
             #    '<input type="text" name="xor_terms" size="30" value="%s">'
             #    % form_get(request, 'xor_terms')),
             # TODO: dropdown-box?
-            (_('belonging to one of the following categories'),
-                '<input type="text" name="categories" size="30" value="%s">'
-                % form_get(request, 'categories')),
             (_('last modified since (e.g. last 2 weeks)'),
                 '<input type="text" name="mtime" size="30" value="%s">'
                 % form_get(request, 'mtime')),
         )])
     ])
 
+    # category selection
+    categories = form_get(request, 'categories')
+    c_select = makeSelection('categories',
+            [('', _('any category', formatted=False))] +
+            [(cat, '%s' % cat) for cat in getCategories(request)],
+            categories, 3, True)
+
     # language selection
     searchedlang = form_get(request, 'language')
     langs = dict([(lang, lmeta['x-language-in-english'])
         for lang, lmeta in languages.items()])
     userlang = macro.request.lang
     lang_select = makeSelection('language',
-            [('', _('any language')), (userlang, langs[userlang])] +
-                sorted(langs.items(), key=lambda i: i[1]),
-            searchedlang)
+            [('', _('any language', formatted=False)),
+            (userlang, langs[userlang])] + sorted(langs.items(), key=lambda i: i[1]),
+            searchedlang, 3, True)
 
     # mimetype selection
     mimetype = form_get(request, 'mimetype')
     mt_select = makeSelection('mimetype',
-            [('', _('any mimetype'))] + [(m[1], '*%s - %s' % m) for m in sorted(mimetypes.types_map.items())],
-            mimetype)
-
+            [('', _('any mimetype', formatted=False))] +
+            [(type, 'all %s files' % type) for type in getMimetypes()] +
+            [(m[1], '*%s - %s' % m) for m in sorted(mimetypes.types_map.items())],
+            mimetype, 3, True)
 
     # misc search options (checkboxes)
     search_options = ''.join([
@@ -107,6 +132,7 @@
             f.table_cell(0),
             f.table_row(0),
             ]) for txt in (
+                (_('Categories'), unicode(c_select), ''),
                 (_('Language'), unicode(lang_select), ''),
                 (_('File Type'), unicode(mt_select), ''),
                 ('', html.INPUT(type='checkbox', name='titlesearch',
--- a/MoinMoin/search/queryparser.py	Mon Feb 25 21:07:30 2008 +0100
+++ b/MoinMoin/search/queryparser.py	Mon Feb 25 21:31:05 2008 +0100
@@ -669,8 +669,13 @@
         match = False
         body = page.getPageHeader()
 
-        if re.findall('#language %s' % self.pattern, body):
-            match = True
+        comma = re.compile(',')
+        iterator = comma.finditer(self.pattern)
+        temp = 0
+        for m_obj in iterator:
+            if re.findall('#language %s' % self.pattern[temp:m_obj.end()-2], body):
+                match = True
+            temp = m_obj.end()
 
         # Decide what to do with the results.
         if self.negated:
--- a/MoinMoin/util/web.py	Mon Feb 25 21:07:30 2008 +0100
+++ b/MoinMoin/util/web.py	Mon Feb 25 21:31:05 2008 +0100
@@ -25,7 +25,7 @@
         return result
 
 
-def makeSelection(name, values, selectedval=None, size=1):
+def makeSelection(name, values, selectedval=None, size=1, multiple=False):
     """ Make a HTML <select> element named `name` from a value list.
         The list can either be a list of strings, or a list of
         (value, label) tuples.
@@ -33,7 +33,7 @@
         `selectedval` is the value that should be pre-selected.
     """
     from MoinMoin.widget import html
-    result = html.SELECT(name=name, size="%d" % int(size))
+    result = html.SELECT(name=name, size="%d" % int(size), multiple=multiple)
     for val in values:
         if not isinstance(val, type(())):
             val = (val, val)