Compare commits
4 Commits
main
...
update_dep
Author | SHA1 | Date |
---|---|---|
Joachim Lusiardi | b72704348c | |
Joachim Lusiardi | cdde09eee9 | |
Joachim Lusiardi | ec132b3e7a | |
Joachim Lusiardi | b8f532d5c2 |
|
@ -54,10 +54,21 @@ def user_registered_sighandler(application, user, confirm_token):
|
|||
tools.db_log_add(new_vehicle)
|
||||
|
||||
|
||||
def assure_consumable(name, ext_id, unit):
|
||||
if not Consumable.query.filter(Consumable.ext_id == ext_id).first():
|
||||
c = Consumable(name, ext_id, unit)
|
||||
db.session.add(c)
|
||||
|
||||
|
||||
@app.before_first_request
|
||||
def before_first_request():
|
||||
db.create_all()
|
||||
|
||||
# make sure all consumables from tankerkoenig exist: diesel, e5, e10
|
||||
assure_consumable('Diesel', 'diesel', 'L')
|
||||
assure_consumable('Super','e5', 'L')
|
||||
assure_consumable('Super E10','e10', 'L')
|
||||
|
||||
user_datastore.find_or_create_role(name='admin', description='Role for administrators')
|
||||
user_datastore.find_or_create_role(name='user', description='Role for all users.')
|
||||
db.session.commit()
|
||||
|
|
|
@ -14,6 +14,15 @@ 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
|
||||
|
@ -48,6 +57,9 @@ 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 '<User id="%r" email="%r" ' % (self.id, self.email)
|
||||
|
@ -162,12 +174,14 @@ class Consumable(db.Model):
|
|||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(255), unique=True)
|
||||
ext_id = db.Column(db.String(255))
|
||||
unit = db.Column(db.String(255))
|
||||
|
||||
vehicles = db.relationship("Vehicle", secondary=vehicles_consumables, viewonly=True)
|
||||
vehicles = db.relationship("Vehicle", secondary=vehicles_consumables)
|
||||
|
||||
def __init__(self, name, unit):
|
||||
def __init__(self, name, ext_id, unit):
|
||||
self.name = name
|
||||
self.ext_id = ext_id
|
||||
self.unit = unit
|
||||
|
||||
def __repr__(self):
|
||||
|
@ -194,3 +208,33 @@ class Service(db.Model):
|
|||
'<Service odometer="%r" date="%r" vehicle_id="%r" costs="%r" description="%r">'
|
||||
% (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
|
||||
|
|
|
@ -10,12 +10,14 @@ class SelectConsumableForm(FlaskForm):
|
|||
|
||||
class CreateConsumableForm(FlaskForm):
|
||||
name = StringField('Name', validators=[Length(1, 255)])
|
||||
ext_id = SelectField('Tankerkönig ID', coerce=int)
|
||||
unit = StringField('Unit', validators=[Length(1, 255)])
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
||||
class EditConsumableForm(FlaskForm):
|
||||
name = StringField('Name', validators=[Length(1, 255)])
|
||||
ext_id = SelectField('Tankerkönig ID', coerce=int)
|
||||
unit = StringField('Unit', validators=[Length(1, 255)])
|
||||
submit = SubmitField(label='Do it!')
|
||||
|
||||
|
|
|
@ -3,4 +3,5 @@ from .admin import *
|
|||
from .misc import *
|
||||
from .pitstop import *
|
||||
from .service import *
|
||||
from .filling_stations import *
|
||||
from .regular_cost import *
|
||||
|
|
|
@ -13,9 +13,13 @@ from .. import app, db, user_datastore
|
|||
@app.route("/account", methods=["GET"])
|
||||
@login_required
|
||||
def get_account_page():
|
||||
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),
|
||||
fs=json.dumps(stations),
|
||||
)
|
||||
|
||||
|
||||
|
@ -138,3 +142,24 @@ def delete_account():
|
|||
|
||||
return render_template("deleteAccountForm.html", form=form)
|
||||
|
||||
|
||||
@app.route("/account/home", methods=["GET"])
|
||||
@login_required
|
||||
def get_users_home():
|
||||
return jsonify(
|
||||
{
|
||||
"lat": float(current_user.home_lat),
|
||||
"long": float(current_user.home_long),
|
||||
"zoom": current_user.home_zoom,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@app.route("/account/home", methods=["POST"])
|
||||
@login_required
|
||||
def set_users_home():
|
||||
current_user.home_lat = request.json["lat"]
|
||||
current_user.home_long = request.json["long"]
|
||||
current_user.home_zoom = request.json["zoom"]
|
||||
db.session.commit()
|
||||
return jsonify({})
|
||||
|
|
|
@ -22,6 +22,8 @@ def get_admin_page():
|
|||
@login_required
|
||||
def create_consumable():
|
||||
form = CreateConsumableForm()
|
||||
choices = [(0, ''), (1, 'diesel'), (2, 'e5'), (3, 'e10')]
|
||||
form.ext_id.choices = choices
|
||||
|
||||
# preinitialize the defaults with potentially existing values from a try before
|
||||
if form.name.data is not None:
|
||||
|
@ -30,7 +32,7 @@ def create_consumable():
|
|||
form.unit.default = form.unit.data
|
||||
|
||||
if form.validate_on_submit():
|
||||
new_consumable = Consumable(form.name.data, form.unit.data)
|
||||
new_consumable = Consumable(form.name.data, choices[form.ext_id.data][1], form.unit.data)
|
||||
db.session.add(new_consumable)
|
||||
try:
|
||||
db.session.commit()
|
||||
|
@ -70,19 +72,28 @@ def edit_consumable(cid):
|
|||
return redirect(url_for('get_admin_page'))
|
||||
|
||||
form = EditConsumableForm()
|
||||
choices = [(0, ''), (1, 'diesel'), (2, 'e5'), (3, 'e10')]
|
||||
form.ext_id.choices = choices
|
||||
|
||||
form.name.default = consumable.name
|
||||
form.unit.default = consumable.unit
|
||||
form.ext_id.default = 3
|
||||
for c in choices:
|
||||
if c[1] == consumable.ext_id:
|
||||
form.ext_id.default = c[0]
|
||||
|
||||
# preinitialize the defaults with potentially existing values from a try before
|
||||
if form.name.data is not None:
|
||||
form.name.default = form.name.data
|
||||
if form.unit.data is not None:
|
||||
form.unit.default = form.unit.data
|
||||
if form.ext_id.data is not None:
|
||||
form.ext_id.default = form.ext_id.data
|
||||
|
||||
if form.validate_on_submit():
|
||||
consumable.name = form.name.data
|
||||
consumable.unit = form.unit.data
|
||||
consumable.ext_id = choices[form.ext_id.data][1]
|
||||
try:
|
||||
db.session.commit()
|
||||
db_log_update(consumable)
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
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/<fsid>')
|
||||
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)
|
|
@ -20,6 +20,7 @@ from ..tools import (
|
|||
db_log_add,
|
||||
pitstop_service_key,
|
||||
get_event_line_for_vehicle,
|
||||
update_filling_station_prices,
|
||||
RegularCostInstance,
|
||||
calculate_regular_cost_instances,
|
||||
get_users_active_vehicle,
|
||||
|
@ -226,6 +227,5 @@ def get_pit_stops():
|
|||
"regulars": vehicle.regulars,
|
||||
}
|
||||
user["vehicles"].append(v)
|
||||
user["vehicles"].sort(key=lambda v: (v["data"][-1].date, v["data"][-1].odometer or 0 ), reverse=True)
|
||||
|
||||
return render_template("pitstops.html", user=user)
|
||||
|
|
|
@ -18,6 +18,7 @@ from ..tools import (
|
|||
db_log_add,
|
||||
pitstop_service_key,
|
||||
get_event_line_for_vehicle,
|
||||
update_filling_station_prices,
|
||||
get_users_active_vehicle,
|
||||
)
|
||||
from .. import app, db
|
||||
|
|
|
@ -94,6 +94,16 @@ and (max-device-width : 568px) {
|
|||
}
|
||||
}
|
||||
|
||||
.filling_station_info {
|
||||
margin: 5px;
|
||||
border: 1px solid;
|
||||
}
|
||||
|
||||
.filling_station_info img{
|
||||
margin-top: 6px;
|
||||
height:48px;
|
||||
}
|
||||
|
||||
/*
|
||||
* styling for sortable tables
|
||||
*/
|
||||
|
@ -110,3 +120,7 @@ th.headerSortUp {
|
|||
th.headerSortDown {
|
||||
background-image: url(../img/up.gif);
|
||||
}
|
||||
|
||||
.filling_station_closed {
|
||||
text-decoration: line-through;
|
||||
}
|
||||
|
|
After Width: | Height: | Size: 725 B |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 29 KiB |
After Width: | Height: | Size: 4.7 KiB |
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 13 KiB |
After Width: | Height: | Size: 51 KiB |
After Width: | Height: | Size: 6.8 KiB |
After Width: | Height: | Size: 3.7 KiB |
After Width: | Height: | Size: 9.0 KiB |
After Width: | Height: | Size: 7.6 KiB |
After Width: | Height: | Size: 3.7 KiB |
After Width: | Height: | Size: 44 KiB |
|
@ -9,6 +9,7 @@
|
|||
<form class='form-horizontal' method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ render_field_with_errors(form.name) }}
|
||||
{{ render_field_with_errors(form.ext_id) }}
|
||||
{{ render_field_with_errors(form.unit) }}
|
||||
{{ render_field_with_errors(form.submit) }}
|
||||
</form>
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
<form class='form-horizontal' method="POST">
|
||||
{{ form.hidden_tag() }}
|
||||
{{ render_field_with_errors(form.name) }}
|
||||
{{ render_field_with_errors(form.ext_id) }}
|
||||
{{ render_field_with_errors(form.unit) }}
|
||||
{{ render_field_with_errors(form.submit) }}
|
||||
</form>
|
||||
|
|
|
@ -183,7 +183,7 @@
|
|||
<div class="col-md-12">
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-body">
|
||||
<a href="https://lusiardi.de/pages/impressum.html" target="_new">Impressum</a> - <a href="https://lusiardi.de/pages/datenschutzerklaerung.html" target="_new">Datenschutzerklärung</a>
|
||||
<a href="https://www.lusiardi.de/impressum/" target="_new">Impressum</a> - <a href="https://www.lusiardi.de/datenschutzerklaerung/" target="_new">Datenschutzerklärung</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
47
app/tools.py
|
@ -3,7 +3,7 @@ import requests
|
|||
import logging
|
||||
from datetime import date, datetime, timedelta
|
||||
|
||||
from .entities import Pitstop
|
||||
from .entities import Pitstop, FillingStation
|
||||
from . import db, app
|
||||
|
||||
|
||||
|
@ -229,6 +229,51 @@ 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:
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
Flask==2.1.2
|
||||
Flask-SQLAlchemy==2.5.1
|
||||
Flask==3.0.3
|
||||
Flask-SQLAlchemy==3.1.1
|
||||
Flask-Security==3.0.0
|
||||
Flask-WTF==1.0.1
|
||||
PyMySQL==1.0.2
|
||||
Flask-WTF==1.2.1
|
||||
PyMySQL==1.1.1
|
||||
markdown
|
||||
Flask-Limiter==2.4.5.1
|
||||
requests==2.27.1
|
||||
email-validator==1.2.1
|
||||
gunicorn==20.1.0
|
||||
pytz==2022.1
|
||||
SQLAlchemy==1.4.36
|
||||
Werkzeug==2.2.2
|
||||
Flask-Limiter==3.7.0
|
||||
requests==2.32.2
|
||||
email-validator==2.1.1
|
||||
gunicorn==22.0.0
|
||||
pytz==2024.1
|
||||
SQLAlchemy==2.0.30
|
||||
Werkzeug==3.0.3
|