You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.7 KiB
62 lines
1.7 KiB
# -*- coding: utf-8 -*-
|
|
import unicodedata as ud
|
|
from django.core.exceptions import ValidationError
|
|
import pytils, re
|
|
from django.utils.translation import ugettext as _
|
|
|
|
def is_positive_integer(data,
|
|
msg=_(u'Введите правильное значение')):
|
|
"""
|
|
function checking if data positive integer
|
|
"""
|
|
if not data:
|
|
return
|
|
elif data.isdigit() and int(data) > 0:
|
|
return int(data)
|
|
else:
|
|
raise ValidationError(msg)
|
|
|
|
from django.utils.encoding import smart_str, smart_unicode
|
|
import unicodedata
|
|
def translit_with_separator(string, separator='-'):
|
|
"""
|
|
Trsanslit string and replace "bad" symbols for separator
|
|
|
|
usage: translit_with_separator('введите, слово', '_') return 'vvedite_slovo'
|
|
|
|
"""
|
|
|
|
#make string unicode
|
|
string = string.strip()
|
|
string = smart_unicode(string)
|
|
|
|
#make string translit
|
|
try:
|
|
st = pytils.translit.translify(string)
|
|
except ValueError:
|
|
string = unicodedata.normalize('NFKD', string).encode('ascii','ignore')
|
|
st = pytils.translit.translify(string)
|
|
|
|
#replace "bad" symbols for '-'symbol
|
|
st = st.replace('.', '')
|
|
st = re.sub('[^\w\-_\.]', separator, st)
|
|
#delete dublicating separators
|
|
st = re.sub('%s+'%separator, separator, st)
|
|
# delete if last symbol == separator
|
|
if st[-1] == separator:
|
|
st = st[:-1]
|
|
|
|
|
|
return st.lower()
|
|
|
|
|
|
def is_latin_char(uchr):
|
|
latin_letters= {}
|
|
try: return latin_letters[uchr]
|
|
except KeyError:
|
|
return latin_letters.setdefault(uchr, 'LATIN' in ud.name(uchr))
|
|
|
|
def is_latin(unistr):
|
|
return all(is_latin_char(uchr)
|
|
for uchr in unistr
|
|
if uchr.isalpha())
|
|
|