2017-04-03 17:29:29 +02:00
# coding: utf-8
"""
This module contains the definition of the Bottle web app .
"""
from __future__ import (
absolute_import , division , print_function , unicode_literals
)
2017-04-13 23:24:31 +02:00
import functools
import json
import logging
2017-04-03 17:29:29 +02:00
import os
import bottle
2017-04-13 23:24:31 +02:00
import canister
2017-04-03 17:29:29 +02:00
from flatisfy import database
2017-04-13 23:24:31 +02:00
from flatisfy . tools import DateAwareJSONEncoder
2017-04-03 17:29:29 +02:00
from flatisfy . web . routes import api as api_routes
2017-04-13 23:24:31 +02:00
from flatisfy . web . configplugin import ConfigPlugin
2017-04-03 17:29:29 +02:00
from flatisfy . web . dbplugin import DatabasePlugin
2017-04-13 23:24:31 +02:00
class QuietWSGIRefServer ( bottle . WSGIRefServer ) :
"""
Quiet implementation of Bottle built - in WSGIRefServer , as ` Canister ` is
handling the logging through standard Python logging .
"""
# pylint: disable=locally-disabled,too-few-public-methods
quiet = True
2017-04-03 17:29:29 +02:00
def _serve_static_file ( filename ) :
"""
Helper function to serve static file .
"""
return bottle . static_file (
filename ,
root = os . path . join (
os . path . dirname ( os . path . realpath ( __file__ ) ) ,
" static "
)
)
def get_app ( config ) :
"""
Get a Bottle app instance with all the routes set - up .
: return : The built bottle app .
"""
2017-05-02 18:35:34 +02:00
get_session = database . init_db ( config [ " database " ] , config [ " search_index " ] )
2017-04-03 17:29:29 +02:00
app = bottle . default_app ( )
app . install ( DatabasePlugin ( get_session ) )
2017-04-13 23:24:31 +02:00
app . install ( ConfigPlugin ( config ) )
app . config . setdefault ( " canister.log_level " , logging . root . level )
app . config . setdefault ( " canister.log_path " , None )
app . config . setdefault ( " canister.debug " , False )
app . install ( canister . Canister ( ) )
# Use DateAwareJSONEncoder to dump JSON strings
# From http://stackoverflow.com/questions/21282040/bottle-framework-how-to-return-datetime-in-json-response#comment55718456_21282666. pylint: disable=locally-disabled,line-too-long
bottle . install (
bottle . JSONPlugin (
json_dumps = functools . partial ( json . dumps , cls = DateAwareJSONEncoder )
)
)
2017-04-03 17:29:29 +02:00
# API v1 routes
app . route ( " /api/v1/ " , " GET " , api_routes . index_v1 )
2017-04-13 23:24:31 +02:00
app . route ( " /api/v1/time_to/places " , " GET " , api_routes . time_to_places_v1 )
2017-04-03 17:29:29 +02:00
app . route ( " /api/v1/flats " , " GET " , api_routes . flats_v1 )
2017-04-13 23:24:31 +02:00
app . route ( " /api/v1/flats/status/:status " , " GET " ,
api_routes . flats_by_status_v1 )
2017-04-13 23:22:11 +02:00
app . route ( " /api/v1/flat/:flat_id " , " GET " , api_routes . flat_v1 )
2017-04-13 23:24:31 +02:00
app . route ( " /api/v1/flat/:flat_id/status " , " POST " ,
api_routes . update_flat_status_v1 )
2017-05-03 19:17:19 +02:00
app . route ( " /api/v1/flat/:flat_id/notes " , " POST " ,
api_routes . update_flat_notes_v1 )
2017-05-03 22:10:24 +02:00
app . route ( " /api/v1/flat/:flat_id/notation " , " POST " ,
api_routes . update_flat_notation_v1 )
2017-04-03 17:29:29 +02:00
2017-05-02 18:35:34 +02:00
app . route ( " /api/v1/search " , " POST " , api_routes . search_v1 )
2017-04-03 17:29:29 +02:00
# Index
app . route ( " / " , " GET " , lambda : _serve_static_file ( " index.html " ) )
# Static files
2017-04-26 19:12:30 +02:00
app . route ( " /favicon.ico " , " GET " ,
lambda : _serve_static_file ( " favicon.ico " ) )
2017-04-25 15:58:06 +02:00
app . route (
" /assets/<filename:path> " , " GET " ,
lambda filename : _serve_static_file ( " /assets/ {} " . format ( filename ) )
)
2017-04-26 19:12:30 +02:00
app . route (
" /img/<filename:path> " , " GET " ,
lambda filename : _serve_static_file ( " /img/ {} " . format ( filename ) )
)
2017-04-03 17:29:29 +02:00
return app