import importlib from django.core.exceptions import ObjectDoesNotExist from hotels.models import Hotel from .models import PMSIntegrationLog import os from pathlib import Path from plugins.base_plugin import BasePMSPlugin class PluginLoader: """ Класс для автоматической загрузки плагинов PMS. """ PLUGIN_PATH = Path(__file__).parent / "plugins" @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) # Ищем класс, наследующийся от BasePMSPlugin 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 except Exception as e: print(f"Ошибка загрузки плагина {module_name}: {e}") return plugins class PMSIntegrationManager: def __init__(self, hotel_id): self.hotel_id = hotel_id self.hotel = None self.pms_config = None self.plugin = None def load_hotel(self): from hotels.models import Hotel # Импорт здесь, чтобы избежать кругового импорта self.hotel = Hotel.objects.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): plugins = PluginLoader.load_plugins() if self.pms_config.name not in plugins: raise ValueError(f"Плагин для PMS {self.pms_config.name} не найден.") self.plugin = plugins[self.pms_config.name](self.pms_config) def fetch_data(self): if not self.plugin: raise ValueError("Плагин не загружен.") return self.plugin.fetch_data() def save_log(self, status, message): from .models import PMSIntegrationLog # Избегаем кругового импорта PMSIntegrationLog.objects.create( hotel=self.hotel, status=status, message=message, )