diff --git a/server.py b/server.py index dec6549..010301b 100644 --- a/server.py +++ b/server.py @@ -1,39 +1,59 @@ import bottle -from models import * +import sqlite3 +import json @bottle.get("/") def index(): return bottle.template('index') -@bottle.get("/locations/") -def location(article_id): - return (Location - .select() - .where(Location.article == article_id) - .order_by(Location.confidence.desc(), Location.id.asc()) - .dicts() - .get()) +# @bottle.get("/locations/") +# def location(article_id): +# return (Location +# .select() +# .where(Location.article == article_id) +# .order_by(Location.confidence.desc(), Location.id.asc()) +# .dicts() +# .get()) -@bottle.get("/categories/") -def category(article_id): - categories = (Category - .select() - .where(Category.article == article_id)) +# @bottle.get("/categories/") +# def category(article_id): +# categories = (Category +# .select() +# .where(Category.article == article_id)) - return { - "article": article_id, - "categories": [c.name for c in categories] - } +# return { +# "article": article_id, +# "categories": [c.name for c in categories] +# } -@bottle.get("/articles/") -def article(article_id): - # articles have a datetime field and need special serialization - # return json_util.dumps(Article - # .select() - # .where(Article.id == article_id) - # .dicts() - # .get()) - pass +@bottle.get("/articles/") +def articles(): + conn = sqlite3.connect('violence.db') + c = conn.cursor() + c = c.execute(""" + SELECT article.id, article.date, article.place, article.description, category.name, + location.confidence, location.lat, location.lng, location.match, location.returned_place + FROM article + JOIN category ON article.id = category.article_id + JOIN location ON article.id = location.article_id + """) + + articles = [] + for article in c.fetchall(): + articles.append({ + "id": article[0], + "date": article[1], + "place": article[2], + "desc": article[3], + "cat": article[4], + "conf": article[5], + "lat": article[6], + "lng": article[7], + "match": article[8], + "returned_place": article[9] + }) + conn.close() + return json.dumps(articles) @bottle.get('/static/') def server_static(filepath):