diff --git a/app/entities.py b/app/entities.py index cd2def0..e2d1cef 100644 --- a/app/entities.py +++ b/app/entities.py @@ -14,15 +14,6 @@ vehicles_consumables = db.Table( ) -users_fillingstations = db.Table( - "users_fillingstations", - db.Column("user_id", db.Integer(), db.ForeignKey("user.id")), - db.Column( - "fillingstation_id", db.Integer(), db.ForeignKey("filling_station.int_id") - ), -) - - class Role(db.Model, RoleMixin): """ Entity to handle different roles for users: Typically user and admin exist @@ -57,9 +48,6 @@ class User(db.Model, UserMixin): roles = db.relationship( "Role", secondary=roles_users, backref=db.backref("users", lazy="dynamic") ) - favourite_filling_stations = db.relationship( - "FillingStation", secondary=users_fillingstations - ) def __repr__(self): return '' % (self.odometer, self.date, self.vehicle_id, self.costs, self.description) ) - - -class FillingStation(db.Model): - int_id = db.Column(db.Integer, primary_key=True) - id = db.Column(db.String(40), unique=True, nullable=False) - name = db.Column(db.Text(), nullable=False) - street = db.Column(db.Text(), nullable=False) - place = db.Column(db.Text(), nullable=False) - houseNumber = db.Column(db.Text()) - postCode = db.Column(db.Integer(), nullable=False) - brand = db.Column(db.Text(), 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) - e10 = db.Column(db.Numeric(10, 3), default=0) - open = db.Column(db.Boolean()) - - def as_dict(self): - res = {} - for c in self.__table__.columns: - val = getattr(self, c.name) - import decimal - - if isinstance(val, decimal.Decimal): - val = float(val) - val = str(val) - res[c.name] = val - return res diff --git a/app/routes/filling_stations.py b/app/routes/filling_stations.py deleted file mode 100644 index b4086fb..0000000 --- a/app/routes/filling_stations.py +++ /dev/null @@ -1,61 +0,0 @@ -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 - - -@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} - - 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 -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) - 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': gas_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) diff --git a/app/tools.py b/app/tools.py index 90a1cb2..71074be 100644 --- a/app/tools.py +++ b/app/tools.py @@ -3,7 +3,7 @@ import requests import logging from datetime import date, datetime, timedelta -from .entities import Pitstop, FillingStation +from .entities import Pitstop from . import db, app @@ -229,51 +229,6 @@ def chunks(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 - ) - ) - .all() - ) - - if len(res) > 0: - 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" - - # 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"]: - 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() - - def calculate_regular_cost_instances(vehicle): data = [] for regular in vehicle.regulars: