def demarshall(self, instance, data, **kwargs): # We don't want to pass file forward. if 'file' in kwargs: if not data: # TODO Yuck! Shouldn't read the whole file, never. # OTOH, if you care about large files, you should be # using the PrimaryFieldMarshaller or something # similar. data = kwargs['file'].read() del kwargs['file'] headers, body = parseRFC822(data) query = {} for k, v in headers.items(): if not k.startswith("query"): continue else: index = int(k[5]) sub_key = k.split("_")[1] query_part = query.get(index, {}) query_part[sub_key] = v query[index] = query_part del headers[k] query = [facet[1] for facet in sorted(query.items())] header = formatRFC822Headers(headers.items()) data = '%s\n\n%s' % (header, body) try: return RFC822Marshaller.demarshall(self, instance, data, **kwargs) finally: instance.query = query
def _updateRequest(self, request, content_type): """ Similar to the same method in atompub. We change the request so that the metadata is in the right place, then append the content. This is only called for multipart posts. """ # This seems to be the safest place to get hold of the actual message # body. request.stdin.seek(0) message = message_from_file(request.stdin) atom, payload = message.get_payload() # Call get_payload with decode=True, so it can handle the transfer # encoding for us, if any. dom = parse(StringIO(atom.get_payload(decode=True))) # Get the payload content = payload.get_payload(decode=True) # Assemble a new request title = self.getValueFromDOM('title', dom) request['Title'] = title headers = self.getHeaders(dom, METADATA_MAPPING) headers.append(('Content-Transfer-Encoding', 'base64')) header = formatRFC822Headers(headers) # make sure content is not None data = '%s\n\n%s' % (header, b64encode(content)) request['Content-Length'] = len(data) request['BODYFILE'] = StringIO(data) return request
def marshall(self, instance, **kwargs): content_type, length, data = RFC822Marshaller.marshall(self, instance, **kwargs) headers, body = parseRFC822(data) headers = headers.items() for i, query in enumerate(instance.query): for key, value in query.items(): if isinstance(value, list): value = "\n".join(value) header_key = 'query%d_%s' % (i, key) headers.append((header_key, value)) header = formatRFC822Headers(headers) data = '%s\n\n%s' % (header, body) length = len(data) return (content_type, length, data)
def _updateRequest(self, request): """ The body.seek(0) looks funny, but I do that to make sure I get all the content, no matter who accessed the body file before me. """ # then we update the body of the request body = request.get('BODYFILE') # update headers from the request body # make sure we read from the beginning body.seek(0) dom = parse(body) title = self.getValueFromDOM('title', dom) request['Title'] = title headers = self.getHeaders(dom, METADATA_MAPPING) header = formatRFC822Headers(headers) content = self.getValueFromDOM('content', dom) # make sure content is not None content = content and content or '' data = '%s\n\n%s' % (header, content.encode('utf-8')) length = len(data) request['Content-Length'] = length body_file = StringIO(data) request['BODYFILE'] = body_file return request