diff options
-rw-r--r-- | bootstrap.py | 9 | ||||
-rw-r--r-- | conf.py | 3 | ||||
-rw-r--r-- | pyaggr3g470r/controllers/abstract.py | 9 | ||||
-rw-r--r-- | pyaggr3g470r/templates/about.html | 2 | ||||
-rw-r--r-- | pyaggr3g470r/templates/admin/user.html | 6 | ||||
-rw-r--r-- | pyaggr3g470r/templates/feed.html | 4 | ||||
-rw-r--r-- | pyaggr3g470r/templates/feeds.html | 8 | ||||
-rw-r--r-- | pyaggr3g470r/templates/home.html | 18 | ||||
-rw-r--r-- | pyaggr3g470r/templates/layout.html | 4 | ||||
-rw-r--r-- | pyaggr3g470r/templates/management.html | 2 | ||||
-rw-r--r-- | pyaggr3g470r/templates/unread.html | 4 | ||||
-rw-r--r-- | pyaggr3g470r/views/feed.py | 149 | ||||
-rw-r--r-- | pyaggr3g470r/views/views.py | 80 | ||||
-rwxr-xr-x | runserver.py | 6 |
14 files changed, 182 insertions, 122 deletions
diff --git a/bootstrap.py b/bootstrap.py index 3b76d0ac..9c2ce049 100644 --- a/bootstrap.py +++ b/bootstrap.py @@ -6,6 +6,7 @@ import os import conf import logging +from urllib.parse import urlsplit def set_logging(log_path, log_level=logging.INFO, log_format='%(asctime)s %(levelname)s %(message)s'): @@ -22,8 +23,12 @@ from flask.ext.sqlalchemy import SQLAlchemy # Create Flask application application = Flask('pyaggr3g470r') application.debug = conf.WEBSERVER_DEBUG -set_logging(conf.LOG_PATH, log_level=logging.DEBUG if conf.WEBSERVER_DEBUG - else logging.INFO) +scheme, domain, _, _, _ = urlsplit(conf.PLATFORM_URL) +application.config['SERVER_NAME'] = domain +application.config['PREFERRED_URL_SCHEME'] = scheme + +set_logging(conf.LOG_PATH, + log_level=logging.DEBUG if conf.WEBSERVER_DEBUG else logging.INFO) # Create dummy secrey key so we can use sessions application.config['SECRET_KEY'] = getattr(conf, 'WEBSERVER_SECRET', None) @@ -66,7 +66,8 @@ if not ON_HEROKU: WEBZINE_ROOT = PATH + "/pyaggr3g470r/var/export/" else: - PLATFORM_URL = os.environ.get('PLATFORM_URL', 'https://pyaggr3g470r.herokuapp.com/') + PLATFORM_URL = os.environ.get('PLATFORM_URL', + 'https://pyaggr3g470r.herokuapp.com/') ADMIN_EMAIL = os.environ.get('ADMIN_EMAIL', '') RECAPTCHA_PUBLIC_KEY = os.environ.get('RECAPTCHA_PUBLIC_KEY', '') RECAPTCHA_PRIVATE_KEY = os.environ.get('RECAPTCHA_PRIVATE_KEY', '') diff --git a/pyaggr3g470r/controllers/abstract.py b/pyaggr3g470r/controllers/abstract.py index f1173817..8f0a8e3f 100644 --- a/pyaggr3g470r/controllers/abstract.py +++ b/pyaggr3g470r/controllers/abstract.py @@ -56,8 +56,8 @@ class AbstractController(object): if not obj: raise NotFound({'message': 'No %r (%r)' % (self._db_cls.__class__.__name__, filters)}) - if self.user_id is not None \ - and getattr(obj, self._user_id_key) != self.user_id: + + if not self._has_right_on(obj): raise Forbidden({'message': 'No authorized to access %r (%r)' % (self._db_cls.__class__.__name__, filters)}) return obj @@ -84,3 +84,8 @@ class AbstractController(object): db.session.delete(obj) db.session.commit() return obj + + def _has_right_on(self, obj): + # user_id == None is like being admin + return self.user_id is None \ + or getattr(obj, self._user_id_key, None) == self.user_id diff --git a/pyaggr3g470r/templates/about.html b/pyaggr3g470r/templates/about.html index 08b80fbb..901b3b35 100644 --- a/pyaggr3g470r/templates/about.html +++ b/pyaggr3g470r/templates/about.html @@ -17,7 +17,7 @@ <h1>{{ _('Help') }}</h1> <p>{{ _('If you have any problem, <a href="http://wiki.cedricbonhomme.org/contact">contact</a> the administrator.') }}</p> <p>{{ _('The documentation of the RESTful API is <a href="https://pyaggr3g470r.readthedocs.org/en/latest/web-services.html">here</a>.') }}</p> - <p>{{ _('You can subscribe to new feeds with a bookmarklet. Drag <a href="%(bookmarklet)s">this link</a> to your browser bookmarks.', bookmarklet='javascript:window.location="https://pyaggr3g470r.herokuapp.com/bookmarklet?url="+encodeURIComponent(document.location)') }}</p> + <p>{{ _('You can subscribe to new feeds with a bookmarklet. Drag <a href="%(bookmarklet)s">this link</a> to your browser bookmarks.', bookmarklet='javascript:window.location="%s?url="+encodeURIComponent(document.location)' % url_for('feed.bookmarklet', _external=True)) }}</p> </div> <div class="well"> <h1>{{ _('Donation') }}</h1> diff --git a/pyaggr3g470r/templates/admin/user.html b/pyaggr3g470r/templates/admin/user.html index f20d53dd..317fef49 100644 --- a/pyaggr3g470r/templates/admin/user.html +++ b/pyaggr3g470r/templates/admin/user.html @@ -39,9 +39,9 @@ <td>{{ feed.site_link }}</td> <td>{{ feed.articles.all()|count }}</td> <td> - <a href="/feed/{{ feed.id }}"><i class="glyphicon glyphicon-th-list" title="{{ _('Feed') }}"></i></a> - <a href="/edit_feed/{{ feed.id }}"><i class="glyphicon glyphicon-edit" title="{{ _('Edit this feed') }}"></i></a> - <a href="/delete_feed/{{ feed.id }}"><i class="glyphicon glyphicon-remove" title="{{ _('Delete this feed') }}" onclick="return confirm('{{ _('You are going to delete this feed.') }}');"></i></a> + <a href="{{ url_for("feed.feed", feed_id=feed.id) }}"><i class="glyphicon glyphicon-th-list" title="{{ _('Feed') }}"></i></a> + <a href="{{ url_for("feed.form", feed_id=feed.id) }}"><i class="glyphicon glyphicon-edit" title="{{ _('Edit this feed') }}"></i></a> + <a href="{{ url_for("feed.delete", feed_id=feed.id) }}"><i class="glyphicon glyphicon-remove" title="{{ _('Delete this feed') }}" onclick="return confirm('{{ _('You are going to delete this feed.') }}');"></i></a> </td> {% endfor %} </tbody> diff --git a/pyaggr3g470r/templates/feed.html b/pyaggr3g470r/templates/feed.html index 268cbf7d..c0c92279 100644 --- a/pyaggr3g470r/templates/feed.html +++ b/pyaggr3g470r/templates/feed.html @@ -4,8 +4,8 @@ <div class="well"> <h2>{{ feed.title }}</h2> {% if feed.description %} <p>{{ feed.description }}</p> {% endif %} - <a href="/delete_feed/{{ feed.id }}"><i class="glyphicon glyphicon-remove" title="{{ _('Delete this feed') }}" onclick="return confirm('{{ _('You are going to delete this feed.') }}');"></i></a> - <a href="/edit_feed/{{ feed.id }}"><i class="glyphicon glyphicon-edit" title="{{ _('Edit this feed') }}"></i></a> + <a href="{{ url_for("feed.delete", feed_id=feed.id) }}"><i class="glyphicon glyphicon-remove" title="{{ _('Delete this feed') }}" onclick="return confirm('{{ _('You are going to delete this feed.') }}');"></i></a> + <a href="{{ url_for("feed.form", feed_id=feed.id) }}"><i class="glyphicon glyphicon-edit" title="{{ _('Edit this feed') }}"></i></a> </div> <div class="well"> <p> diff --git a/pyaggr3g470r/templates/feeds.html b/pyaggr3g470r/templates/feeds.html index 460ae4a2..9c57bf42 100644 --- a/pyaggr3g470r/templates/feeds.html +++ b/pyaggr3g470r/templates/feeds.html @@ -1,7 +1,7 @@ {% extends "layout.html" %} {% block content %} <div class="container"> - <h1>{{ _('You are subscribed to') }} {{ feeds.count() }} {{ _('feeds') }} · {{ _('Add a') }} <a href="/create_feed">{{ _('feed') }}</a></h1> + <h1>{{ _('You are subscribed to') }} {{ feeds.count() }} {{ _('feeds') }} · {{ _('Add a') }} <a href="{{ url_for("feed.form") }}">{{ _('feed') }}</a></h1> <div class="table-responsive"> <table class="table table-striped"> <thead> @@ -25,14 +25,14 @@ <i class="glyphicon glyphicon-eye-close" title="{{ _('Feed disabled') }}"></i> {% endif %} </td> - <td><a href="/feed/{{ feed.id }}" {% if feed.description %}title="{{ feed.description }}"{% endif %}>{{ feed.title }}</a></td> + <td><a href="{{ url_for("feed.feed", feed_id=feed.id) }}" {% if feed.description %}title="{{ feed.description }}"{% endif %}>{{ feed.title }}</a></td> <td><a href="{{ feed.site_link }}">{{ feed.site_link }}</a></td> <td>{{ feed.articles.count() }}</td> <td> <a href="/articles/{{ feed.id }}/100"><i class="glyphicon glyphicon-th-list" title="{{ _('Articles') }}"></i></a> - <a href="/edit_feed/{{ feed.id }}"><i class="glyphicon glyphicon-edit" title="{{ _('Edit this feed') }}"></i></a> + <a href="{{ url_for("feed.form", feed_id=feed.id) }}"><i class="glyphicon glyphicon-edit" title="{{ _('Edit this feed') }}"></i></a> <a href="/duplicates/{{ feed.id }}"><i class="glyphicon glyphicon-book" title="{{ _('Duplicate articles') }}"></i></a> - <a href="/delete_feed/{{ feed.id }}"><i class="glyphicon glyphicon-remove" title="{{ _('Delete this feed') }}" onclick="return confirm('{{ _('You are going to delete this feed.') }}');"></i></a> + <a href="{{ url_for("feed.delete", feed_id=feed.id) }}"><i class="glyphicon glyphicon-remove" title="{{ _('Delete this feed') }}" onclick="return confirm('{{ _('You are going to delete this feed.') }}');"></i></a> </td> </tr> {% endfor %} diff --git a/pyaggr3g470r/templates/home.html b/pyaggr3g470r/templates/home.html index c20cacfa..390ac5f6 100644 --- a/pyaggr3g470r/templates/home.html +++ b/pyaggr3g470r/templates/home.html @@ -3,7 +3,7 @@ {% if feeds|count == 0 %} <div class="col-md-4 col-md-offset-4"> <h1>{{ _("You don't have any feeds.") }}</h1> - <h1><a href="/create_feed">{{ _('Add some') }}</a>, {{ _('or') }} <a href="/management">{{ _('upload an OPML file.') }}</a></h1> + <h1><a href="{{ url_for("feed.form") }}">{{ _('Add some') }}</a>, {{ _('or') }} <a href="/management">{{ _('upload an OPML file.') }}</a></h1> </div> {% else %} <div id="affix-nav" class="col-md-3 sidebar hidden-xs hidden-sm"> @@ -26,10 +26,10 @@ <li class="feed-commands"><span> <a href="/feed/{{ fid }}"><i class="glyphicon glyphicon-info-sign" title="{{ _('Details') }}"></i></a> <a href="/articles/{{ fid }}/100"><i class="glyphicon glyphicon-th-list" title="{{ _('Articles') }}"></i></a> - <a href="/edit_feed/{{ fid }}"><i class="glyphicon glyphicon-edit" title="{{ _('Edit this feed') }}"></i></a> - <a href="/delete_feed/{{ fid }}"><i class="glyphicon glyphicon-remove" title="{{ _('Delete this feed') }}" onclick="return confirm('{{ _('You are going to delete this feed.') }}');"></i></a> - <a href="/mark_as/read/feed/{{ fid }}"><i class="glyphicon glyphicon-check" title="{{ _('Mark this feed as read') }}"></i></a> - <a href="/mark_as/unread/feed/{{ fid }}"><i class="glyphicon glyphicon-unchecked" title="{{ _('Mark this feed as unread') }}"></i></a> + <a href="{{ url_for("feed.form", feed_id=fid) }}"><i class="glyphicon glyphicon-edit" title="{{ _('Edit this feed') }}"></i></a> + <a href="{{ url_for("feed.delete", feed_id=fid) }}"><i class="glyphicon glyphicon-remove" title="{{ _('Delete this feed') }}" onclick="return confirm('{{ _('You are going to delete this feed.') }}');"></i></a> + <a href="{{ url_for("feed.update", feed_id=fid, action="read") }}"><i class="glyphicon glyphicon-check" title="{{ _('Mark this feed as read') }}"></i></a> + <a href="{{ url_for("feed.update", feed_id=fid, action="unread") }}"><i class="glyphicon glyphicon-unchecked" title="{{ _('Mark this feed as unread') }}"></i></a> </span></li> {% endfor %} {% for fid, ftitle in feeds|dictsort(case_sensitive=False, by='value') if not fid in unread %} @@ -44,10 +44,10 @@ <li class="feed-commands"><span> <a href="/feed/{{ fid }}"><i class="glyphicon glyphicon-info-sign" title="{{ _('Details') }}"></i></a> <a href="/articles/{{ fid }}/100"><i class="glyphicon glyphicon-th-list" title="{{ _('Articles') }}"></i></a> - <a href="/edit_feed/{{ fid }}"><i class="glyphicon glyphicon-edit" title="{{ _('Edit this feed') }}"></i></a> - <a href="/delete_feed/{{ fid }}"><i class="glyphicon glyphicon-remove" title="{{ _('Delete this feed') }}" onclick="return confirm('{{ _('You are going to delete this feed.') }}');"></i></a> - <a href="/mark_as/read/feed/{{ fid }}"><i class="glyphicon glyphicon-check" title="{{ _('Mark this feed as read') }}"></i></a> - <a href="/mark_as/unread/feed/{{ fid }}"><i class="glyphicon glyphicon-unchecked" title="{{ _('Mark this feed as unread') }}"></i></a> + <a href="{{ url_for("feed.form", feed_id=fid) }}"><i class="glyphicon glyphicon-edit" title="{{ _('Edit this feed') }}"></i></a> + <a href="{{ url_for("feed.delete", feed_id=fid) }}"><i class="glyphicon glyphicon-remove" title="{{ _('Delete this feed') }}" onclick="return confirm('{{ _('You are going to delete this feed.') }}');"></i></a> + <a href="{{ url_for("feed.update", feed_id=fid, action="read") }}"><i class="glyphicon glyphicon-check" title="{{ _('Mark this feed as read') }}"></i></a> + <a href="{{ url_for("feed.update", feed_id=fid, action="unread") }}"><i class="glyphicon glyphicon-unchecked" title="{{ _('Mark this feed as unread') }}"></i></a> </span></li> {% endfor %} </ul> diff --git a/pyaggr3g470r/templates/layout.html b/pyaggr3g470r/templates/layout.html index 60efa69e..7f8288f8 100644 --- a/pyaggr3g470r/templates/layout.html +++ b/pyaggr3g470r/templates/layout.html @@ -38,7 +38,7 @@ <li><a accesskey="r" href="/fetch">{{ _('Fetch') }}</a></li> <li><a href="/mark_as/read">{{ _('Mark all as read') }}</a></li> <li role="presentation" class="divider"></li> - <li><a href="/create_feed">{{ _('Add a feed') }}</a></li> + <li><a href="{{ url_for("feed.form") }}">{{ _('Add a feed') }}</a></li> </ul> </li> <li class="dropdown"> @@ -47,7 +47,7 @@ <li><a accesskey="u" href="/unread">{{ _('Unread') }}</a></li> <li><a accesskey="f" href="/favorites">{{ _('Favorites') }}</a></li> <li><a accesskey="i" href="/inactives">{{ _('Inactive feeds') }}</a></li> - <li><a href="/feeds">{{ _('All feeds') }}</a></li> + <li><a href="{{ url_for("feeds.feeds") }}">{{ _('All feeds') }}</a></li> <li><a href="/history">{{ _('History') }}</a></li> </ul> </li> diff --git a/pyaggr3g470r/templates/management.html b/pyaggr3g470r/templates/management.html index 722300af..b9d02de5 100644 --- a/pyaggr3g470r/templates/management.html +++ b/pyaggr3g470r/templates/management.html @@ -3,7 +3,7 @@ <div class="container"> <div class="well"> <h1>{{ _('Your subscriptions') }}</h1> - <p>{{ _('You are subscribed to') }} {{ nb_feeds }} <a href="/feeds">{{ _('feeds') }}</a>. {{ _('Add a') }} <a href="/create_feed">{{ _('feed') }}</a>.</p> + <p>{{ _('You are subscribed to') }} {{ nb_feeds }} <a href="/feeds">{{ _('feeds') }}</a>. {{ _('Add a') }} <a href="{{ url_for("feed.form") }}">{{ _('feed') }}</a>.</p> <p>{{ nb_articles }} {{ _('articles are stored in the database with') }} {{ nb_unread_articles }} <a href="/unread">{{ _('unread articles') }}</a>.</p> {% if not_on_heroku %} <a href="/index_database" class="btn btn-default">{{ _('Index database') }}</a> diff --git a/pyaggr3g470r/templates/unread.html b/pyaggr3g470r/templates/unread.html index 9808572b..d6f51bff 100644 --- a/pyaggr3g470r/templates/unread.html +++ b/pyaggr3g470r/templates/unread.html @@ -16,8 +16,8 @@ <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> - <a href="/mark_as/read/feed/{{ feed.id }}"><i class="glyphicon glyphicon-check" title="{{ _('Mark all feed as read') }}"></i></a> - <a href="/mark_as/unread/feed/{{ feed.id }}"><i class="glyphicon glyphicon-unchecked" title="{{ _('Mark all feed as unread') }}"></i></a> + <a href="{{ url_for("feed.update", feed_id=fid, action="read") }}"><i class="glyphicon glyphicon-check" title="{{ _('Mark all feed as read') }}"></i></a> + <a href="{{ url_for("feed.update", feed_id=fid, action="unread") }}"><i class="glyphicon glyphicon-unchecked" title="{{ _('Mark all feed as unread') }}"></i></a> <h3>{{ feed.articles|length }} {{ _('unread articles') }}.</h3> </div> </div> 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 diff --git a/runserver.py b/runserver.py index db1c4410..d1f5702c 100755 --- a/runserver.py +++ b/runserver.py @@ -19,7 +19,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import calendar -from bootstrap import conf, application, db, populate_g +from bootstrap import conf, application, populate_g from flask.ext.babel import Babel from flask.ext.babel import format_datetime @@ -30,12 +30,10 @@ if conf.ON_HEROKU: babel = Babel(application) # Jinja filters -application.jinja_env.filters['datetime'] = format_datetime - -#@register.filter def month_name(month_number): return calendar.month_name[month_number] application.jinja_env.filters['month_name'] = month_name +application.jinja_env.filters['datetime'] = format_datetime # Views from flask.ext.restful import Api |