Add week filter and already passed times today

This commit is contained in:
Lea 2024-08-28 17:01:17 +02:00
parent 9291018b3e
commit 50271d1607
5 changed files with 88 additions and 30 deletions

7
app.py
View File

@ -1,6 +1,6 @@
import locale
from flask import Flask, render_template, request
from flask import Flask, render_template, request, redirect
from forms.SearchForm import SearchForm
from arztapi.APIHandler import APIHandler
@ -25,15 +25,18 @@ def search_for_doctors_with_params():
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}")
else:
print(search_form.errors)
return redirect("/")
api_handler = APIHandler()
locations = api_handler.get_lat_lon_location_list(location)[0]
@ -42,7 +45,7 @@ def search_for_doctors_with_params():
therapy_age_range, therapy_setting)
api_handler.get_general_doctor_information()
api_handler.filter_doctor_information_for_distance(distance*1000)
sorted_doctor_phone_times = api_handler.get_doctor_phone_times_sorted()
sorted_doctor_phone_times = api_handler.get_doctor_phone_times_sorted(amount_of_weeks)
return render_template("result.html", doctors=sorted_doctor_phone_times)

View File

@ -3,7 +3,7 @@ from typing import List
import requests
from requests import JSONDecodeError
import base64
from datetime import datetime
from datetime import datetime, timedelta
from arztapi.ArztPraxisDatas import ArztPraxisDatas
from arztapi.DoctorInformation import DoctorInformation, PhoneTime
from arztapi.DoctorPhoneTime import DoctorPhoneTime
@ -14,6 +14,7 @@ class APIHandler:
self.base_api_url = "https://arztsuche.116117.de/api/"
self.phone_times = []
self.general_information = []
self.processed_doctor_phone_times = []
def get_lat_lon_location_list(self, location):
api_path = self.base_api_url + "location"
@ -138,8 +139,7 @@ class APIHandler:
self.general_information = [doctor_information for doctor_information in self.general_information
if doctor_information.distance <= distance]
def get_doctor_phone_times_sorted(self):
doctor_phone_times = []
def get_doctor_phone_times_sorted(self, therapy_phone_weeks):
for doctor_information in self.general_information:
for phone_time in doctor_information.telefonzeiten:
doctor_phone_time_dict = {
@ -152,12 +152,32 @@ class APIHandler:
"doctor_phone_number": doctor_information.tel
}
doctor_phone_time = DoctorPhoneTime(**doctor_phone_time_dict)
doctor_phone_times.append(doctor_phone_time)
self.processed_doctor_phone_times.append(doctor_phone_time)
doctor_phone_times.sort(key=lambda dpt: dpt.phone_time.start)
self.filter_for_relevant_weeks(therapy_phone_weeks)
self.processed_doctor_phone_times.sort(key=lambda dpt: dpt.phone_time.start)
self.filter_for_already_passed_times_today()
self.assign_numbers_to_doctor_phone_times()
return self.processed_doctor_phone_times
def filter_for_relevant_weeks(self, therapy_phone_weeks):
current_date = datetime.now()
end_date = current_date + timedelta(weeks=therapy_phone_weeks)
self.processed_doctor_phone_times = [dpt for dpt in self.processed_doctor_phone_times
if current_date <= dpt.phone_time.start <= end_date]
def filter_for_already_passed_times_today(self):
current_datetime = datetime.now()
self.processed_doctor_phone_times = [dpt for dpt in self.processed_doctor_phone_times
if (dpt.phone_time.start.date() == current_datetime.date() and
dpt.phone_time.end > current_datetime)
or dpt.phone_time.start.date() != current_datetime.date()]
def assign_numbers_to_doctor_phone_times(self):
known_doctor_names_with_nr = {}
doctor_count = 1
for doctor_phone_time in doctor_phone_times:
for doctor_phone_time in self.processed_doctor_phone_times:
if doctor_phone_time.doctor_name not in known_doctor_names_with_nr:
doctor_phone_time.doctor_nr = doctor_count
known_doctor_names_with_nr[doctor_phone_time.doctor_name] = doctor_count
@ -166,8 +186,6 @@ class APIHandler:
known_doctor_count = known_doctor_names_with_nr[doctor_phone_time.doctor_name]
doctor_phone_time.doctor_nr = known_doctor_count
return doctor_phone_times
@staticmethod
def calculate_req_value_base64(lat, lon):
"""

View File

@ -10,12 +10,20 @@ def validate_therapy_types(form, field):
raise validators.ValidationError("Invalid value in therapy types selection.")
def validate_therapy_phone_times(form, field):
valid_choices = {1, 2, 3, 4}
if not field.data:
raise validators.ValidationError("Please select at least one option.")
if not all(choice in valid_choices for choice in field.data):
raise validators.ValidationError("Invalid value in therapy phone types selection.")
class RangeInput(Input):
input_type = "range"
class SearchForm(Form):
location = StringField("Standort", validators=[validators.InputRequired(), validators.Length(min=2, max=50),
location = StringField("Standort/PLZ", validators=[validators.InputRequired(), validators.Length(min=2, max=50),
validators.Regexp("^[a-zA-ZäöüßÄÖÜẞ]+$")])
therapy_distance = IntegerField(
"Distanz (in km)",
@ -35,5 +43,10 @@ class SearchForm(Form):
therapy_setting = RadioField("Therapiesetting", choices=[
("E", "Einzeltherapie"),
("G", "Gruppentherapie")], validators=[validators.InputRequired(), validators.any_of(["E", "G"])])
# TODO: find a better name for label
amount_of_weeks = IntegerField(
"Telefonzeiten für die nächsten Wochen",
widget=RangeInput(),
default=1,
validators=[validators.InputRequired(), validators.NumberRange(min=1, max=4)]
)

View File

@ -8,7 +8,7 @@
{{ form.csrf_token }}
<fieldset>
<legend>Suche</legend>
<div class="mb-3">
<div class="mb-2">
<label for="locationInput" class="form-label mt-4">{{ form.location.label }}</label>
{{ form.location(class="form-control", id="locationInput", placeholder="Standort/PLZ eingeben") }}
{% if form.location.errors %}
@ -17,7 +17,7 @@
</div>
{% endif %}
<fieldset>
<div class="mt-4">
<div class="mt-2">
<label for="distanceRange" class="form-label">{{ form.therapy_distance.label }}</label>
{{ form.therapy_distance(class="form-range", id="distanceRange", min=0, max=50, step=1, oninput="this.nextElementSibling.value = this.value") }}
<output>{{ form.therapy_distance.data or 25 }}</output>
@ -31,7 +31,7 @@
</fieldset>
</div>
<div class="mb-3">
<div class="mb-2">
<fieldset>
<legend class="mt-4">{{ form.therapy_type.label }}</legend>
{% for subfield in form.therapy_type %}
@ -50,7 +50,7 @@
</fieldset>
</div>
<div class="mb-3">
<div class="mb-2">
<fieldset>
<legend class="mt-4">{{ form.therapy_age_range.label }}</legend>
{% for subfield in form.therapy_age_range %}
@ -69,7 +69,7 @@
</fieldset>
</div>
<div class="mb-3">
<div class="mb-2">
<fieldset>
<legend class="mt-4">{{ form.therapy_setting.label }}</legend>
{% for subfield in form.therapy_setting %}
@ -88,6 +88,22 @@
</fieldset>
</div>
<div class="mb-2">
<fieldset>
<div class="mt-2">
<label for="timeRange" class="form-label">{{ form.amount_of_weeks.label }}</label>
{{ form.amount_of_weeks(class="form-range", id="timeRange", min=1, max=4, step=1, oninput="this.nextElementSibling.value = this.value") }}
<output>{{ form.amount_of_weeks.data or 4 }}</output>
nächste(n) Woche(n)
{% if form.therapy_distance.errors %}
<div class="text-danger">
{{ form.amount_of_weeks.errors[0] }}
</div>
{% endif %}
</div>
</fieldset>
</div>
<button type="submit" class="btn btn-primary">Suche</button>
</fieldset>
</form>

View File

@ -3,7 +3,7 @@
{% block content %}
<div class="container">
<div class="row">
<div class="col-lg-8 col-md-7 col-sm-6">
<div class="col-lg-12 col-md-11 col-sm-10">
<h2>Suchergebnisse</h2>
<table class="table table-hover">
<thead>
@ -17,12 +17,20 @@
</tr>
</thead>
<tbody>
{% set day_classes = {
"1": "table-secondary",
"2": "table-success",
"3": "table-info",
"4": "table-light",
"5": "table-primary",
} %}
{% for doctor in doctors %}
<tr class="table-secondary">
<tr class="{{ day_classes.get(doctor.phone_time.start.strftime("%w")) }}">
<th scope="row">{{ doctor.doctor_nr }}</th>
<th scope="row">{{ doctor.doctor_name }}</th>
<td>{{ doctor.phone_time.start.strftime("%A %d.%m") }}</td>
<td>{{ doctor.phone_time.start.strftime("%H:%M") }} bis {{ doctor.phone_time.end.strftime("%H:%M") }}</td>
<td>{{ doctor.phone_time.start.strftime("%H:%M") }}
bis {{ doctor.phone_time.end.strftime("%H:%M") }}</td>
<td>{{ doctor.doctor_phone_number }}</td>
<td>{{ doctor.doctor_address }}</td>
</tr>