From b343dc73e5ea4aaf1314b6b277c3806f15ac0635 Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Wed, 8 Apr 2015 12:33:40 +0200 Subject: moving feed views related code in views.feed and massive use of url_for --- pyaggr3g470r/views/feed.py | 149 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 138 insertions(+), 11 deletions(-) (limited to 'pyaggr3g470r/views/feed.py') 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('/', 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/', 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/', 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/', 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//', 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/', 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) -- cgit From a4d30ad7b4481fe68e37d01a2c0b289445cf27a2 Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Wed, 8 Apr 2015 15:53:39 +0200 Subject: adding a reset link to the feed page --- pyaggr3g470r/views/feed.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pyaggr3g470r/views/feed.py') diff --git a/pyaggr3g470r/views/feed.py b/pyaggr3g470r/views/feed.py index 9d74fa75..895584a4 100644 --- a/pyaggr3g470r/views/feed.py +++ b/pyaggr3g470r/views/feed.py @@ -75,7 +75,7 @@ def delete(feed_id=None): @login_required def reset_errors(feed_id): feed_contr = FeedController(g.user.id) - feed = feed_contr.get(feed_id) + feed = feed_contr.get(id=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') -- cgit From 6de7a942771dfd00bb67f1c6087905570f8adcb6 Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Sat, 11 Apr 2015 14:46:43 +0200 Subject: correcting a bug where we didn't allow to edit a feed --- pyaggr3g470r/views/feed.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pyaggr3g470r/views/feed.py') diff --git a/pyaggr3g470r/views/feed.py b/pyaggr3g470r/views/feed.py index 895584a4..f0e35e4b 100644 --- a/pyaggr3g470r/views/feed.py +++ b/pyaggr3g470r/views/feed.py @@ -135,7 +135,7 @@ def form(feed_id=None): 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: + if existing_feeds and feed_id is None: flash(gettext("Couldn't add feed: feed already exists."), "warning") return redirect(url_for('feed.form', -- cgit From 21da20f5d592aabcb60096768dcac76baa626315 Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Sat, 11 Apr 2015 16:14:19 +0200 Subject: first take on redoing the menu --- pyaggr3g470r/views/feed.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'pyaggr3g470r/views/feed.py') diff --git a/pyaggr3g470r/views/feed.py b/pyaggr3g470r/views/feed.py index f0e35e4b..e4c0dc9a 100644 --- a/pyaggr3g470r/views/feed.py +++ b/pyaggr3g470r/views/feed.py @@ -112,15 +112,16 @@ def read(feed_id): return redirect(request.referrer or url_for('home')) -@feed_bp.route('/update//', methods=['GET', 'POST']) +@feed_bp.route('/update//', methods=['GET', 'POST']) +@feeds_bp.route('/update/', methods=['GET', 'POST']) @login_required -def update(feed_id, action): +def update(action, feed_id=None): 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') + filters = {'readed__ne': readed} + if feed_id: + filters['feed_id'] = feed_id + ArticleController(g.user.id).update(filters, {'readed': readed}) + flash(gettext('Feed successfully updated.'), 'success') return redirect(request.referrer or url_for('home')) -- cgit