Export date range validation

This commit is contained in:
clerie 2020-04-11 14:27:42 +02:00
parent 44f23acadd
commit cb6e845d52
4 changed files with 65 additions and 14 deletions

View File

@ -13,6 +13,10 @@
<div class="alert alert-danger" role="alert"> <div class="alert alert-danger" role="alert">
Ein oder mehrere Eingabefelder sind leer oder haben das falsche Format. Ein oder mehrere Eingabefelder sind leer oder haben das falsche Format.
</div> </div>
{% elif e == "end-before-start" %}
<div class="alert alert-danger" role="alert">
Das Enddatum darf nicht vor dem Startdatum liegen.
</div>
{% endif %} {% endif %}
<h2> <h2>
Export Export
@ -23,20 +27,38 @@
<div class="col-sm-6"> <div class="col-sm-6">
<div class="form-group"> <div class="form-group">
<label for="datetimepicker-from">von</label> <label for="datetimepicker-from">von</label>
<input type="date" value="{{ fr }}" name="from" class="form-control" id="datetimepicker-from" required> <input type="date" value="{{ fr }}" name="from" class="form-control" id="datetimepicker-from" required max="{{ today }}">
</div> </div>
</div> </div>
<div class="col-sm-6"> <div class="col-sm-6">
<div class="form-group"> <div class="form-group">
<label for="datetimepicker-to">bis</label> <label for="datetimepicker-to">bis</label>
<input type="date" value="{{ to }}" name="to" class="form-control" id="datetimepicker-to" required> <input type="date" value="{{ to }}" name="to" class="form-control" id="datetimepicker-to" required max="{{ today }}">
</div> </div>
</div> </div>
</div> </div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<div class="custom-control custom-checkbox">
<input type="checkbox" name="workdays-only" value="true" class="custom-control-input" id="workdays-only">
<label class="custom-control-label" for="workdays-only">Nur Montag bis Freitag</label>
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Weiter</button> <button type="submit" class="btn btn-primary">Weiter</button>
</form> </form>
<section> <section>
{% endblock %} {% endblock %}
{% block foot %} {% block foot %}
<script>
$("#datetimepicker-from").change(() => {
$("#datetimepicker-to").attr('min', $("#datetimepicker-from").val());
});
$("#datetimepicker-to").change(() => {
$("#datetimepicker-from").attr('max', $("#datetimepicker-to").val());
});
</script>
{% endblock %} {% endblock %}

View File

@ -27,6 +27,16 @@
</div> </div>
</div> </div>
</div> </div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="workdays-only" {% if workdays_only %}checked {% endif %}disabled>
<label class="custom-control-label" for="workdays-only">Nur Montag bis Freitag</label>
</div>
</div>
</div>
</div>
<div class="row"> <div class="row">
<div class="col-md-6"> <div class="col-md-6">
<div class="card"> <div class="card">

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from datetime import datetime from datetime import datetime, timedelta
def fromisoformat(str): def fromisoformat(str):
return datetime.strptime(str, '%Y-%m-%d').date() return datetime.strptime(str, '%Y-%m-%d').date()
@ -10,3 +10,12 @@ def toisoformat(str, alt=""):
return fromisoformat(str).isoformat() return fromisoformat(str).isoformat()
except: except:
return alt return alt
def daterangeofdays(fr, to):
return [fr + timedelta(days=x) for x in range(0, (to - fr).days + 1)]
def daterangefilterweekend(dates):
return [date for date in dates if date.weekday() < 5]
def strtobool(s):
return str(s).lower() in ['true', '1', 't', 'y', 'yes', 'on']

View File

@ -2,7 +2,7 @@
from wetter import app, excel from wetter import app, excel
from wetter.models import Stations, Climate from wetter.models import Stations, Climate
from wetter.utils import fromisoformat, toisoformat from wetter.utils import fromisoformat, toisoformat, strtobool
from flask import request, make_response, render_template, redirect, jsonify from flask import request, make_response, render_template, redirect, jsonify
from datetime import datetime from datetime import datetime
from urllib.parse import urlencode from urllib.parse import urlencode
@ -29,34 +29,44 @@ def station(dwd_id):
@app.route('/station/<dwd_id>/export/') @app.route('/station/<dwd_id>/export/')
def export(dwd_id): def export(dwd_id):
today = datetime.today().date().isoformat()
fr = toisoformat(request.args.get('from')) fr = toisoformat(request.args.get('from'))
to = toisoformat(request.args.get('to')) to = toisoformat(request.args.get('to'))
e = request.args.get('e') e = request.args.get('e')
if fr and to and not fr <= to:
fr, to = "", ""
station = Stations.query.filter_by(dwd_id=dwd_id).first_or_404() station = Stations.query.filter_by(dwd_id=dwd_id).first_or_404()
return render_template('export.html', e=e, station=station, fr=fr, to=to) return render_template('export.html', e=e, station=station, today=today, fr=fr, to=to)
@app.route('/station/<dwd_id>/export/target/') @app.route('/station/<dwd_id>/export/target/')
def export_target(dwd_id): def export_target(dwd_id):
fr = toisoformat(request.args.get('from')) fr = toisoformat(request.args.get('from'))
to = toisoformat(request.args.get('to')) to = toisoformat(request.args.get('to'))
workdays_only = strtobool(request.args.get('workdays-only'))
if fr and to: if fr and to and fr <= to:
station = Stations.query.filter_by(dwd_id=dwd_id).first_or_404() station = Stations.query.filter_by(dwd_id=dwd_id).first_or_404()
return render_template('target.html', station=station, fr=fr, to=to) return render_template('target.html', station=station, fr=fr, to=to, workdays_only=workdays_only)
else: else:
qs = { qs = {}
"e": "empty",
}
if fr: if not fr <= to:
qs["from"] = fr qs["e"] = "end-before-start"
if to: else:
qs["to"] = to qs["e"] = "empty"
if fr:
qs["from"] = fr
if to:
qs["to"] = to
return redirect('/station/' + dwd_id + '/export/?' + urlencode(qs), code=302) return redirect('/station/' + dwd_id + '/export/?' + urlencode(qs), code=302)