def write(self, field, name, type, elementName='field'): element = super(ChoiceHandler, self).write(field, name, type, elementName) # write vocabulary or values list # Named vocabulary if field.vocabularyName is not None and field.vocabulary is None: attributeField = self.fieldAttributes['vocabulary'] child = valueToElement( attributeField, field.vocabularyName, name='vocabulary', force=True ) element.append(child) # Listed vocabulary - attempt to convert to a simple list of values elif field.vocabularyName is None \ and IVocabularyTokenized.providedBy(field.vocabulary): value = [] for term in field.vocabulary: if (not isinstance(term.value, (str, text_type), ) or term.token.encode('utf-8') != # token is 'str' term.value.encode('unicode_escape')): raise NotImplementedError( u"Cannot export a vocabulary that is not " u"based on a simple list of values" ) if term.title and term.title != term.value: value.append((term.value, term.title)) else: value.append(term.value) attributeField = self.fieldAttributes['values'] if any(map(lambda v: isinstance(v, tuple), value)): _pair = lambda v: v if len(v) == 2 else (v[0],) * 2 value = OrderedDict(map(_pair, value)) attributeField = OrderedDictField( key_type=zope.schema.TextLine(), value_type=zope.schema.TextLine(), ) child = valueToElement( attributeField, value, name='values', force=True ) element.append(child) # Anything else is not allowed - we can't export ISource/IVocabulary or # IContextSourceBinder objects. else: raise NotImplementedError( u"Choice fields with vocabularies not based on " u"a simple list of values or a named vocabulary " u"cannot be exported" ) return element
def write(self, field, name, type, elementName='field'): element = super(ChoiceHandler, self).write(field, name, type, elementName) # write vocabulary or values list # Named vocabulary if field.vocabularyName is not None and field.vocabulary is None: attributeField = self.fieldAttributes['vocabulary'] child = valueToElement(attributeField, field.vocabularyName, name='vocabulary', force=True) element.append(child) # Listed vocabulary - attempt to convert to a simple list of values elif field.vocabularyName is None and IVocabularyTokenized.providedBy(field.vocabulary): value = [] for term in field.vocabulary: if (not isinstance(term.value, (str, unicode), ) or term.token != term.value.encode('unicode_escape')): raise NotImplementedError(u"Cannot export a vocabulary that is not " "based on a simple list of values") value.append(term.value) attributeField = self.fieldAttributes['values'] child = valueToElement(attributeField, value, name='values', force=True) element.append(child) # Anything else is not allowed - we can't export ISource/IVocabulary or # IContextSourceBinder objects. else: raise NotImplementedError(u"Choice fields with vocabularies not based on " "a simple list of values or a named vocabulary " "cannot be exported") return element
def write(self, field, name, type, elementName='field'): element = super(ChoiceHandler, self).write(field, name, type, elementName) # write vocabulary or values list # Named vocabulary if field.vocabularyName is not None and field.vocabulary is None: attributeField = self.fieldAttributes['vocabulary'] child = valueToElement( attributeField, field.vocabularyName, name='vocabulary', force=True ) element.append(child) # Listed vocabulary - attempt to convert to a simple list of values elif field.vocabularyName is None \ and IVocabularyTokenized.providedBy(field.vocabulary): value = [] for term in field.vocabulary: if (not isinstance(term.value, (str, unicode), ) or term.token != term.value.encode('unicode_escape')): raise NotImplementedError( u"Cannot export a vocabulary that is not " u"based on a simple list of values" ) if term.title and term.title != term.value: value.append((term.value, term.title)) else: value.append(term.value) attributeField = self.fieldAttributes['values'] if any(map(lambda v: isinstance(v, tuple), value)): _pair = lambda v: v if len(v) == 2 else (v[0],) * 2 value = OrderedDict(map(_pair, value)) attributeField = OrderedDictField( key_type=zope.schema.TextLine(), value_type=zope.schema.TextLine(), ) child = valueToElement( attributeField, value, name='values', force=True ) element.append(child) # Anything else is not allowed - we can't export ISource/IVocabulary or # IContextSourceBinder objects. else: raise NotImplementedError( u"Choice fields with vocabularies not based on " u"a simple list of values or a named vocabulary " u"cannot be exported" ) return element
def _assertSerialized(self, field, value, expected): element = utils.valueToElement(field, value, 'value') sio = StringIO() etree.ElementTree(element).write(sio) self.assertEquals(sio.getvalue(), expected) unserialized = utils.elementToValue(field, element) self.assertEquals(value, unserialized)
def write(self, widgetNode, params): for attributeName, attributeField in self.fieldAttributes.items(): elementName = attributeField.__name__ value = params.get(elementName, attributeField.default) if value != attributeField.default: child = valueToElement(attributeField, value, name=elementName) widgetNode.append(child)
def exportRecord(self, record): node = etree.Element('record') node.attrib['name'] = record.__name__ if IInterfaceAwareRecord.providedBy(record): node.attrib['interface'] = record.interfaceName node.attrib['field'] = record.fieldName # write field field = record.field if IFieldRef.providedBy(field): field_element = etree.Element('field') field_element.attrib['ref'] = field.recordName node.append(field_element) else: field_type = IFieldNameExtractor(record.field)() handler = queryUtility(IFieldExportImportHandler, name=field_type) if handler is None: self.logger.warning("Field type %s specified for record %s \ cannot be exported" % (field_type, record.__name__)) else: field_element = handler.write(record.field, None, field_type, elementName='field') node.append(field_element) # write value value_element = valueToElement(record.field, record.value, name='value', force=True) node.append(value_element) return node
def writeAttribute(self, attributeField, field, ignoreDefault=True): """Create and return a element that describes the given attribute field on the given field """ elementName = attributeField.__name__ attributeField = attributeField.bind(field) value = attributeField.get(field) force = (elementName in self.forcedFields) if ignoreDefault and value == attributeField.default: return None # The value points to another field. Recurse. if IField.providedBy(value): value_fieldType = IFieldNameExtractor(value)() handler = queryUtility(IFieldExportImportHandler, name=value_fieldType) if handler is None: return None return handler.write(value, name=None, type=value_fieldType, elementName=elementName) # For 'default', 'missing_value' etc, we want to validate against # the imported field type itself, not the field type of the attribute if elementName in self.fieldTypeAttributes or \ elementName in self.nonValidatedfieldTypeAttributes: attributeField = field return valueToElement(attributeField, value, name=elementName, force=force)
def write(self, field, name, type, elementName='field'): element = super(ChoiceHandler, self).write(field, name, type, elementName) # write vocabulary or values list # Named vocabulary if field.vocabularyName is not None and field.vocabulary is None: attributeField = self.fieldAttributes['vocabulary'] child = valueToElement(attributeField, field.vocabularyName, name='vocabulary', force=True) element.append(child) # Listed vocabulary - attempt to convert to a simple list of values elif field.vocabularyName is None and IVocabularyTokenized.providedBy( field.vocabulary): value = [] for term in field.vocabulary: if (not isinstance( term.value, (str, unicode), ) or term.token != term.value.encode('unicode_escape')): raise NotImplementedError( u"Cannot export a vocabulary that is not " "based on a simple list of values") value.append(term.value) attributeField = self.fieldAttributes['values'] child = valueToElement(attributeField, value, name='values', force=True) element.append(child) # Anything else is not allowed - we can't export ISource/IVocabulary or # IContextSourceBinder objects. else: raise NotImplementedError( u"Choice fields with vocabularies not based on " "a simple list of values or a named vocabulary " "cannot be exported") return element
def write(self, field, name, type, elementName="field"): element = super(RelationChoiceBaseHandler, self).write(field, name, type, elementName) portal_type = [] portal_type.extend(field.source.query.get("portal_type") or []) if portal_type: attributeField = self.fieldAttributes["portal_type"] child = valueToElement(attributeField, portal_type, name="portal_type", force=True) element.append(child) return element
def write(self, field, name, type, elementName='field'): element = super(plone.supermodel.exportimport.ChoiceHandler, self).write(field, name, type, elementName) if field.vocabulary is not None: value = "{}.{}".format(field.vocabulary.__module__, field.vocabulary.__name__) child = valueToElement(self.fieldAttributes['vocabulary'], value, name='vocabulary', force=True) element.append(child) else: value = "{}.{}".format(field.source.__module__, field.source.__name__) child = valueToElement(self.fieldAttributes['source'], value, name='source', force=True) element.append(child) return element
def write(self, field, name, type, elementName='field'): element = super(RelationChoiceBaseHandler, self).write(field, name, type, elementName) portal_type = [] portal_type.extend(field.source.query.get('portal_type') or []) if portal_type: attributeField = self.fieldAttributes['portal_type'] child = valueToElement(attributeField, portal_type, name='portal_type', force=True) element.append(child) return element
def export_field(self, doc, field): """Turn a zope.schema field into a node and return it """ field = field.bind(self.element) value = field.get(self.element) child = doc.createElement('property') child.setAttribute('name', field.__name__) # supermodel gives us an etree node but GS uses minidom so we need to convert it node = valueToElement(field, value) if node.text: child.appendChild(doc.createTextNode(six.text_type(node.text))) # Assumes there are not other text nodes and we can throw away the parent node for node in node.iterchildren(): xml = etree.tostring(node, encoding="utf8") child.appendChild(minidom.parseString(xml).firstChild) return child
def exportRecord(self, record): node = etree.Element('record') node.attrib['name'] = record.__name__ if IInterfaceAwareRecord.providedBy(record): node.attrib['interface'] = record.interfaceName node.attrib['field'] = record.fieldName # write field field = record.field if IFieldRef.providedBy(field): field_element = etree.Element('field') field_element.attrib['ref'] = field.recordName node.append(field_element) else: field_type = IFieldNameExtractor(record.field)() handler = queryUtility(IFieldExportImportHandler, name=field_type) if handler is None: self.logger.warning( 'Field type {0} specified for record {1} ' 'cannot be exported'.format(field_type, record.__name__) ) else: field_element = handler.write( record.field, None, field_type, elementName='field' ) node.append(field_element) # write value value_element = valueToElement( record.field, record.value, name='value', force=True ) node.append(value_element) return node
def write(self, field, name, type, elementName='field'): element = super(RelationChoiceBaseHandler, self).write(field, name, type, elementName) portal_type = [] if HAS_CONTENTTREE and not HAS_WIDGETS: filter_ = getattr(field.source, 'selectable_filter', None) or {} portal_type.extend( filter_.criteria.get('portal_type') or []) else: portal_type.extend( field.source.query.get('portal_type') or []) if portal_type: attributeField = self.fieldAttributes['portal_type'] child = valueToElement(attributeField, portal_type, name='portal_type', force=True) element.append(child) return element
def write(self, field, name, type, elementName='field'): element = super(RelationChoiceBaseHandler, self).write(field, name, type, elementName) portal_type = [] if HAS_CONTENTTREE and not HAS_WIDGETS: filter_ = getattr(field.source, 'selectable_filter', None) or {} portal_type.extend(filter_.criteria.get('portal_type') or []) else: portal_type.extend(field.source.query.get('portal_type') or []) if portal_type: attributeField = self.fieldAttributes['portal_type'] child = valueToElement(attributeField, portal_type, name='portal_type', force=True) element.append(child) return element
def writeAttribute(self, attributeField, field, ignoreDefault=True): """Create and return a element that describes the given attribute field on the given field """ elementName = attributeField.__name__ attributeField = attributeField.bind(field) value = attributeField.get(field) force = (elementName in self.forcedFields) if ignoreDefault and value == attributeField.default: return None # The value points to another field. Recurse. if IField.providedBy(value): value_fieldType = IFieldNameExtractor(value)() handler = queryUtility( IFieldExportImportHandler, name=value_fieldType ) if handler is None: return None return handler.write( value, name=None, type=value_fieldType, elementName=elementName ) # For 'default', 'missing_value' etc, we want to validate against # the imported field type itself, not the field type of the attribute if elementName in self.fieldTypeAttributes or \ elementName in self.nonValidatedfieldTypeAttributes: attributeField = field return valueToElement( attributeField, value, name=elementName, force=force )