2017-06-10 19:07:23 +02:00
|
|
|
import logging
|
|
|
|
|
2017-07-04 20:01:49 +02:00
|
|
|
from flask import Flask, jsonify, url_for, redirect, abort, request
|
2017-06-10 19:00:18 +02:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
from sqlalchemy import text
|
2017-08-06 11:47:54 +02:00
|
|
|
from raven.contrib.flask import Sentry
|
2017-06-09 07:56:55 +02:00
|
|
|
import config
|
|
|
|
|
2017-06-10 19:00:18 +02:00
|
|
|
app = Flask(__name__)
|
2017-08-06 11:47:54 +02:00
|
|
|
if not config.DEBUG:
|
|
|
|
sentry = Sentry(app=app, dsn=config.sentry_url, logging=True, level=logging.ERROR)
|
2017-06-11 18:48:42 +02:00
|
|
|
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://{user}:{password}@{host}/{db}?{settings}".format(**config.db)
|
2017-06-10 19:00:18 +02:00
|
|
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
|
|
|
db = SQLAlchemy(app)
|
2017-06-09 07:56:55 +02:00
|
|
|
|
2017-06-10 21:53:49 +02:00
|
|
|
|
2017-06-10 19:07:23 +02:00
|
|
|
@app.before_first_request
|
|
|
|
def setup_logging():
|
|
|
|
if not app.debug:
|
|
|
|
# In production mode, add log handler to sys.stderr.
|
|
|
|
app.logger.addHandler(logging.StreamHandler())
|
|
|
|
app.logger.setLevel(logging.INFO)
|
|
|
|
|
2017-06-09 13:27:41 +02:00
|
|
|
|
2017-06-10 21:53:49 +02:00
|
|
|
@app.errorhandler(404)
|
|
|
|
@app.errorhandler(400)
|
|
|
|
def handle_invalid_usage(error):
|
|
|
|
response = jsonify({
|
|
|
|
"code": error.code,
|
|
|
|
"name": error.name,
|
|
|
|
"description": error.description
|
|
|
|
})
|
|
|
|
response.status_code = error.code
|
|
|
|
return response
|
|
|
|
|
2017-06-11 18:48:42 +02:00
|
|
|
|
2017-06-09 13:28:28 +02:00
|
|
|
@app.route('/api/venue/')
|
2017-06-09 07:56:55 +02:00
|
|
|
def get_venues():
|
2017-06-10 19:00:18 +02:00
|
|
|
sql = text("SELECT * FROM venues")
|
|
|
|
results = db.engine.execute(sql).fetchall()
|
|
|
|
venues = []
|
|
|
|
for row in results:
|
|
|
|
venue = dict(row)
|
|
|
|
venue["meals_url"] = url_for("get_meals", venueid=venue["id"], _external=True)
|
|
|
|
venues.append(venue)
|
|
|
|
return jsonify(venues)
|
2017-06-09 07:56:55 +02:00
|
|
|
|
|
|
|
|
2017-06-09 13:28:28 +02:00
|
|
|
@app.route('/api/venue/<int:venueid>/')
|
2017-07-04 20:01:49 +02:00
|
|
|
def get_meals(venueid):
|
|
|
|
if request.args.get('mode', "week") == "week":
|
2017-06-10 21:53:49 +02:00
|
|
|
sql_range = " AND YEARWEEK(date, 1) = YEARWEEK(CURDATE(), 1) + :offset"
|
2017-07-04 20:01:49 +02:00
|
|
|
elif request.args.get('mode') == "day":
|
2017-06-10 21:53:49 +02:00
|
|
|
sql_range = " AND date=DATE_ADD(CURDATE(), INTERVAL :offset DAY) "
|
2017-06-10 19:00:18 +02:00
|
|
|
else:
|
|
|
|
return abort(400)
|
|
|
|
sql = """SELECT venue, date, name, description
|
2017-06-09 07:56:55 +02:00
|
|
|
FROM menus
|
|
|
|
JOIN meals ON meals.id = menus.meal
|
2017-06-10 19:00:18 +02:00
|
|
|
WHERE venue = :venue"""
|
2017-07-04 20:01:49 +02:00
|
|
|
results = db.engine.execute(text(sql + sql_range),
|
|
|
|
{"venue": venueid, "offset": request.args.get('offset', 0)}
|
|
|
|
).fetchall()
|
2017-06-10 19:00:18 +02:00
|
|
|
menus = []
|
|
|
|
for row in results:
|
|
|
|
menu = dict(row)
|
|
|
|
menu["date"] = menu["date"].isoformat()
|
|
|
|
menus.append(menu)
|
|
|
|
return jsonify(menus)
|
2017-06-09 07:56:55 +02:00
|
|
|
|
|
|
|
|
2017-06-10 19:00:18 +02:00
|
|
|
@app.route("/api/")
|
2017-06-09 07:56:55 +02:00
|
|
|
def redirect_to_correct_api():
|
|
|
|
return redirect(url_for("get_venues"))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2017-06-09 13:27:41 +02:00
|
|
|
app.run(debug=True)
|