Parent: [6d0994] (diff)

Child: [d32c7e] (diff)

Download this file

validators.py    64 lines (49 with data), 1.8 kB

 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
59
60
61
62
63
import json
from bson import ObjectId
import formencode as fe
from formencode import validators as fev
from . import helpers as h
class Ming(fev.FancyValidator):
def __init__(self, cls, **kw):
self.cls = cls
super(Ming, self).__init__(**kw)
def _to_python(self, value, state):
result = self.cls.query.get(_id=value)
if result is None:
try:
result = self.cls.query.get(_id=ObjectId(value))
except:
pass
return result
def _from_python(self, value, state):
return value._id
class UniqueOAuthApplicationName(fev.UnicodeString):
def _to_python(self, value, state):
from allura import model as M
app = M.OAuthConsumerToken.query.get(name=value)
if app is not None:
raise fe.Invalid('That name is already taken, please choose another', value, state)
return value
class NullValidator(fev.Validator):
def to_python(self, value, state):
return value
def from_python(self, value, state):
return value
def validate(self, value, state):
return value
class MaxBytesValidator(fev.FancyValidator):
max=255
def _to_python(self, value, state):
value = h.really_unicode(value or '').encode('utf-8')
if len(value) > self.max:
raise fe.Invalid("Please enter a value less than %s bytes long." % self.max, value, state)
return value
def from_python(self, value, state):
return h.really_unicode(value or '')
class JsonValidator(fev.FancyValidator):
def _to_python(self, value, state):
try:
json.loads(value)
except ValueError, e:
raise fe.Invalid('Invalid JSON: ' + str(e), value, state)
return value