Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
irfm
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
6
Issues
6
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
regardscitoyens
irfm
Commits
df6ec8f0
Commit
df6ec8f0
authored
May 08, 2017
by
Nicolas Joyard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Stockage PDFs générés et utilisation send_file
parent
584a1c5e
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
88 additions
and
58 deletions
+88
-58
.gitignore
.gitignore
+2
-1
README.md
README.md
+1
-0
irfm/cli.py
irfm/cli.py
+12
-0
irfm/routes/__init__.py
irfm/routes/__init__.py
+2
-2
irfm/routes/files.py
irfm/routes/files.py
+67
-0
irfm/routes/parlementaires.py
irfm/routes/parlementaires.py
+4
-19
irfm/routes/pdf.py
irfm/routes/pdf.py
+0
-36
No files found.
.gitignore
View file @
df6ec8f0
...
...
@@ -3,5 +3,6 @@
*.pyc
*~
data/secret.txt
data/preuve-envoi-*
data/files
data/uploads
README.md
View file @
df6ec8f0
...
...
@@ -32,6 +32,7 @@ $ irfm db upgrade
$
irfm import_etapes
$
irfm import_nd
$
irfm import_adresses
$
irfm clear_cache
```
## Développement
...
...
irfm/cli.py
View file @
df6ec8f0
...
...
@@ -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
))
irfm/routes/__init__.py
View file @
df6ec8f0
...
...
@@ -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
)
irfm/routes/files.py
0 → 100644
View file @
df6ec8f0
# -*- 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
]
)
irfm/routes/parlementaires.py
View file @
df6ec8f0
...
...
@@ -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
irfm/routes/pdf.py
deleted
100644 → 0
View file @
584a1c5e
# -*- 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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment