File size: 3,652 Bytes
7083742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import requests
from datetime import datetime

import pandas as pd
import gradio as gr


def make_appointment():
    url = "http://localhost:8000/appointments/available"
    available_dates = requests.get(url)
    available_days = list(available_dates.json()['available_dates'].keys())

    update_home_tab = gr.update(visible=False)
    update_schedule_tab = gr.update(visible=True)
    update_search_tab = gr.update(visible=False)
    update_available_days = gr.update(
        choices=available_days, value=available_days[0], allow_custom_value=True, interactive=True
    )
    update_hours = get_available_hours(available_days[0], available_dates)

    return update_home_tab, update_schedule_tab, update_search_tab, available_dates, update_available_days, update_hours


def search_appointments_days():
    url = "http://localhost:8000/appointments/reserved"
    reserved_days = requests.get(url)
    reserved_days = list(reserved_days.json()['reserved_days'])

    update_home_tab = gr.update(visible=False)
    update_schedule_tab = gr.update(visible=False)
    update_search_tab = gr.update(visible=True)
    update_reserved_days = gr.update(
        choices=reserved_days, value=reserved_days[0], allow_custom_value=True, interactive=True
    )
    return update_home_tab, update_schedule_tab, update_search_tab, update_reserved_days


def get_back_home():
    update_home_tab = gr.update(visible=True)
    update_schedule_tab = gr.update(visible=False)
    update_search_tab = gr.update(visible=False)
    return update_home_tab, update_schedule_tab, update_search_tab


def _get_long_hour(hours):
    formatted_hours = [
        datetime.strptime(hour, '%H:%M').strftime('%I:%M %p').replace("AM", "a.m.").replace("PM", "p.m.")
        for hour in hours
    ]
    return formatted_hours


def _get_short_hour(hour):
    return datetime.strptime(hour.replace("a.m.", "AM").replace("p.m.", "PM"), '%I:%M %p').strftime('%H:%M')


def get_available_hours(selected_day, available_dates):
    available_hours = available_dates.json()['available_dates'][selected_day]
    formatted_hours = _get_long_hour(available_hours)
    update_hours = gr.update(choices=formatted_hours, value=formatted_hours[0], allow_custom_value=True, interactive=True)
    return update_hours


def create_appointment(identification, name, last_name, age, day, hour):
    url = "http://localhost:8000/appointments/"
    data = {
        "user": {
            "identification": identification,
            "name": name,
            "last_name": last_name,
            "age": age
        },
        "full_date": {
            "date": day,
            "hour": _get_short_hour(hour)
        }
    }
    response = requests.post(url, json=data)
    if response.status_code != 200:
        update_home_tab, update_schedule_tab, update_search_tab = gr.update(), gr.update(), gr.update()
        gr.Warning(f"Error: {response.json()}")
        return update_home_tab, update_schedule_tab, update_search_tab

    update_home_tab = gr.update(visible=True)
    update_schedule_tab = gr.update(visible=False)
    update_search_tab = gr.update(visible=False)
    gr.Info("Cita agendada con éxito")

    return update_home_tab, update_schedule_tab, update_search_tab


def search_appointments(selected_day):
    url = f"http://localhost:8000/appointments/{selected_day}"
    appointments = requests.get(url)
    appointments = appointments.json()['appointments']
    df = pd.DataFrame(data=appointments)
    df = df.rename(columns={
        'hour': 'Hora', 'identification': 'Cedula', 'name': 'Nombres'
    })
    update_appointments = gr.update(value=df)
    return update_appointments