Total Complexity | 5 |
Total Lines | 86 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | from datetime import date |
||
2 | from html import escape as html_escape |
||
3 | |||
4 | import jinja2 |
||
5 | from jinja2 import Environment, Undefined |
||
6 | from jinja2.filters import do_mark_safe |
||
7 | from satcfdi import render as cfdi_render |
||
8 | from satcfdi.transform.helpers import iterate as h_iterate |
||
9 | import satdigitalinvoice.formatting_functions.common as common |
||
10 | |||
11 | from . import TEMPLATES_DIRECTORY |
||
12 | |||
13 | |||
14 | class FacturacionEnvironment(Environment): |
||
15 | @property |
||
16 | def filter(self): |
||
17 | def sub(f): |
||
18 | self.filters[f.__name__] = f |
||
19 | return f |
||
20 | |||
21 | return sub |
||
22 | |||
23 | @property |
||
24 | def glob(self): |
||
25 | def sub(f): |
||
26 | self.globals[f.__name__] = f |
||
27 | return f |
||
28 | |||
29 | return sub |
||
30 | |||
31 | def __init__(self): |
||
32 | super().__init__( |
||
33 | loader=jinja2.FileSystemLoader(searchpath=[TEMPLATES_DIRECTORY]), |
||
34 | autoescape=True, |
||
35 | trim_blocks=True, |
||
36 | lstrip_blocks=True, |
||
37 | undefined=jinja2.StrictUndefined, |
||
38 | ) |
||
39 | |||
40 | @self.glob |
||
41 | def iterate(v): |
||
42 | if isinstance(v, Undefined): |
||
43 | return v |
||
44 | return h_iterate(v) |
||
45 | |||
46 | @self.glob |
||
47 | def today(): |
||
48 | return date.today() |
||
49 | |||
50 | @self.filter |
||
51 | def bold(k): |
||
52 | return do_mark_safe( |
||
53 | tag(html_escape(str(k)), "b") |
||
54 | ) |
||
55 | |||
56 | @self.filter |
||
57 | def moneda_nacional(k): |
||
58 | return common.pesos(k) |
||
59 | |||
60 | @self.filter |
||
61 | def numero(k): |
||
62 | return common.numero(k) |
||
63 | |||
64 | @self.filter |
||
65 | def porcentaje(k): |
||
66 | return common.porcentaje(k) |
||
67 | |||
68 | @self.filter |
||
69 | def fecha(k): |
||
70 | return common.fecha(k) |
||
71 | |||
72 | @self.filter |
||
73 | def delta_tiempo(k): |
||
74 | return common.delta_tiempo(k) |
||
75 | |||
76 | @self.glob |
||
77 | def html_str(cdfi): |
||
78 | return cfdi_render.html_str(cdfi) |
||
79 | |||
80 | |||
81 | def tag(text, tag): |
||
82 | return '<' + tag + '>' + text + '</' + tag + '>' |
||
83 | |||
84 | |||
85 | facturacion_environment = FacturacionEnvironment() |
||
86 |