Validate time string
This commit is contained in:
parent
341ebf6f8d
commit
bd0ecbafb1
6
wetter/utils.py
Normal file
6
wetter/utils.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
def fromisoformat(str):
|
||||||
|
return datetime.strptime(str, '%Y-%m-%d').date()
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from wetter import app
|
from wetter import app
|
||||||
from wetter.models import Stations, Climate
|
from wetter.models import Stations, Climate
|
||||||
|
from wetter.utils import fromisoformat
|
||||||
from flask import request, make_response, render_template
|
from flask import request, make_response, render_template
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
@ -33,38 +34,38 @@ def export(dwd_id):
|
|||||||
|
|
||||||
@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 = request.args.get('from')
|
fr = fromisoformat(request.args.get('from'))
|
||||||
to = request.args.get('to')
|
to = fromisoformat(request.args.get('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.isoformat(), to=to.isoformat())
|
||||||
|
|
||||||
@app.route('/station/<dwd_id>/export/target/ce.csv/')
|
@app.route('/station/<dwd_id>/export/target/ce.csv/')
|
||||||
def export_target_ce_csv_render(dwd_id):
|
def export_target_ce_csv_render(dwd_id):
|
||||||
fr = request.args.get('from')
|
fr = fromisoformat(request.args.get('from'))
|
||||||
to = request.args.get('to')
|
to = fromisoformat(request.args.get('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()
|
||||||
climate = Climate.query.filter_by(station=station.id).filter(Climate.date >= fr, Climate.date <= to).order_by(Climate.date.asc())
|
climate = Climate.query.filter_by(station=station.id).filter(Climate.date >= fr.isoformat(), Climate.date <= to.isoformat()).order_by(Climate.date.asc())
|
||||||
|
|
||||||
r = make_response(render_template('export/ce.csv', climate=climate))
|
r = make_response(render_template('export/ce.csv', climate=climate))
|
||||||
r.headers['Content-Type'] = 'text/csv; charset=utf-8'
|
r.headers['Content-Type'] = 'text/csv; charset=utf-8'
|
||||||
r.headers['Content-Disposition'] = 'attachment; filename="wetter_' +station.dwd_id +'_' + fr + '_' + to +'_ce.csv'
|
r.headers['Content-Disposition'] = 'attachment; filename="wetter_' +station.dwd_id +'_' + fr.isoformat() + '_' + to.isoformat() +'_ce.csv'
|
||||||
|
|
||||||
return r
|
return r
|
||||||
|
|
||||||
@app.route('/station/<dwd_id>/export/target/dwd.txt/')
|
@app.route('/station/<dwd_id>/export/target/dwd.txt/')
|
||||||
def export_target_dwd_txt_render(dwd_id):
|
def export_target_dwd_txt_render(dwd_id):
|
||||||
fr = request.args.get('from')
|
fr = fromisoformat(request.args.get('from'))
|
||||||
to = request.args.get('to')
|
to = fromisoformat(request.args.get('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()
|
||||||
climate = Climate.query.filter_by(station=station.id).filter(Climate.date >= fr, Climate.date <= to).order_by(Climate.date.asc())
|
climate = Climate.query.filter_by(station=station.id).filter(Climate.date >= fr.isoformat(), Climate.date <= to.isoformat()).order_by(Climate.date.asc())
|
||||||
|
|
||||||
r = make_response(render_template('export/dwd.txt', station=station, climate=climate))
|
r = make_response(render_template('export/dwd.txt', station=station, climate=climate))
|
||||||
r.headers['Content-Type'] = 'text/txt; charset=utf-8'
|
r.headers['Content-Type'] = 'text/txt; charset=utf-8'
|
||||||
r.headers['Content-Disposition'] = 'attachment; filename="wetter_' + station.dwd_id +'_' + fr + '_' + to +'_dwd.txt'
|
r.headers['Content-Disposition'] = 'attachment; filename="wetter_' + station.dwd_id +'_' + fr.isoformat() + '_' + to.isoformat() +'_dwd.txt'
|
||||||
|
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user