Commit dd325153 authored by Cédric's avatar Cédric
Browse files

You can now specify any kind of conditions for each restaurants.

parent 44e3cf28
#! /usr/bin/env python
# -*- coding: utf-8 -*-
def default():
"""
Default function, called if no 'condition' function is defined for the restaurant (in restaurants.json).
"""
return True
def quick():
"""
Quick only on fridays.
"""
import datetime
return datetime.datetime.today().weekday() == 4
def rain():
"""
This test can be used for the 'Chambre de Commerce' because it is far from us.
"""
import requests
import json
try:
result = requests.get("http://api.openweathermap.org/data/2.5/weather?q=Luxembourg,Lux")
except:
return True
if result.status_code != 200:
return True
json_result = json.loads(result.text)
return json_result['weather'][0]['main'] != 'Rain'
\ No newline at end of file
......@@ -9,12 +9,14 @@
"name":"Quick",
"url":"http://www.quick.lu/",
"coordinates":"",
"comment":"Ne pas oublier les reductions."
"comment":"Ne pas oublier les reductions.",
"condition":"quick"
},
{
"name":"Chambre de commerce",
"url":"http://www.cc.lu/autres-services/restaurant-la-table-de-mercure/",
"coordinates":""
"coordinates":"",
"condition":"rain"
},
{
"name":"EXKi",
......
......@@ -5,21 +5,15 @@ import random
import datetime
from flask import jsonify, request
import conditions
from refuge import app, restaurants
@app.route('/', methods=['GET'])
def index():
# QUICK on Fridays
if datetime.date.weekday(datetime.datetime.today()) == 4:
fridayMessage = {
"name":"Quick",
"url":"http://www.quick.lu/",
"coordinates":"",
"comment":"Ne pas oublier les reductions."
}
return jsonify(fridayMessage)
else:
return jsonify(random.choice(restaurants))
currently_avaible_restaurants = [dict((k , v) for k,v in restaurant.items() if k != "condition") \
for restaurant in restaurants \
if getattr(conditions, restaurant.get("condition", "default"))()]
return jsonify(random.choice(currently_avaible_restaurants))
@app.errorhandler(404)
def not_found(error=None):
......@@ -29,4 +23,4 @@ def not_found(error=None):
}
resp = jsonify(message)
resp.status_code = 404
return resp
return resp
\ No newline at end of file
......@@ -3,3 +3,4 @@ Jinja2
Werkzeug
distribute
wsgiref
requests
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment