def serialize(self, dom, parent_node, instance): values = self.get(instance) if not values: return False for value in values: elname = "%s:%s"%(self.namespace.prefix, self.name) node = dom.createElementNS(base.DublinCore.xmlns, elname) # try to get 'utf-8' encoded string if isinstance(value, unicode): value = value.encode('utf-8') elif IBaseUnit.isImplementedBy(value): value = value.getRaw(encoding='utf-8') else: value = str(value) if isinstance(value, str) and has_ctrlchars(value): value = value.encode('base64') attr = dom.createAttributeNS(base.DublinCore.xmlns, 'transfer_encoding') attr.value = 'base64' node.setAttributeNode(attr) value_node = dom.createCDATASection(value) else: value_node = dom.createTextNode(value) node.appendChild(value_node) node.normalize() parent_node.appendChild(node) return True
def test_getbaseunit(self): obj = self._dummy for field in obj.Schema().fields(): if not hasattr(field,'getBaseUnit'): continue bu = field.getBaseUnit(obj) self.failUnless(IBaseUnit.isImplementedBy(bu), 'Return value of %s.getBaseUnit() does not implement BaseUnit: %s' % (field.__class__, type(bu)))
def getPreparedValue(value): if isinstance(value, unicode): value = value.encode('utf-8') elif IBaseUnit.isImplementedBy(value): value = value.getRaw(encoding='utf-8') else: value = str(value) return value
def test1_getbaseunit(self): obj = self._dummy for field in obj.Schema().fields(): if not hasattr(field, "getBaseUnit"): continue bu = field.getBaseUnit(obj) self.failUnless( IBaseUnit.providedBy(bu), ("Return value of %s.getBaseUnit() does not " "implement BaseUnit: %s" % (field.__class__, type(bu))), )
def test1_getbaseunit(self): obj = self._dummy for field in obj.Schema().fields(): if not hasattr(field, 'getBaseUnit'): continue bu = field.getBaseUnit(obj) self.assertTrue(IBaseUnit.providedBy(bu), ('Return value of %s.getBaseUnit() does not ' 'implement BaseUnit: %s' % (field.__class__, type(bu))))
def marshall(self, instance, **kwargs): p = instance.getPrimaryField() body = p and instance[p.getName()] or '' pname = p and p.getName() or None content_type = length = None # Gather/Guess content type if IBaseUnit.providedBy(body): content_type = str(body.getContentType()) body = body.getRaw() else: if p and hasattr(p, 'getContentType'): content_type = p.getContentType(instance) or 'text/plain' else: content_type = body and guess_content_type( body) or 'text/plain' headers = [] fields = [ f for f in instance.Schema().fields() if f.getName() != pname ] # Include the internal version of IUUID(instance). We're setting this # value in the same class, so accessing it directly here saves a # Component Registry lookup for every object being exported if hasattr(aq_base(instance), UUID_ATTR): # Non-referencable content isn't exportable by default, but we should # shouldn't include the uuid if it's not there. headers.append(('UID', getattr(aq_base(instance), UUID_ATTR, None))) for field in fields: if field.type in ('file', 'image', 'object'): continue accessor = field.getEditAccessor(instance) if not accessor: continue kw = {'raw': 1, 'field': field.__name__} value = mapply(accessor, **kw) if type(value) in [ListType, TupleType]: value = '\n'.join([str(v) for v in value]) headers.append((field.getName(), str(value))) headers.append(('Content-Type', content_type or 'text/plain')) header = formatRFC822Headers(headers) data = '%s\n\n%s' % (header, body) length = len(data) return (content_type, length, data)
def serialize(self, dom, parent_node, instance, options={}): values = self.get(instance) if not values: return is_ref = self.isReference(instance) for value in values: node = dom.createElementNS(self.namespace.xmlns, "field") name_attr = dom.createAttribute("name") name_attr.value = self.name node.setAttributeNode(name_attr) # try to get 'utf-8' encoded string if isinstance(value, unicode): value = value.encode('utf-8') elif IBaseUnit.isImplementedBy(value): value = value.getRaw(encoding='utf-8') else: value = str(value) if is_ref: if config.HANDLE_REFS: ref_node = dom.createElementNS(self.namespace.xmlns, 'reference') uid_node = dom.createElementNS(self.namespace.xmlns, 'uid') value = response.createTextNode(value) uid_node.append(value) ref_node.append(uid_node) node.append(ref_node) elif isinstance(value, str) and has_ctrlchars(value): value = value.encode('base64') attr = dom.createAttributeNS(self.namespace.xmlns, 'transfer_encoding') attr.value = 'base64' node.setAttributeNode(attr) value_node = dom.createCDATASection(value) node.appendChild(value_node) else: value_node = dom.createTextNode(value) node.appendChild(value_node) node.normalize() parent_node.appendChild(node) return True
def marshall(self, instance, **kwargs): p = instance.getPrimaryField() body = p and instance[p.getName()] or '' pname = p and p.getName() or None content_type = length = None # Gather/Guess content type if IBaseUnit.providedBy(body): content_type = str(body.getContentType()) body = body.getRaw() else: if p and hasattr(p, 'getContentType'): content_type = p.getContentType(instance) or 'text/plain' else: content_type = body and guess_content_type(body) or 'text/plain' headers = [] fields = [f for f in instance.Schema().fields() if f.getName() != pname] # Include the internal version of IUUID(instance). We're setting this # value in the same class, so accessing it directly here saves a # Component Registry lookup for every object being exported if hasattr(aq_base(instance), UUID_ATTR): # Non-referencable content isn't exportable by default, but we should # shouldn't include the uuid if it's not there. headers.append(('UID', getattr(aq_base(instance), UUID_ATTR, None))) for field in fields: if field.type in ('file', 'image', 'object'): continue accessor = field.getEditAccessor(instance) if not accessor: continue kw = {'raw': 1, 'field': field.__name__} value = mapply(accessor, **kw) if type(value) in [ListType, TupleType]: value = '\n'.join([str(v) for v in value]) headers.append((field.getName(), str(value))) headers.append(('Content-Type', content_type or 'text/plain')) header = formatRFC822Headers(headers) data = '%s\n\n%s' % (header, body) length = len(data) return (content_type, length, data)
def marshall(self, instance, **kwargs): p = instance.getPrimaryField() if not p: raise TypeError, "Primary Field could not be found." data = p and instance[p.getName()] or "" content_type = length = None # Gather/Guess content type if IBaseUnit.providedBy(data): content_type = data.getContentType() length = data.get_size() data = data.getRaw() elif isinstance(data, File): content_type = data.content_type length = data.get_size() data = data.data else: log( "WARNING: PrimaryFieldMarshaller(%r): " "field %r does not return a IBaseUnit " "instance." % (instance, p.getName()) ) if hasattr(p, "getContentType"): content_type = p.getContentType(instance) or "text/plain" else: content_type = data and guess_content_type(data) or "text/plain" # DM 2004-12-01: "FileField"s represent a major field class # that does not use "IBaseUnit" yet. # Ensure, the used "File" objects get the correct length. if hasattr(p, "get_size"): length = p.get_size(instance) else: # DM: this almost surely is stupid! length = len(data) # ObjectField without IBaseUnit? if shasattr(data, "data"): data = data.data else: data = str(data) # DM 2004-12-01: recompute 'length' as we now know it # definitely length = len(data) return (content_type, length, data)
def marshall(self, instance, **kwargs): p = instance.getPrimaryField() if not p: raise TypeError, 'Primary Field could not be found.' data = p and instance[p.getName()] or '' content_type = length = None # Gather/Guess content type if IBaseUnit.providedBy(data): content_type = data.getContentType() length = data.get_size() data = data.getRaw() elif isinstance(data, File): content_type = data.content_type length = data.get_size() data = data.data else: log('WARNING: PrimaryFieldMarshaller(%r): ' 'field %r does not return a IBaseUnit ' 'instance.' % (instance, p.getName())) if hasattr(p, 'getContentType'): content_type = p.getContentType(instance) or 'text/plain' else: content_type = (data and guess_content_type(data) or 'text/plain') # DM 2004-12-01: "FileField"s represent a major field class # that does not use "IBaseUnit" yet. # Ensure, the used "File" objects get the correct length. if hasattr(p, 'get_size'): length = p.get_size(instance) else: # DM: this almost surely is stupid! length = len(data) # ObjectField without IBaseUnit? if shasattr(data, 'data'): data = data.data else: data = str(data) # DM 2004-12-01: recompute 'length' as we now know it # definitely length = len(data) return (content_type, length, data)
def marshall(self, instance, **kwargs): p = instance.getPrimaryField() body = p and instance[p.getName()] or '' pname = p and p.getName() or None content_type = length = None # Gather/Guess content type if IBaseUnit.providedBy(body): content_type = str(body.getContentType()) body = body.getRaw() else: if p and hasattr(p, 'getContentType'): content_type = p.getContentType(instance) or 'text/plain' else: content_type = body and guess_content_type( body) or 'text/plain' headers = [] fields = [ f for f in instance.Schema().fields() if f.getName() != pname ] for field in fields: if field.type in ('file', 'image', 'object'): continue accessor = field.getEditAccessor(instance) if not accessor: continue kw = {'raw': 1, 'field': field.__name__} value = mapply(accessor, **kw) if type(value) in [ListType, TupleType]: value = '\n'.join([str(v) for v in value]) headers.append((field.getName(), str(value))) headers.append(('Content-Type', content_type or 'text/plain')) header = formatRFC822Headers(headers) data = '%s\n\n%s' % (header, body) length = len(data) return (content_type, length, data)
def set(self, name, instance, value, **kwargs): """Set value of a field""" # Ignore initialize process initializing = kwargs.get('_initializing_', False) if initializing: return # Remove acquisition wrappers value = aq_base(value) # Create File System Storage Info info = self.setFSSInfo(name, instance, value, **kwargs) # Wrap value if IObjectField.isImplementedBy(value): value = value.getRaw(self.instance) if IBaseUnit.isImplementedBy(value): value = value.getRaw() elif isinstance(value, File): value = value.data else: value = str(value) # Copy file on filesystem strategy = self.getStorageStrategy(name, instance) props = self.getStorageStrategyProperties(name, instance, info) strategy.setValueFile(value, **props) # Create RDF file fss_tool = getToolByName(instance, 'portal_fss') is_rdf_enabled = fss_tool.isRDFEnabled() rdf_script = fss_tool.getRDFScript() if is_rdf_enabled: # Replace rdf file rdf_value = info.getRDFValue(name, instance, rdf_script) strategy.setRDFFile(rdf_value, **props)
def marshall(self, instance, **kwargs): p = instance.getPrimaryField() body = p and instance[p.getName()] or '' pname = p and p.getName() or None content_type = length = None # Gather/Guess content type if IBaseUnit.providedBy(body): content_type = str(body.getContentType()) body = body.getRaw() else: if p and hasattr(p, 'getContentType'): content_type = p.getContentType(instance) or 'text/plain' else: content_type = body and guess_content_type( body) or 'text/plain' headers = [] fields = [f for f in instance.Schema().fields() if f.getName() != pname] for field in fields: if field.type in ('file', 'image', 'object'): continue accessor = field.getEditAccessor(instance) if not accessor: continue kw = {'raw': 1, 'field': field.__name__} value = mapply(accessor, **kw) if type(value) in [ListType, TupleType]: value = '\n'.join([str(v) for v in value]) headers.append((field.getName(), str(value))) headers.append(('Content-Type', content_type or 'text/plain')) header = formatRFC822Headers(headers) data = '%s\n\n%s' % (header, body) length = len(data) return (content_type, length, data)
def marshall(self, instance, **kwargs): p = instance.getPrimaryField() body = p and instance[p.getName()] or "" pname = p and p.getName() or None content_type = length = None # Gather/Guess content type if IBaseUnit.isImplementedBy(body): content_type = str(body.getContentType()) body = body.getRaw() else: if p and hasattr(p, "getContentType"): content_type = p.getContentType(instance) or "text/plain" else: content_type = body and guess_content_type(body) or "text/plain" headers = [] fields = [f for f in instance.Schema().fields() if f.getName() != pname] for field in fields: if field.type in ("file", "image", "object"): continue accessor = field.getEditAccessor(instance) if not accessor: continue kw = {"raw": 1, "field": field.__name__} value = mapply(accessor, **kw) if type(value) in [ListType, TupleType]: value = "\n".join([str(v) for v in value]) headers.append((field.getName(), str(value))) headers.append(("Content-Type", content_type or "text/plain")) header = formatRFC822Headers(headers) data = "%s\n\n%s" % (header, body) length = len(data) return (content_type, length, data)
def _process_input(self, value, file=None, default=None, mimetype=None, instance=None, **kwargs): """ """ if file is None: file = self._make_file(self.getName(), title='', file='', instance=instance) filename = kwargs.get('filename') or '' body = None if IBaseUnit.isImplementedBy(value): mimetype = value.getContentType() or mimetype filename = value.getFilename() or filename return value, mimetype, filename elif isinstance(value, self.content_class): filename = getattr(value, 'filename', value.getId()) mimetype = getattr(value, 'content_type', mimetype) return value, mimetype, filename elif isinstance(value, File): # In case someone changes the 'content_class' filename = getattr(value, 'filename', value.getId()) mimetype = getattr(value, 'content_type', mimetype) value = value.data elif isinstance(value, FileUpload) or shasattr(value, 'filename'): filename = value.filename # XXX Should be fixed eventually body = value.read(CHUNK) value.seek(0) elif isinstance(value, FileType) or shasattr(value, 'name'): # In this case, give preference to a filename that has # been detected before. Usually happens when coming from PUT(). if not filename: filename = value.name # Should we really special case here? for v in (filename, repr(value)): # Windows unnamed temporary file has '<fdopen>' in # repr() and full path in 'file.name' if '<fdopen>' in v: filename = '' # XXX Should be fixed eventually body = value.read(CHUNK) value.seek(0) elif isinstance(value, basestring): # Let it go, mimetypes_registry will be used below if available # if mimetype is None: # mimetype, enc = guess_content_type(filename, value, mimetype) pass elif isinstance(value, Pdata): pass elif shasattr(value, 'read') and shasattr(value, 'seek'): # Can't get filename from those. body = value.read(CHUNK) value.seek(0) elif value is None: # Special case for setDefault. value = '' else: klass = getattr(value, '__class__', None) raise TextFieldException('Value is not File or String (%s - %s)' % (type(value), klass)) if isinstance(value, Pdata): # XXX Should be fixed eventually value = str(value) filename = filename[max( filename.rfind('/'), filename.rfind('\\'), filename.rfind(':'), ) + 1:] if mimetype is None or mimetype == 'text/x-unknown-content-type': if body is None: body = value[:CHUNK] mtr = getToolByName(instance, 'mimetypes_registry', None) if mtr is not None: kw = {'mimetype': None, 'filename': filename} d, f, mimetype = mtr(body, **kw) else: mimetype, enc = guess_content_type(filename, body, mimetype) # mimetype, if coming from request can be like: # text/plain; charset='utf-8' #mimetype = str(mimetype).split(';')[0] mimetype = 'text/plain' file.update(value, instance, mimetype=mimetype, filename=filename) file.setContentType(instance, mimetype) file.setFilename(filename) return file, str(file.getContentType()), file.getFilename()
def _process_input(self, value, file=None, default=None, mimetype=None, instance=None, filename='', **kwargs): if file is None: file = self._make_file(self.getName(), title='', file='', instance=instance) if IBaseUnit.isImplementedBy(value): mimetype = value.getContentType() or mimetype filename = value.getFilename() or filename value = value.getRaw() elif isinstance(value, self.content_class): filename = getattr(value, 'filename', value.getId()) mimetype = getattr(value, 'content_type', mimetype) return value, mimetype, filename elif isinstance(value, File): # In case someone changes the 'content_class' filename = getattr(value, 'filename', value.getId()) mimetype = getattr(value, 'content_type', mimetype) value = value.data elif isinstance(value, xmlrpclib.Binary): value = value.data elif isinstance(value, FileUpload) or shasattr(value, 'filename'): filename = value.filename elif isinstance(value, FileType) or shasattr(value, 'name'): # In this case, give preference to a filename that has # been detected before. Usually happens when coming from PUT(). if not filename: filename = value.name # Should we really special case here? for v in (filename, repr(value)): # Windows unnamed temporary file has '<fdopen>' in # repr() and full path in 'file.name' if '<fdopen>' in v: filename = '' elif isinstance(value, basestring): # Let it go, mimetypes_registry will be used below if available pass elif (isinstance(value, Pdata) or (shasattr(value, 'read') and shasattr(value, 'seek'))): # Can't get filename from those. pass elif value is None: # Special case for setDefault value = '' else: klass = getattr(value, '__class__', None) raise FileFieldException('Value is not File or String (%s - %s)' % (type(value), klass)) filename = filename[max(filename.rfind('/'), filename.rfind('\\'), filename.rfind(':'), )+1:] file.manage_upload(value) if mimetype is None or mimetype == 'text/x-unknown-content-type': body = file.data if not isinstance(body, basestring): body = body.data mtr = getToolByName(instance, 'mimetypes_registry', None) if mtr is not None: kw = {'mimetype':None, 'filename':filename} # this may split the encoded file inside a multibyte character try: d, f, mimetype = mtr(body[:8096], **kw) except UnicodeDecodeError: d, f, mimetype = mtr(len(body) < 8096 and body or '', **kw) else: mimetype = getattr(file, 'content_type', None) if mimetype is None: mimetype, enc = guess_content_type(filename, body, mimetype) # mimetype, if coming from request can be like: # text/plain; charset='utf-8' mimetype = str(mimetype).split(';')[0].strip() setattr(file, 'content_type', mimetype) setattr(file, 'filename', filename) return file, mimetype, filename
def _process_input(self, value, file=None, default=None, mimetype=None, instance=None, **kwargs): """ """ if file is None: file = self._make_file(self.getName(), title='', file='', instance=instance) filename = kwargs.get('filename') or '' body = None if IBaseUnit.isImplementedBy(value): mimetype = value.getContentType() or mimetype filename = value.getFilename() or filename return value, mimetype, filename elif isinstance(value, self.content_class): filename = getattr(value, 'filename', value.getId()) mimetype = getattr(value, 'content_type', mimetype) return value, mimetype, filename elif isinstance(value, File): # In case someone changes the 'content_class' filename = getattr(value, 'filename', value.getId()) mimetype = getattr(value, 'content_type', mimetype) value = value.data elif isinstance(value, FileUpload) or shasattr(value, 'filename'): filename = value.filename # XXX Should be fixed eventually body = value.read(CHUNK) value.seek(0) elif isinstance(value, FileType) or shasattr(value, 'name'): # In this case, give preference to a filename that has # been detected before. Usually happens when coming from PUT(). if not filename: filename = value.name # Should we really special case here? for v in (filename, repr(value)): # Windows unnamed temporary file has '<fdopen>' in # repr() and full path in 'file.name' if '<fdopen>' in v: filename = '' # XXX Should be fixed eventually body = value.read(CHUNK) value.seek(0) elif isinstance(value, basestring): # Let it go, mimetypes_registry will be used below if available # if mimetype is None: # mimetype, enc = guess_content_type(filename, value, mimetype) pass elif isinstance(value, Pdata): pass elif shasattr(value, 'read') and shasattr(value, 'seek'): # Can't get filename from those. body = value.read(CHUNK) value.seek(0) elif value is None: # Special case for setDefault. value = '' else: klass = getattr(value, '__class__', None) raise TextFieldException('Value is not File or String (%s - %s)' % (type(value), klass)) if isinstance(value, Pdata): # XXX Should be fixed eventually value = str(value) filename = filename[max(filename.rfind('/'), filename.rfind('\\'), filename.rfind(':'), )+1:] if mimetype is None or mimetype == 'text/x-unknown-content-type': if body is None: body = value[:CHUNK] mtr = getToolByName(instance, 'mimetypes_registry', None) if mtr is not None: kw = {'mimetype':None, 'filename':filename} d, f, mimetype = mtr(body, **kw) else: mimetype, enc = guess_content_type(filename, body, mimetype) # mimetype, if coming from request can be like: # text/plain; charset='utf-8' #mimetype = str(mimetype).split(';')[0] mimetype = 'text/plain' file.update(value, instance, mimetype=mimetype, filename=filename) file.setContentType(instance, mimetype) file.setFilename(filename) return file, str(file.getContentType()), file.getFilename()
def testExternalEditor(self): #really a test that baseobject.__getitem__ returns something #which externaleditor can use obj = makeContent(self.folder, portal_type='SimpleType', id='obj') self.assertTrue(IBaseUnit.providedBy(obj.body))