From 600391ac74a2ab29bd5c1f1433c14dae9ce6ea9b Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Sun, 11 Oct 2015 23:50:23 +0200 Subject: moving 'expire_article' and improving its perf --- src/web/templates/management.html | 2 +- src/web/views/article.py | 22 +++++++++++++++++++++- src/web/views/views.py | 17 ----------------- 3 files changed, 22 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/web/templates/management.html b/src/web/templates/management.html index ac4f3a4a..01179b5e 100644 --- a/src/web/templates/management.html +++ b/src/web/templates/management.html @@ -5,7 +5,7 @@

{{ _('Your subscriptions') }}

{{ _('You are subscribed to') }} {{ nb_feeds }} {{ _('feeds') }}. {{ _('Add') }} {{ _('a feed') }}.

{{ nb_articles }} {{ _('articles are stored in the database with') }} {{ nb_unread_articles }} {{ _('unread articles') }}.

- {{ _('Delete articles older than 10 weeks') }} + {{ _('Delete articles older than 10 weeks') }}

{{ _('OPML import/export') }}

diff --git a/src/web/views/article.py b/src/web/views/article.py index d308d37e..664b66c8 100644 --- a/src/web/views/article.py +++ b/src/web/views/article.py @@ -1,6 +1,8 @@ #! /usr/bin/env python # -*- coding: utf-8 - -from flask import Blueprint, g, render_template, redirect, flash, url_for +from datetime import datetime, timedelta +from flask import (Blueprint, g, render_template, redirect, + flash, url_for, request) from flask.ext.babel import gettext from flask.ext.login import login_required @@ -101,3 +103,21 @@ def mark_as(new_value='read', feed_id=None, article_id=None): if readed: return redirect(redirect_url()) return redirect('home') + + +@articles_bp.route('/expire_articles', methods=['GET']) +@login_required +def expire(): + """ + Delete articles older than the given number of weeks. + """ + current_time = datetime.utcnow() + weeks_ago = current_time - timedelta(int(request.args.get('weeks', 10))) + art_contr = ArticleController(g.user.id) + + query = art_contr.read(__or__={'date__lt': weeks_ago, + 'retrieved_date__lt': weeks_ago}) + count = query.count() + query.delete() + flash(gettext('%(count)d articles deleted', count=count), 'info') + return redirect(redirect_url()) diff --git a/src/web/views/views.py b/src/web/views/views.py index f543c6fa..4abea96e 100644 --- a/src/web/views/views.py +++ b/src/web/views/views.py @@ -416,20 +416,3 @@ def export_opml(): response.headers['Content-Type'] = 'application/xml' response.headers['Content-Disposition'] = 'attachment; filename=feeds.opml' return response - - -@app.route('/expire_articles', methods=['GET']) -@login_required -def expire_articles(): - """ - Delete articles older than the given number of weeks. - """ - weeks_ago = datetime.datetime.utcnow() - \ - datetime.timedelta(weeks=int(request.args.get('weeks', 10))) - Article.query.filter( - and_(Article.user_id == g.user.id, - or_(Article.date < weeks_ago, - Article.retrieved_date < weeks_ago))).delete() - flash(gettext('Articles deleted.'), 'info') - db.session.commit() - return redirect(redirect_url()) -- cgit