def deserialize(self, node, value): if value is colander.null: return colander.null if not hasattr(value, '__iter__'): raise colander.Invalid( node, _('${value} is not iterable', mapping={'value': value})) value = set(value) if not value and not self.allow_empty: raise colander.Invalid(node, _('Required')) return value
def deserialize(self, node, value): if value in (colander.null, None, ""): return colander.null if not hasattr(value, '__iter__'): raise colander.Invalid( node, _('${value} is not iterable', mapping={'value':value}) ) value = set(value) if not value and not self.allow_empty: raise colander.Invalid(node, _('Required')) return value
def serialize(self, field, cstruct, readonly=False): """ Overrided serialize method """ if (self.render_initial_item and self.min_len is None): # This is for compat only: ``render_initial_item=True`` should # now be spelled as ``min_len = 1`` self.min_len = 1 if cstruct in (colander.null, None): if self.min_len is not None: cstruct = [colander.null] * self.min_len else: cstruct = [] cstructlen = len(cstruct) if self.min_len is not None and (cstructlen < self.min_len): cstruct = list(cstruct) + \ ([colander.null] * (self.min_len - cstructlen)) item_field = field.children[0] if getattr(field, 'sequence_fields', None): # this serialization is being performed as a result of a # validation failure (``deserialize`` was previously run) assert(len(cstruct) == len(field.sequence_fields)) subfields = list(zip(cstruct, field.sequence_fields)) else: # this serialization is being performed as a result of a # first-time rendering subfields = [] for val in cstruct: if val is colander.null: val = item_field.schema.serialize(val) subfields.append((val, item_field.clone())) template = readonly and self.readonly_template or self.template translate = field.translate add_template_mapping = dict( subitem_title=translate(item_field.title), subitem_description=translate(item_field.description), subitem_name=item_field.name) if isinstance(self.add_subitem_text_template, TranslationString): add_subitem_text = self.add_subitem_text_template % \ add_template_mapping else: add_subitem_text = _(self.add_subitem_text_template, mapping=add_template_mapping) return field.renderer(template, field=field, cstruct=cstruct, subfields=subfields, item_field=item_field, add_subitem_text=add_subitem_text)
def serialize(self, field, cstruct, readonly=False): """ Overrided serialize method """ if (self.render_initial_item and self.min_len is None): # This is for compat only: ``render_initial_item=True`` should # now be spelled as ``min_len = 1`` self.min_len = 1 if cstruct in (colander.null, None): if self.min_len is not None: cstruct = [colander.null] * self.min_len else: cstruct = [] cstructlen = len(cstruct) if self.min_len is not None and (cstructlen < self.min_len): cstruct = list(cstruct) + \ ([colander.null] * (self.min_len - cstructlen)) item_field = field.children[0] if getattr(field, 'sequence_fields', None): # this serialization is being performed as a result of a # validation failure (``deserialize`` was previously run) assert (len(cstruct) == len(field.sequence_fields)) subfields = list(zip(cstruct, field.sequence_fields)) else: # this serialization is being performed as a result of a # first-time rendering subfields = [] for val in cstruct: if val is colander.null: val = item_field.schema.serialize(val) subfields.append((val, item_field.clone())) template = readonly and self.readonly_template or self.template translate = field.translate add_template_mapping = dict(subitem_title=translate(item_field.title), subitem_description=translate( item_field.description), subitem_name=item_field.name) if isinstance(self.add_subitem_text_template, TranslationString): add_subitem_text = self.add_subitem_text_template % \ add_template_mapping else: add_subitem_text = _(self.add_subitem_text_template, mapping=add_template_mapping) return field.renderer(template, field=field, cstruct=cstruct, subfields=subfields, item_field=item_field, add_subitem_text=add_subitem_text)
class Schema(colander.Schema): date_time = colander.SchemaNode( colander.DateTime(), validator=colander.Range( min=datetime.datetime(2010, 5, 5, 12, 30, tzinfo=iso8601.Utc()), min_err=_( '${val} is earlier than earliest datetime ${min}')), widget=DateTimeInputWidget())
def serialize(self, field, cstruct, readonly=False): if self.render_initial_item and self.min_len is None: # This is for compat only: ``render_initial_item=True`` should # now be spelled as ``min_len = 1`` self.min_len = 1 if cstruct in (null, None): if self.min_len is not None: cstruct = [null] * self.min_len else: cstruct = [] cstructlen = len(cstruct) if self.min_len is not None and (cstructlen < self.min_len): cstruct = list(cstruct) + ([null] * (self.min_len - cstructlen)) item_field = field.children[0] if getattr(field, "sequence_fields", None): # this serialization is being performed as a result of a # validation failure (``deserialize`` was previously run) assert len(cstruct) == len(field.sequence_fields) subfields = list(zip(cstruct, field.sequence_fields)) else: # this serialization is being performed as a result of a # first-time rendering subfields = [(val, item_field.clone()) for val in cstruct] template = readonly and self.readonly_template or self.template translate = field.translate add_template_mapping = dict( subitem_title=translate(item_field.title), subitem_description=translate(item_field.description), subitem_name=item_field.name, ) add_subitem_text = _(self.add_subitem_text_template, mapping=add_template_mapping) return field.renderer( template, field=field, cstruct=cstruct, subfields=subfields, item_field=item_field, add_subitem_text=add_subitem_text, )
def __init__(self, tmpstore, request, **kw): deform.widget.Widget.__init__(self, **kw) options = kw['options'] if 'options' in kw else {} #jcrop configuration self.options = { 'sizeWarning': options.get('sizeWarning', 'True'), 'ratioWidth': options.get('ratioWidth', ''), 'ratioHeight': options.get('ratioHeight', ''), 'modalButtonLabel': options.get('modalButtonLabel', _('Upload image')), 'changeButtonText': options.get('changeButtonText', _('Change Image')), 'sizeAlertMessage': options.get('sizeAlertMessage', _('Warning: The area selected is too small. Min size:')), 'sizeErrorMessage': options.get('sizeErrorMessage', _("Image doesn't meet the minimum size requirements ")), 'modalSaveCropMessage': options.get('modalSaveCropMessage', _('Set image')), 'modalCloseCropMessage': options.get('modalCloseCropMessage', _('Close')), 'uploadingMessage': options.get('uploadingMessage', _('Uploading your image')), 'fileUploadLabel': options.get('fileUploadLabel', _('Select image from your computer')), } self.request = request
def deserialize(self, field, pstruct): if pstruct is null: return null else: year = pstruct['year'].strip() month = pstruct['month'].strip() day = pstruct['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'), result) return result
def deserialize(self, field, pstruct): if pstruct is null: return null else: _date = pstruct["date"].strip() _time = pstruct["time"].strip() if not _date and not _time: return null if not _date: raise Invalid(field.schema, _("Incomplete date"), "") if not _time: _time = "00:00:00" result = " ".join([_date, _time]) return result
def deserialize(self, field, pstruct): if pstruct is null: return null else: _date = pstruct['date'].strip() _time = pstruct['time'].strip() if (not _date and not _time): return null if not _date: raise Invalid(field.schema, _('Incomplete date'), "") if not _time: _time = "00:00:00" result = ' '.join([_date, _time]) return result
def deserialize(self, field, pstruct): if pstruct is null: return null else: year = pstruct["year"].strip() month = pstruct["month"].strip() day = pstruct["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) return result
def serialize(self, field, cstruct, readonly=False): if (self.render_initial_item and self.min_len is None): # This is for compat only: ``render_initial_item=True`` should # now be spelled as ``min_len = 1`` self.min_len = 1 if cstruct in (null, None): if self.min_len is not None: cstruct = [null] * self.min_len else: cstruct = [] cstructlen = len(cstruct) if self.min_len is not None and (cstructlen < self.min_len): cstruct = list(cstruct) + ([null] * (self.min_len-cstructlen)) item_field = field.children[0] if getattr(field, 'sequence_fields', None): # this serialization is being performed as a result of a # validation failure (``deserialize`` was previously run) assert(len(cstruct) == len(field.sequence_fields)) subfields = zip(cstruct, field.sequence_fields) else: # this serialization is being performed as a result of a # first-time rendering subfields = [ (val, item_field.clone()) for val in cstruct ] template = readonly and self.readonly_template or self.template add_template_mapping = dict( subitem_title=item_field.title, subitem_description=item_field.description, subitem_name=item_field.name) add_subitem_text = _(self.add_subitem_text_template, mapping=add_template_mapping) return field.renderer(template, field=field, cstruct=cstruct, subfields=subfields, item_field=item_field, add_subitem_text=add_subitem_text)
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``. readonly_template The template name used to render the widget in read-only mode. Default: ``readonly/checked_password``. size The ``size`` attribute of the password input field (default: ``None``). """ template = 'checked_password' readonly_template = 'readonly/checked_password' mismatch_message = _('Password did not match confirm') size = None
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``. readonly_template The template name used to render the widget in read-only mode. Default: ``readonly/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' readonly_template = 'readonly/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, readonly=False): if cstruct in (null, None): cstruct = '' confirm = getattr(field, 'confirm', '') template = readonly and self.readonly_template or 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): if pstruct is null: return null value = pstruct.get('value') or '' confirm = pstruct.get('confirm') or '' field.confirm = confirm if (value or confirm) and (value != confirm): raise Invalid(field.schema, self.mismatch_message, value) if not value: return null return value
def serialize(self, field, cstruct, **kw): # XXX make it possible to override min_len in kw if cstruct in (null, None): if self.min_len is not None: cstruct = [null] * self.min_len else: cstruct = [] cstructlen = len(cstruct) if self.min_len is not None and (cstructlen < self.min_len): cstruct = list(cstruct) + ([null] * (self.min_len - cstructlen)) item_field = field.children[0] if getattr(field, 'sequence_fields', None): # this serialization is being performed as a result of a # validation failure (``deserialize`` was previously run) assert (len(cstruct) == len(field.sequence_fields)) subfields = list(zip(cstruct, field.sequence_fields)) else: # this serialization is being performed as a result of a # first-time rendering subfields = [] for val in cstruct: cloned = item_field.clone() # root ++ Ecreall++ Amen if item_field.parent is not None: cloned._parent = weakref.ref(item_field.parent) # root ++ Ecreall++ Amen if val is not null: # item field has already been set up with a default by # virtue of its constructor and setting cstruct to null # here wil overwrite the real default cloned.cstruct = val subfields.append((cloned.cstruct, cloned)) readonly = kw.get('readonly', self.readonly) template = readonly and self.readonly_template or self.template translate = field.translate subitem_title = kw.get('subitem_title', item_field.title) subitem_description = kw.get('subitem_description', item_field.description) add_subitem_text_template = kw.get('add_subitem_text_template', self.add_subitem_text_template) add_template_mapping = dict( subitem_title=translate(subitem_title), subitem_description=translate(subitem_description), subitem_name=item_field.name, ) if isinstance(add_subitem_text_template, TranslationString): add_subitem_text = add_subitem_text_template % add_template_mapping else: add_subitem_text = _(add_subitem_text_template, mapping=add_template_mapping) kw.setdefault('subfields', subfields) kw.setdefault('add_subitem_text', add_subitem_text) kw.setdefault('item_field', item_field) values = self.get_template_values(field, cstruct, kw) return field.renderer(template, **values)
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 is colander.null: return colander.null if not hasattr(value, 'get'): mapping = {'value': repr(value)} raise colander.Invalid( node, _('${value} is not a dictionary', mapping=mapping)) for n in ('filename', 'uid'): if not n in value: mapping = {'value': repr(value), 'key': n} raise colander.Invalid( node, _('${value} has no ${key} key', mapping=mapping)) result = widget.filedict() result['filename'] = value['filename'] result['uid'] = value['uid'] result['mimetype'] = value.get('mimetype') result['size'] = value.get('size') result['fp'] = value.get('fp') result['preview_url'] = value.get('preview_url') return result
class SequenceWidget(Widget): """Renders a sequence (0 .. N widgets, each the same as the other) into a set of fields. **Attributes/Arguments** template The template name used to render the widget. Default: ``sequence``. readonly_template The template name used to render the widget in read-only mode. Default: ``readonly/sequence``. item_template The template name used to render each value in the sequence. Default: ``sequence_item``. add_subitem_text_template The string used as the add link text for the widget. Interpolation markers in the template will be replaced in this string during serialization with a value as follows: ``${subitem_title}`` The title of the subitem field ``${subitem_description}`` The description of the subitem field ``${subitem_name}`` The name of the subitem field Default: ``Add ${subitem_title}``. render_initial_item Deprecated boolean attribute indicating whether, on the first rendering of a form including this sequence widget, a single child widget rendering should be performed. Default: ``False``. This attribute is honored for backwards compatibility only: in new applications, please use ``min_len=1`` instead. min_len Integer indicating minimum number of acceptable subitems. Default: ``None`` (meaning no minimum). On the first rendering of a form including this sequence widget, at least this many subwidgets will be rendered. The JavaScript sequence management will not allow fewer than this many subwidgets to be present in the sequence. max_len Integer indicating maximum number of acceptable subwidgets. Default: ``None`` (meaning no maximum). The JavaScript sequence management will not allow more than this many subwidgets to be added to the sequence. """ template = 'sequence' readonly_template = 'readonly/sequence' item_template = 'sequence_item' readonly_item_template = 'readonly/sequence_item' error_class = None add_subitem_text_template = _('Add ${subitem_title}') render_initial_item = False min_len = None max_len = None requirements = ( ('deform', None), ) def prototype(self, field): # we clone the item field to bump the oid (for easier # automated testing; finding last node) item_field = field.children[0].clone() proto = field.renderer(self.item_template, field=item_field, cstruct=null, parent=field) if isinstance(proto, unicode): proto = proto.encode('utf-8') proto = urllib.quote(proto) return proto def serialize(self, field, cstruct, readonly=False): if (self.render_initial_item and self.min_len is None): # This is for compat only: ``render_initial_item=True`` should # now be spelled as ``min_len = 1`` self.min_len = 1 if cstruct in (null, None): if self.min_len is not None: cstruct = [null] * self.min_len else: cstruct = [] cstructlen = len(cstruct) if self.min_len is not None and (cstructlen < self.min_len): cstruct = list(cstruct) + ([null] * (self.min_len-cstructlen)) item_field = field.children[0] if getattr(field, 'sequence_fields', None): # this serialization is being performed as a result of a # validation failure (``deserialize`` was previously run) assert(len(cstruct) == len(field.sequence_fields)) subfields = zip(cstruct, field.sequence_fields) else: # this serialization is being performed as a result of a # first-time rendering subfields = [ (val, item_field.clone()) for val in cstruct ] template = readonly and self.readonly_template or self.template add_template_mapping = dict( subitem_title=item_field.title, subitem_description=item_field.description, subitem_name=item_field.name) add_subitem_text = _(self.add_subitem_text_template, mapping=add_template_mapping) return field.renderer(template, field=field, cstruct=cstruct, subfields=subfields, item_field=item_field, add_subitem_text=add_subitem_text) def deserialize(self, field, pstruct): result = [] error = None if pstruct is null: pstruct = [] field.sequence_fields = [] item_field = field.children[0] for num, substruct in enumerate(pstruct): subfield = item_field.clone() try: subval = subfield.deserialize(substruct) except Invalid, e: subval = e.value if error is None: error = Invalid(field.schema, value=result) error.add(e, num) result.append(subval) field.sequence_fields.append(subfield) if error is not None: raise error 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 (colander.null, None, ""): return colander.null if not hasattr(value, 'get'): mapping = {'value':repr(value)} raise colander.Invalid( node, _('${value} is not a dictionary', mapping=mapping) ) for n in ('filename', 'uid'): if not n in value: mapping = {'value':repr(value), 'key':n} raise colander.Invalid( node, _('${value} has no ${key} key', mapping=mapping) ) result = widget.filedict() result['filename'] = value['filename'] result['uid'] = value['uid'] result['mimetype'] = value.get('mimetype') result['size'] = value.get('size') result['fp'] = value.get('fp') result['preview_url'] = value.get('preview_url') return result
def serialize(self, field, cstruct, **kw): # XXX make it possible to override min_len in kw if cstruct in (null, None): if self.min_len is not None: cstruct = [null] * self.min_len else: cstruct = [] cstructlen = len(cstruct) if self.min_len is not None and (cstructlen < self.min_len): cstruct = list(cstruct) + ([null] * (self.min_len-cstructlen)) item_field = field.children[0] if getattr(field, 'sequence_fields', None): # this serialization is being performed as a result of a # validation failure (``deserialize`` was previously run) assert(len(cstruct) == len(field.sequence_fields)) subfields = list(zip(cstruct, field.sequence_fields)) else: # this serialization is being performed as a result of a # first-time rendering subfields = [] for val in cstruct: cloned = item_field.clone() # root ++ Ecreall++ Amen if item_field.parent is not None: cloned._parent = weakref.ref(item_field.parent) # root ++ Ecreall++ Amen if val is not null: # item field has already been set up with a default by # virtue of its constructor and setting cstruct to null # here wil overwrite the real default cloned.cstruct = val subfields.append((cloned.cstruct, cloned)) readonly = kw.get('readonly', self.readonly) template = readonly and self.readonly_template or self.template translate = field.translate subitem_title = kw.get('subitem_title', item_field.title) subitem_description = kw.get( 'subitem_description', item_field.description ) add_subitem_text_template = kw.get( 'add_subitem_text_template', self.add_subitem_text_template ) add_template_mapping = dict( subitem_title=translate(subitem_title), subitem_description=translate(subitem_description), subitem_name=item_field.name, ) if isinstance(add_subitem_text_template, TranslationString): add_subitem_text = add_subitem_text_template % add_template_mapping else: add_subitem_text = _(add_subitem_text_template, mapping=add_template_mapping) kw.setdefault('subfields', subfields) kw.setdefault('add_subitem_text', add_subitem_text) kw.setdefault('item_field', item_field) values = self.get_template_values(field, cstruct, kw) return field.renderer(template, **values)