Saturday, May 2, 2009

Custom Django Filters in Google Application Engine

Here is a very good description how can you use custom Django filters in Google Application Engine.

I've modified a filter found on Django Snippets to convert ISO8859-2 / ISO8859-1 strings into URL friendly strings:

match_non_alnum = re.compile("[^a-zA-Z0-9]+")

def dig_url_encode(string):
"""Converts common ISO88592/1 chars to ASCII and removes any special chars"""
r = {
u'á': u'a',
u'é': u'e',
u'í': u'i',
u'ó': u'o',
u'ö': u'o',
u'õ': u'o',
u'ő': u'o',
u'ú': u'u',
u'ü': u'u',
u'ű': u'u',
u'û': u'u',
u'Á': u'A',
u'É': u'E',
u'Í': u'I',
u'Ó': u'O',
u'Ö': u'O',
u'Ő': u'O',
u'Ú': u'U',
u'Ü': u'U',
u'Ű': u'U'
}
for key, value in r.items():
string = string.replace(key, value)
return match_non_alnum.sub(' ', string).strip().replace(' ', '_')

No comments:

Post a Comment