tex.py 5.19 KB
Newer Older
Gabriel Detraz's avatar
Gabriel Detraz committed
1
# -*- mode: python; coding: utf-8 -*-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017  Gabriel Détraz
# Copyright © 2017  Goulven Kermarec
# Copyright © 2017  Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 24 25 26
"""tex.py
Module in charge of rendering some LaTex templates.
Used to generated PDF invoice.
"""
27

28

29 30 31 32 33
import tempfile
from subprocess import Popen, PIPE
import os
from datetime import datetime

34
from django.db import models
chirac's avatar
chirac committed
35
from django.template.loader import get_template
36 37
from django.template import Context
from django.http import HttpResponse
chirac's avatar
chirac committed
38
from django.conf import settings
39
from django.utils.text import slugify
40 41 42
from django.utils.translation import ugettext_lazy as _

from re2o.mixins import AclMixin, RevMixin
43
from preferences.models import CotisationsOption
chirac's avatar
chirac committed
44 45 46 47 48 49 50


TEMP_PREFIX = getattr(settings, 'TEX_TEMP_PREFIX', 'render_tex-')
CACHE_PREFIX = getattr(settings, 'TEX_CACHE_PREFIX', 'render-tex')
CACHE_TIMEOUT = getattr(settings, 'TEX_CACHE_TIMEOUT', 86400)  # 1 day


51
def render_invoice(_request, ctx={}):
52 53 54 55
    """
    Render an invoice using some available information such as the current
    date, the user, the articles, the prices, ...
    """
56
    options, _ = CotisationsOption.objects.get_or_create()
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
57
    is_estimate = ctx.get('is_estimate', False)
58
    filename = '_'.join([
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
59
        'cost_estimate' if is_estimate else 'invoice',
60 61 62 63 64
        slugify(ctx.get('asso_name', "")),
        slugify(ctx.get('recipient_name', "")),
        str(ctx.get('DATE', datetime.now()).year),
        str(ctx.get('DATE', datetime.now()).month),
        str(ctx.get('DATE', datetime.now()).day),
65
    ])
66 67
    templatename = options.invoice_template.template.name.split('/')[-1]
    r = render_tex(_request, templatename, ctx)
68 69 70
    r['Content-Disposition'] = 'attachment; filename="{name}.pdf"'.format(
        name=filename
    )
71 72
    return r

73

Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
74 75 76 77 78 79 80 81
def render_voucher(_request, ctx={}):
    """
    Render a subscribtion voucher.
    """
    options, _ = CotisationsOption.objects.get_or_create()
    filename = '_'.join([
        'voucher',
        slugify(ctx.get('asso_name', "")),
82 83 84 85 86
        slugify(ctx.get('firstname', "")),
        slugify(ctx.get('lastname', "")),
        str(ctx.get('date_begin', datetime.now()).year),
        str(ctx.get('date_begin', datetime.now()).month),
        str(ctx.get('date_begin', datetime.now()).day),
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
87 88
    ])
    templatename = options.voucher_template.template.name.split('/')[-1]
89
    r = render_tex(_request, templatename, ctx)
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
90 91 92 93 94 95
    r['Content-Disposition'] = 'attachment; filename="{name}.pdf"'.format(
        name=filename
    )
    return r


96
def create_pdf(template, ctx={}):
97 98 99 100 101 102 103 104 105 106
    """Creates and returns a PDF from a LaTeX template using pdflatex.

    It create a temporary file for the PDF then read it to return its content.

    Args:
        template: Path to the LaTeX template.
        ctx: Dict with the context for rendering the template.

    Returns:
        The content of the temporary PDF file generated.
107
    """
Dalahro's avatar
Dalahro committed
108
    context = Context(ctx)
109
    template = get_template(template)
Dalahro's avatar
Dalahro committed
110
    rendered_tpl = template.render(context).encode('utf-8')
111

Dalahro's avatar
Dalahro committed
112
    with tempfile.TemporaryDirectory() as tempdir:
113
        for _ in range(2):
114 115 116 117 118 119 120
            with open("/var/www/re2o/out.log", "w") as f:
                process = Popen(
                    ['pdflatex', '-output-directory', tempdir],
                    stdin=PIPE,
                    stdout=f,#PIPE,
                )
                process.communicate(rendered_tpl)
Dalahro's avatar
Dalahro committed
121 122
        with open(os.path.join(tempdir, 'texput.pdf'), 'rb') as f:
            pdf = f.read()
123 124 125 126

    return pdf


127 128 129 130 131 132 133 134 135 136 137 138 139 140
def escape_chars(string):
    """Escape the '%' and the '€' signs to avoid messing with LaTeX"""
    if not isinstance(string, str):
        return string
    mapping = (
        ('€', r'\euro'),
        ('%', r'\%'),
    )
    r = str(string)
    for k, v in mapping:
        r = r.replace(k, v)
    return r


141
def render_tex(_request, template, ctx={}):
142 143 144
    """Creates a PDF from a LaTex templates using pdflatex.

    Calls `create_pdf` and send back an HTTP response for
145
    accessing this file.
146 147 148 149 150 151 152 153

    Args:
        _request: Unused, but allow using this function as a Django view.
        template: Path to the LaTeX template.
        ctx: Dict with the context for rendering the template.

    Returns:
        An HttpResponse with type `application/pdf` containing the PDF file.
154
    """
Hugo LEVY-FALK's avatar
Hugo LEVY-FALK committed
155
    pdf = create_pdf(template, ctx)
Dalahro's avatar
Dalahro committed
156 157 158
    r = HttpResponse(content_type='application/pdf')
    r.write(pdf)
    return r