2015-12-23 23:58:16 +01:00
|
|
|
#!/usr/bin/env python3
|
2015-12-24 20:34:34 +01:00
|
|
|
import bottle
|
2015-12-23 23:58:16 +01:00
|
|
|
from bottle.ext import sqlalchemy
|
2015-12-25 01:43:49 +01:00
|
|
|
from sqlalchemy import create_engine
|
2015-12-23 23:58:16 +01:00
|
|
|
|
2015-12-25 20:11:56 +01:00
|
|
|
import config
|
2015-12-24 20:34:34 +01:00
|
|
|
import database
|
|
|
|
import routes
|
2015-12-25 01:57:02 +01:00
|
|
|
import tools
|
2015-12-23 23:58:16 +01:00
|
|
|
|
2015-12-24 20:34:34 +01:00
|
|
|
# Initialize db and include the SQLAlchemy plugin in bottle
|
2015-12-25 20:11:56 +01:00
|
|
|
engine = create_engine('sqlite:///%s' % (config.database,), echo=True)
|
2015-12-23 23:58:16 +01:00
|
|
|
|
2015-12-24 20:34:34 +01:00
|
|
|
app = bottle.Bottle()
|
|
|
|
plugin = sqlalchemy.Plugin(
|
|
|
|
# SQLAlchemy engine created with create_engine function.
|
|
|
|
engine,
|
|
|
|
# SQLAlchemy metadata, required only if create=True.
|
|
|
|
database.Base.metadata,
|
|
|
|
# Keyword used to inject session database in a route (default 'db').
|
|
|
|
keyword='db',
|
|
|
|
# If it is true, execute `metadata.create_all(engine)` when plugin is
|
|
|
|
# applied (default False).
|
|
|
|
create=True,
|
|
|
|
# If it is true, plugin commit changes after route is executed (default
|
|
|
|
# True).
|
|
|
|
commit=True,
|
|
|
|
# If it is true and keyword is not defined, plugin uses **kwargs argument
|
|
|
|
# to inject session database (default False).
|
|
|
|
use_kwargs=False
|
|
|
|
)
|
2015-12-23 23:58:16 +01:00
|
|
|
|
2015-12-24 20:34:34 +01:00
|
|
|
app.install(plugin)
|
2015-12-23 23:58:16 +01:00
|
|
|
|
|
|
|
|
2015-12-24 20:34:34 +01:00
|
|
|
# Routes
|
2015-12-25 20:09:56 +01:00
|
|
|
# TODO: Routes for deletion
|
2015-12-25 01:57:02 +01:00
|
|
|
@app.get("/")
|
|
|
|
def index():
|
|
|
|
return tools.APIResponse(tools.pretty_json({
|
|
|
|
"papers": "/papers/?id={id}&doi={doi}&arxiv_id={arxiv_id}",
|
|
|
|
}))
|
|
|
|
|
2015-12-24 20:34:34 +01:00
|
|
|
app.get("/papers", callback=routes.get.fetch_papers)
|
|
|
|
app.get("/papers/<id:int>", callback=routes.get.fetch_by_id)
|
2015-12-25 20:09:56 +01:00
|
|
|
app.get("/papers/<id:int>/relationships/<name>",
|
|
|
|
callback=routes.get.fetch_relationship)
|
2015-12-25 21:38:59 +01:00
|
|
|
app.get("/papers/<id:int>/<name>",
|
|
|
|
callback=routes.get.fetch_relationship)
|
2015-12-24 20:34:34 +01:00
|
|
|
|
|
|
|
|
|
|
|
app.post("/papers", callback=routes.post.create_paper)
|
|
|
|
|
2015-12-25 20:09:56 +01:00
|
|
|
app.post("/papers/<id:int>/relationships/<name>",
|
|
|
|
callback=routes.post.update_relationships)
|
|
|
|
# Complete replacement of relationships is forbidden
|
|
|
|
app.route("/papers/<id:int>/relationships/<name>", method="PATCH",
|
|
|
|
callback=lambda id, name: bottle.HTTPError(403, "Forbidden"))
|
2015-12-23 23:58:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2015-12-25 20:11:56 +01:00
|
|
|
app.run(host=config.host, port=config.port, debug=(not config.production))
|