75 lines
3.0 KiB
Python
75 lines
3.0 KiB
Python
import importlib
|
||
import os
|
||
from pathlib import Path
|
||
from django.conf import settings
|
||
from .plugins.base_plugin import BasePMSPlugin
|
||
from asgiref.sync import sync_to_async
|
||
|
||
class PluginLoader:
|
||
PLUGIN_PATH = Path(__file__).parent / "plugins"
|
||
print("Путь к папке плагинов:", PLUGIN_PATH.resolve())
|
||
print("Содержимое папки:", list(PLUGIN_PATH.iterdir()))
|
||
@staticmethod
|
||
def load_plugins():
|
||
plugins = {}
|
||
for file in os.listdir(PluginLoader.PLUGIN_PATH):
|
||
if file.endswith("_pms.py") and not file.startswith("__"):
|
||
module_name = f"pms_integration.plugins.{file[:-3]}"
|
||
try:
|
||
module = importlib.import_module(module_name)
|
||
for attr in dir(module):
|
||
cls = getattr(module, attr)
|
||
if isinstance(cls, type) and issubclass(cls, BasePMSPlugin) and cls is not BasePMSPlugin:
|
||
plugins[cls.__name__] = cls
|
||
print(f"Загружен плагин: {cls.__name__}")
|
||
except Exception as e:
|
||
print(f"Ошибка при загрузке модуля {module_name}: {e}")
|
||
print(f"Итоговый список плагинов: {list(plugins.keys())}")
|
||
return plugins
|
||
|
||
|
||
class PMSIntegrationManager:
|
||
def __init__(self, hotel_id):
|
||
self.hotel_id = hotel_id
|
||
self.hotel = None
|
||
self.pms_config = None
|
||
self.plugin = None
|
||
|
||
async def load_hotel(self):
|
||
"""
|
||
Загружает данные отеля и PMS конфигурацию.
|
||
"""
|
||
from hotels.models import Hotel
|
||
self.hotel = await sync_to_async(Hotel.objects.select_related("pms").get)(id=self.hotel_id)
|
||
self.pms_config = self.hotel.pms
|
||
if not self.pms_config:
|
||
raise ValueError(f"Отель {self.hotel.name} не имеет связанной PMS конфигурации.")
|
||
|
||
def load_plugin(self):
|
||
"""
|
||
Загружает плагин для PMS на основе конфигурации.
|
||
"""
|
||
plugins = PluginLoader.load_plugins()
|
||
if self.pms_config.plugin_name not in plugins:
|
||
raise ValueError(f"Плагин для PMS {self.pms_config.plugin_name} не найден.")
|
||
self.plugin = plugins[self.pms_config.plugin_name](self.pms_config)
|
||
|
||
def fetch_data(self):
|
||
"""
|
||
Получает данные из PMS с использованием загруженного плагина.
|
||
"""
|
||
if not self.plugin:
|
||
self.load_plugin()
|
||
return self.plugin.fetch_data()
|
||
|
||
async def save_log(self, status, message):
|
||
"""
|
||
Сохраняет запись в лог интеграции.
|
||
"""
|
||
from .models import PMSIntegrationLog
|
||
await sync_to_async(PMSIntegrationLog.objects.create)(
|
||
hotel=self.hotel,
|
||
status=status,
|
||
message=message,
|
||
)
|