Shelter PMS fully functional
This commit is contained in:
@@ -1,18 +1,14 @@
|
||||
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
|
||||
|
||||
from django.conf import settings
|
||||
from .plugins.base_plugin import BasePMSPlugin
|
||||
from asgiref.sync import sync_to_async
|
||||
|
||||
class PluginLoader:
|
||||
"""
|
||||
Класс для автоматической загрузки плагинов PMS.
|
||||
"""
|
||||
PLUGIN_PATH = Path(__file__).parent / "plugins"
|
||||
|
||||
print("Путь к папке плагинов:", PLUGIN_PATH.resolve())
|
||||
print("Содержимое папки:", list(PLUGIN_PATH.iterdir()))
|
||||
@staticmethod
|
||||
def load_plugins():
|
||||
plugins = {}
|
||||
@@ -21,19 +17,17 @@ class PluginLoader:
|
||||
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
|
||||
print(f"Загружен плагин: {cls.__name__}")
|
||||
except Exception as e:
|
||||
print(f"Ошибка загрузки плагина {module_name}: {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
|
||||
@@ -41,28 +35,40 @@ class PMSIntegrationManager:
|
||||
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)
|
||||
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 системой.")
|
||||
raise ValueError(f"Отель {self.hotel.name} не имеет связанной PMS конфигурации.")
|
||||
|
||||
def load_plugin(self):
|
||||
"""
|
||||
Загружает плагин для PMS на основе конфигурации.
|
||||
"""
|
||||
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)
|
||||
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:
|
||||
raise ValueError("Плагин не загружен.")
|
||||
self.load_plugin()
|
||||
return self.plugin.fetch_data()
|
||||
|
||||
def save_log(self, status, message):
|
||||
from .models import PMSIntegrationLog # Избегаем кругового импорта
|
||||
PMSIntegrationLog.objects.create(
|
||||
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,
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user