diff options
author | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2014-04-29 23:08:36 +0200 |
---|---|---|
committer | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2014-04-29 23:08:36 +0200 |
commit | e762c6b0a257dbbc90f51e069f708bbf037fac57 (patch) | |
tree | 6ed6a400d2e359cc97a090b50d7469982da15fed /pyaggr3g470r | |
parent | added templates for emails (diff) | |
download | newspipe-e762c6b0a257dbbc90f51e069f708bbf037fac57.tar.gz newspipe-e762c6b0a257dbbc90f51e069f708bbf037fac57.tar.bz2 newspipe-e762c6b0a257dbbc90f51e069f708bbf037fac57.zip |
Added export to JSON.
Diffstat (limited to 'pyaggr3g470r')
-rw-r--r-- | pyaggr3g470r/export.py | 25 | ||||
-rw-r--r-- | pyaggr3g470r/models.py | 8 | ||||
-rw-r--r-- | pyaggr3g470r/templates/management.html | 3 | ||||
-rw-r--r-- | pyaggr3g470r/views.py | 33 |
4 files changed, 57 insertions, 12 deletions
diff --git a/pyaggr3g470r/export.py b/pyaggr3g470r/export.py index 243b6843..502c82db 100644 --- a/pyaggr3g470r/export.py +++ b/pyaggr3g470r/export.py @@ -39,6 +39,8 @@ import time import tarfile from datetime import datetime +from flask import jsonify + import conf import models @@ -197,4 +199,25 @@ def export_html(user): shutil.rmtree(webzine_root) with open(conf.WEBZINE_ROOT + archive_file_name, 'r') as export_file: - return export_file.read(), archive_file_name
\ No newline at end of file + return export_file.read(), archive_file_name + +def export_json(user): + """ + """ + result = [] + for feed in user.feeds: + result.append({ + "title": feed.title, + "description": feed.description, + "link": feed.link, + "site_link": feed.site_link, + "articles": [ { + "title": article.title, + "link": article.link, + "content": article.content + } + for article in feed.articles + ] + }) + + return jsonify(result=result) diff --git a/pyaggr3g470r/models.py b/pyaggr3g470r/models.py index 52d2ef51..f4aa0521 100644 --- a/pyaggr3g470r/models.py +++ b/pyaggr3g470r/models.py @@ -26,10 +26,12 @@ __revision__ = "$Date: 2014/04/12 $" __copyright__ = "Copyright (c) Cedric Bonhomme" __license__ = "GPLv3" +import json from datetime import datetime from sqlalchemy import desc from werkzeug import generate_password_hash, check_password_hash from flask.ext.login import UserMixin + from pyaggr3g470r import db class User(db.Model, UserMixin): @@ -122,4 +124,8 @@ class Article(db.Model): feed_id = db.Column(db.Integer, db.ForeignKey('feed.id')) def __repr__(self): - return '<Article %r>' % (self.title) + return json.dumps({ + "title": self.title, + "link": self.link, + "content": self.content + }) diff --git a/pyaggr3g470r/templates/management.html b/pyaggr3g470r/templates/management.html index 2f95daaa..78f4399b 100644 --- a/pyaggr3g470r/templates/management.html +++ b/pyaggr3g470r/templates/management.html @@ -34,7 +34,8 @@ </div> <div class="jumbotron"> <h1>Export articles</h1> - <a href="/export/" class="btn btn-default">HTML</a> + <a href="/export/?format=HTML" class="btn btn-default">HTML</a> + <a href="/export/?format=JSON" class="btn btn-default">JSON</a> </div> </div><!-- /.container --> {% endblock %} diff --git a/pyaggr3g470r/views.py b/pyaggr3g470r/views.py index 6d394186..491d44a2 100644 --- a/pyaggr3g470r/views.py +++ b/pyaggr3g470r/views.py @@ -401,17 +401,32 @@ def index_database(): @login_required def export_articles(): """ - Export all articles. + Export all articles to HTML or JSON. """ user = User.query.filter(User.id == g.user.id).first() - try: - archive_file, archive_file_name = export.export_html(user) - except: - flash("Error when exporting articles.", 'danger') - return redirect(url_for('management')) - response = make_response(archive_file) - response.headers['Content-Type'] = 'application/x-compressed' - response.headers['Content-Disposition'] = 'attachment; filename='+archive_file_name + if request.args.get('format') == "HTML": + # Export to HTML + try: + archive_file, archive_file_name = export.export_html(user) + except: + flash("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='+archive_file_name + elif request.args.get('format') == "JSON": + # Export to JSON + try: + json_result = export.export_json(user) + except: + flash("Error when exporting articles.", 'danger') + return redirect(redirect_url()) + response = make_response(json_result) + response.mimetype = 'application/json' + response.headers["Content-Disposition"] = 'attachment; filename=articles.json' + else: + flash('Export format not supported.', 'warning') + return redirect(redirect_url()) return response @app.route('/export_opml/', methods=['GET']) |