🐛 memory.py: исправлена опечатка /data -> /"data"

This commit is contained in:
DevProject Agent
2026-06-14 19:28:44 +04:00
parent fb329ab6e2
commit b29a033831
+24 -32
View File
@@ -1,31 +1,28 @@
import json
import os
from pathlib import Path
DB_DIR = Path(__file__).resolve().parent.parent.parent / data
DB_DIR = Path(__file__).resolve().parent.parent.parent / "data"
DB_DIR.mkdir(exist_ok=True)
USERS_FILE = DB_DIR / users.json
BOOKINGS_FILE = DB_DIR / bookings.json
USERS_FILE = DB_DIR / "users.json"
BOOKINGS_FILE = DB_DIR / "bookings.json"
def _load_json(path, default):
if path.exists():
try:
with open(path, r, encoding=utf-8) as f:
return json.load(f)
with open(path, "r", encoding="utf-8") as fh:
return json.load(fh)
except (json.JSONDecodeError, OSError):
return default
return default
def _save_json(path, data):
with open(path, w, encoding=utf-8) as f:
json.dump(data, f, ensure_ascii=False, indent=2)
with open(path, "w", encoding="utf-8") as fh:
json.dump(data, fh, ensure_ascii=False, indent=2)
# ——— Users ———
def load_users():
return _load_json(USERS_FILE, {})
@@ -45,8 +42,6 @@ def set_user(user_id, data):
save_users(users)
# ——— Bookings ———
def load_bookings():
return _load_json(BOOKINGS_FILE, [])
@@ -58,13 +53,13 @@ def save_bookings(bookings):
def create_booking(user_id, service, master, date, time):
bookings = load_bookings()
booking = {
id: len(bookings) + 1,
user_id: user_id,
service: service,
master: master,
date: date,
time: time,
status: active,
"id": len(bookings) + 1,
"user_id": user_id,
"service": service,
"master": master,
"date": date,
"time": time,
"status": "active",
}
bookings.append(booking)
save_bookings(bookings)
@@ -74,10 +69,8 @@ def create_booking(user_id, service, master, date, time):
def get_busy_times(master, date):
bookings = load_bookings()
return [
b[time] for b in bookings
if b[master] == master
and b[date] == date
and b[status] == active
b["time"] for b in bookings
if b["master"] == master and b["date"] == date and b["status"] == "active"
]
@@ -85,16 +78,15 @@ def get_user_bookings(user_id):
bookings = load_bookings()
return [
b for b in bookings
if b[user_id] == user_id
and b[status] == active
if b["user_id"] == user_id and b["status"] == "active"
]
def cancel_booking(booking_id):
bookings = load_bookings()
for b in bookings:
if b[id] == booking_id:
b[status] = cancelled
if b["id"] == booking_id:
b["status"] = "cancelled"
save_bookings(bookings)
return True
return False
@@ -103,11 +95,11 @@ def cancel_booking(booking_id):
def update_booking(booking_id, service, master, date, time):
bookings = load_bookings()
for b in bookings:
if b[id] == booking_id:
b[service] = service
b[master] = master
b[date] = date
b[time] = time
if b["id"] == booking_id:
b["service"] = service
b["master"] = master
b["date"] = date
b["time"] = time
save_bookings(bookings)
return b
return None