def elementToValue(field, element, default=_marker): """Read the contents of an element that is assumed to represent a value allowable by the given field. If converter is given, it should be an IToUnicode instance. If not, the field will be adapted to this interface to obtain a converter. """ value = default if IDict.providedBy(field): key_converter = IFromUnicode(field.key_type) value = OrderedDict() for child in element.iterchildren(tag=etree.Element): if noNS(child.tag.lower()) != 'element': continue parseinfo.stack.append(child) key_text = child.attrib.get('key', None) if key_text is None: k = None else: k = key_converter.fromUnicode(unicode(key_text)) value[k] = elementToValue(field.value_type, child) parseinfo.stack.pop() value = fieldTypecast(field, value) elif ICollection.providedBy(field): value = [] for child in element.iterchildren(tag=etree.Element): if noNS(child.tag.lower()) != 'element': continue parseinfo.stack.append(child) v = elementToValue(field.value_type, child) value.append(v) parseinfo.stack.pop() value = fieldTypecast(field, value) # Unicode else: text = element.text if text is None: value = field.missing_value else: converter = IFromUnicode(field) value = converter.fromUnicode(unicode(text)) # handle i18n if isinstance(value, unicode) and parseinfo.i18n_domain is not None: translate_attr = ns('translate', I18N_NAMESPACE) domain_attr = ns('domain', I18N_NAMESPACE) msgid = element.attrib.get(translate_attr) domain = element.attrib.get(domain_attr, parseinfo.i18n_domain) if msgid: value = Message(msgid, domain=domain, default=value) elif translate_attr in element.attrib: value = Message(value, domain=domain) return value
def _validate_fields(self, raw_data): errors = [] for name, field, schema in self.fields: if name not in raw_data: continue field = field.bind(self.obj or self.model) raw_value = raw_data.pop(name) if isinstance(raw_value, str): raw_value = raw_value.decode('utf8') # We don't want to accidentally swallow any adaptation TypeErrors from here: from_unicode = IFromUnicode(field) try: if raw_value in (None, u'', '') and field.required: raise RequiredMissing(name) try: value = from_unicode.fromUnicode(raw_value) except (ValueError, TypeError): raise WrongType(name) # TODO: make this more descriptive as to which validation failed, # where was it defined etc. except zope.schema.ValidationError as exc: errors.append((name, exc)) else: setattr(self.adapted_tmp_obj(self.tmp_obj, schema), name, value) return errors
def __call__(self, value, filestore, item, disable_constraints=False, logger=None): field = self.field if field is not None: try: if isinstance(value, str): value = safe_unicode(value) if str(type(value)) == "<type 'unicode'>": value = IFromUnicode(field).fromUnicode(value) self.field.validate(value) except Exception as e: if not disable_constraints: raise e else: if logger: logger("%s is invalid in %s: %s" % ( self.field.__name__, item["_path"], e.__repr__(), )) return value
def __call__(self, value): if not isinstance(value, str): self.field.validate(value) return value value = IFromUnicode(self.field).fromUnicode(value) # IFromUnicode.fromUnicode() will validate, no need to do it twice return value
def elementToValue(field, element, default=_marker): """Read the contents of an element that is assumed to represent a value allowable by the given field. If converter is given, it should be an IToUnicode instance. If not, the field will be adapted to this interface to obtain a converter. """ value = default if IDict.providedBy(field): key_converter = IFromUnicode(field.key_type) value = {} for child in element: if noNS(child.tag.lower()) != 'element': continue key_text = child.attrib.get('key', None) if key_text is None: k = None else: k = key_converter.fromUnicode(unicode(key_text)) value[k] = elementToValue(field.value_type, child) value = fieldTypecast(field, value) elif ICollection.providedBy(field): value = [] for child in element: if noNS(child.tag.lower()) != 'element': continue v = elementToValue(field.value_type, child) value.append(v) value = fieldTypecast(field, value) # Unicode else: text = element.text if text is None: value = field.missing_value else: converter = IFromUnicode(field) value = converter.fromUnicode(unicode(text)) return value
def __call__(self, value): # Proper initialization of IContextSourceBinder # Check zope.schema IChoice field bind method. cloned_field = self.field.bind(self.context) if not isinstance(value, unicode): return value return IFromUnicode(cloned_field).fromUnicode(value)
def fromUnicode(self, value): if self.context.value_type: value_converter = IFromUnicode(self.context.value_type) from_unicode = lambda x: value_converter.fromUnicode(x) else: from_unicode = lambda x: x if isinstance(value, basestring): value = [from_unicode(unicode(i.strip())) for i in value.strip(', ').split(',')] return self.context._type(value)
def extract_data(schema, raw_data, prefix): data = Data({}) for name in schema.names(): field = schema[name] raw_value = raw_data.get(prefix + name, field.default) value = IFromUnicode(field).fromUnicode(raw_value) data[name] = value # convert(raw_value, field) return data
def __call__(self, value): if isinstance(value, str): value = IFromUnicode(self.field).fromUnicode(value) # Mimic what z3c.form does in it's BaseDataConverter. if isinstance(value, str): value = value.strip() if value == "": value = self.field.missing_value self.field.validate(value) return value
def __call__(self, value, filestore, item, disable_constraints=False): field = self.field if field is not None: if isinstance(value, str): value = value.decode('utf-8') if isinstance(value, unicode): try: value = IFromUnicode(field).fromUnicode(value) except ConstraintNotSatisfied: if not disable_constraints: raise ConstraintNotSatisfied(value) except TypeError, e: # TODO: This TypeError may happen because zope.schema._field.Choice._validate # tries to iterate over a vocabulary factory without calling it if not disable_constraints: raise e if not disable_constraints: self.field.validate(value)
def __call__(self, value, filestore, item, disable_constraints=False, logger=None): field = self.field if field is not None: try: if isinstance(value, str): value = value.decode('utf-8') if isinstance(value, unicode): value = IFromUnicode(field).fromUnicode(value) self.field.validate(value) except Exception, e: if not disable_constraints: raise e else: if logger: logger( "%s is invalid in %s: %s" % ( self.field.__name__, item['_path'], e.__repr__()) )
def __call__(self, value): if not isinstance(value, unicode): return value return IFromUnicode(self.field).fromUnicode(value)
def elementToValue(field, element, default=_marker): """Read the contents of an element that is assumed to represent a value allowable by the given field. If converter is given, it should be an IToUnicode instance. If not, the field will be adapted to this interface to obtain a converter. """ value = default if IDict.providedBy(field): key_converter = IFromUnicode(field.key_type) value = OrderedDict() for child in element.iterchildren(tag=etree.Element): if noNS(child.tag.lower()) != 'element': continue parseinfo.stack.append(child) key_text = child.attrib.get('key') if key_text is None: k = None else: k = key_converter.fromUnicode(six.text_type(key_text)) value[k] = elementToValue(field.value_type, child) parseinfo.stack.pop() value = fieldTypecast(field, value) elif ICollection.providedBy(field): value = [] for child in element.iterchildren(tag=etree.Element): if noNS(child.tag.lower()) != 'element': continue parseinfo.stack.append(child) v = elementToValue(field.value_type, child) value.append(v) parseinfo.stack.pop() value = fieldTypecast(field, value) elif IChoice.providedBy(field): vocabulary = None try: vcf = getUtility(IVocabularyFactory, field.vocabularyName) vocabulary = vcf(None) except: pass if vocabulary and hasattr(vocabulary, 'by_value'): try: field._type = type(list(vocabulary.by_value.keys())[0]) except: pass value = fieldTypecast(field, element.text) # Unicode else: text = element.text if text is None: value = field.missing_value else: converter = IFromUnicode(field) if isinstance(text, six.binary_type): text = text.decode() else: text = six.text_type(text) value = converter.fromUnicode(text) # handle i18n if isinstance(value, six.string_types) and \ parseinfo.i18n_domain is not None: translate_attr = ns('translate', I18N_NAMESPACE) domain_attr = ns('domain', I18N_NAMESPACE) msgid = element.attrib.get(translate_attr) domain = element.attrib.get(domain_attr, parseinfo.i18n_domain) if msgid: value = Message(msgid, domain=domain, default=value) elif translate_attr in element.attrib: value = Message(value, domain=domain) return value
def convert(k, v): return (IFromUnicode(self.context.key_type).fromUnicode(unicode(k)), IFromUnicode(self.context.value_type).fromUnicode(unicode(v)))
def __call__(self, value): if not isinstance(value, str) or not isinstance(value, bytes): return value return IFromUnicode(self.field).fromUnicode(value) # @implementer(IFieldDeserializer) # @adapter(IDatetime, IDexterityContent, IBrowserRequest) # class DatetimeFieldDeserializer(DefaultFieldDeserializer): # def __call__(self, value): # try: # value = datetime(value).toZone(DateTime().localZone()).asdatetime( # ).replace(tzinfo=None) # except (SyntaxError, DateTimeError) as e: # raise ValueError(e.message) # self.field.validate(value) # return value # @implementer(IFieldDeserializer) # @adapter(ICollection, IDexterityContent, IBrowserRequest) # class CollectionFieldDeserializer(DefaultFieldDeserializer): # def __call__(self, value): # if not isinstance(value, list): # value = [value] # if IField.providedBy(self.field.value_type): # deserializer = getMultiAdapter( # (self.field.value_type, self.context, self.request), # IFieldDeserializer) # for i, v in enumerate(value): # value[i] = deserializer(v) # value = self.field._type(value) # self.field.validate(value) # return value # @implementer(IFieldDeserializer) # @adapter(IDict, IDexterityContent, IBrowserRequest) # class DictFieldDeserializer(DefaultFieldDeserializer): # def __call__(self, value): # kdeserializer = lambda k: k # vdeserializer = lambda v: v # if IField.providedBy(self.field.key_type): # kdeserializer = getMultiAdapter( # (self.field.key_type, self.context, self.request), # IFieldDeserializer) # if IField.providedBy(self.field.value_type): # vdeserializer = getMultiAdapter( # (self.field.value_type, self.context, self.request), # IFieldDeserializer) # new_value = {} # for k, v in value.items(): # new_value[kdeserializer(k)] = vdeserializer(v) # self.field.validate(new_value) # return new_value # @implementer(IFieldDeserializer) # @adapter(ITime, IDexterityContent, IBrowserRequest) # class TimeFieldDeserializer(DefaultFieldDeserializer): # def __call__(self, value): # try: # # Create an ISO 8601 datetime string and parse it with Zope's # # DateTime module and then convert it to a timezone naive time # # in local time # value = DateTime(u'2000-01-01T' + value).toZone(DateTime( # ).localZone()).asdatetime().replace(tzinfo=None).time() # except (SyntaxError, DateTimeError): # raise ValueError(u'Invalid time: {}'.format(value)) # self.field.validate(value) # return value # @implementer(IFieldDeserializer) # @adapter(ITimedelta, IDexterityContent, IBrowserRequest) # class TimedeltaFieldDeserializer(DefaultFieldDeserializer): # def __call__(self, value): # try: # value = timedelta(seconds=value) # except TypeError as e: # raise ValueError(e.message) # self.field.validate(value) # return value # @implementer(IFieldDeserializer) # @adapter(INamedField, IDexterityContent, IBrowserRequest) # class NamedFieldDeserializer(DefaultFieldDeserializer): # def __call__(self, value): # content_type = 'application/octet-stream' # filename = None # if isinstance(value, dict): # content_type = value.get(u'content-type', content_type).encode( # 'utf8') # filename = value.get(u'filename', filename) # if u'encoding' in value: # data = value.get('data', '').decode(value[u'encoding']) # else: # data = value.get('data', '') # else: # data = value # value = self.field._type( # data=data, contentType=content_type, filename=filename) # self.field.validate(value) # return value # @implementer(IFieldDeserializer) # @adapter(IRichText, IDexterityContent, IBrowserRequest) # class RichTextFieldDeserializer(DefaultFieldDeserializer): # def __call__(self, value): # content_type = self.field.default_mime_type # encoding = 'utf8' # if isinstance(value, dict): # content_type = value.get(u'content-type', content_type) # encoding = value.get(u'encoding', encoding) # data = value.get(u'data', u'') # else: # data = value # value = RichTextValue( # raw=data, # mimeType=content_type, # outputMimeType=self.field.output_mime_type, # encoding=encoding, # ) # self.field.validate(value) # return value # @implementer(IFieldDeserializer) # @adapter(IRelationChoice, IDexterityContent, IBrowserRequest) # class RelationChoiceFieldDeserializer(DefaultFieldDeserializer): # def __call__(self, value): # obj = None # if isinstance(value, int): # # Resolve by intid # intids = queryUtility(IIntIds) # obj = intids.queryObject(value) # elif isinstance(value, basestring): # portal = getMultiAdapter((self.context, self.request), # name='plone_portal_state').portal() # portal_url = portal.absolute_url() # if value.startswith(portal_url): # # Resolve by URL # obj = portal.restrictedTraverse( # value[len(portal_url) + 1:].encode('utf8'), None) # elif value.startswith('/'): # # Resolve by path # obj = portal.restrictedTraverse( # value.encode('utf8').lstrip('/'), None) # else: # # Resolve by UID # catalog = getToolByName(self.context, 'portal_catalog') # brain = catalog(UID=value) # if brain: # obj = brain[0].getObject() # self.field.validate(obj) # return obj