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/') 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/', 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())