11# web_app/routes/weather_routes.py
22
3- from flask import Blueprint , request , jsonify
3+ from flask import Blueprint , request , jsonify , render_template , redirect
44
55from app .weather_service import get_hourly_forecasts
66
77weather_routes = Blueprint ("weather_routes" , __name__ )
88
99@weather_routes .route ("/weather/forecast.json" )
1010def weather_forecast_api ():
11+
1112 print ("WEATHER FORECAST (API)..." )
1213 print ("URL PARAMS:" , dict (request .args ))
1314
@@ -18,4 +19,31 @@ def weather_forecast_api():
1819 if results :
1920 return jsonify (results )
2021 else :
21- return jsonify ({"message" :"Invalid Geography. Please try again." }), 404
22+ return jsonify ({"message" :"Invalid Geography. Please try again." }), 404
23+
24+ @weather_routes .route ("/weather/form" )
25+ def weather_form ():
26+ print ("WEATHER FORM..." )
27+ return render_template ("weather_form.html" )
28+
29+ @weather_routes .route ("/weather/forecast" , methods = ["GET" , "POST" ])
30+ def weather_forecast ():
31+ print ("WEATHER FORECAST..." )
32+
33+ if request .method == "GET" :
34+ print ("URL PARAMS:" , dict (request .args ))
35+ request_data = dict (request .args )
36+ elif request .method == "POST" : # the form will send a POST
37+ print ("FORM DATA:" , dict (request .form ))
38+ request_data = dict (request .form )
39+
40+ country_code = request_data .get ("country_code" ) or "US"
41+ zip_code = request_data .get ("zip_code" ) or "20057"
42+
43+ results = get_hourly_forecasts (country_code = country_code , zip_code = zip_code )
44+ if results :
45+ #flash(f"Weather Forecast Generated Successfully!", "success")
46+ return render_template ("weather_forecast.html" , country_code = country_code , zip_code = zip_code , results = results )
47+ else :
48+ #flash(f"Geography Error. Please try again!", "danger")
49+ return redirect ("/weather/form" )
0 commit comments