From 6a3e6d5a651296c59a1a5897eb9d4116f56856f1 Mon Sep 17 00:00:00 2001 From: trevor Date: Wed, 11 Dec 2024 18:59:06 +0900 Subject: [PATCH] PMS_integrations admin template --- pms_integration/admin.py | 52 ++++++--- .../templates/admin/check_plugins.html | 16 --- .../pms_integration/admin/check_plugins.html | 104 ++++++++++++++++++ pms_integration/utils.py | 21 ++++ touchh/settings.py | 4 +- 5 files changed, 162 insertions(+), 35 deletions(-) delete mode 100644 pms_integration/templates/admin/check_plugins.html create mode 100644 pms_integration/templates/pms_integration/admin/check_plugins.html create mode 100644 pms_integration/utils.py diff --git a/pms_integration/admin.py b/pms_integration/admin.py index ff70a6b5..35660069 100644 --- a/pms_integration/admin.py +++ b/pms_integration/admin.py @@ -1,13 +1,13 @@ from django.contrib import admin -# Register your models here. -from .manager import PluginLoader -from django.http import HttpResponseRedirect from django.urls import path -from django.utils.html import format_html from django.shortcuts import render -from django import forms +from django.utils.html import format_html +from django.http import HttpResponseRedirect +from .manager import PluginLoader from pms_integration.models import PMSConfiguration, PMSIntegrationLog - +from django import forms +from pms_integration.utils import get_all_plugins +from django.urls import reverse class PMSConfigurationForm(forms.ModelForm): class Meta: @@ -16,7 +16,6 @@ class PMSConfigurationForm(forms.ModelForm): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) - # Загружаем доступные плагины plugins = PluginLoader.load_plugins() plugin_choices = [(plugin_name, plugin_name) for plugin_name in plugins.keys()] self.fields['plugin_name'] = forms.ChoiceField(choices=plugin_choices, required=False) @@ -24,21 +23,40 @@ class PMSConfigurationForm(forms.ModelForm): @admin.register(PMSConfiguration) class PMSConfigurationAdmin(admin.ModelAdmin): form = PMSConfigurationForm - list_display = ('name', 'plugin_name', 'created_at') + list_display = ('name', 'plugin_name', 'created_at', 'check_plugins_button') search_fields = ('name', 'plugin_name') ordering = ('-created_at',) - def save_model(self, request, obj, form, change): - # Проверка на наличие плагина - plugins = PluginLoader.load_plugins() - if obj.plugin_name and obj.plugin_name not in plugins.keys(): - raise ValueError(f"Выберите корректный плагин. '{obj.plugin_name}' нет среди допустимых значений.") - super().save_model(request, obj, form, change) - - + def check_plugins_button(self, obj=None): + """ + Возвращает кнопку для проверки плагинов. + """ + url = reverse('admin:check-plugins') # Генерируем URL с помощью reverse + return format_html( + 'Проверить плагины', + url + ) + check_plugins_button.short_description = "Действия" + + def check_plugins_view(self, request, *args, **kwargs): + """ + Custom admin view to check and display available plugins. + """ + plugins = get_all_plugins() + return render(request, "pms_integration/admin/check_plugins.html", {"plugins": plugins}) + + def get_urls(self): + """ + Add custom URLs to the admin panel. + """ + urls = super().get_urls() + custom_urls = [ + path("check-plugins/", self.admin_site.admin_view(self.check_plugins_view), name="check-plugins"), + ] + return custom_urls + urls @admin.register(PMSIntegrationLog) class PMSIntegrationLogAdmin(admin.ModelAdmin): list_display = ('hotel', 'checked_at', 'status', 'message') search_fields = ('hotel__name', 'status', 'message') list_filter = ('status', 'checked_at') - ordering = ('-checked_at',) + ordering = ('-checked_at',) \ No newline at end of file diff --git a/pms_integration/templates/admin/check_plugins.html b/pms_integration/templates/admin/check_plugins.html deleted file mode 100644 index 6d757f8c..00000000 --- a/pms_integration/templates/admin/check_plugins.html +++ /dev/null @@ -1,16 +0,0 @@ -{% extends "admin/base_site.html" %} - -{% block content %} -

Проверка доступных плагинов

-

Ниже перечислены плагины, доступные в системе:

- -

- Вернуться в админку -

-{% endblock %} diff --git a/pms_integration/templates/pms_integration/admin/check_plugins.html b/pms_integration/templates/pms_integration/admin/check_plugins.html new file mode 100644 index 00000000..ba197519 --- /dev/null +++ b/pms_integration/templates/pms_integration/admin/check_plugins.html @@ -0,0 +1,104 @@ +{% extends 'admin/base.html' %} + +{% block content %} + + + +
+
+
+
+
+
Загруженные плагины PMS систем
+
+
+
+ + + + + + + + + {% for plugin_name, plugin_info in plugins.items %} + + + + + + {% empty %} + + + + {% endfor %} + +
Plugin NameDescription
{{ plugin_name }}{{ plugin_info.description|default:"No description available" }}
Нет доступных плагинов.
+
+
+ +
+
+
+
+ + + + + + + + +{% endblock %} diff --git a/pms_integration/utils.py b/pms_integration/utils.py new file mode 100644 index 00000000..75c162ea --- /dev/null +++ b/pms_integration/utils.py @@ -0,0 +1,21 @@ +from pms_integration.admin import PluginLoader + +def get_all_plugins(): + """ + Загружает все плагины и возвращает их данные. + :return: Словарь с данными о плагинах. + """ + plugins_data = {} + try: + # Загружаем плагины + plugins = PluginLoader.load_plugins() + + for plugin_name, plugin_class in plugins.items(): + plugins_data[plugin_name] = { + "class_name": plugin_class.__name__, # Имя класса + "description": plugin_class.__doc__, # Описание из DocString + "module": plugin_class.__module__, # Модуль, где определён класс + } + except Exception as e: + print(f"[ERROR] Ошибка загрузки данных о плагинах: {e}") + return plugins_data diff --git a/touchh/settings.py b/touchh/settings.py index 33e94d28..b7151258 100644 --- a/touchh/settings.py +++ b/touchh/settings.py @@ -62,8 +62,8 @@ ROOT_URLCONF = 'touchh.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, + 'DIRS': [], # Вы можете оставить пустым или указать глобальные пути к шаблонам + 'APP_DIRS': True, # Включаем автоматический поиск шаблонов в приложениях 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug',