Files
Touchh/pms_integration/manager.py

54 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# pms_integration/manager.py
import importlib
from hotels.models import Hotel, PMSIntegrationLog, Reservation
from asgiref.sync import sync_to_async
class PMSIntegrationManager:
"""Универсальный менеджер интеграции с PMS."""
def __init__(self, hotel_id):
self.hotel_id = hotel_id
self.hotel = None
self.plugin = None
async def load_hotel_data(self):
"""Загружает данные отеля и инициализирует соответствующий плагин."""
self.hotel = await sync_to_async(Hotel.objects.get)(id=self.hotel_id)
pms_system = self.hotel.pms
if not pms_system:
raise ValueError("PMS система не настроена для отеля.")
plugin_module = f"pms_integration.plugins.{pms_system.name.lower()}_pms"
try:
plugin_class = getattr(importlib.import_module(plugin_module), f"{pms_system.name.capitalize()}PMSPlugin")
self.plugin = plugin_class(self.hotel.api)
except (ImportError, AttributeError) as e:
raise ValueError(f"Плагин для PMS '{pms_system.name}' не найден: {e}")
async def fetch_and_save_data(self):
"""Получение данных от PMS и их сохранение."""
raw_data = self.plugin.fetch_data()
parsed_data = self.plugin.parse_data(raw_data)
# Сохраняем данные в БД
for res in parsed_data:
await sync_to_async(Reservation.objects.update_or_create)(
reservation_id=res["id"],
defaults={
"hotel": self.hotel,
"room_number": res["room_number"],
"room_type": res["room_type"],
"check_in": res["check_in"],
"check_out": res["check_out"],
"status": res["status"],
"price": res.get("price"),
},
)
# Логируем успех
await sync_to_async(PMSIntegrationLog.objects.create)(
hotel=self.hotel, status="success", message="Данные успешно обновлены."
)