-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblueprint.py
31 lines (24 loc) · 972 Bytes
/
blueprint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from jinja2 import TemplateNotFound
from flask import Flask, Blueprint, request, jsonify, send_file, current_app
from tempfile import TemporaryDirectory
def make_blueprint(name, module):
ret = Blueprint(name, module.__name__)
template = module.template()
def compile_with_data(data):
with TemporaryDirectory() as tempdir:
current_app.logger.info('Working on %s' % tempdir)
pdf = template.compile(data, tempdir)
return send_file(pdf,
as_attachment=False,
attachment_filename='basic.pdf')
@ret.route('/')
def show_report():
if request.is_json:
data = request.get_json()
return compile_with_data(data)
else:
return jsonify(error=400, message='Body is not valid json'), 400
@ret.route('/sample')
def show_sample():
return compile_with_data(template.sample_data())
return ret