-
Notifications
You must be signed in to change notification settings - Fork 76
/
TextValidators.py
44 lines (37 loc) · 1.42 KB
/
TextValidators.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
from zope.interface import implements
from Products.validation.interfaces.IValidator import IValidator
from Products.validation import validation
class LinkSpamValidator:
""" Validates whether or not a string has anything that seems link-like. For
these purposes, we're considering the following fragments to be linky:
"<a "
"www"
"http"
".com"
(See Products.PloneFormGen.config's stringValidators for an unfortunate
repeat of this logic.)
"""
implements(IValidator)
name = 'LinkSpamValidator'
def __init__(self, name, title='', description=''):
self.name = name
self.title = title or name
self.description = description
def __call__(self, value, *args, **kwargs):
# validation is optional and configured on the field
obj = kwargs.get('field')
if not obj:
return 1
if hasattr(obj,'validate_no_link_spam') and obj.validate_no_link_spam:
bad_signs = ("<a ",
"www.",
"http:",
".com",
)
value = value.lower()
for s in bad_signs:
if s in value:
return ("Validation failed(%(name)s): links are not allowed." %
{ 'name' : self.name, })
return 1
validation.register(LinkSpamValidator('isNotLinkSpam'))