| 1 |
""" |
| 2 |
Form types. |
| 3 |
""" |
| 4 |
|
| 5 |
from zope.interface import implementer |
| 6 |
|
| 7 |
from . import iformal, validation |
| 8 |
|
| 9 |
|
| 10 |
@implementer( iformal.IType ) |
| 11 |
class Type(object): |
| 12 |
|
| 13 |
|
| 14 |
# Name of the instance |
| 15 |
name = None |
| 16 |
# Value to use if no value entered |
| 17 |
missing = None |
| 18 |
# Instance cannot be changed |
| 19 |
immutable = False |
| 20 |
# List of validators to test the value against |
| 21 |
validators = () |
| 22 |
|
| 23 |
def __init__(self, name=None, required=None, missing=None, immutable=None, validators=None): |
| 24 |
if name is not None: |
| 25 |
self.name = name |
| 26 |
if missing is not None: |
| 27 |
self.missing = missing |
| 28 |
if immutable is not None: |
| 29 |
self.immutable = immutable |
| 30 |
if validators is not None: |
| 31 |
self.validators = list(validators) |
| 32 |
else: |
| 33 |
self.validators = list(self.validators) |
| 34 |
if required is None: |
| 35 |
required = self.required |
| 36 |
if required: |
| 37 |
self.validators.append(validation.RequiredValidator()) |
| 38 |
|
| 39 |
def validate(self, value): |
| 40 |
for validator in self.validators: |
| 41 |
validator.validate(self, value) |
| 42 |
if value is None: |
| 43 |
value = self.missing |
| 44 |
return value |
| 45 |
|
| 46 |
def hasValidator(self, validatorType): |
| 47 |
""" |
| 48 |
Check for the existence of a validator of a specific type. |
| 49 |
""" |
| 50 |
for v in self.validators: |
| 51 |
if isinstance(v, validatorType): |
| 52 |
return True |
| 53 |
return False |
| 54 |
|
| 55 |
def required(): |
| 56 |
def get(self): |
| 57 |
return self.hasValidator(validation.RequiredValidator) |
| 58 |
return get, |
| 59 |
required = property(*required()) |
| 60 |
|
| 61 |
|
| 62 |
class String(Type): |
| 63 |
|
| 64 |
# Strip the value before validation |
| 65 |
strip = False |
| 66 |
|
| 67 |
def __init__(self, **k): |
| 68 |
strip = k.pop('strip', None) |
| 69 |
if strip is not None: |
| 70 |
self.strip = strip |
| 71 |
super(String, self).__init__(**k) |
| 72 |
|
| 73 |
def validate(self, value): |
| 74 |
if value is not None and self.strip: |
| 75 |
value = value.strip() |
| 76 |
if not value: |
| 77 |
value = None |
| 78 |
return super(String, self).validate(value) |
| 79 |
|
| 80 |
|
| 81 |
class Integer(Type): |
| 82 |
pass |
| 83 |
|
| 84 |
|
| 85 |
class Float(Type): |
| 86 |
pass |
| 87 |
|
| 88 |
|
| 89 |
class Boolean(Type): |
| 90 |
pass |
| 91 |
|
| 92 |
|
| 93 |
class Date(Type): |
| 94 |
pass |
| 95 |
|
| 96 |
|
| 97 |
class Time(Type): |
| 98 |
pass |
| 99 |
|
| 100 |
|
| 101 |
class Decimal(Type): |
| 102 |
""" |
| 103 |
Forms type for Python 2.4's decimal.Decimal type. |
| 104 |
""" |
| 105 |
|
| 106 |
|
| 107 |
class Sequence(Type): |
| 108 |
|
| 109 |
# Type of items in the sequence |
| 110 |
type = None |
| 111 |
|
| 112 |
def __init__(self, type=None, **k): |
| 113 |
super(Sequence, self).__init__(**k) |
| 114 |
if type is not None: |
| 115 |
self.type = type |
| 116 |
|
| 117 |
def validate(self, value): |
| 118 |
# Map empty sequence to None |
| 119 |
if not value: |
| 120 |
value = None |
| 121 |
return super(Sequence, self).validate(value) |
| 122 |
|
| 123 |
|
| 124 |
class File(Type): |
| 125 |
pass |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
class RichText: |
| 130 |
""" |
| 131 |
A data structure for the RichTextType to use |
| 132 |
""" |
| 133 |
|
| 134 |
def __init__(self,type,value): |
| 135 |
self.type = type |
| 136 |
self.value = value |
| 137 |
|
| 138 |
def __repr__(self): |
| 139 |
shortvalue = self.value[:30] |
| 140 |
if len(self.value) > 30: |
| 141 |
shortvalue += '...' |
| 142 |
return '<formal.types.RichText instance, (%s,"%s")>'%(self.type,shortvalue) |
| 143 |
|
| 144 |
|
| 145 |
class RichTextType(Type): |
| 146 |
"""Forms type used for rich text""" |
| 147 |
|
| 148 |
def __init__(self, **kwds): |
| 149 |
strip = kwds.pop('strip', None) |
| 150 |
super(RichTextType, self).__init__(**kwds) |
| 151 |
self.strip = strip or False |
| 152 |
|
| 153 |
|
| 154 |
def validate(self, value): |
| 155 |
# For the moment all the validation is against the content |
| 156 |
|
| 157 |
if self.strip: |
| 158 |
value.value = value.value.strip() |
| 159 |
if not value.value: |
| 160 |
value=None |
| 161 |
|
| 162 |
return super(RichTextType, self).validate(value) |
| 163 |
|
| 164 |
|
| 165 |
__all__ = [ |
| 166 |
'Boolean', 'Date', 'File', 'Float', 'Integer', 'Sequence', 'String', 'Time', 'RichTextType', 'RichText', 'Decimal', |
| 167 |
] |