Browse Source
Budu zlobit a vysvětlení napíšu až do pull-requestu. Tady to IMHO čtu jen já :-P Pull-Request-URL: #26 (Adresa platí, pokud mě nikdo nepředběhne :-P)pull/26/head
Pavel "LEdoian" Turinsky
2 years ago
5 changed files with 77 additions and 1 deletions
@ -0,0 +1,9 @@ |
|||||
|
from django import template |
||||
|
register = template.Library() |
||||
|
|
||||
|
from personalni.utils import normalizuj_jmeno |
||||
|
import seminar.models as m # jen kvůli typové anotaci… |
||||
|
|
||||
|
@register.filter |
||||
|
def jmeno_jako_prefix(o: m.Osoba): |
||||
|
return normalizuj_jmeno(o).replace(' ', '_') |
@ -0,0 +1,11 @@ |
|||||
|
import seminar.models as m |
||||
|
from various.utils import bez_diakritiky_translate |
||||
|
import re |
||||
|
|
||||
|
def normalizuj_jmeno(o: m.Osoba) -> str: |
||||
|
# FIXME: Možná není potřeba vázat na model? |
||||
|
cele_jmeno = f'{o.jmeno} {o.prijmeni}' |
||||
|
cele_jmeno = cele_jmeno.translate(bez_diakritiky_translate) |
||||
|
cele_jmeno = re.sub(r'[^a-zA-Z- ]', '', cele_jmeno) |
||||
|
return cele_jmeno |
||||
|
|
@ -0,0 +1,33 @@ |
|||||
|
bez_diakritiky = ({} |
||||
|
# FIXME: funguje jen pro český a slovenský text, jinak jsou špatně |
||||
|
# transliterace. Potenciální řešení: |
||||
|
# https://stackoverflow.com/questions/517923/what-is-the-best-way-to-remove-accents-normalize-in-a-python-unicode-string |
||||
|
# (ale přidává to další závislosti…) |
||||
|
|
||||
|
# Tisknutelné ASCII |
||||
|
| {chr(a): chr(a) for a in range(32, 126+1)} |
||||
|
|
||||
|
# České, slovenské a blízké diakritiky a divnoznaky |
||||
|
| { x: 'a' for x in 'áÁäÄ'} |
||||
|
| { x: 'c' for x in 'čČ'} |
||||
|
| { x: 'd' for x in 'ďĎ'} |
||||
|
| { x: 'e' for x in 'éÉěĚëË'} |
||||
|
| { x: 'i' for x in 'íÍ'} |
||||
|
| { x: 'l' for x in 'ľĽĺĹ'} |
||||
|
| { x: 'n' for x in 'ňŇ'} |
||||
|
| { x: 'o' for x in 'óÓöÖôÔ'} |
||||
|
| { x: 'r' for x in 'řŘŕŔ'} |
||||
|
| { x: 's' for x in 'šŠßẞ'} |
||||
|
| { x: 't' for x in 'ťŤ'} |
||||
|
| { x: 'u' for x in 'úÚůŮ'} |
||||
|
| { x: 'y' for x in 'ýÝ'} |
||||
|
| { x: 'z' for x in 'žŽ'} |
||||
|
) |
||||
|
|
||||
|
# Tabulka pro str.translate |
||||
|
class _bez_diakritiky_translate: |
||||
|
def __getitem__(self, it): |
||||
|
return ord(bez_diakritiky.get(chr(it), None)) |
||||
|
bez_diakritiky_translate = _bez_diakritiky_translate() |
||||
|
|
||||
|
# TODO: testy? |
Loading…
Reference in new issue