theraPy/app.py

103 lines
3.3 KiB
Python
Raw Permalink Normal View History

2024-08-26 21:05:34 +02:00
import locale
2024-11-07 17:03:19 +01:00
import redis
from flask import Flask, render_template, request, redirect
2024-11-07 17:03:19 +01:00
from flask_caching import Cache
2024-08-26 21:05:34 +02:00
from forms.SearchForm import SearchForm
from arztapi.APIHandler import APIHandler
app = Flask(__name__)
2024-10-14 19:58:24 +02:00
app.config.from_prefixed_env()
app.secret_key = app.config["THERAPY_SECRET"]
2024-08-26 21:05:34 +02:00
locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
2024-11-07 17:03:19 +01:00
app.config["CACHE_TYPE"] = "redis"
2024-11-08 09:49:19 +01:00
app.config["CACHE_REDIS_HOST"] = "redis"
2024-11-07 17:03:19 +01:00
app.config["CACHE_REDIS_PORT"] = 6379
app.config["CACHE_REDIS_DB"] = 0
cache = Cache(app=app)
cache.init_app(app)
2024-11-08 09:49:19 +01:00
redis_client = redis.Redis(
host=app.config["CACHE_REDIS_HOST"],
port=app.config["CACHE_REDIS_PORT"],
db=app.config["CACHE_REDIS_DB"]
)
2024-08-26 21:05:34 +02:00
@app.route('/')
def main_page():
2024-09-07 21:12:55 +02:00
"""
Show the main page, currently with the search form
"""
2024-08-26 21:05:34 +02:00
search_form = SearchForm()
return render_template("index.html", form=search_form)
@app.route("/search", methods=["POST"])
def search_for_doctors_with_params():
2024-09-07 21:12:55 +02:00
"""
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
2024-08-26 21:05:34 +02:00
search_form = SearchForm(request.form)
2024-09-07 21:12:55 +02:00
# Validate the form and its data as well as getting the relevant data
2024-08-26 21:05:34 +02:00
if search_form.validate():
location = search_form.location.data
2024-08-27 23:09:19 +02:00
distance = search_form.therapy_distance.data
2024-08-26 21:05:34 +02:00
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
2024-08-26 21:05:34 +02:00
2024-09-07 21:12:55 +02:00
# If errors in data -> redirect to index
2024-08-26 21:05:34 +02:00
else:
return redirect("/")
2024-08-26 21:05:34 +02:00
2024-09-07 21:12:55 +02:00
# Create an APIHandler to process the given data by gathering the desired phone times
2024-11-08 09:49:19 +01:00
api_handler = APIHandler(redis_client)
2024-09-07 21:12:55 +02:00
# Get a list of locations
locations = api_handler.get_lat_lon_location_list(location)
2024-09-07 21:12:55 +02:00
# Location not found at Arztsuche API
if not locations:
return render_template("no_result.html")
2024-09-07 21:12:55 +02:00
# Use first location match TODO: find better solution than just using first best result lol
location_match = locations[0]
2024-09-07 21:12:55 +02:00
# Calculate the required base64 value why so ever
base64_value = api_handler.calculate_req_value_base64(float(location_match["lat"]), float(location_match["lon"]))
2024-09-07 21:12:55 +02:00
# 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)
2024-09-07 21:12:55 +02:00
# Get their information
2024-08-26 21:05:34 +02:00
api_handler.get_general_doctor_information()
2024-09-07 21:12:55 +02:00
# Filter for the distance
2024-08-27 23:09:19 +02:00
api_handler.filter_doctor_information_for_distance(distance*1000)
2024-09-07 21:12:55 +02:00
# 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)
2024-08-26 21:05:34 +02:00
2024-09-07 21:12:55 +02:00
# Return the resulting data
2024-08-26 21:05:34 +02:00
return render_template("result.html", doctors=sorted_doctor_phone_times)
2024-10-12 20:13:27 +02:00
@app.route("/search", methods=["GET"])
def search_direct():
return redirect("/")
@app.route('/about')
def about():
return render_template("about.html")
2024-10-12 20:13:27 +02:00
2024-10-12 21:39:20 +02:00
@app.route('/privacy')
def privacy():
return render_template("privacy.html")
@app.route('/impress')
def impress():
return render_template("impress.html")
2024-10-12 20:13:27 +02:00
if __name__ == "__main__":
app.run()