def soql2atom(loginResult, soql, title): soqlWithFields = addRequiredFieldsToSoql(soql) userInfo = loginResult[beatbox._tPartnerNS.userInfo] serverUrl = str(loginResult[beatbox._tPartnerNS.serverUrl]) (scheme, host, path, params, query, frag) = urlparse(serverUrl) sfbaseUrl = scheme + "://" + host + "/" thisUrl = "http://" + os.environ["HTTP_HOST"] + os.environ["REQUEST_URI"] qr = svc.query(soqlWithFields) atom_ns = "http://www.w3.org/2005/Atom" ent_ns = "urn:sobject.enterprise.soap.sforce.com" print("content-type: application/atom+xml") doGzip = "HTTP_ACCEPT_ENCODING" in os.environ and "gzip" in string.lower(os.environ["HTTP_ACCEPT_ENCODING"]).split(',') if doGzip: print("content-encoding: gzip") print("") x = beatbox.XmlWriter(doGzip) x.startPrefixMapping("a", atom_ns) x.startPrefixMapping("s", ent_ns) x.startElement(atom_ns, "feed") x.writeStringElement(atom_ns, "title", title) x.characters("\n") x.startElement(atom_ns, "author") x.writeStringElement(atom_ns, "name", str(userInfo.userFullName)) x.endElement() x.characters("\n") rel = AttributesNSImpl( {(None, "rel"): "self", (None, "href"): thisUrl}, {(None, "rel"): "rel", (None, "href"): "href"}) x.startElement(atom_ns, "link", rel) x.endElement() x.writeStringElement(atom_ns, "updated", datetime.datetime.utcnow().isoformat() + "Z") x.writeStringElement(atom_ns, "id", thisUrl + "&userid=" + str(loginResult[beatbox._tPartnerNS.userId])) x.characters("\n") type = AttributesNSImpl({(None, "type"): "html"}, {(None, "type"): "type"}) for row in qr[sf.records:]: x.startElement(atom_ns, "entry") desc = "" x.writeStringElement(atom_ns, "title", str(row[2])) for col in row[2:]: if col._name[1] == 'Id': x.writeStringElement(atom_ns, "id", sfbaseUrl + str(col)) writeLink(x, atom_ns, "link", "alternate", "text/html", sfbaseUrl + str(col)) elif col._name[1] == 'SystemModstamp': x.writeStringElement(atom_ns, "updated", str(col)) elif col._name[1] == 'CreatedDate': x.writeStringElement(atom_ns, "published", str(col)) elif str(col) != "": desc = desc + "<b>" + col._name[1] + "</b> : " + str(col) + "<br>" x.writeStringElement(ent_ns, col._name[1], str(col)) x.startElement(atom_ns, "content", type) x.characters(desc) x.endElement() # content x.characters("\n") x.endElement() # entry x.endElement() # feed print(x.endDocument())
def test_gzip(self): # note this doesn't use self.w as that's not configured to zip w = beatbox.XmlWriter(True) w.startPrefixMapping("q", "urn:test") w.startElement("urn:test", "root") w.writeStringElement("urn:test", "child", "text") w.endElement() zipped = w.endDocument() gz = gzip.GzipFile(fileobj=BytesIO(zipped)) xml = gz.read() self.assertEqual(b'<?xml version="1.0" encoding="utf-8"?>\n' b'<q:root xmlns:q="urn:test"><q:child>text</q:child></q:root>', xml)
def setUp(self): self.w = beatbox.XmlWriter(False) self.w.startPrefixMapping("q", "urn:test") self.w.startElement("urn:test", "root")