Fundraising in PDF.
[wolnelektury.git] / src / funding / utils.py
1 # This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
3 #
4 import re
5 import string
6 from fnpdjango.utils.text import char_map
7
8 # PayU chokes on non-Polish diacritics.
9 # Punctuation is handled correctly and escaped as needed,
10 # with the notable exception of backslash.
11 sane_in_payu_title = re.escape(
12     string.ascii_uppercase +
13     string.ascii_lowercase +
14     'ąćęłńóśźżĄĆĘŁŃÓŚŹŻ' +
15     string.digits +
16     ' ' +
17     "".join(set(string.punctuation) - set('\\'))
18 )
19
20
21 def replace_char(m):
22     char = m.group()
23     return char_map.get(char, '')
24
25
26 def sanitize_payment_title(value):
27     return re.sub('[^%s]' % sane_in_payu_title, replace_char, str(value))