Export form validation

This commit is contained in:
clerie 2020-04-08 17:50:25 +02:00
parent 0b5ae309ce
commit b6da7dc2ce
4 changed files with 41 additions and 25 deletions

View File

@ -9,6 +9,11 @@
{% block content %}
<section>
{% if e == "empty" %}
<div class="alert alert-danger" role="alert">
Ein oder mehrere Eingabefelder sind leer oder haben das falsche Format.
</div>
{% endif %}
<h2>
Export
<small class="text-muted">Zeitraum wählen</small>
@ -18,23 +23,13 @@
<div class="col-sm-6">
<div class="form-group">
<label for="datetimepicker-from">von</label>
<div class="input-group" id="datetimepicker-from">
<input type="text" name="from" class="form-control" placeholder="yyyy-mm-dd">
<div class="input-group-append">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
<input type="date" value="{{ fr }}" name="from" class="form-control" id="datetimepicker-from" required>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="datetimepicker-to">bis</label>
<div class="input-group" id="datetimepicker-to">
<input type="text" name="to" class="form-control" placeholder="yyyy-mm-dd">
<div class="input-group-append">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
<input type="date" value="{{ to }}" name="to" class="form-control" id="datetimepicker-to" required>
</div>
</div>
</div>

View File

@ -17,17 +17,13 @@
<div class="col-sm-6">
<div class="form-group">
<label for="datetimepicker-from">von</label>
<div class="input-group" id="datetimepicker-from">
<input type="text" value="{{ fr }}" class="form-control" placeholder="yyyy-mm-dd" disabled>
</div>
<input type="date" value="{{ fr }}" class="form-control" id="datetimepicker-from" disabled>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label for="datetimepicker-to">bis</label>
<div class="input-group" id="datetimepicker-to">
<input type="text" value="{{ to }}" class="form-control" placeholder="yyyy-mm-dd" disabled>
</div>
<input type="date" value="{{ to }}" class="form-control" id="datetimepicker-to" disabled>
</div>
</div>
</div>

View File

@ -4,3 +4,9 @@ from datetime import datetime
def fromisoformat(str):
return datetime.strptime(str, '%Y-%m-%d').date()
def toisoformat(str, alt=""):
try:
return fromisoformat(str).isoformat()
except:
return alt

View File

@ -2,9 +2,10 @@
from wetter import app, excel
from wetter.models import Stations, Climate
from wetter.utils import fromisoformat
from flask import request, make_response, render_template, jsonify
from wetter.utils import fromisoformat, toisoformat
from flask import request, make_response, render_template, redirect, jsonify
from datetime import datetime
from urllib.parse import urlencode
@app.route('/')
def index():
@ -28,18 +29,36 @@ def station(dwd_id):
@app.route('/station/<dwd_id>/export/')
def export(dwd_id):
fr = toisoformat(request.args.get('from'))
to = toisoformat(request.args.get('to'))
e = request.args.get('e')
station = Stations.query.filter_by(dwd_id=dwd_id).first_or_404()
return render_template('export.html', station=station)
return render_template('export.html', e=e, station=station, fr=fr, to=to)
@app.route('/station/<dwd_id>/export/target/')
def export_target(dwd_id):
fr = fromisoformat(request.args.get('from'))
to = fromisoformat(request.args.get('to'))
fr = toisoformat(request.args.get('from'))
to = toisoformat(request.args.get('to'))
if fr and to:
station = Stations.query.filter_by(dwd_id=dwd_id).first_or_404()
return render_template('target.html', station=station, fr=fr.isoformat(), to=to.isoformat())
return render_template('target.html', station=station, fr=fr, to=to)
else:
qs = {
"e": "empty",
}
if fr:
qs["from"] = fr
if to:
qs["to"] = to
return redirect('/station/' + dwd_id + '/export/?' + urlencode(qs), code=302)
def export_target_ce(request, dwd_id):
fr = fromisoformat(request.args.get('from'))