2018-02-27 17:47:32 +01:00
|
|
|
import json
|
2018-03-01 17:57:45 +01:00
|
|
|
import os
|
2018-02-27 17:47:32 +01:00
|
|
|
|
2018-02-26 21:38:16 +01:00
|
|
|
import bottle
|
|
|
|
|
|
|
|
from cuizin import db
|
2018-03-01 17:57:45 +01:00
|
|
|
from cuizin.scraping import add_recipe
|
|
|
|
|
|
|
|
MODULE_DIR = os.path.dirname(os.path.realpath(__file__))
|
2018-02-26 21:38:16 +01:00
|
|
|
|
|
|
|
|
2018-02-27 17:47:32 +01:00
|
|
|
app = bottle.Bottle()
|
|
|
|
|
|
|
|
@app.hook('after_request')
|
|
|
|
def enable_cors():
|
|
|
|
"""
|
|
|
|
Add CORS headers at each request.
|
|
|
|
"""
|
|
|
|
# The str() call is required as we import unicode_literal and WSGI
|
|
|
|
# headers list should have plain str type.
|
|
|
|
bottle.response.headers[str('Access-Control-Allow-Origin')] = str('*')
|
|
|
|
bottle.response.headers[str('Access-Control-Allow-Methods')] = str(
|
|
|
|
'PUT, GET, POST, DELETE, OPTIONS, PATCH'
|
|
|
|
)
|
|
|
|
bottle.response.headers[str('Access-Control-Allow-Headers')] = str(
|
|
|
|
'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
|
|
|
|
)
|
2018-02-26 21:38:16 +01:00
|
|
|
|
|
|
|
|
2018-02-27 17:47:32 +01:00
|
|
|
@app.route('/api/v1', ['GET', 'OPTIONS'])
|
2018-02-26 21:38:16 +01:00
|
|
|
def api_v1_index():
|
2018-03-01 17:57:45 +01:00
|
|
|
"""
|
|
|
|
API index route
|
|
|
|
"""
|
2018-02-26 21:38:16 +01:00
|
|
|
return {
|
2018-03-01 17:57:45 +01:00
|
|
|
'recipes': '/api/v1/recipes',
|
|
|
|
'recipe': '/api/v1/recipe/:id'
|
2018-02-26 21:38:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-27 17:47:32 +01:00
|
|
|
@app.route('/api/v1/recipes', ['GET', 'OPTIONS'])
|
2018-02-26 21:38:16 +01:00
|
|
|
def api_v1_recipes():
|
2018-03-01 17:57:45 +01:00
|
|
|
"""
|
|
|
|
List all recipes
|
|
|
|
"""
|
2018-02-27 17:47:32 +01:00
|
|
|
# CORS
|
|
|
|
if bottle.request.method == 'OPTIONS':
|
|
|
|
return ''
|
|
|
|
|
2018-02-26 21:38:16 +01:00
|
|
|
return {
|
|
|
|
'recipes': [
|
|
|
|
recipe.to_dict() for recipe in db.Recipe.select()
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-27 17:47:32 +01:00
|
|
|
@app.post('/api/v1/recipes')
|
|
|
|
def api_v1_recipes_post():
|
2018-03-01 17:57:45 +01:00
|
|
|
"""
|
|
|
|
Create a new recipe from URL
|
|
|
|
"""
|
2018-02-27 17:47:32 +01:00
|
|
|
data = json.load(bottle.request.body)
|
|
|
|
if 'url' not in data:
|
|
|
|
return {
|
|
|
|
'error': 'No URL provided'
|
|
|
|
}
|
|
|
|
|
|
|
|
recipes = []
|
|
|
|
try:
|
2018-02-28 22:06:21 +01:00
|
|
|
recipe = db.Recipe.select().where(
|
2018-02-27 17:47:32 +01:00
|
|
|
db.Recipe.url == data['url']
|
2018-02-28 22:06:21 +01:00
|
|
|
).first()
|
|
|
|
assert recipe
|
|
|
|
recipes = [recipe.to_dict()]
|
|
|
|
except AssertionError:
|
|
|
|
recipes = [add_recipe(data['url']).to_dict()]
|
2018-02-27 17:47:32 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
'recipes': recipes
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/api/v1/recipe/:id', ['GET', 'OPTIONS'])
|
2018-02-26 21:38:16 +01:00
|
|
|
def api_v1_recipe(id):
|
2018-03-01 17:57:45 +01:00
|
|
|
"""
|
|
|
|
Get a given recipe from db
|
|
|
|
"""
|
2018-02-27 17:47:32 +01:00
|
|
|
# CORS
|
|
|
|
if bottle.request.method == 'OPTIONS':
|
|
|
|
return ''
|
|
|
|
|
2018-02-26 21:38:16 +01:00
|
|
|
return {
|
|
|
|
'recipes': [
|
|
|
|
recipe.to_dict() for recipe in db.Recipe.select().where(
|
|
|
|
db.Recipe.id == id
|
|
|
|
)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-28 22:06:21 +01:00
|
|
|
@app.delete('/api/v1/recipe/:id', ['DELETE', 'OPTIONS'])
|
|
|
|
def api_v1_recipe_delete(id):
|
2018-03-01 17:57:45 +01:00
|
|
|
"""
|
|
|
|
Delete a given recipe from db
|
|
|
|
"""
|
2018-02-28 22:06:21 +01:00
|
|
|
# CORS
|
|
|
|
if bottle.request.method == 'OPTIONS':
|
|
|
|
return ''
|
|
|
|
|
|
|
|
db.Recipe.delete().where(
|
|
|
|
db.Recipe.id == id
|
|
|
|
).execute()
|
|
|
|
|
|
|
|
return {
|
|
|
|
'status': 'OK'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-01 17:57:45 +01:00
|
|
|
@app.get('/')
|
|
|
|
def index():
|
|
|
|
"""
|
|
|
|
Return built index.html file
|
|
|
|
"""
|
|
|
|
return bottle.static_file('index.html',
|
|
|
|
root=os.path.join(MODULE_DIR, 'dist'))
|
|
|
|
|
|
|
|
|
2018-02-26 21:38:16 +01:00
|
|
|
@app.get('/static/<filename:path>')
|
|
|
|
def get_static_files(filename):
|
2018-03-01 17:57:45 +01:00
|
|
|
"""
|
|
|
|
Get Static files
|
|
|
|
"""
|
|
|
|
return bottle.static_file(filename,
|
|
|
|
root=os.path.join(MODULE_DIR, 'dist', 'static'))
|