moves routes for filling stations to extra file

This commit is contained in:
Joachim Lusiardi 2017-11-12 09:44:51 +01:00
parent eaacd3f42e
commit e9bb7986f4
2 changed files with 92 additions and 84 deletions

View File

@ -2,13 +2,12 @@ from flask import url_for, redirect, render_template, request, jsonify
from flask_security import login_required
from flask_security.core import current_user
from sqlalchemy.exc import IntegrityError
import requests
import json
from ..entities import Vehicle, Consumable, FillingStation
from ..entities import Vehicle, Consumable
from ..forms import EditVehicleForm, DeleteVehicleForm, DeleteAccountForm
from ..tools import db_log_update, db_log_delete, db_log_add
from .. import app, db, user_datastore, limiter
from .. import app, db, user_datastore
@app.route('/account', methods=['GET'])
@ -154,84 +153,3 @@ def set_users_home():
return jsonify({})
@app.route('/filling_stations/update')
def update_filling_stations():
print(FillingStation.query.all())
return jsonify({})
@app.route('/filling_stations/favourites/toggle/<fsid>')
def add_favourite_filling_stations(fsid):
favourite_ids = {x.id:x for x in current_user.favourite_filling_stations}
print(favourite_ids)
if fsid in favourite_ids:
current_user.favourite_filling_stations.remove(favourite_ids[fsid])
state = 'normal'
else:
fs = FillingStation.query.filter(FillingStation.id == fsid).first()
current_user.favourite_filling_stations.append(fs)
state = 'favourite'
db.session.commit()
return jsonify({'state':state})
@app.route('/filling_stations', methods=['GET'])
@login_required
@limiter.limit('1 per second')
def query_filling_stations():
api_key = app.config['TANKERKOENIG_API_KEY']
latitude = request.args.get('latitude')
longitude = request.args.get('longitude')
radius = request.args.get('radius', default=1.5)
type = request.args.get('type', default='all')
sort = request.args.get('sort', default='dist')
url = 'https://creativecommons.tankerkoenig.de/json/list.php'
params = {
'lat': latitude, 'lng': longitude, 'rad': radius, 'apikey': api_key, 'type': type, 'sort': sort
}
response = requests.get(url, params=params)
data = response.json()
for station in data['stations']:
fs = FillingStation.query.filter(FillingStation.id == station['id']).first()
if not fs:
fs = FillingStation()
fs.id = station['id']
fs.brand = station['brand']
fs.lat = station['lat']
fs.lng = station['lng']
fs.name = station['name']
fs.street = station['street']
fs.place = station['place']
fs.houseNumber = station['houseNumber']
fs.postCode = station['postCode']
db.session.add(fs)
if fs in current_user.favourite_filling_stations:
station['state'] = 'favourite'
else:
station['state'] = 'normal'
db.session.commit()
return jsonify(data)
@app.route('/filling_stations/<fsid>', methods=['GET'])
@login_required
@limiter.limit('1 per second')
def query_filling_station_details(fsid):
api_key = app.config['TANKERKOENIG_API_KEY']
if ',' in fsid:
# more than one id, redirect to method 2 (preisabfrage)
url = 'https://creativecommons.tankerkoenig.de/json/prices.php'
params = {
'apikey': api_key, 'ids': fsid
}
else:
url = 'https://creativecommons.tankerkoenig.de/json/detail.php'
params = {
'apikey': api_key, 'id': fsid
}
response = requests.get(url, params=params)
return jsonify(response.json())

View File

@ -0,0 +1,90 @@
from flask import request, jsonify
from flask_security import login_required
from flask_security.core import current_user
import requests
from ..entities import FillingStation
from .. import app, db, limiter
@app.route('/filling_stations/update')
def update_filling_stations():
print(FillingStation.query.all())
return jsonify({})
@app.route('/filling_stations/favourites/toggle/<fsid>')
def add_favourite_filling_stations(fsid):
favourite_ids = {x.id: x for x in current_user.favourite_filling_stations}
print(favourite_ids)
if fsid in favourite_ids:
current_user.favourite_filling_stations.remove(favourite_ids[fsid])
state = 'normal'
else:
fs = FillingStation.query.filter(FillingStation.id == fsid).first()
current_user.favourite_filling_stations.append(fs)
state = 'favourite'
db.session.commit()
return jsonify({'state': state})
@app.route('/filling_stations', methods=['GET'])
@login_required
@limiter.limit('1 per second')
def query_filling_stations():
api_key = app.config['TANKERKOENIG_API_KEY']
latitude = request.args.get('latitude')
longitude = request.args.get('longitude')
radius = request.args.get('radius', default=1.5)
type = request.args.get('type', default='all')
sort = request.args.get('sort', default='dist')
url = 'https://creativecommons.tankerkoenig.de/json/list.php'
params = {
'lat': latitude, 'lng': longitude, 'rad': radius, 'apikey': api_key, 'type': type, 'sort': sort
}
response = requests.get(url, params=params)
data = response.json()
for station in data['stations']:
fs = FillingStation.query.filter(FillingStation.id == station['id']).first()
if not fs:
fs = FillingStation()
fs.id = station['id']
fs.brand = station['brand']
fs.lat = station['lat']
fs.lng = station['lng']
fs.name = station['name']
fs.street = station['street']
fs.place = station['place']
fs.houseNumber = station['houseNumber']
fs.postCode = station['postCode']
db.session.add(fs)
if fs in current_user.favourite_filling_stations:
station['state'] = 'favourite'
else:
station['state'] = 'normal'
db.session.commit()
return jsonify(data)
@app.route('/filling_stations/<fsid>', methods=['GET'])
@login_required
@limiter.limit('1 per second')
def query_filling_station_details(fsid):
api_key = app.config['TANKERKOENIG_API_KEY']
if ',' in fsid:
# more than one id, redirect to method 2 (preisabfrage)
url = 'https://creativecommons.tankerkoenig.de/json/prices.php'
params = {
'apikey': api_key, 'ids': fsid
}
else:
url = 'https://creativecommons.tankerkoenig.de/json/detail.php'
params = {
'apikey': api_key, 'id': fsid
}
response = requests.get(url, params=params)
return jsonify(response.json())