Browse Source

Export form validation

dependabot/npm_and_yarn/jquery-3.5.0
clerie 2 years ago
parent
commit
b6da7dc2ce
  1. 19
      wetter/templates/export.html
  2. 8
      wetter/templates/target.html
  3. 6
      wetter/utils.py
  4. 33
      wetter/views.py

19
wetter/templates/export.html

@ -9,6 +9,11 @@ @@ -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 @@ @@ -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>

8
wetter/templates/target.html

@ -17,17 +17,13 @@ @@ -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>

6
wetter/utils.py

@ -4,3 +4,9 @@ from datetime import datetime @@ -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

33
wetter/views.py

@ -2,9 +2,10 @@ @@ -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): @@ -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'))
station = Stations.query.filter_by(dwd_id=dwd_id).first_or_404()
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, to=to)
else:
qs = {
"e": "empty",
}
if fr:
qs["from"] = fr
if to:
qs["to"] = to
return render_template('target.html', station=station, fr=fr.isoformat(), to=to.isoformat())
return redirect('/station/' + dwd_id + '/export/?' + urlencode(qs), code=302)
def export_target_ce(request, dwd_id):
fr = fromisoformat(request.args.get('from'))

Loading…
Cancel
Save