/
validators.py
58 lines (51 loc) · 1.88 KB
/
validators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import re
from django.core.exceptions import ValidationError
from jinja2 import Template
from django.utils.translation import ugettext_lazy as _
import ast
try:
from HTMLParser import HTMLParser
except ImportError:
from html.parser import HTMLParser # noqa
def validate_token(value):
if not re.match('[0-9]+:[-_a-zA-Z0-9]+', value):
raise ValidationError(_("%(value)s is not a valid token"), params={'value': value})
def validate_template(value):
try:
Template(value)
except:
raise ValidationError(_("Not correct jinja2 template: %(value)s"), params={'value': value})
def validate_pattern(value):
try:
re.compile(value)
except:
raise ValidationError(_("Not correct Regex: %(value)s"), params={'value': value})
def validate_telegram_keyboard(value):
try:
# TODO: just check array after rendering template. Some cases are not validated
# If template not valid let the other validator work
try:
template = Template(value)
except:
pass
else:
ast.literal_eval(template.render())
except:
raise ValidationError(_("Not correct keyboard: %(value)s. Check https://core.telegram.org/bots/api#replykeyboardmarkup"), params={'value': value})
def validate_telegram_text_html(value):
tags = ['b', 'i', 'a', 'code', 'pre']
found = []
msg = _("Not correct HTML for Telegram message. Check https://core.telegram.org/bots/api#html-style")
class TelegramHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
tags.index(tag)
found.append(tag)
def handle_endtag(self, tag):
found.pop(found.index(tag))
parser = TelegramHTMLParser()
try:
parser.feed(value)
if found:
raise ValidationError(msg)
except:
raise ValidationError(msg)