I wrote a piece of code for correct slugification:

_MAP = {
    # LATIN
    u'À': 'A', u'Á': 'A', u'Â': 'A', u'Ã': 'A', u'Ä': 'A', u'Å': 'A',
u'Æ': 'AE', u'Ç':'C',
    u'È': 'E', u'É': 'E', u'Ê': 'E', u'Ë': 'E', u'Ì': 'I', u'Í': 'I',
u'Î': 'I',
    u'Ï': 'I', u'Ð': 'D', u'Ñ': 'N', u'Ò': 'O', u'Ó': 'O', u'Ô': 'O',
u'Õ': 'O', u'Ö':'O',
    u'Ő': 'O', u'Ø': 'O', u'Ù': 'U', u'Ú': 'U', u'Û': 'U', u'Ü': 'U',
u'Ű': 'U',
    u'Ý': 'Y', u'Þ': 'TH', u'ß': 'ss', u'à':'a', u'á':'a', u'â': 'a',
u'ã': 'a', u'ä':'a',
    u'å': 'a', u'æ': 'ae', u'ç': 'c', u'è': 'e', u'é': 'e', u'ê': 'e',
u'ë': 'e',
    u'ì': 'i', u'í': 'i', u'î': 'i', u'ï': 'i', u'ð': 'd', u'ñ': 'n',
u'ò': 'o', u'ó':'o',
    u'ô': 'o', u'õ': 'o', u'ö': 'o', u'ő': 'o', u'ø': 'o', u'ù': 'u',
u'ú': 'u',
    u'û': 'u', u'ü': 'u', u'ű': 'u', u'ý': 'y', u'þ': 'th', u'ÿ': 'y',
    # LATIN_SYMBOLS
    u'©':'(c)',
    # GREEK
    u'α':'a', u'β':'b', u'γ':'g', u'δ':'d', u'ε':'e', u'ζ':'z',
u'η':'h', u'θ':'8',
    u'ι':'i', u'κ':'k', u'λ':'l', u'μ':'m', u'ν':'n', u'ξ':'3',
u'ο':'o', u'π':'p',
    u'ρ':'r', u'σ':'s', u'τ':'t', u'υ':'y', u'φ':'f', u'χ':'x',
u'ψ':'ps', u'ω':'w',
    u'ά':'a', u'έ':'e', u'ί':'i', u'ό':'o', u'ύ':'y', u'ή':'h',
u'ώ':'w', u'ς':'s',
    u'ϊ':'i', u'ΰ':'y', u'ϋ':'y', u'ΐ':'i',
    u'Α':'A', u'Β':'B', u'Γ':'G', u'Δ':'D', u'Ε':'E', u'Ζ':'Z',
u'Η':'H', u'Θ':'8',
    u'Ι':'I', u'Κ':'K', u'Λ':'L', u'Μ':'M', u'Ν':'N', u'Ξ':'3',
u'Ο':'O', u'Π':'P',
    u'Ρ':'R', u'Σ':'S', u'Τ':'T', u'Υ':'Y', u'Φ':'F', u'Χ':'X',
u'Ψ':'PS', u'Ω':'W',
    u'Ά':'A', u'Έ':'E', u'Ί':'I', u'Ό':'O', u'Ύ':'Y', u'Ή':'H',
u'Ώ':'W', u'Ϊ':'I', u'Ϋ':'Y',
    # TURKISH
    u'ş':'s', u'Ş':'S', u'ı':'i', u'İ':'I', u'ç':'c', u'Ç':'C',
u'ü':'u', u'Ü':'U',
    u'ö':'o', u'Ö':'O', u'ğ':'g', u'Ğ':'G',
    # RUSSIAN
    u'а':'a', u'б':'b', u'в':'v', u'г':'g', u'д':'d', u'е':'e',
u'ё':'yo', u'ж':'zh',
    u'з':'z', u'и':'i', u'й':'j', u'к':'k', u'л':'l', u'м':'m',
u'н':'n', u'о':'o',
    u'п':'p', u'р':'r', u'с':'s', u'т':'t', u'у':'u', u'ф':'f',
u'х':'h', u'ц':'c',
    u'ч':'ch', u'ш':'sh', u'щ':'sh', u'ъ':'', u'ы':'y', u'ь':'',
u'э':'e', u'ю':'yu', u'я':'ya',
    u'А':'A', u'Б':'B', u'В':'V', u'Г':'G', u'Д':'D', u'Е':'E',
u'Ё':'Yo', u'Ж':'Zh',
    u'З':'Z', u'И':'I', u'Й':'J', u'К':'K', u'Л':'L', u'М':'M',
u'Н':'N', u'О':'O',
    u'П':'P', u'Р':'R', u'С':'S', u'Т':'T', u'У':'U', u'Ф':'F',
u'Х':'H', u'Ц':'C',
    u'Ч':'Ch', u'Ш':'Sh', u'Щ':'Sh', u'Ъ':'', u'Ы':'Y', u'Ь':'',
u'Э':'E', u'Ю':'Yu', u'Я':'Ya',
    # UKRAINIAN
    u'Є':'Ye', u'І':'I', u'Ї':'Yi', u'Ґ':'G', u'є':'ye', u'і':'i',
u'ї':'yi', u'ґ':'g',
    # CZECH
    u'č':'c', u'ď':'d', u'ě':'e', u'ň':'n', u'ř':'r', u'š':'s',
u'ť':'t', u'ů':'u',
    u'ž':'z', u'Č':'C', u'Ď':'D', u'Ě':'E', u'Ň':'N', u'Ř':'R',
u'Š':'S', u'Ť':'T', u'Ů':'U', u'Ž':'Z',
    # POLISH
    u'ą':'a', u'ć':'c', u'ę':'e', u'ł':'l', u'ń':'n', u'ó':'o',
u'ś':'s', u'ź':'z',
    u'ż':'z', u'Ą':'A', u'Ć':'C', u'Ę':'e', u'Ł':'L', u'Ń':'N',
u'Ó':'o', u'Ś':'S',
    u'Ź':'Z', u'Ż':'Z',
    # LATVIAN
    u'ā':'a', u'č':'c', u'ē':'e', u'ģ':'g', u'ī':'i', u'ķ':'k',
u'ļ':'l', u'ņ':'n',
    u'š':'s', u'ū':'u', u'ž':'z', u'Ā':'A', u'Č':'C', u'Ē':'E',
u'Ģ':'G', u'Ī':'i',
    u'Ķ':'k', u'Ļ':'L', u'Ņ':'N', u'Š':'S', u'Ū':'u', u'Ž':'Z'
}

