From f7660befccccb25475e19963926b6200fcbb500b Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Sun, 8 Dec 2013 14:16:25 +0100 Subject: The name of the archive is composed of the current date. --- pyaggr3g470r/export.py | 19 ++++++++++++++----- pyaggr3g470r/views.py | 15 ++++++++------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/pyaggr3g470r/export.py b/pyaggr3g470r/export.py index 987be217..9d03f42b 100644 --- a/pyaggr3g470r/export.py +++ b/pyaggr3g470r/export.py @@ -34,8 +34,10 @@ __license__ = "GPLv3" # import os +import shutil import time import tarfile +from datetime import datetime import conf import utils @@ -134,13 +136,14 @@ def export_html(feeds): """ Export the articles given in parameter in a simple Webzine. """ + webzine_root = conf.PATH + "/pyaggr3g470r/var/export/webzine/" nb_articles = format(len(models.Article.objects()), ",d") index = HTML_HEADER("News archive") index += "

List of feeds

\n" index += """

%s articles.

\n\n" index += "

" + time.strftime("Generated on %d %b %Y at %H:%M.") + "

\n" index += HTML_FOOTER - with open(conf.PATH + "/pyaggr3g470r/var/export/webzine/" + "index.html", "w") as f: + with open(webzine_root + "index.html", "w") as f: f.write(index.encode("utf-8")) - with open(conf.PATH + "/pyaggr3g470r/var/export/webzine/" + "style.css", "w") as f: + with open(webzine_root + "style.css", "w") as f: f.write(CSS.encode("utf-8")) - with tarfile.open(conf.PATH + "/pyaggr3g470r/var/export.tar.gz", "w:gz") as tar: - tar.add(conf.PATH + "/pyaggr3g470r/var/export/webzine/", arcname=os.path.basename(conf.PATH + "/pyaggr3g470r/var/export/webzine/")) + archive_file_name = datetime.now().strftime('%Y-%m-%d') + '.tar.gz' + with tarfile.open(conf.PATH + "/pyaggr3g470r/var/export/" + archive_file_name, "w:gz") as tar: + tar.add(webzine_root, arcname=os.path.basename(webzine_root)) + + shutil.rmtree(webzine_root) + + with open(conf.PATH + "/pyaggr3g470r/var/export/" + archive_file_name, 'r') as export_file: + return export_file.read(), archive_file_name def export_txt(mongo_db): """ diff --git a/pyaggr3g470r/views.py b/pyaggr3g470r/views.py index a458c3eb..c30c9983 100644 --- a/pyaggr3g470r/views.py +++ b/pyaggr3g470r/views.py @@ -264,13 +264,14 @@ def export_articles(): Export all articles. """ user = models.User.objects(email=g.user.email).first() - export.export_html(user.feeds) - with open(conf.PATH + '/pyaggr3g470r/var/export.tar.gz', 'r') as export_file: - response = make_response(export_file.read()) - response.headers['Content-Type'] = 'application/x-compressed' - response.headers['Content-Disposition'] = 'attachment; filename=export.tar.gz' - return response - return redirect(url_for('management')) + try: + archive_file, archive_file_name = export.export_html(user.feeds) + except: + 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 + return response @app.route('/search/', methods=['GET']) @login_required -- cgit