def deserialize(self, node, value, formstruct=None): if value in (null, None): return null if value == u"": value = [] if isinstance(value, basestring): value = value.split(self.lb) if self.remove_empty: try: # remove empty lines while True: value.remove(u"") except: pass elif not isinstance(value, (list, tuple)): raise Invalid(node, _("${value} is not iterable", mapping={"value": value})) value = list(value) if not value and not self.allow_empty: raise Invalid(node, _("Required")) return value
def deserialize(self, node, value, formstruct=None): if value in (null, None): return null if value == u"": value = [] if isinstance(value, basestring): value = value.split(self.lb) if self.remove_empty: try: # remove empty lines while True: value.remove(u"") except: pass elif not isinstance(value, (list, tuple)): raise Invalid( node, _('${value} is not iterable', mapping={'value': value})) value = list(value) if not value and not self.allow_empty: raise Invalid(node, _('Required')) return value
class CheckedPasswordWidget(CheckedInputWidget): """ Renders two password input fields: 'password' and 'confirm'. Validates that the 'password' value matches the 'confirm' value. **Attributes/Arguments** template The template name used to render the widget. Default: ``checked_password``. size The ``size`` attribute of the password input field (default: ``None``). update The ``update`` attribute prevents rendering of existing passwords. Instead ``placeholder`` is used. """ template = 'checked_password' mismatch_message = _('Password did not match confirm') size = None update = False placeholder = '' def serialize(self, field, cstruct): if self.update and cstruct in (null, None, ''): cstruct = confirm = self.placeholder elif cstruct in (null, None): cstruct = confirm = '' else: confirm = getattr(field, '%s-confirm' % (field.name,), '') template = self.template return field.renderer(template, field=field, cstruct=cstruct, confirm=confirm, subject=self.subject, confirm_subject=self.confirm_subject, ) def deserialize(self, field, pstruct, formstruct=None): if pstruct in (null, self.placeholder): return null value = pstruct confirm = formstruct.get('%s-confirm' % (field.name,)) or '' setattr(field, '%s-confirm' % (field.name,), confirm) if (value or confirm) and (value != confirm): raise Invalid(field.schema, self.mismatch_message, value) if not value: return null return value
def deserialize(self, field, pstruct, formstruct=None): if formstruct and 'year' in formstruct and 'month' in formstruct and 'day' in formstruct: year = formstruct['year'].strip() month = formstruct['month'].strip() day = formstruct['day'].strip() if (not year and not month and not day): return null if self.assume_y2k and len(year) == 2: year = '20' + year result = '-'.join([year, month, day]) if (not year or not month or not day): raise Invalid(field.schema, _('Incomplete date'), result) elif pstruct is null: return null else: result = pstruct return result
def serialize(self, node, value): """ Serialize a dictionary representing partial file information to a dictionary containing information expected by a file upload widget. The file data dictionary passed as ``value`` to this ``serialize`` method *must* include: filename Filename of this file (not a full filesystem path, just the filename itself). uid Unique string id for this file. Needs to be unique enough to disambiguate it from other files that may use the same temporary storage mechanism before a successful validation, and must be adequate for the calling code to reidentify it after deserialization. A fully populated dictionary *may* also include the following values: fp File-like object representing this file's content or ``None``. ``None`` indicates this file has already been committed to permanent storage. When serializing a 'committed' file, the ``fp`` value should ideally not be passed or it should be passed as ``None``; ``None`` as an ``fp`` value is a signifier to the file upload widget that the file data has already been committed. Using ``None`` as an ``fp`` value helps prevent unnecessary data copies to temporary storage when a form is rendered, however its use requires cooperation from the calling code; in particular, the calling code must be willing to translate a ``None`` ``fp`` value returned from a deserialization into the file data via the ``uid`` in the deserialization. mimetype File content type (e.g. ``application/octet-stream``). size File content length (integer). preview_url URL which provides an image preview of this file's data. If a ``size`` is not provided, the widget will have no access to size display data. If ``preview_url`` is not provided, the widget will not be able to show a file preview. If ``mimetype`` is not provided, the widget will not be able to display mimetype information. """ if value in (null, None, ""): return null if not hasattr(value, 'get'): mapping = {'value': repr(value)} raise Invalid(node, _('${value} is not a dictionary', mapping=mapping)) for n in ('filename', ): if not n in value: mapping = {'value': repr(value), 'key': n} raise Invalid(node, _('${value} has no ${key} key', mapping=mapping)) if isinstance(value, basestring): # from path file = File() file.fromPath(value) return file elif not isinstance(value, File): # dictionary or similar file = File() file.filename = value.get('filename', '') file.file = value.get('file') file.filekey = node.name file.uid = value.get('uid', node.name) file.mime = value.get('mimetype') file.size = value.get('size') file.tempfile = True return file return value
def serialize(self, node, value): """ Serialize a dictionary representing partial file information to a dictionary containing information expected by a file upload widget. The file data dictionary passed as ``value`` to this ``serialize`` method *must* include: filename Filename of this file (not a full filesystem path, just the filename itself). uid Unique string id for this file. Needs to be unique enough to disambiguate it from other files that may use the same temporary storage mechanism before a successful validation, and must be adequate for the calling code to reidentify it after deserialization. A fully populated dictionary *may* also include the following values: fp File-like object representing this file's content or ``None``. ``None`` indicates this file has already been committed to permanent storage. When serializing a 'committed' file, the ``fp`` value should ideally not be passed or it should be passed as ``None``; ``None`` as an ``fp`` value is a signifier to the file upload widget that the file data has already been committed. Using ``None`` as an ``fp`` value helps prevent unnecessary data copies to temporary storage when a form is rendered, however its use requires cooperation from the calling code; in particular, the calling code must be willing to translate a ``None`` ``fp`` value returned from a deserialization into the file data via the ``uid`` in the deserialization. mimetype File content type (e.g. ``application/octet-stream``). size File content length (integer). preview_url URL which provides an image preview of this file's data. If a ``size`` is not provided, the widget will have no access to size display data. If ``preview_url`` is not provided, the widget will not be able to show a file preview. If ``mimetype`` is not provided, the widget will not be able to display mimetype information. """ if value in (null, None, ""): return null if not hasattr(value, "get"): mapping = {"value": repr(value)} raise Invalid(node, _("${value} is not a dictionary", mapping=mapping)) for n in ("filename",): if not n in value: mapping = {"value": repr(value), "key": n} raise Invalid(node, _("${value} has no ${key} key", mapping=mapping)) if isinstance(value, basestring): # from path file = File() file.fromPath(value) return file elif not isinstance(value, File): # dictionary or similar file = File() file.filename = value.get("filename", "") file.file = value.get("file") file.filekey = node.name file.uid = value.get("uid", node.name) file.mime = value.get("mimetype") file.size = value.get("size") file.tempfile = True return file return value
class CheckedInputWidget(Widget): """ Renders two text input fields: 'value' and 'confirm'. Validates that the 'value' value matches the 'confirm' value. **Attributes/Arguments** template The template name used to render the widget. Default: ``checked_input``. size The ``size`` attribute of the input fields (default: ``None``, default browser size). mismatch_message The message to be displayed when the value in the primary field doesn't match the value in the confirm field. mask A :term:`jquery.maskedinput` input mask, as a string. Both input fields will use this mask. a - Represents an alpha character (A-Z,a-z) 9 - Represents a numeric character (0-9) * - Represents an alphanumeric character (A-Z,a-z,0-9) All other characters in the mask will be considered mask literals. Example masks: Date: 99/99/9999 US Phone: (999) 999-9999 US SSN: 999-99-9999 When this option is used, the :term:`jquery.maskedinput` library must be loaded into the page serving the form for the mask argument to have any effect. See :ref:`masked_input`. mask_placeholder The placeholder for required nonliteral elements when a mask is used. Default: ``_`` (underscore). """ template = 'checked_input' size = None mismatch_message = _('Fields did not match') subject = _('Value') confirm_subject = _('Confirm Value') mask = None mask_placeholder = "_" requirements = ( ('jquery.maskedinput', None), ) def serialize(self, field, cstruct): if cstruct in (null, None): cstruct = '' confirm = getattr(field, '%s-confirm' % (field.name,), '') template = self.template return field.renderer(template, field=field, cstruct=cstruct, confirm=confirm, subject=self.subject, confirm_subject=self.confirm_subject, ) def deserialize(self, field, pstruct, formstruct=None): if pstruct is null: return null value = pstruct confirm = '' if formstruct: confirm = formstruct.get('%s-confirm' % (field.name,)) or '' setattr(field, '%s-confirm' % (field.name,), confirm) if (value or confirm) and (value != confirm): raise Invalid(field.schema, self.mismatch_message, value) if not value: return null return value