From f7cb273254e65725ee737ae2368f9137be9d6ad1 Mon Sep 17 00:00:00 2001 From: Joachim Lusiardi Date: Fri, 24 Nov 2017 09:08:04 +0100 Subject: [PATCH] preparation commit for henni This commit includes: * sql conversion script * fixes for the column type for lat and lng fields * removes prints / console logs * unneccessary comments * if no home data is available, we center to germany * hide non working brand logos * on closed stations the word "closed" is printed instead of outdated price * updates on filling stations is done in chunks of max 10 * configuration separates between development and testing --- app/entities.py | 8 +-- app/routes/admin.py | 2 - app/routes/filling_stations.py | 32 +----------- app/routes/pitstop.py | 1 - app/static/js/fillingstations.js | 26 ++++------ app/templates/account.html | 6 +-- app/templates/planPitStopForm.html | 11 ++++- app/tools.py | 49 ++++++++++++------- .../upgrade_gillian_to_henni.sql | 39 +++++++++++++++ 9 files changed, 98 insertions(+), 76 deletions(-) create mode 100644 database_upgrades/upgrade_gillian_to_henni.sql diff --git a/app/entities.py b/app/entities.py index 275692f..b2f8006 100644 --- a/app/entities.py +++ b/app/entities.py @@ -39,8 +39,8 @@ class User(db.Model, UserMixin): password = db.Column(db.String(255)) active = db.Column(db.Boolean()) confirmed_at = db.Column(db.DateTime()) - home_lat = db.Column(db.Numeric(2, 5), default=0) - home_long = db.Column(db.Numeric(2, 5), default=0) + home_lat = db.Column(db.Numeric(8, 5), default=0) + home_long = db.Column(db.Numeric(8, 5), default=0) home_zoom = db.Column(db.Integer(), default=0) vehicles = db.relationship( @@ -188,8 +188,8 @@ class FillingStation(db.Model): houseNumber = db.Column(db.Text()) postCode = db.Column(db.Integer(), nullable=False) brand = db.Column(db.Text(), nullable=False) - lat = db.Column(db.Numeric(2, 5), nullable=False) - lng = db.Column(db.Numeric(2, 5), nullable=False) + lat = db.Column(db.Numeric(8, 5), nullable=False) + lng = db.Column(db.Numeric(8, 5), nullable=False) last_update = db.Column(db.DateTime) diesel = db.Column(db.Numeric(10, 3), default=0) e5 = db.Column(db.Numeric(10, 3), default=0) diff --git a/app/routes/admin.py b/app/routes/admin.py index dc63702..a8bb527 100644 --- a/app/routes/admin.py +++ b/app/routes/admin.py @@ -93,9 +93,7 @@ def edit_consumable(cid): if form.validate_on_submit(): consumable.name = form.name.data consumable.unit = form.unit.data - print(form.ext_id.data) consumable.ext_id = choices[form.ext_id.data][1] - print(consumable.ext_id) try: db.session.commit() db_log_update(consumable) diff --git a/app/routes/filling_stations.py b/app/routes/filling_stations.py index 8727063..11d0b7e 100644 --- a/app/routes/filling_stations.py +++ b/app/routes/filling_stations.py @@ -7,16 +7,9 @@ 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]) @@ -38,12 +31,12 @@ def query_filling_stations(): 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') + gas_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 + 'lat': latitude, 'lng': longitude, 'rad': radius, 'apikey': api_key, 'type': gas_type, 'sort': sort } response = requests.get(url, params=params) data = response.json() @@ -67,24 +60,3 @@ def query_filling_stations(): 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()) diff --git a/app/routes/pitstop.py b/app/routes/pitstop.py index 77a145a..40b739b 100644 --- a/app/routes/pitstop.py +++ b/app/routes/pitstop.py @@ -236,7 +236,6 @@ def plan_pit_stop_form(vid, cid): offers = [] for fs in current_user.favourite_filling_stations: -# if fs.open: offers.append((fs, getattr(fs, consumable.ext_id),)) return render_template('planPitStopForm.html', vehicle=vehicle, consumable=consumable, offers=offers) diff --git a/app/static/js/fillingstations.js b/app/static/js/fillingstations.js index 951aeb6..ccd38b8 100644 --- a/app/static/js/fillingstations.js +++ b/app/static/js/fillingstations.js @@ -1,7 +1,7 @@ -// initially go to Brandenburger Tor -var lat = 52.516275, - lon = 13.377704, - zoom = 13; +// initially display germany +var lat = 50.75653081787912, + lon = 9.262980794432847, + zoom = 5; var map; @@ -13,6 +13,7 @@ query_location = function(updater) { navigator.geolocation.getCurrentPosition(function(position) { lat = position.coords.latitude; lon = position.coords.longitude; + zoom = 11; if(updater){ updater(lat, lon); } @@ -48,15 +49,12 @@ load_filling_stations = function() { clicked_on_filling_station_marker = function(station, marker) { return function(data) { - console.log(station); - console.log(marker); $.ajax({ type: 'GET', url: '/filling_stations/favourites/toggle/'+station.id, dataType: 'json', timeout: 1000, success: function(data) { - console.log(data); if (data.state == 'favourite') { marker.setUrl('/static/img/filling_station_favourite_marker.png'); } else { @@ -70,8 +68,6 @@ clicked_on_filling_station_marker = function(station, marker) { display_station_information = function(station) { return function(event) { - console.log(station); - var info = $('#station_info'); info.empty(); info.addClass('filling_station_info'); @@ -80,6 +76,10 @@ display_station_information = function(station) { var cell2 = $('
', {'class':'col-md-4'}) var img = $('', {'src': '/static/logos/'+station.brand.toLowerCase()+'.png'}); + // hide if the brand icon loads with error + img.error(function(){ + $(this).hide(); + }); var name = $('
').text(station.name); @@ -94,7 +94,6 @@ display_station_information = function(station) { .append(cell2 .append(img)); - console.log(cell1.height()) cell2.height(cell1.height()); }; } @@ -103,7 +102,6 @@ update_filling_station_markers = function() { for(id in filling_stations) { var station = filling_stations[id]; if(!station.marker) { - console.log(station.id); var lonLat = new OpenLayers.LonLat(station.lng, station.lat) .transform(new OpenLayers.Projection('EPSG:4326'), map.getProjectionObject()); if (station.state == 'favourite') { @@ -114,7 +112,6 @@ update_filling_station_markers = function() { var marker = new OpenLayers.Marker(lonLat, icon); marker.events.register('click', marker, clicked_on_filling_station_marker(station, marker)); marker.events.register('mouseover', null, display_station_information(station)); - marker.events.register('mouseout', null, function(e){console.log('out');}); filling_station_markers.addMarker(marker); station.marker = true; } @@ -150,18 +147,15 @@ activate_map = function(map_div_id, button_ids, home_lat, home_long, home_zoom, lat = home_lat; lon = home_long; zoom = home_zoom; - console.log(lat, long); update_map(); } // get button $('#'+button_ids[0]).click(function(e){ - console.log('clicked get button'); load_filling_stations(); }); // set home button $('#'+button_ids[1]).click(function(e){ - console.log('clicked set home'); $.ajax({ type: 'POST', url: '/account/home', @@ -173,14 +167,12 @@ activate_map = function(map_div_id, button_ids, home_lat, home_long, home_zoom, }); // go home button $('#'+button_ids[2]).click(function(e){ - console.log('clicked the go home button'); $.ajax({ type: 'GET', url: '/account/home', dataType: 'json', timeout: 1000, success: function(data) { - console.log(data); lat = data.lat; lon = data.long; zoom = data.zoom; diff --git a/app/templates/account.html b/app/templates/account.html index 4874361..cd47fc3 100644 --- a/app/templates/account.html +++ b/app/templates/account.html @@ -82,9 +82,9 @@
diff --git a/app/templates/planPitStopForm.html b/app/templates/planPitStopForm.html index 44b8c0d..b2c4e4d 100644 --- a/app/templates/planPitStopForm.html +++ b/app/templates/planPitStopForm.html @@ -30,8 +30,12 @@ - - {{ offer[1] }} €/{{ consumable.unit }} + + {% if offer[0].open %} + {{ offer[1] }} €/{{ consumable.unit }} + {% else %} + Closed + {% endif %} {% endfor %} @@ -44,6 +48,9 @@
diff --git a/app/tools.py b/app/tools.py index 67e66a2..9518b77 100644 --- a/app/tools.py +++ b/app/tools.py @@ -167,33 +167,48 @@ def get_event_line_for_vehicle(vehicle): return data +def chunks(l, n): + """Yield successive n-sized chunks from l.""" + for i in range(0, len(l), n): + yield l[i:i + n] + + def update_filling_station_prices(ids): max_age = (datetime.now() - timedelta(minutes=15)).strftime('%Y-%m-%d %H:%M') res = db.session. \ query(FillingStation). \ filter(FillingStation.id.in_(ids)). \ - filter(or_(FillingStation.last_update==None, FillingStation.last_update < max_age)).\ + filter(or_(FillingStation.last_update == None, FillingStation.last_update < max_age)). \ all() if len(res) > 0: - map = {x.id:x for x in res} + id_map = {x.id: x for x in res} query_ids = [x.id for x in res] api_key = app.config['TANKERKOENIG_API_KEY'] url = 'https://creativecommons.tankerkoenig.de/json/prices.php' - params = { - 'apikey': api_key, 'ids': ','.join(query_ids) - } - response = requests.get(url, params=params) - prices = response.json()['prices'] - for price in prices: - id = price - station_status = prices[id] - print(id, station_status) - map[id].open = station_status['status'] == 'open' - if map[id].open: - map[id].diesel = station_status['diesel'] - map[id].e10 = station_status['e10'] - map[id].e5 = station_status['e5'] - map[id].last_update = datetime.now() + + # documentation tells us to query max 10 filling stations at a time... + for c in chunks(query_ids, 10): + params = { + 'apikey': api_key, 'ids': ','.join(c) + } + response = requests.get(url, params=params) + response_json = response.json() + if response_json['ok']: + print(response_json) + prices = response_json['prices'] + for price in prices: + id = price + station_status = prices[id] + id_map[id].open = station_status['status'] == 'open' + if id_map[id].open: + id_map[id].diesel = station_status['diesel'] + id_map[id].e10 = station_status['e10'] + id_map[id].e5 = station_status['e5'] + id_map[id].last_update = datetime.now() + else: + logging.error( + 'could not update filling stations because of {r} on URL {u}.'.format(r=str(response_json), + u=response.url)) db.session.commit() diff --git a/database_upgrades/upgrade_gillian_to_henni.sql b/database_upgrades/upgrade_gillian_to_henni.sql new file mode 100644 index 0000000..743a9f8 --- /dev/null +++ b/database_upgrades/upgrade_gillian_to_henni.sql @@ -0,0 +1,39 @@ +CREATE TABLE `filling_station` ( + `int_id` int(11) NOT NULL AUTO_INCREMENT, + `id` varchar(40) NOT NULL, + `name` text NOT NULL, + `street` text NOT NULL, + `place` text NOT NULL, + `houseNumber` text, + `postCode` int(11) NOT NULL, + `brand` text NOT NULL, + `lat` decimal(8,5) NOT NULL, + `lng` decimal(8,5) NOT NULL, + `last_update` datetime DEFAULT NULL, + `diesel` decimal(10,3) DEFAULT NULL, + `e5` decimal(10,3) DEFAULT NULL, + `e10` decimal(10,3) DEFAULT NULL, + `open` tinyint(1) DEFAULT NULL, + PRIMARY KEY (`int_id`), + UNIQUE KEY `id` (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=latin1; + +CREATE TABLE `users_fillingstations` ( + `user_id` int(11) DEFAULT NULL, + `fillingstation_id` int(11) DEFAULT NULL, + KEY `user_id` (`user_id`), + KEY `fillingstation_id` (`fillingstation_id`), + CONSTRAINT `users_fillingstations_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`), + CONSTRAINT `users_fillingstations_ibfk_2` FOREIGN KEY (`fillingstation_id`) REFERENCES `filling_station` (`int_id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1; + +ALTER TABLE `consumable` ADD COLUMN `ext_id` varchar(255) DEFAULT NULL; +update consumable set ext_id = 'e5' where id = 1; +update consumable set ext_id = 'e10' where id = 34; +update consumable set ext_id = 'diesel' where id = 32; + + +ALTER TABLE `user` ADD COLUMN `home_lat` decimal(8,5) DEFAULT NULL; +ALTER TABLE `user` ADD COLUMN `home_long` decimal(8,5) DEFAULT NULL; +ALTER TABLE `user` ADD COLUMN `home_zoom` int(11) DEFAULT NULL; +