aboutsummaryrefslogtreecommitdiff
path: root/pyaggr3g470r/views
diff options
context:
space:
mode:
authorFrançois Schmidts <francois.schmidts@gmail.com>2015-04-08 12:33:40 +0200
committerFrançois Schmidts <francois.schmidts@gmail.com>2015-04-12 14:31:06 +0200
commitb343dc73e5ea4aaf1314b6b277c3806f15ac0635 (patch)
treead31cf775685f51977b9a6dcad9e255e9087302f /pyaggr3g470r/views
parentUpdated bootstrap. (diff)
downloadnewspipe-b343dc73e5ea4aaf1314b6b277c3806f15ac0635.tar.gz
newspipe-b343dc73e5ea4aaf1314b6b277c3806f15ac0635.tar.bz2
newspipe-b343dc73e5ea4aaf1314b6b277c3806f15ac0635.zip
moving feed views related code in views.feed and massive use of url_for
Diffstat (limited to 'pyaggr3g470r/views')
-rw-r--r--pyaggr3g470r/views/feed.py149
-rw-r--r--pyaggr3g470r/views/views.py80
2 files changed, 140 insertions, 89 deletions
diff --git a/pyaggr3g470r/views/feed.py b/pyaggr3g470r/views/feed.py
index 4fe4e5da..9d74fa75 100644
--- a/pyaggr3g470r/views/feed.py
+++ b/pyaggr3g470r/views/feed.py
@@ -2,33 +2,40 @@
# -*- coding: utf-8 -
from datetime import datetime
-from flask import Blueprint, g, render_template
from sqlalchemy import desc
+from werkzeug.exceptions import BadRequest
-from pyaggr3g470r import controllers, utils
-from pyaggr3g470r.decorators import pyagg_default_decorator, \
- feed_access_required
+from flask import Blueprint, g, render_template, flash, \
+ redirect, request, url_for
+from flask.ext.babel import gettext
+from flask.ext.login import login_required
+
+import conf
+from pyaggr3g470r import utils
+from pyaggr3g470r.forms import AddFeedForm
+from pyaggr3g470r.controllers import FeedController, ArticleController
feeds_bp = Blueprint('feeds', __name__, url_prefix='/feeds')
feed_bp = Blueprint('feed', __name__, url_prefix='/feed')
+
@feeds_bp.route('/', methods=['GET'])
+@login_required
def feeds():
"Lists the subscribed feeds in a table."
return render_template('feeds.html',
- feeds=controllers.FeedController(g.user.id).read())
+ feeds=FeedController(g.user.id).read())
@feed_bp.route('/<int:feed_id>', methods=['GET'])
-@pyagg_default_decorator
-@feed_access_required
+@login_required
def feed(feed_id=None):
"Presents detailed information about a feed."
- feed = controllers.FeedController(g.user.id).get(id=feed_id)
+ feed = FeedController(g.user.id).get(id=feed_id)
word_size = 6
- articles = controllers.ArticleController(g.user.id) \
- .read(feed_id=feed_id) \
- .order_by(desc("Article.date")).all()
+ articles = ArticleController(g.user.id) \
+ .read(feed_id=feed_id) \
+ .order_by(desc("Article.date")).all()
top_words = utils.top_words(articles, n=50, size=int(word_size))
tag_cloud = utils.tag_cloud(top_words)
@@ -51,3 +58,123 @@ def feed(feed_id=None):
first_post_date=first_article,
end_post_date=last_article,
average=average, delta=delta, elapsed=elapsed)
+
+
+@feed_bp.route('/delete/<feed_id>', methods=['GET'])
+@login_required
+def delete(feed_id=None):
+ feed_contr = FeedController(g.user.id)
+ feed = feed_contr.get(id=feed_id)
+ feed_contr.delete(feed_id)
+ flash(gettext("Feed %(feed_title)s successfully deleted.",
+ feed_title=feed.title), 'success')
+ return redirect(url_for('home'))
+
+
+@feed_bp.route('/reset_errors/<int:feed_id>', methods=['GET', 'POST'])
+@login_required
+def reset_errors(feed_id):
+ feed_contr = FeedController(g.user.id)
+ feed = feed_contr.get(feed_id)
+ feed_contr.update({'id': feed_id}, {'error_count': 0, 'last_error': ''})
+ flash(gettext('Feed %(feed_title)r successfully updated.',
+ feed_title=feed.title), 'success')
+ return redirect(request.referrer or url_for('home'))
+
+
+@feed_bp.route('/bookmarklet', methods=['GET'])
+@login_required
+def bookmarklet():
+ feed_contr = FeedController(g.user.id)
+ url = request.args.get('url', None)
+ if not url:
+ flash(gettext("Couldn't add feed: url missing."), "error")
+ raise BadRequest("url is missing")
+
+ existing_feeds = list(feed_contr.read(link=url))
+ if existing_feeds:
+ flash(gettext("Couldn't add feed: feed already exists."),
+ "warning")
+ return redirect(url_for('feed.form',
+ feed_id=existing_feeds[0].id))
+
+ feed = feed_contr.create(link=url)
+ flash(gettext('Feed was successfully created.'), 'success')
+ return redirect(url_for('feed.form', feed_id=feed.id))
+
+
+@feed_bp.route('/read/<int:feed_id>', methods=['GET', 'POST'])
+@login_required
+def read(feed_id):
+ FeedController(g.user.id).update(readed=True)
+ flash(gettext('Feed successfully updated.',
+ feed_title=feed.title), 'success')
+ return redirect(request.referrer or url_for('home'))
+
+
+@feed_bp.route('/update/<int:feed_id>/<action>', methods=['GET', 'POST'])
+@login_required
+def update(feed_id, action):
+ readed = action == 'read'
+ ArticleController(g.user.id).update({'readed__ne': readed,
+ 'feed_id': feed_id},
+ {'readed': readed})
+ flash(gettext('Feed successfully updated.',
+ feed_title=feed.title), 'success')
+ return redirect(request.referrer or url_for('home'))
+
+
+@feed_bp.route('/create', methods=['GET', 'POST', 'PUT'])
+@feed_bp.route('/edit/<int:feed_id>', methods=['GET', 'POST'])
+@login_required
+def form(feed_id=None):
+ form = AddFeedForm()
+ feed_contr = FeedController(g.user.id)
+
+ if request.method == 'POST':
+ if not form.validate():
+ return render_template('edit_feed.html', form=form)
+ existing_feeds = list(feed_contr.read(link=form.link.data))
+ if existing_feeds:
+ flash(gettext("Couldn't add feed: feed already exists."),
+ "warning")
+ return redirect(url_for('feed.form',
+ feed_id=existing_feeds[0].id))
+
+ # Edit an existing feed
+ if feed_id is not None:
+ feed_contr.update({'id': feed_id},
+ {'title': form.title.data,
+ 'link': form.link.data,
+ 'enabled': form.enabled.data,
+ 'site_link': form.site_link.data})
+ flash(gettext('Feed %(feed_title)r successfully updated.',
+ feed_title=form.title.data), 'success')
+ return redirect(url_for('feed.form', feed_id=feed_id))
+
+ # Create a new feed
+ new_feed = FeedController(g.user.id).create(
+ title=form.title.data,
+ description="",
+ link=form.link.data,
+ site_link=form.site_link.data,
+ enabled=form.enabled.data)
+
+ flash(gettext('Feed %(feed_title)r successfully created.',
+ feed_title=new_feed.title), 'success')
+
+ return redirect(url_for('feed.form',
+ feed_id=new_feed.id))
+
+ # Getting the form for an existing feed
+ if feed_id is not None:
+ feed = FeedController(g.user.id).get(id=feed_id)
+ form = AddFeedForm(obj=feed)
+ return render_template('edit_feed.html',
+ action=gettext("Edit the feed"),
+ form=form, feed=feed,
+ not_on_heroku=not conf.ON_HEROKU)
+
+ # Return an empty form in order to create a new feed
+ return render_template('edit_feed.html', action=gettext("Add a feed"),
+ form=form, not_on_heroku=not conf.ON_HEROKU)
diff --git a/pyaggr3g470r/views/views.py b/pyaggr3g470r/views/views.py
index 17d79cab..b329afae 100644
--- a/pyaggr3g470r/views/views.py
+++ b/pyaggr3g470r/views/views.py
@@ -249,8 +249,9 @@ def home():
.group_by(Article.feed_id).all()
in_error = {feed.id: feed.error_count for feed in
FeedController(g.user.id).read(error_count__gt=2).all()}
+
def gen_url(filter_=filter_, limit=limit, feed=feed_id):
- return '?filter_=%s&limit=%s&feed=%d' % (filter_, limit, feed)
+ return url_for('home', filter_=filter_, limit=limit, feed=feed)
return render_template('home.html', gen_url=gen_url, feed_id=feed_id,
filter_=filter_, limit=limit, feeds=feeds,
unread=dict(unread), articles=articles.all(),
@@ -283,7 +284,6 @@ def about():
@app.route('/mark_as/<string:new_value>', methods=['GET'])
-@app.route('/mark_as/<string:new_value>/feed/<int:feed_id>', methods=['GET'])
@app.route('/mark_as/<string:new_value>/article/<int:article_id>', methods=['GET'])
@login_required
@feed_access_required
@@ -557,82 +557,6 @@ def history(year=None, month=None):
articles=articles,
year=year, month=month)
-@app.route('/bookmarklet', methods=['GET'])
-@app.route('/create_feed', methods=['GET', 'POST'])
-@app.route('/edit_feed/<int:feed_id>', methods=['GET', 'POST'])
-@login_required
-@feed_access_required
-def edit_feed(feed_id=None):
- """
- Add or edit a feed.
- """
- form = AddFeedForm()
-
- if request.method == 'POST':
- if form.validate() == False:
- return render_template('edit_feed.html', form=form)
- if feed_id is not None:
- # Edit an existing feed
- feed = FeedController(g.user.id).get(id=feed_id)
- form.populate_obj(feed)
- if feed.enabled:
- # set the error count to 0
- feed.error_count = 0
- feed.last_error = ""
- db.session.commit()
- flash(gettext('Feed successfully updated.'), 'success')
- return redirect('/edit_feed/' + str(feed_id))
- else:
- # Create a new feed
- existing_feed = [f for f in g.user.feeds if f.link == form.link.data]
- if len(existing_feed) == 0:
- new_feed = Feed(title=form.title.data, description="", link=form.link.data, \
- site_link=form.site_link.data, enabled=form.enabled.data)
- g.user.feeds.append(new_feed)
- #user.feeds = sorted(user.feeds, key=lambda t: t.title.lower())
- db.session.commit()
- flash(gettext('Feed successfully created.'), 'success')
-
- utils.fetch(g.user.id, Feed.query.filter(Feed.link == form.link.data).first().id)
- flash(gettext("Downloading articles for the new feed..."), 'info')
-
- return redirect('/edit_feed/' + str(new_feed.id))
- else:
- flash(gettext('Feed already in the database.'), 'warning')
- return redirect('/edit_feed/' + str(existing_feed[0].id))
-
- if request.method == 'GET':
- if feed_id is not None:
- feed = FeedController(g.user.id).get(id=feed_id)
- form = AddFeedForm(obj=feed)
- return render_template('edit_feed.html', action=gettext("Edit the feed"), form=form, feed=feed, \
- not_on_heroku = not conf.ON_HEROKU)
-
- # Enable the user to add a feed with a bookmarklet
- if None is not request.args.get('url', None):
- existing_feed = [f for f in g.user.feeds if feed.link == request.args.get('url', None)]
- if len(existing_feed) == 0:
- g.user.feeds.append(Feed(link=request.args.get('url', None)))
- db.session.commit()
- return jsonify({"message":"ok"})
- return jsonify({"message":"Feed already in the database."})
-
- # Return an empty form in order to create a new feed
- return render_template('edit_feed.html', action=gettext("Add a feed"), form=form, \
- not_on_heroku = not conf.ON_HEROKU)
-
-@app.route('/delete_feed/<feed_id>', methods=['GET'])
-@login_required
-@feed_access_required
-def delete_feed(feed_id=None):
- """
- Delete a feed with all associated articles.
- """
- feed = Feed.query.filter(Feed.id == feed_id).first()
- db.session.delete(feed)
- db.session.commit()
- flash(gettext('Feed') + ' ' + feed.title + ' ' + gettext('successfully deleted.'), 'success')
- return redirect(redirect_url())
@app.route('/profile', methods=['GET', 'POST'])
@login_required
bgstack15