Commits

Ashutosh Singla committed d993f93

Lookup view modified to support multiple names

Comments (0)

Files changed (2)

MoinMoin/apps/frontend/views.py

 from MoinMoin.util import crypto
 from MoinMoin.util.interwiki import url_for_item
 from MoinMoin.search import SearchForm
+from MoinMoin.search.analyzers import item_name_analyzer
 from MoinMoin.security.textcha import TextCha, TextChaizedForm
 from MoinMoin.signalling import item_displayed, item_modified
 from MoinMoin.storage.middleware.protecting import AccessDenied
     submit_label = L_('Lookup')
 
 
+def analyze(analyzer, text):
+    return [token.text for token in analyzer(text, mode='index')]
+
+
 @frontend.route('/+lookup', methods=['GET', 'POST'])
 def lookup():
     """
                     term = Term(key, value)
                 terms.append(term)
         if terms:
+            LookupEntry = namedtuple('LookupEntry', 'name revid wikiname')
+            name = lookup_form[NAME].value
+            name_exact = lookup_form[NAME_EXACT].value or u''
             terms.append(Term(WIKINAME, app.cfg.interwikiname))
             q = And(terms)
             with flaskg.storage.indexer.ix[idx_name].searcher() as searcher:
                 flaskg.clock.start('lookup')
                 results = searcher.search(q, limit=100)
                 flaskg.clock.stop('lookup')
-                num_results = results.scored_length()
-                if num_results == 1:
-                    result = results[0]
-                    rev = result[REVID] if history else CURRENT
-                    url = url_for('.show_item', item_name=result[NAME], rev=rev)
+                lookup_results = []
+                for result in results:
+                    analyzer = item_name_analyzer()
+                    lookup_results += [LookupEntry(n, result[REVID], result[WIKINAME])
+                                       for n in result[NAME]
+                                       if not name or name.lower() in analyze(analyzer, n)
+                                       if n.startswith(name_exact)]
+
+                if len(lookup_results) == 1:
+                    result = lookup_results[0]
+                    rev = result.revid if history else CURRENT
+                    url = url_for('.show_item', item_name=result.name, rev=rev)
                     return redirect(url)
                 else:
                     flaskg.clock.start('lookup render')
                     html = render_template('lookup.html',
                                            title_name=title_name,
                                            lookup_form=lookup_form,
-                                           results=results,
+                                           results=lookup_results,
                     )
                     flaskg.clock.stop('lookup render')
-                    if not num_results:
+                    if not lookup_results:
                         status = 404
                     return Response(html, status)
     html = render_template('lookup.html',

MoinMoin/templates/lookup.html

             {% for result in results %}
                 {% if result['wikiname'] == cfg.interwikiname %}
                     <tr>
-                        <td class="moin-wordbreak">{{ result.pos + 1 }}
-                        <a href="{{ url_for_item(item_name=result['name'], wiki_name='Self', rev=result['revid']) }}"><b>{{ result['name'] }}</b></a>
+                        <td class="moin-wordbreak">{{ loop.index }}
+                        <a href="{{ url_for_item(item_name=result.name, wiki_name='Self', rev=result.revid) }}"><b>{{ result.name }}</b></a>
                         </td>
                     </tr>
                 {% else %}
                     <tr>
-                        <td class="moin-wordbreak">{{ result.pos + 1 }}
-                        <a class="moin-interwiki" href="{{ url_for_item(item_name=result['name'], wiki_name=result['wikiname'], rev=result['revid']) }}"><b>{{ "%s:%s" % (result['wikiname'], result['name']) }}</b></a>
+                        <td class="moin-wordbreak">{{ loop.index }}
+                        <a class="moin-interwiki" href="{{ url_for_item(item_name=result.name, wiki_name=result.wikiname, rev=result.revid) }}"><b>{{ "%s:%s" % (result.wikiname, result.name) }}</b></a>
                         </td>
                     </tr>
                 {% endif %}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.