def slugify(name):
    u"""
    >>> slugify(u'Žabovitá zmiešaná kaša s.r.o')
    u'zabovita-zmiesana-kasa-sro'
    """
    for key, value in _MAP.iteritems():
        name = name.replace(key, value)
    return defaultfilters.slugify(name)

I know the code is slow and it can be written better, but that is not
the point. When I run the manage.py test, it work and everything is
ok, but if I change some letter, for example the excpected string from
u'zabovita-zmiesana-kasa-sro' to u'Zabovita-zmiesana-kasa-sro' I got
an error message:

FAIL: Doctest: bookit.businesses.models.slugify
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Library/Python/2.5/site-packages/django/test/_doctest.py",
line 2180, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: <unprintable AssertionError object>

Seems to be my Python interepreter doesn't recognize unicode
characters. After a while browsing I found some solutions. One of them
is modify the Django software and correct the bug for myself, but I'm
not sure it is the way I want to go. Second is not use Django test
framework and run tests in python like this:

if __name__ == "__main__":
    import sys, codecs
    sys.stdout = codecs.getwriter('utf8')(sys.stdout)

    print u"Žabovitá zmiešaná kaša"
    import doctest
    doctest.testmod()

Finally, I found this answer on the internet:

The standard solution for doctests is to never use print in these
cases.
Rather, just display the string value, which will appear with hex
encoding for any non-ASCII characaters. The output is then completely
safe from decoding problems. This is what we do throughout the Django
internal tests whenever the output is likely to produce non-ASCII
characters.

Regards,
Malcolm

My question is. Can anybody explain, what does it mean? How should I
rewrite my doctests in above way? How this piece of code should be?

def slugify(name):
    u"""
    >>> slugify(u'Žabovitá zmiešaná kaša s.r.o')
    u'zabovita-zmiesana-kasa-sro'
    """
    for key, value in _MAP.iteritems():
        name = name.replace(key, value)
    return defaultfilters.slugify(name)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to