Added MongoDB
Browse files- .gitignore +4 -1
- services/appointment_manager.py +35 -75
.gitignore
CHANGED
|
@@ -1,2 +1,5 @@
|
|
| 1 |
__pycache__/
|
| 2 |
-
.idea/
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
__pycache__/
|
| 2 |
+
.idea/
|
| 3 |
+
|
| 4 |
+
.env
|
| 5 |
+
Barberia/
|
services/appointment_manager.py
CHANGED
|
@@ -1,115 +1,75 @@
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
from datetime import datetime, timedelta
|
|
|
|
|
|
|
| 3 |
|
| 4 |
from models.client import Client
|
| 5 |
from models.appointment import Appointment
|
| 6 |
-
from services.utils import save_json, create_folder, load_json
|
| 7 |
|
| 8 |
|
| 9 |
class AppointmentManager:
|
| 10 |
-
def __init__(self,
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
self._opening_hours: list[str] = []
|
| 15 |
self.set_opening_hours(interval_hours)
|
| 16 |
|
| 17 |
def set_opening_hours(self, interval_hours: tuple[tuple[int]]) -> None:
|
| 18 |
opening_hours = []
|
| 19 |
-
|
| 20 |
for start, end in interval_hours:
|
| 21 |
-
|
| 22 |
end_hour = datetime.strptime(f"{end}:00", '%H:%M')
|
| 23 |
-
current_hour = start_hour
|
| 24 |
-
|
| 25 |
while current_hour < end_hour:
|
| 26 |
-
|
| 27 |
-
opening_hours.append(formatted_current_hour)
|
| 28 |
current_hour += timedelta(minutes=30)
|
| 29 |
-
|
| 30 |
self._opening_hours = opening_hours
|
| 31 |
|
| 32 |
def create_appointment(self, user_data: dict, date_data: dict) -> None:
|
| 33 |
client = Client(**user_data)
|
| 34 |
appointment = Appointment(client, **date_data)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
file_path = os.path.join(date_folder, f'{client.get_identification()}.json')
|
| 40 |
-
save_json(file_path, appointment.to_dict())
|
| 41 |
|
| 42 |
def get_available_appointments(self) -> dict[str, list[str]]:
|
| 43 |
-
"""
|
| 44 |
-
:return: A dictionary with the keys being the date as (YYYY-MM-DD) and the value is a list
|
| 45 |
-
of all the available hours as HH:MM for that day.
|
| 46 |
-
"""
|
| 47 |
today = datetime.now()
|
| 48 |
-
next_days = [today + timedelta(days=i) for i in range(28)]
|
| 49 |
|
| 50 |
-
# Get all possible appointments for the next 4 weeks
|
| 51 |
available_appointments = {
|
| 52 |
day.strftime('%Y-%m-%d'): [hour for hour in self._opening_hours] for day in next_days
|
| 53 |
}
|
| 54 |
|
| 55 |
-
# Delete hours from the current day that have already passed
|
| 56 |
current_day = today.strftime('%Y-%m-%d')
|
| 57 |
current_hour = today.strftime('%H:%M')
|
| 58 |
available_appointments[current_day] = [hour for hour in available_appointments[current_day] if hour > current_hour]
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
if
|
| 64 |
-
|
| 65 |
-
for client_file in os.listdir(os.path.join(self._saving_path, reserved_day_folder)):
|
| 66 |
-
client_data = load_json(os.path.join(self._saving_path, reserved_day_folder, client_file))
|
| 67 |
-
for appointment_data in client_data['appointments']:
|
| 68 |
-
appointment = Appointment.from_dict(appointment_data)
|
| 69 |
-
reserved_hour = appointment.get_formatted_hour()
|
| 70 |
-
if reserved_hour in available_appointments[formatted_reserved_day_folder]:
|
| 71 |
-
available_appointments[formatted_reserved_day_folder].remove(reserved_hour)
|
| 72 |
-
|
| 73 |
-
if not available_appointments[formatted_reserved_day_folder]:
|
| 74 |
-
del available_appointments[formatted_reserved_day_folder]
|
| 75 |
|
| 76 |
-
return available_appointments
|
| 77 |
|
| 78 |
def get_reserved_days(self) -> list[str]:
|
| 79 |
-
"""
|
| 80 |
-
:return: list with the days that have appointments reserved. The format for the date is (YYYY-MM-DD)
|
| 81 |
-
"""
|
| 82 |
-
reserved_appointments = list()
|
| 83 |
-
|
| 84 |
-
for day_folder in os.listdir(self._saving_path):
|
| 85 |
-
formatted_day = datetime.strptime(day_folder, '%Y%m%d').strftime('%Y-%m-%d')
|
| 86 |
-
reserved_appointments.append(formatted_day)
|
| 87 |
-
|
| 88 |
-
return reserved_appointments
|
| 89 |
|
| 90 |
def get_appointments(self, date: str) -> list:
|
| 91 |
-
""
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
for day_folder in os.listdir(self._saving_path):
|
| 101 |
-
if formatted_date != day_folder:
|
| 102 |
-
continue
|
| 103 |
-
|
| 104 |
-
for client_file in os.listdir(os.path.join(self._saving_path, day_folder)):
|
| 105 |
-
client_data = load_json(os.path.join(self._saving_path, day_folder, client_file))
|
| 106 |
-
for appointment_data in client_data['appointments']:
|
| 107 |
-
appointment = Appointment.from_dict(appointment_data)
|
| 108 |
-
appointment_info = {
|
| 109 |
-
'identification': appointment.get_client_id(),
|
| 110 |
-
'name': f'{appointment.get_client_name()} {appointment.get_client_last_name()}',
|
| 111 |
-
'hour': appointment.get_formatted_hour()
|
| 112 |
-
}
|
| 113 |
-
appointments.append(appointment_info)
|
| 114 |
-
|
| 115 |
-
return appointments
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
import os
|
| 3 |
+
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
+
from pymongo import MongoClient
|
| 6 |
+
|
| 7 |
|
| 8 |
from models.client import Client
|
| 9 |
from models.appointment import Appointment
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
class AppointmentManager:
|
| 13 |
+
def __init__(self, interval_hours=((8, 12), (14, 18))):
|
| 14 |
+
load_dotenv()
|
| 15 |
+
mongo_uri = os.getenv("MONGO_URI")
|
| 16 |
+
mongo_db = os.getenv("MONGO_DB", "barberia")
|
| 17 |
+
|
| 18 |
+
self.client = MongoClient(mongo_uri)
|
| 19 |
+
self.db = self.client[mongo_db]
|
| 20 |
+
self.collection = self.db["appointments"]
|
| 21 |
|
| 22 |
self._opening_hours: list[str] = []
|
| 23 |
self.set_opening_hours(interval_hours)
|
| 24 |
|
| 25 |
def set_opening_hours(self, interval_hours: tuple[tuple[int]]) -> None:
|
| 26 |
opening_hours = []
|
|
|
|
| 27 |
for start, end in interval_hours:
|
| 28 |
+
current_hour = datetime.strptime(f"{start}:00", '%H:%M')
|
| 29 |
end_hour = datetime.strptime(f"{end}:00", '%H:%M')
|
|
|
|
|
|
|
| 30 |
while current_hour < end_hour:
|
| 31 |
+
opening_hours.append(current_hour.strftime('%H:%M'))
|
|
|
|
| 32 |
current_hour += timedelta(minutes=30)
|
|
|
|
| 33 |
self._opening_hours = opening_hours
|
| 34 |
|
| 35 |
def create_appointment(self, user_data: dict, date_data: dict) -> None:
|
| 36 |
client = Client(**user_data)
|
| 37 |
appointment = Appointment(client, **date_data)
|
| 38 |
+
data = appointment.to_dict()
|
| 39 |
+
data["date"] = appointment.get_formatted_date()
|
| 40 |
+
data["hour"] = appointment.get_formatted_hour()
|
| 41 |
+
self.collection.insert_one(data)
|
|
|
|
|
|
|
| 42 |
|
| 43 |
def get_available_appointments(self) -> dict[str, list[str]]:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
today = datetime.now()
|
| 45 |
+
next_days = [today + timedelta(days=i) for i in range(28)]
|
| 46 |
|
|
|
|
| 47 |
available_appointments = {
|
| 48 |
day.strftime('%Y-%m-%d'): [hour for hour in self._opening_hours] for day in next_days
|
| 49 |
}
|
| 50 |
|
|
|
|
| 51 |
current_day = today.strftime('%Y-%m-%d')
|
| 52 |
current_hour = today.strftime('%H:%M')
|
| 53 |
available_appointments[current_day] = [hour for hour in available_appointments[current_day] if hour > current_hour]
|
| 54 |
|
| 55 |
+
for record in self.collection.find():
|
| 56 |
+
day = record.get("date")
|
| 57 |
+
hour = record.get("hour")
|
| 58 |
+
if day in available_appointments and hour in available_appointments[day]:
|
| 59 |
+
available_appointments[day].remove(hour)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
return {k: v for k, v in available_appointments.items() if v}
|
| 62 |
|
| 63 |
def get_reserved_days(self) -> list[str]:
|
| 64 |
+
return sorted(list(set(doc["date"] for doc in self.collection.find({}, {"date": 1}))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
def get_appointments(self, date: str) -> list:
|
| 67 |
+
results = self.collection.find({"date": date})
|
| 68 |
+
return [
|
| 69 |
+
{
|
| 70 |
+
"identification": r["client"]["identification"],
|
| 71 |
+
"name": f"{r['client']['name']} {r['client']['last_name']}",
|
| 72 |
+
"hour": r["hour"]
|
| 73 |
+
}
|
| 74 |
+
for r in results
|
| 75 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|