import locale from flask import Flask, render_template, request, redirect from forms.SearchForm import SearchForm from arztapi.APIHandler import APIHandler app = Flask(__name__) app.secret_key = "nosecretkey" locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8') @app.route('/') def main_page(): """ Show the main page, currently with the search form """ search_form = SearchForm() return render_template("index.html", form=search_form) @app.route("/search", methods=["POST"]) def search_for_doctors_with_params(): """ Use the search form with its data to parse and process it, show the results to the user. """ # Get the relevant data from the search form search_form = SearchForm(request.form) # Validate the form and its data as well as getting the relevant data if search_form.validate(): location = search_form.location.data distance = search_form.therapy_distance.data therapy_types = search_form.therapy_type.data therapy_age_range = search_form.therapy_age_range.data therapy_setting = search_form.therapy_setting.data amount_of_weeks = search_form.amount_of_weeks.data print(f"Location: {location}") print(f"Distance: {distance}") print(f"Therapy Types: {therapy_types}") print(f"Age Range: {therapy_age_range}") print(f"Therapy Setting: {therapy_setting}") print(f"Therapy Phone Types: {amount_of_weeks}") # If errors in data -> redirect to index else: print(search_form.errors) return redirect("/") # Create an APIHandler to process the given data by gathering the desired phone times api_handler = APIHandler() # Get a list of locations locations = api_handler.get_lat_lon_location_list(location) # Location not found at Arztsuche API if not locations: return render_template("no_result.html") # Use first location match TODO: find better solution than just using first best result lol location_match = locations[0] # Calculate the required base64 value why so ever base64_value = api_handler.calculate_req_value_base64(float(location_match["lat"]), float(location_match["lon"])) # Get the general list of doctors api_handler.get_list_of_doctors(float(location_match["lat"]), float(location_match["lon"]), base64_value, therapy_types, therapy_age_range, therapy_setting, amount_of_weeks) # Get their information api_handler.get_general_doctor_information() # Filter for the distance api_handler.filter_doctor_information_for_distance(distance*1000) # Get the sorted phone times for showing them at the web page sorted_doctor_phone_times = api_handler.get_doctor_phone_times_sorted(amount_of_weeks) # Return the resulting data return render_template("result.html", doctors=sorted_doctor_phone_times) if __name__ == '__main__': app.run()