From 1d2bb730821a9fa27d276a4396b09e97c3abb826 Mon Sep 17 00:00:00 2001 From: "yukivachot@gmail.com" Date: Mon, 18 Oct 2021 15:22:27 +0200 Subject: [PATCH] Update --- app.py | 33 +++++++++++++++++++++++++++++---- requirements.txt | 3 ++- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 7f61b03..33eeac8 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,9 @@ import os import sys from flask import Flask, request, render_template +from flask_wtf import FlaskForm +from wtforms import StringField, IntegerField, FloatField, validators +from wtforms.validators import InputRequired from contextlib import closing from urllib.request import urlopen import json @@ -14,6 +17,17 @@ def log(*args): sys.stdout.flush() +class Search(FlaskForm): + search = StringField('Search', [validators.InputRequired()]) + + +class City(FlaskForm): + insee = StringField('Insee', [validators.InputRequired()]) + + +class Around(FlaskForm): + insee = StringField('Insee', [validators.InputRequired()]) + def getApiCity(search): if METEOCONCEPT_TOKEN is None: log('Env variable METEOCONCEPT_TOKEN not passed') @@ -85,9 +99,19 @@ def index(): response_cities = False response_ephemeride = False response_around = False - if request.method == 'POST': - search = request.form['search'] - response_cities = getApiCity(search) + + search = Search() + city = City() + around = Around() + + if search.validate_on_submit(): + return render_template('index.html', cities=getApiCity(search)) + + if city.validate_on_submit(): + return render_template('index.html', ephemeride=getEphemeride(city)) + + if around.validate_on_submit(): + return render_template('index.html', around=getAround(around, 1)) # TODO Multiple form for insee ? or ajax ? # insee = request.form['insee'] @@ -144,4 +168,5 @@ if __name__ == '__main__': # On Linux or MAC 'export METEOCONCEPT_TOKEN=...' (check shell echo $METEOCONCEPT_TOKEN) # On Windows 'set METEOCONCEPT_TOKEN=...' (check on Powershell echo $Env:METEOCONCEPT_TOKEN) METEOCONCEPT_TOKEN = os.environ.get('METEOCONCEPT_TOKEN', None) - app.run(host='0.0.0.0', port=PORT, debug=True) + app.config['SECRET_KEY'] = 'secret_key' + app.run(host='0.0.0.0', port=PORT, debug=True) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 2db5aea..73ebb9a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -Flask==2.0.2 \ No newline at end of file +Flask_WTF==0.15.1 +Flask==2.0.2