From 7a961b9fc5ed17d54d6c866082073b4aa3ac6ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arne=20Schl=C3=BCter?= Date: Sat, 7 Feb 2015 19:22:01 +0100 Subject: [PATCH] Massage the API response to look like what we want --- server.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/server.py b/server.py index 549be5b..ed0c307 100644 --- a/server.py +++ b/server.py @@ -49,22 +49,26 @@ def articles(): JOIN category ON article.id = category.article_id """) - articles = [] + articles = {} for article in c.fetchall(): article_id = article[0] if locations.get(article_id): - articles.append({ - "id": article_id, - "date": article[1], - "place": article[2], - "description": article[3], - "category": article[4], - "lat": locations[article_id][0], - "lng": locations[article_id][1], - "place": locations[article_id][2] - }) + if articles.get(article_id): + articles[article_id]["categories"].append(article[4]) + else: + articles[article_id] = { + "id": article_id, + "date": article[1], + "place": article[2], + "description": article[3], + "categories": [article[4]], + "lat": locations[article_id][0], + "lng": locations[article_id][1], + "place": locations[article_id][2] + } conn.close() - return json.dumps(articles) + + return json.dumps([article for article_id, article in articles.items()]) @bottle.get('/static/') def server_static(filepath):