From 38c05e3ce3d3c11b238dac2ada41ecc727cac0ad Mon Sep 17 00:00:00 2001 From: Andrew Davidson Date: Sun, 2 Nov 2014 16:07:07 -0500 Subject: [PATCH] Fixed XML and JSON export --- bookie.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/bookie.py b/bookie.py index d0684f6..b3a0644 100644 --- a/bookie.py +++ b/bookie.py @@ -304,10 +304,7 @@ def encode_model(self, obj): @login_required def list(count=100, format="HTML"): loc = '/all/' + str(count) + '/' - if format == "JSON": - blist = Bookmark.objects(deleted=False).order_by("-created_at").only("url","title","note","created_at","tags","unread").limit(count) - return blist.to_json() - elif format == "CSV": + if format == "csv": blist = Bookmark.objects(deleted=False).order_by("-created_at").limit(count) out = "" tags = '' @@ -316,6 +313,53 @@ def list(count=100, format="HTML"): tags = tags + t.name + ' ' out = out + b.url + ',' + b.title + ',' + b.note + ',' + b.created_at.isoformat() + ',' + tags + ',' + str(b.unread) + ',bookie\n' return out + elif format == "xml": + blist = Bookmark.objects(deleted=False).order_by("-created_at").only("url","title","short","note","created_at","tags","unread").limit(count) + out = "\n" + for b in blist: + out += "\t\n" + out += "\t\t"+b.title+"\n" + out += "\t\t"+b.short+"\n" + out += "\t\t"+b.created_at.strftime("%Y-%m-%d %H:%M:%S")+"\n" + out += "\t\t" + if b.unread: + out += "True" + else: + out += "False" + out += "\n" + out += "\t\t"+b.url+"\n" + out += "\t\t" + for t in b.tags: + out += t.name + " " + out += "\n" + out += "\t\t\n" + out += "\t\t\t"+b.note+"\n" + out += "\t\t\n" + out += "\t\n" + out += "\n" + return out + elif format == "json": + blist = Bookmark.objects(deleted=False).order_by("-created_at").only("url","title","short","note","created_at","tags","unread").limit(count) + out = "" + for b in blist: + out += "{\n" + out += '\t"title": "'+b.title+'",\n' + out += '\t"short": "'+b.short+'",\n' + out += '\t"created_at": "'+b.created_at.strftime("%Y-%m-%d %H:%M:%S")+'",\n' + out += '\t"unread": "' + if b.unread: + out += "True" + else: + out += "False" + out += '",\n' + out += '\t"url": "'+b.url+'",\n' + out += '\t"tags": "' + for t in b.tags: + out += t.name + " " + out += '",\n' + out += '\t"note": "'+b.note+'"\n' + out += "}\n" + return out else: blist = Bookmark.objects(deleted=False).order_by("-created_at").limit(count) return render_template("list.html", blist=blist, loc=loc)