Commits

Cédric Bonhomme  committed 33999a6

Article searching with Whoosh OK.

  • Participants
  • Parent commits f516418

Comments (0)

Files changed (4)

File pyaggr3g470r/search.py

 from whoosh.query import *
 from whoosh.qparser import QueryParser
 from whoosh.writing import AsyncWriter
+from collections import defaultdict
 
 import conf
 import utils
     Search for `term` in the index.
     Returns a list of articles.
     """
+    result_dict = defaultdict(list)
     try:
         ix = open_dir(indexdir)
     except (EmptyIndexError, OSError) as e:
     with ix.searcher() as searcher:
         query = QueryParser("content", ix.schema).parse(term)
         results = searcher.search(query, limit=None)
-        return [(article["feed_id"], article["article_id"]) for article in results]
+        for article in results:
+            result_dict[article["feed_id"]].append(article["article_id"])
+        return result_dict
 
 def nb_documents():
     """

File pyaggr3g470r/templates/layout.html

                 <li><a href="/about/">About</a></li>
                 <li><a href="{{ url_for('logout') }}">Logout</a></li>
                 <li>
-                    <form class="navbar-form" method=get action="/" role="search">
+                    <form class="navbar-form" method=get action="/search/" role="search">
                         <div class="input-group">
-                            <input type="text" class="form-control" size="8" placeholder="Search">
+                            <input type="text" class="form-control" name="query" size="8" placeholder="Search">
                             <span class="input-group-btn">
                                 <button class="btn btn-default" type="submit"><span class="glyphicon glyphicon-search"></span></button>
                             </span>

File pyaggr3g470r/templates/search.html

+ {% extends "layout.html" %}
+{% block content %}
+<div class="container">
+    {% if feeds|count == 0  %}
+        <h1>No results.</h1>
+    {% else %}
+        {% for feed in feeds|sort(attribute="title") %}
+            <div class="row">
+                <div class="col-md-6 col-md-offset-3">
+                <h1>{{ feed.title }}</h1>
+                <a href="/articles/{{ feed.oid }}"><i class="glyphicon glyphicon-th-list" title="All articles"></i></a>
+                <a href="/feed/{{ feed.oid }}"><i class="glyphicon glyphicon-eye-open" title="Details"></i></a>
+                <a href="/edit_feed/{{ feed.oid }}"><i class="glyphicon glyphicon-edit" title="Edit this feed"></i></a>
+                </div>
+            </div>
+            {% for number in range(0, feed.articles|length-(feed.articles|length % 3), 3) %}
+                <div class="row">
+                    {% for n in range(number, number+3) %}
+                    <div class="col-xs-6 col-sm-4 col-md-4">
+                        {% if feed.articles[n].readed %}<h3>{% else %}<h1>{% endif %}
+                            <a href="/article/{{ feed.articles[n].id }}">{{ feed.articles[n].title }}</a><h2>
+                        {% if feed.articles[n].readed %}</h3>{% else %}</h1>{% endif %}
+                        <h6>{{ feed.articles[n].date }}</h6>
+                    </div>
+                    {% endfor %}
+                </div>
+            {% endfor %}
+            {% if feed.articles|length % 3 != 0 %}
+                <div class="row">
+                     {% for n in range(feed.articles|length-(feed.articles|length % 3), feed.articles|length) %}
+                        <div class="col-xs-6 col-sm-4 col-md-4">
+                            {% if feed.articles[n].readed %}<h3>{% else %}<h1>{% endif %}
+                                <a href="/article/{{ feed.articles[n].id }}">{{ feed.articles[n].title }}</a><h2>
+                            {% if feed.articles[n].readed %}</h3>{% else %}</h1>{% endif %}
+                            <h6>{{ feed.articles[n].date }}</h6>
+                        </div>
+                    {% endfor %}
+                </div>
+            {% endif %}
+        {% endfor %}
+    {% endif %}
+</div><!-- /.container -->
+{% endblock %}

File pyaggr3g470r/views.py

 
 import feedgetter
 import models
+import search as fastsearch
 
 
 login_manager = LoginManager()
             result.append(feed)
     return render_template('unread.html', feeds=result)
 
+@app.route('/search/', methods=['GET'])
+@login_required
+def search():
+    user = models.User.objects(email=g.user.email).first()
+    result = []
+    query = request.args.get('query', None)
+    if query != None:
+        results = fastsearch.search(query)
+        for feed_id in results:
+            for feed in user.feeds:
+                if str(feed.oid) == feed_id:
+                    feed.articles = []
+                    for article_id in results[feed_id]:
+                        current_article = models.Article.objects(id=article_id).first()
+                        feed.articles.append(current_article)
+                    result.append(feed)
+    return render_template('search.html', feeds=result)
+
 @app.route('/management/', methods=['GET'])
 @login_required
 def management():