theraPy/app.py
2024-11-08 09:49:19 +01:00

103 lines
3.3 KiB
Python

import locale
import redis
from flask import Flask, render_template, request, redirect
from flask_caching import Cache
from forms.SearchForm import SearchForm
from arztapi.APIHandler import APIHandler
app = Flask(__name__)
app.config.from_prefixed_env()
app.secret_key = app.config["THERAPY_SECRET"]
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
app.config["CACHE_TYPE"] = "redis"
app.config["CACHE_REDIS_HOST"] = "redis"
app.config["CACHE_REDIS_PORT"] = 6379
app.config["CACHE_REDIS_DB"] = 0
cache = Cache(app=app)
cache.init_app(app)
redis_client = redis.Redis(
host=app.config["CACHE_REDIS_HOST"],
port=app.config["CACHE_REDIS_PORT"],
db=app.config["CACHE_REDIS_DB"]
)
@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
# If errors in data -> redirect to index
else:
return redirect("/")
# Create an APIHandler to process the given data by gathering the desired phone times
api_handler = APIHandler(redis_client)
# 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)
@app.route("/search", methods=["GET"])
def search_direct():
return redirect("/")
@app.route('/about')
def about():
return render_template("about.html")
@app.route('/privacy')
def privacy():
return render_template("privacy.html")
@app.route('/impress')
def impress():
return render_template("impress.html")
if __name__ == "__main__":
app.run()