diff options
author | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2016-04-06 23:45:06 +0200 |
---|---|---|
committer | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2016-04-06 23:45:06 +0200 |
commit | 0e42e6d8ce1a056b4426148651b741f345968be2 (patch) | |
tree | 982b511677d05142726d92410e0d67ec1d25574b /src/web/views/article.py | |
parent | replace g.user by current_user (diff) | |
download | newspipe-0e42e6d8ce1a056b4426148651b741f345968be2.tar.gz newspipe-0e42e6d8ce1a056b4426148651b741f345968be2.tar.bz2 newspipe-0e42e6d8ce1a056b4426148651b741f345968be2.zip |
major problems fixed.
Diffstat (limited to 'src/web/views/article.py')
-rw-r--r-- | src/web/views/article.py | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/src/web/views/article.py b/src/web/views/article.py index 416bb96c..94f661fa 100644 --- a/src/web/views/article.py +++ b/src/web/views/article.py @@ -2,12 +2,14 @@ # -*- coding: utf-8 - from datetime import datetime, timedelta from flask import (Blueprint, g, render_template, redirect, - flash, url_for, request) + flash, url_for, make_response, request) from flask.ext.babel import gettext from flask.ext.login import login_required, current_user +from web.export import export_json, export_html from web.lib.utils import clear_string, redirect_url -from web.controllers import ArticleController +from web.controllers import (ArticleController, UserController, + CategoryController) from web.lib.view_utils import etag_match articles_bp = Blueprint('articles', __name__, url_prefix='/articles') @@ -124,3 +126,47 @@ def expire(): query.delete() flash(gettext('%(count)d articles deleted', count=count), 'info') return redirect(redirect_url()) + + +@articles_bp.route('/export', methods=['GET']) +@login_required +def export(): + """ + Export all articles to HTML or JSON. + """ + user = UserController(current_user.id).get(id=current_user.id) + if request.args.get('format') == "HTML": + # Export to HTML + try: + archive_file, archive_file_name = export_html(user) + except Exception as e: + print(e) + flash(gettext("Error when exporting articles."), 'danger') + return redirect(redirect_url()) + response = make_response(archive_file) + response.headers['Content-Type'] = 'application/x-compressed' + response.headers['Content-Disposition'] = 'attachment; filename=%s' \ + % archive_file_name + elif request.args.get('format') == "JSON": + # Export to JSON + try: + json_result = export_json(user) + except Exception as e: + flash(gettext("Error when exporting articles."), 'danger') + return redirect(redirect_url()) + response = make_response(json_result) + response.mimetype = 'application/json' + response.headers["Content-Disposition"] \ + = 'attachment; filename=account.json' + elif request.args.get('format') == "OPML": + categories = {cat.id: cat.dump() + for cat in CategoryController(user.id).read()} + response = make_response(render_template('opml.xml', user=user, + categories=categories, + now=datetime.now())) + response.headers['Content-Type'] = 'application/xml' + response.headers['Content-Disposition'] = 'attachment; filename=feeds.opml' + else: + flash(gettext('Export format not supported.'), 'warning') + return redirect(redirect_url()) + return response |