get all articles using sqlite3

This commit is contained in:
Joshua Widmann 2015-02-07 18:49:51 +01:00
commit 677a839012

View file

@ -1,39 +1,59 @@
import bottle import bottle
from models import * import sqlite3
import json
@bottle.get("/") @bottle.get("/")
def index(): def index():
return bottle.template('index') return bottle.template('index')
@bottle.get("/locations/<article_id:int>") # @bottle.get("/locations/<article_id:int>")
def location(article_id): # def location(article_id):
return (Location # return (Location
.select() # .select()
.where(Location.article == article_id) # .where(Location.article == article_id)
.order_by(Location.confidence.desc(), Location.id.asc()) # .order_by(Location.confidence.desc(), Location.id.asc())
.dicts() # .dicts()
.get()) # .get())
@bottle.get("/categories/<article_id:int>") # @bottle.get("/categories/<article_id:int>")
def category(article_id): # def category(article_id):
categories = (Category # categories = (Category
.select() # .select()
.where(Category.article == article_id)) # .where(Category.article == article_id))
return { # return {
"article": article_id, # "article": article_id,
"categories": [c.name for c in categories] # "categories": [c.name for c in categories]
} # }
@bottle.get("/articles/<article_id:int>") @bottle.get("/articles/")
def article(article_id): def articles():
# articles have a datetime field and need special serialization conn = sqlite3.connect('violence.db')
# return json_util.dumps(Article c = conn.cursor()
# .select() c = c.execute("""
# .where(Article.id == article_id) SELECT article.id, article.date, article.place, article.description, category.name,
# .dicts() location.confidence, location.lat, location.lng, location.match, location.returned_place
# .get()) FROM article
pass 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/<filepath:path>') @bottle.get('/static/<filepath:path>')
def server_static(filepath): def server_static(filepath):