Django 1.8 and other updates.
[wolnelektury.git] / apps / 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 def replace_char(m):
22     char = m.group()
23     return char_map.get(char, '')
24
25 def sanitize_payment_title(value):
26     return re.sub('[^%s]{1}' % sane_in_payu_title, replace_char, unicode(value))