| | 146 | |
|---|
| | 147 | |
|---|
| | 148 | class TextAreaList(object): |
|---|
| | 149 | """ |
|---|
| | 150 | A text area that allows a list of values to be entered, one per line. Any |
|---|
| | 151 | empty lines are discarded. |
|---|
| | 152 | """ |
|---|
| | 153 | implements( iformal.IWidget ) |
|---|
| | 154 | |
|---|
| | 155 | cols = 48 |
|---|
| | 156 | rows = 6 |
|---|
| | 157 | |
|---|
| | 158 | def __init__(self, original, cols=None, rows=None): |
|---|
| | 159 | self.original = original |
|---|
| | 160 | if cols is not None: |
|---|
| | 161 | self.cols = cols |
|---|
| | 162 | if rows is not None: |
|---|
| | 163 | self.rows = rows |
|---|
| | 164 | |
|---|
| | 165 | def _renderTag(self, ctx, key, values, readonly): |
|---|
| | 166 | value = '\n'.join(values) |
|---|
| | 167 | tag=T.textarea(name=key, id=render_cssid(key), cols=self.cols, rows=self.rows)[value] |
|---|
| | 168 | if readonly: |
|---|
| | 169 | tag(class_='readonly', readonly='readonly') |
|---|
| | 170 | return tag |
|---|
| | 171 | |
|---|
| | 172 | def render(self, ctx, key, args, errors): |
|---|
| | 173 | converter = iformal.IStringConvertible(self.original.type) |
|---|
| | 174 | if errors: |
|---|
| | 175 | values = args.get(key, []) |
|---|
| | 176 | else: |
|---|
| | 177 | values = args.get(key) |
|---|
| | 178 | if values is not None: |
|---|
| | 179 | values = [converter.fromType(v) for v in values] |
|---|
| | 180 | else: |
|---|
| | 181 | values = [] |
|---|
| | 182 | return self._renderTag(ctx, key, values, False) |
|---|
| | 183 | |
|---|
| | 184 | def renderImmutable(self, ctx, key, args, errors): |
|---|
| | 185 | converter = iformal.IStringConvertible(self.original.type) |
|---|
| | 186 | values = args.get(key) |
|---|
| | 187 | if values is not None: |
|---|
| | 188 | values = [converter.fromType(v) for v in values] |
|---|
| | 189 | else: |
|---|
| | 190 | values = [] |
|---|
| | 191 | return self._renderTag(ctx, key, values, True) |
|---|
| | 192 | |
|---|
| | 193 | def processInput(self, ctx, key, args): |
|---|
| | 194 | # Get the whole string |
|---|
| | 195 | value = args.get(key, [''])[0].decode(util.getPOSTCharset(ctx)) |
|---|
| | 196 | # Split into lines |
|---|
| | 197 | values = value.splitlines() |
|---|
| | 198 | # Strip each line |
|---|
| | 199 | values = [v.strip() for v in values] |
|---|
| | 200 | # Discard empty lines |
|---|
| | 201 | values = [v for v in values if v] |
|---|
| | 202 | # Convert values to correct type |
|---|
| | 203 | converter = iformal.IStringConvertible(self.original.type) |
|---|
| | 204 | values = [converter.toType(v) for v in values] |
|---|
| | 205 | # Validate and return |
|---|
| | 206 | return self.original.validate(values) |
|---|