aboutsummaryrefslogtreecommitdiff
path: root/pyaggr3g470r
diff options
context:
space:
mode:
authorCédric Bonhomme <kimble.mandel+bitbucket@gmail.com>2015-04-17 00:34:45 +0200
committerCédric Bonhomme <kimble.mandel+bitbucket@gmail.com>2015-04-17 00:34:45 +0200
commit6199b9ef59583d580f1b83afa436c0ccd60ddb76 (patch)
tree1f5aad5dd88763f791c7d27a16ed44d1684f229d /pyaggr3g470r
parent'CRAWLING_METHOD wasn't initialized on Heroku. (diff)
parentfacto on config and more option in log levels (diff)
downloadnewspipe-6199b9ef59583d580f1b83afa436c0ccd60ddb76.tar.gz
newspipe-6199b9ef59583d580f1b83afa436c0ccd60ddb76.tar.bz2
newspipe-6199b9ef59583d580f1b83afa436c0ccd60ddb76.zip
Merged in jaesivsm/pyaggr3g470r (pull request #10)
misc fix and search integration in the new style page
Diffstat (limited to 'pyaggr3g470r')
-rw-r--r--pyaggr3g470r/controllers/abstract.py13
-rw-r--r--pyaggr3g470r/models/feed.py2
-rw-r--r--pyaggr3g470r/templates/home.html8
-rw-r--r--pyaggr3g470r/templates/layout.html12
-rw-r--r--pyaggr3g470r/templates/search.html48
-rw-r--r--pyaggr3g470r/views/views.py114
6 files changed, 81 insertions, 116 deletions
diff --git a/pyaggr3g470r/controllers/abstract.py b/pyaggr3g470r/controllers/abstract.py
index 8f0a8e3f..9a9004af 100644
--- a/pyaggr3g470r/controllers/abstract.py
+++ b/pyaggr3g470r/controllers/abstract.py
@@ -1,5 +1,6 @@
import logging
from bootstrap import db
+from sqlalchemy import or_
from werkzeug.exceptions import Forbidden, NotFound
logger = logging.getLogger(__name__)
@@ -25,13 +26,13 @@ class AbstractController(object):
each parameters of the function is treated as an equality unless the
name of the parameter ends with either "__gt", "__lt", "__ge", "__le",
- "__ne" or "__in".
+ "__ne", "__in" ir "__like".
"""
- if self.user_id is not None:
- filters[self._user_id_key] = self.user_id
db_filters = set()
for key, value in filters.items():
- if key.endswith('__gt'):
+ if key == '__or__':
+ db_filters.add(or_(*self._to_filters(**value)))
+ elif key.endswith('__gt'):
db_filters.add(getattr(self._db_cls, key[:-4]) > value)
elif key.endswith('__lt'):
db_filters.add(getattr(self._db_cls, key[:-4]) < value)
@@ -43,11 +44,15 @@ class AbstractController(object):
db_filters.add(getattr(self._db_cls, key[:-4]) != value)
elif key.endswith('__in'):
db_filters.add(getattr(self._db_cls, key[:-4]).in_(value))
+ elif key.endswith('__like'):
+ db_filters.add(getattr(self._db_cls, key[:-6]).like(value))
else:
db_filters.add(getattr(self._db_cls, key) == value)
return db_filters
def _get(self, **filters):
+ if self.user_id is not None:
+ filters[self._user_id_key] = self.user_id
return self._db_cls.query.filter(*self._to_filters(**filters))
def get(self, **filters):
diff --git a/pyaggr3g470r/models/feed.py b/pyaggr3g470r/models/feed.py
index a36d9573..aff11460 100644
--- a/pyaggr3g470r/models/feed.py
+++ b/pyaggr3g470r/models/feed.py
@@ -36,7 +36,7 @@ class Feed(db.Model):
Represent a feed.
"""
id = db.Column(db.Integer, primary_key=True)
- title = db.Column(db.String(), default="No title")
+ title = db.Column(db.String(), default="")
description = db.Column(db.String(), default="FR")
link = db.Column(db.String())
site_link = db.Column(db.String(), default="")
diff --git a/pyaggr3g470r/templates/home.html b/pyaggr3g470r/templates/home.html
index 461b6928..a31d65e1 100644
--- a/pyaggr3g470r/templates/home.html
+++ b/pyaggr3g470r/templates/home.html
@@ -14,7 +14,7 @@
{% if not feed_id %}</b>{% endif %}
</a></li>
{% for fid, nbunread in unread|dictsort(by='value')|reverse %}
- <li class="feed-menu"><a href="{{ gen_url(feed=fid) }}">
+ <li class="feed-menu"><a href="{{ gen_url(feed_id=fid) }}">
{% if feed_id == fid %}<b>{% endif %}
{% if in_error.get(fid, 0) > 0 %}
<span style="background-color: {{ "red" if in_error[fid] > conf.DEFAULT_MAX_ERROR -1 else "orange" }} ;" class="badge pull-right" title="Some errors occured while trying to retrieve that feed.">{{ in_error[fid] }} {{ _("error") }}{% if in_error[fid] > 1 %}s{% endif %}</span>
@@ -33,7 +33,7 @@
</span></li>
{% endfor %}
{% for fid, ftitle in feeds|dictsort(case_sensitive=False, by='value') if not fid in unread %}
- <li class="feed-menu"><a href="{{ gen_url(feed=fid) }}">
+ <li class="feed-menu"><a href="{{ gen_url(feed_id=fid) }}">
{% if in_error.get(fid, 0) > 0 %}
<span style="background-color: {{ "red" if in_error[fid] > conf.DEFAULT_MAX_ERROR - 1 else "orange" }} ;" class="badge pull-right" title="Some errors occured while trying to retrieve that feed.">{{ in_error[fid] }} {{ _("error") }}{% if in_error[fid] > 1 %}s{% endif %}</span>
{% endif %}
@@ -73,9 +73,9 @@
<thead>
<tr>
<th></th>
- <th><a href="{{ gen_url(sort_='feed') }}">{{ _('Feed') }}</a></th>
+ <th><a href="{{ gen_url(sort_='-feed' if sort_ == 'feed' else 'feed') }}">{{ _('Feed') }}</a></th>
<th>{{ _('Article') }}</th>
- <th><a href="{{ gen_url(sort_='date') }}">{{ _('Date') }}</a></th>
+ <th><a href="{{ gen_url(sort_='-date' if sort_ == 'date' else 'date') }}">{{ _('Date') }}</a></th>
</tr>
</thead>
<tbody>
diff --git a/pyaggr3g470r/templates/layout.html b/pyaggr3g470r/templates/layout.html
index 484bbdc7..a54125ea 100644
--- a/pyaggr3g470r/templates/layout.html
+++ b/pyaggr3g470r/templates/layout.html
@@ -86,6 +86,7 @@
<li><a href="{{ url_for("profile") }}"><span class="glyphicon glyphicon-user"></span> {{ _('Profile') }}</a></li>
<li><a href="{{ url_for("management") }}"><span class="glyphicon glyphicon-cog"></span> {{ _('Your data') }}</a></li>
{% if g.user.is_admin() %}
+ <li><a href="{{ url_for("about") }}"><span class="glyphicon glyphicon-question-sign"></span> {{ _('About') }}</a></li>
<li role="presentation" class="divider"></li>
<li><a href="{{ url_for("dashboard") }}"><span class="glyphicon glyphicon-dashboard"></span> {{ _('Dashboard') }}</a></li>
<li role="presentation" class="divider"></li>
@@ -101,9 +102,16 @@
</a>
<ul class="dropdown-menu">
<li>
- <form class="navbar-form" method=get action="/search" role="search">
+ <form class="navbar-form" method=get action="{{ url_for("search") }}" role="search">
<div class="input-group">
- <input type="text" class="form-control" name="query" placeholder="Search">
+ {% if filter_ %}<input type="hidden" name="filter_" value="{{ filter_ }}" />{% endif %}
+ {% if limit %}<input type="hidden" name="limit" value="{{ limit }}" />{% endif %}
+ {% if feed_id %}<input type="hidden" name="feed_id" value="{{ feed_id }}" />{% endif %}
+ <label for="search_title">{{ _("Title") }}</label>
+ <input type="checkbox" name="search_title" {% if search_title == 'on' or not (search_title == 'on' or search_content == 'on') %}checked{%endif%}/>
+ <label for="search_content">{{ _("Content") }}</label>
+ <input type="checkbox" name="search_content" {% if search_content == 'on' %}checked{%endif%}/>
+ <input type="text" class="form-control" name="query" placeholder="{{ _("Search") }}" {% if search_query %} value="{{ search_query }}"{% endif %} />
</div>
</form>
</li>
diff --git a/pyaggr3g470r/templates/search.html b/pyaggr3g470r/templates/search.html
deleted file mode 100644
index d3a1558d..00000000
--- a/pyaggr3g470r/templates/search.html
+++ /dev/null
@@ -1,48 +0,0 @@
- {% extends "layout.html" %}
-{% block content %}
-<div class="container">
- {% if feeds|count == 0 %}
- <div class="page-header">
- <h1>No results</h1>
- </div>
- {% else %}
- <div class="page-header">
- <h1>{{ nb_articles }} {% if nb_articles !=1 %} results {% else %} result {% endif %} for <i>{{ query }}</i></h1>
- </div>
- {% for feed in feeds|sort(attribute="title") %}
- <div class="row">
- <div class="col-md-6 col-md-offset-3">
- <h1>{{ feed.title|safe }}</h1>
- <a href="/articles/{{ feed.id }}/100"><i class="glyphicon glyphicon-th-list" title="More articles"></i></a>
- <a href="/feed/{{ feed.id }}"><i class="glyphicon glyphicon-info-sign" title="Details"></i></a>
- <a href="/edit_feed/{{ feed.id }}"><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|safe }}</a>
- {% 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|safe }}</a>
- {% if feed.articles[n].readed %}</h3>{% else %}</h1>{% endif %}
- <h6>{{ feed.articles[n].date }}</h6>
- </div>
- {% endfor %}
- </div>
- {% endif %}
- {% endfor %}
- {% endif %}
-</div><!-- /.container -->
-{% endblock %}
diff --git a/pyaggr3g470r/views/views.py b/pyaggr3g470r/views/views.py
index d0c06da0..3b018c6d 100644
--- a/pyaggr3g470r/views/views.py
+++ b/pyaggr3g470r/views/views.py
@@ -235,13 +235,10 @@ def signup():
return render_template('signup.html', form=form)
-@app.route('/')
-@login_required
-def home(favorites=False):
- """
- Home page for connected users. Displays by default unread articles.
- """
- head_title = gettext('Favorites') if favorites else ''
+
+def render_home(filters=None, head_title='', page_to_render='home', **kwargs):
+ if filters is None:
+ filters = {}
feed_contr = FeedController(g.user.id)
arti_contr = ArticleController(g.user.id)
feeds = {feed.id: feed.title for feed in feed_contr.read()}
@@ -250,48 +247,83 @@ def home(favorites=False):
in_error = {feed.id: feed.error_count for feed in
feed_contr.read(error_count__gt=2)}
- filter_ = request.args.get('filter_', 'all' if favorites else 'unread')
+ filter_ = request.args.get('filter_',
+ 'unread' if page_to_render == 'home' else 'all')
sort_ = request.args.get('sort_', 'date')
- feed_id = int(request.args.get('feed', 0))
+ feed_id = int(request.args.get('feed_id', 0))
limit = request.args.get('limit', 1000)
- filters = {}
- if favorites:
- filters['like'] = True
if filter_ != 'all':
filters['readed'] = filter_ == 'read'
if feed_id:
filters['feed_id'] = feed_id
- if head_title:
- head_title += ' - '
- head_title += feed_contr.get(id=feed_id).title
+ head_title = "%s%s" % (feed_contr.get(id=feed_id).title,
+ (' - %s' % head_title) if head_title else '')
- articles = arti_contr.read(**filters).order_by(Article.date.desc())
+ sort_param = {"feed": Article.title.desc(),
+ "date": Article.date.desc(),
+ "-feed": Article.title.asc(),
+ "-date": Article.date.asc(),
+ }.get(sort_, Article.date.desc())
+
+ articles = arti_contr.read(**filters).order_by(sort_param)
if limit != 'all':
limit = int(limit)
articles = articles.limit(limit)
- def gen_url(filter_=filter_, sort_=sort_, limit=limit, feed=feed_id):
- return url_for('favorites' if favorites else 'home',
- filter_=filter_, sort_=sort_, limit=limit, feed=feed)
+ def gen_url(filter_=filter_, sort_=sort_, limit=limit, feed_id=feed_id,
+ **kwargs):
+ if page_to_render == 'search':
+ kwargs['query'] = request.args.get('query', '')
+ kwargs['search_title'] = request.args.get('search_title', 'on')
+ kwargs['search_content'] = request.args.get('searc_content', 'off')
+ return url_for(page_to_render, filter_=filter_, sort_=sort_,
+ limit=limit, feed_id=feed_id, **kwargs)
articles = list(articles)
- if not articles and not favorites and feed_id:
+ if (page_to_render == 'home' and feed_id or page_to_render == 'search') \
+ and filter_ != 'all' and not articles:
return redirect(gen_url(filter_='all'))
- if sort_ == "feed":
- articles.sort(key=lambda article: article.source.title)
-
return render_template('home.html', gen_url=gen_url, feed_id=feed_id,
filter_=filter_, limit=limit, feeds=feeds,
unread=unread, articles=articles, in_error=in_error,
- head_title=head_title, favorites=favorites)
+ head_title=head_title, sort_=sort_, **kwargs)
+
+
+@app.route('/')
+@login_required
+def home():
+ "Home page for connected users. Displays by default unread articles."
+ return render_home()
@app.route('/favorites')
@login_required
def favorites():
- return home(favorites=True)
+ return render_home({'like': True}, gettext('Favorites'), 'favorites')
+
+
+@app.route('/search', methods=['GET'])
+@login_required
+def search():
+ "Search articles corresponding to the query."
+ if 'query' not in request.args:
+ flash(gettext("No text to search were provided."), "warning")
+ return render_home()
+ query = request.args['query']
+ filters = {}
+ search_title = request.args.get('search_title')
+ search_content = request.args.get('search_content')
+ if search_title == 'on':
+ filters['title__like'] = "%%%s%%" % query
+ if search_content == 'on':
+ filters['content__like'] = "%%%s%%" % query
+ if len(filters) > 1:
+ filters = {"__or__": filters}
+ return render_home(filters, "%s %s" % (gettext('Search:'), query),
+ 'search', search_query=query, search_title=search_title,
+ search_content=search_content)
@app.route('/fetch', methods=['GET'])
@@ -472,38 +504,6 @@ def export_opml():
response.headers['Content-Disposition'] = 'attachment; filename=feeds.opml'
return response
-@app.route('/search', methods=['GET'])
-@login_required
-def search():
- """
- Search articles corresponding to the query.
- """
- if conf.ON_HEROKU:
- flash(gettext("Full text search is not yet implemented for Heroku."), "warning")
- return redirect(url_for('home'))
- user = User.query.filter(User.id == g.user.id).first()
-
- search_result, result = [], []
- nb_articles = 0
-
- query = request.args.get('query', None)
- if query is not None:
- try:
- search_result, nb_articles = fastsearch.search(user.id, query)
- except Exception as e:
- flash(gettext('An error occured') + ' (%s).' % e, 'danger')
- light_feed = namedtuple('Feed', ['id', 'title', 'articles'], verbose=False, rename=False)
- for feed_id in search_result:
- for feed in user.feeds:
- if feed.id == feed_id:
- articles = []
- for article_id in search_result[feed_id]:
- current_article = Article.query.filter(Article.user_id == g.user.id, Article.id == article_id).first()
- articles.append(current_article)
- articles = sorted(articles, key=lambda t: t.date, reverse=True)
- result.append(light_feed(feed.id, feed.title, articles))
- break
- return render_template('search.html', feeds=result, nb_articles=nb_articles, query=query)
@app.route('/management', methods=['GET', 'POST'])
@login_required
bgstack15