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.5 KiB
62 lines
1.5 KiB
# -*- coding: utf-8 -*-
|
|
import unicodedata as ud
|
|
from django.core.exceptions import ValidationError
|
|
import pytils, re
|
|
|
|
def is_positive_integer(data,
|
|
msg='Введите правильное значение'):
|
|
"""
|
|
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 slugify import slugify
|
|
|
|
def translit_with_separator(string, separator='-'):
|
|
|
|
return slugify(string, to_lower=True)
|
|
"""
|
|
Trsanslit string and replace "bad" symbols for separator
|
|
|
|
usage: translit_with_separator('введите, слово', '_') return 'vvedite_slovo'
|
|
|
|
"""
|
|
|
|
#make string unicode
|
|
string = string.strip()
|
|
string = u'%s'%string
|
|
string = string.replace(u'\u200e', '')
|
|
|
|
#make string translit
|
|
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())
|
|
|