adds functionality to handle favourit filling stations
This commit is contained in:
@@ -3,8 +3,9 @@ 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
|
||||
from ..entities import Vehicle, Consumable, FillingStation
|
||||
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
|
||||
@@ -13,9 +14,12 @@ from .. import app, db, user_datastore, limiter
|
||||
@app.route('/account', methods=['GET'])
|
||||
@login_required
|
||||
def get_account_page():
|
||||
print(current_user.home_lat, current_user.home_long)
|
||||
stations = [x.as_dict() for x in current_user.favourite_filling_stations]
|
||||
for station in stations:
|
||||
station['state'] = 'favourite'
|
||||
return render_template('account.html',
|
||||
map_pos=(current_user.home_lat, current_user.home_long, current_user.home_zoom))
|
||||
map_pos=(current_user.home_lat, current_user.home_long, current_user.home_zoom),
|
||||
fs=json.dumps(stations))
|
||||
|
||||
|
||||
@app.route('/account/vehicle/edit/<int:vid>', methods=['GET', 'POST'])
|
||||
@@ -147,7 +151,29 @@ def set_users_home():
|
||||
current_user.home_long = request.json['long']
|
||||
current_user.home_zoom = request.json['zoom']
|
||||
db.session.commit()
|
||||
return jsonify({'lat': float(current_user.home_lat)})
|
||||
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'])
|
||||
@@ -167,8 +193,27 @@ def query_filling_stations():
|
||||
'lat': latitude, 'lng': longitude, 'rad': radius, 'apikey': api_key, 'type': type, 'sort': sort
|
||||
}
|
||||
response = requests.get(url, params=params)
|
||||
print(response.url)
|
||||
return jsonify(response.json())
|
||||
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'])
|
||||
|
||||
Reference in New Issue
Block a user