diff options
-rw-r--r-- | pyaggr3g470r/export.py | 19 | ||||
-rw-r--r-- | 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 += "<h1>List of feeds</h1>\n" index += """<p>%s articles.</p>\n<ul>\n""" % (nb_articles,) for feed in feeds: # creates a folder for each stream - feed_folder = conf.PATH + "/pyaggr3g470r/var/export/webzine/" + str(feed.oid) + feed_folder = webzine_root + str(feed.oid) try: os.makedirs(feed_folder) except OSError: @@ -180,13 +183,19 @@ def export_html(feeds): index += "</ul>\n" index += "<p>" + time.strftime("Generated on %d %b %Y at %H:%M.") + "</p>\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 |