Commit df6ec8f0 authored by Nicolas Joyard's avatar Nicolas Joyard

Stockage PDFs générés et utilisation send_file

parent 584a1c5e
......@@ -3,5 +3,6 @@
*.pyc
*~
data/secret.txt
data/preuve-envoi-*
data/files
data/uploads
......@@ -32,6 +32,7 @@ $ irfm db upgrade
$ irfm import_etapes
$ irfm import_nd
$ irfm import_adresses
$ irfm clear_cache
```
## Développement
......
......@@ -2,6 +2,7 @@
from getpass import getpass
import hmac
import os
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
......@@ -54,3 +55,14 @@ def import_adresses():
"""Importe adresses postales des parlementaires"""
app.config.update(SQLALCHEMY_ECHO=False)
AdressesImporter(app).run()
@manager.command
def clear_cache():
"""Vide le cache des fichiers générés"""
files_root = os.path.join(app.config['DATA_DIR'], 'files')
if os.path.exists(files_root):
for item in os.listdir(files_root):
print('Suppression %s' % item)
os.unlink(os.path.join(files_root, item))
......@@ -4,9 +4,9 @@ from .context_processors import setup as setup_cp
from .filters import setup as setup_filters
from .admin import setup_routes as setup_admin
from .files import setup_routes as setup_files
from .home import setup_routes as setup_home
from .parlementaires import setup_routes as setup_parl
from .pdf import setup_routes as setup_pdf
from .session import setup_routes as setup_session
......@@ -16,5 +16,5 @@ def setup_routes(app):
setup_home(app)
setup_session(app)
setup_parl(app)
setup_pdf(app)
setup_files(app)
setup_admin(app)
# -*- coding: utf-8 -*-
from io import BytesIO
import os
from flask import make_response, render_template, send_file
from xhtml2pdf import pisa
from .util import not_found, slugify
from ..models import Action, Etape, Parlementaire
from ..models.constants import ETAPE_ENVOYE, EXTENSIONS
def setup_routes(app):
files_root = os.path.join(app.config['DATA_DIR'], 'files')
if not os.path.exists(files_root):
os.mkdir(files_root)
uploads_root = os.path.join(app.config['DATA_DIR'], 'uploads')
if not os.path.exists(uploads_root):
os.mkdir(uploads_root)
@app.route('/parlementaire/<id>/demande/<mode>', endpoint='demande_pdf')
def demande_pdf(id, mode='download'):
parl = Parlementaire.query.filter_by(id=id).first()
if not parl:
return not_found()
filename = 'demande-irfm-%s.pdf' % slugify(parl.nom_complet)
path = os.path.join(files_root, filename)
if not os.path.exists(filename):
html = render_template('demande.html.j2',
parlementaire=parl)
with open(path, 'wb') as pdf:
pisa.CreatePDF(html, pdf)
attach = None
if mode == 'download':
attach = filename
return send_file(
path,
mimetype='application/pdf',
as_attachment=bool(attach),
attachment_filename=attach
)
@app.route('/parlementaire/<id>/preuve-envoi', endpoint='preuve_envoi')
def preuve_envoi(id):
act = Action.query.join(Action.etape) \
.filter(Etape.ordre == ETAPE_ENVOYE,
Action.parlementaire_id == id) \
.first()
if not act or not act.attachment:
return not_found()
path = os.path.join(uploads_root, act.attachment)
ext = path.rsplit('.', 1)[1].lower()
return send_file(
path,
mimetype=EXTENSIONS[ext]
)
......@@ -175,7 +175,10 @@ def setup_routes(app):
ext = 'jpg'
filename = 'preuve-envoi-%s.%s' % (slugify(parl.nom_complet), ext)
file.save(os.path.join(app.config['DATA_DIR'], filename))
uploads_root = os.path.join(app.config['DATA_DIR'], 'uploads')
file.save(os.path.join(uploads_root, filename))
parl.etape = Etape.query.filter_by(ordre=ETAPE_ENVOYE).first()
......@@ -194,21 +197,3 @@ def setup_routes(app):
flash('Confirmation reçue, merci beaucoup !', category='success')
return redirect(url_for('parlementaire', id=id))
@app.route('/parlementaire/<id>/preuve-envoi', endpoint='preuve_envoi')
def preuve_envoi(id):
act = Action.query.join(Action.etape) \
.filter(Etape.ordre == ETAPE_ENVOYE,
Action.parlementaire_id == id) \
.first()
if not act or not act.attachment:
return not_found()
path = os.path.join(app.config['DATA_DIR'], act.attachment)
ext = path.rsplit('.', 1)[1].lower()
with open(path, 'rb') as preuve:
response = make_response(preuve.read())
response.headers['Content-Type'] = EXTENSIONS[ext]
return response
# -*- coding: utf-8 -*-
from io import BytesIO
from flask import make_response, render_template
from xhtml2pdf import pisa
from .util import not_found, slugify
from ..models import Parlementaire
def setup_routes(app):
@app.route('/parlementaire/<id>/demande/<mode>', endpoint='demande_pdf')
def demande_pdf(id, mode='download'):
parl = Parlementaire.query.filter_by(id=id).first()
if not parl:
return not_found()
slug = slugify(parl.nom_complet)
html = render_template('demande.html.j2',
parlementaire=parl)
pdf = BytesIO()
pisa.CreatePDF(html, pdf)
response = make_response(pdf.getvalue())
pdf.close()
response.headers['Content-Type'] = 'application/pdf'
if mode == 'download':
response.headers['Content-Disposition'] = \
'attachment; filename="courrier-irfm-%s.pdf"' % slug
return response
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment