changeset 779:815c46100e2c pytest2

whooshify get_templates, adapt MimeType analyzer so it yields also a full and normalized contenttype
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 27 Aug 2011 18:21:04 +0200
parents 338852297cfd
children f5e0f85e66ce
files MoinMoin/items/__init__.py MoinMoin/search/_tests/test_analyzers.py MoinMoin/search/analyzers.py
diffstat 3 files changed, 31 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/items/__init__.py	Sat Aug 27 16:11:08 2011 +0200
+++ b/MoinMoin/items/__init__.py	Sat Aug 27 18:21:04 2011 +0200
@@ -25,6 +25,9 @@
 
 from flatland import Form, String, Integer, Boolean, Enum
 from flatland.validation import Validator, Present, IsEmail, ValueBetween, URLValidator, Converted
+
+from whoosh.query import Term, And
+
 from MoinMoin.util.forms import FileStorage
 
 from MoinMoin.security.textcha import TextCha, TextChaizedForm, TextChaValid
@@ -705,13 +708,12 @@
 
     def get_templates(self, contenttype=None):
         """ create a list of templates (for some specific contenttype) """
-        from MoinMoin.storage.terms import AND, LastRevisionMetaDataMatch
-        term = LastRevisionMetaDataMatch(TAGS, ['template']) # XXX there might be other tags
-        if contenttype:
-            term = AND(term, LastRevisionMetaDataMatch(CONTENTTYPE, contenttype))
-        item_iterator = self.search_items(term)
-        items = [item.name for item in item_iterator]
-        return sorted(items)
+        terms = [Term("wikiname", app.cfg.interwikiname), Term(TAGS, u'template')]
+        if contenttype is not None:
+            terms.append(Term(CONTENTTYPE, contenttype))
+        query = And(terms)
+        results = flaskg.storage.search(query, all_revs=False, sortedby="name_exact", limit=None)
+        return [result[NAME] for result in results]
 
     def do_modify(self, contenttype, template_name):
         # XXX think about and add item template support
--- a/MoinMoin/search/_tests/test_analyzers.py	Sat Aug 27 16:11:08 2011 +0200
+++ b/MoinMoin/search/_tests/test_analyzers.py	Sat Aug 27 18:21:04 2011 +0200
@@ -119,12 +119,20 @@
 
     test_cases_query = [
                   # (query, tokens)
-                  (u'text/plain', [u'text', u'plain']),
-                  (u'text/plain;charset=utf-8', [u'text', u'plain', u'charset=utf-8']),
+                  (u'text/plain',
+                   [u'text/plain', u'text', u'plain']),
+                  (u'text/plain;charset=utf-8',
+                   [u'text/plain;charset=utf-8', u'text', u'plain', u'charset=utf-8']),
                   (u'text/html;value1=foo;value2=bar',
-                   [u'text', u'html', u'value1=foo', u'value2=bar'],
+                   [u'text/html;value1=foo;value2=bar', u'text', u'html', u'value1=foo', u'value2=bar'],
                   ),
-                  (u'text/html;value1=foo;value1=bar', [u'text', u'html', u'value1=bar'])
+                  # we normalize, sort the params:
+                  (u'text/html;value2=bar;value1=foo',
+                   [u'text/html;value1=foo;value2=bar', u'text', u'html', u'value1=foo', u'value2=bar'],
+                  ),
+                  # later values for same key overwrite earlier ones:
+                  (u'text/html;value1=foo;value1=bar',
+                   [u'text/html;value1=bar', u'text', u'html', u'value1=bar'])
                  ]
 
     def make_tokenizer(self):
--- a/MoinMoin/search/analyzers.py	Sat Aug 27 16:11:08 2011 +0200
+++ b/MoinMoin/search/analyzers.py	Sat Aug 27 18:21:04 2011 +0200
@@ -37,6 +37,16 @@
         pos = start_pos
         tk = Token()
         tp = Type(value)
+        # we need to yield the complete contenttype in one piece,
+        # so we can find it with Term(CONTENTTYPE, contenttype):
+        if tp.type is not None and tp.subtype is not None:
+            # note: we do not use "value" directly, so Type.__unicode__ can normalize it:
+            tk.text = unicode(tp)
+            if positions:
+                tk.pos = pos
+                pos += 1
+            yield tk
+        # now yield the pieces:
         tk.text = tp.type
         if positions:
             tk.pos = pos