Fixed XML and JSON export

This commit is contained in:
Andrew Davidson 2014-11-02 16:07:07 -05:00
parent 7bcc1b5ef0
commit 38c05e3ce3

View file

@ -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 = "<xml>\n"
for b in blist:
out += "\t<bookmark>\n"
out += "\t\t<title>"+b.title+"</title>\n"
out += "\t\t<short>"+b.short+"</short>\n"
out += "\t\t<created_at>"+b.created_at.strftime("%Y-%m-%d %H:%M:%S")+"</created_at>\n"
out += "\t\t<unread>"
if b.unread:
out += "True"
else:
out += "False"
out += "</unread>\n"
out += "\t\t<url>"+b.url+"</url>\n"
out += "\t\t<tags>"
for t in b.tags:
out += t.name + " "
out += "</tags>\n"
out += "\t\t<note>\n"
out += "\t\t\t"+b.note+"\n"
out += "\t\t</note>\n"
out += "\t</bookmark>\n"
out += "</xml>\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)