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