feat(integrations): добавлен фундамент для интеграций с внешними сервисами
- Создано приложение integrations с базовой архитектурой - BaseIntegration (абстрактная модель) для всех интеграций - BaseIntegrationService (абстрактный сервисный класс) - IntegrationConfig модель для тумблеров в system_settings - Добавлена вкладка "Интеграции" в системные настройки - Заготовка UI с тумблерами для включения интеграций Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1
myproject/integrations/__init__.py
Normal file
1
myproject/integrations/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
default_app_config = 'integrations.apps.IntegrationsConfig'
|
||||
7
myproject/integrations/admin.py
Normal file
7
myproject/integrations/admin.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.contrib import admin
|
||||
|
||||
|
||||
# Регистрация конкретных интеграций будет здесь
|
||||
# @admin.register(WooCommerceIntegration)
|
||||
# class WooCommerceIntegrationAdmin(admin.ModelAdmin):
|
||||
# pass
|
||||
7
myproject/integrations/apps.py
Normal file
7
myproject/integrations/apps.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class IntegrationsConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'integrations'
|
||||
verbose_name = 'Интеграции'
|
||||
3
myproject/integrations/models/__init__.py
Normal file
3
myproject/integrations/models/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .base import BaseIntegration
|
||||
|
||||
__all__ = ['BaseIntegration']
|
||||
93
myproject/integrations/models/base.py
Normal file
93
myproject/integrations/models/base.py
Normal file
@@ -0,0 +1,93 @@
|
||||
from django.db import models
|
||||
from django.core.exceptions import ValidationError
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class IntegrationType(models.TextChoices):
|
||||
MARKETPLACE = 'marketplace', 'Маркетплейс'
|
||||
PAYMENT = 'payment', 'Платёжная система'
|
||||
SHIPPING = 'shipping', 'Служба доставки'
|
||||
|
||||
|
||||
class BaseIntegration(models.Model):
|
||||
"""
|
||||
Абстрактный базовый класс для всех интеграций.
|
||||
Содержит общие поля и логику валидации.
|
||||
"""
|
||||
|
||||
integration_type = models.CharField(
|
||||
max_length=20,
|
||||
choices=IntegrationType.choices,
|
||||
editable=False,
|
||||
verbose_name="Тип интеграции"
|
||||
)
|
||||
|
||||
is_active = models.BooleanField(
|
||||
default=True,
|
||||
verbose_name="Активна",
|
||||
db_index=True,
|
||||
help_text="Интеграция используется только если включена здесь и в системных настройках"
|
||||
)
|
||||
|
||||
name = models.CharField(
|
||||
max_length=100,
|
||||
verbose_name="Название",
|
||||
help_text="Произвольное название для удобства"
|
||||
)
|
||||
|
||||
created_at = models.DateTimeField(
|
||||
auto_now_add=True,
|
||||
verbose_name="Дата создания"
|
||||
)
|
||||
|
||||
updated_at = models.DateTimeField(
|
||||
auto_now=True,
|
||||
verbose_name="Дата обновления"
|
||||
)
|
||||
|
||||
# Общие credential поля (можно переопределить в наследниках)
|
||||
api_key = models.CharField(
|
||||
max_length=255,
|
||||
blank=True,
|
||||
verbose_name="API ключ",
|
||||
help_text="Будет зашифрован при сохранении"
|
||||
)
|
||||
|
||||
api_secret = models.CharField(
|
||||
max_length=255,
|
||||
blank=True,
|
||||
verbose_name="API секрет",
|
||||
help_text="Будет зашифрован при сохранении"
|
||||
)
|
||||
|
||||
# Дополнительные настройки в JSON для гибкости
|
||||
extra_config = models.JSONField(
|
||||
default=dict,
|
||||
blank=True,
|
||||
verbose_name="Доп. настройки"
|
||||
)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
ordering = ['-is_active', 'name']
|
||||
indexes = [
|
||||
models.Index(fields=['is_active', 'integration_type']),
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.get_integration_type_display()}: {self.name}"
|
||||
|
||||
@property
|
||||
def is_configured(self) -> bool:
|
||||
"""
|
||||
Проверить, есть ли необходимые credentials.
|
||||
Можно переопределить в наследниках.
|
||||
"""
|
||||
return bool(self.api_key)
|
||||
|
||||
def clean(self):
|
||||
"""Валидацияcredentials"""
|
||||
if self.is_active and not self.is_configured:
|
||||
raise ValidationError({
|
||||
'api_key': 'API ключ обязателен для активной интеграции'
|
||||
})
|
||||
3
myproject/integrations/services/__init__.py
Normal file
3
myproject/integrations/services/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .base import BaseIntegrationService
|
||||
|
||||
__all__ = ['BaseIntegrationService']
|
||||
48
myproject/integrations/services/base.py
Normal file
48
myproject/integrations/services/base.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
class BaseIntegrationService(ABC):
|
||||
"""
|
||||
Базовый класс для всех интеграционных сервисов.
|
||||
Определяет общий интерфейс для работы с внешними API.
|
||||
"""
|
||||
|
||||
def __init__(self, config):
|
||||
"""
|
||||
Args:
|
||||
config: Экземпляр модели интеграции (наследник BaseIntegration)
|
||||
"""
|
||||
self.config = config
|
||||
|
||||
@abstractmethod
|
||||
def test_connection(self) -> Tuple[bool, str]:
|
||||
"""
|
||||
Проверить соединение с внешним API.
|
||||
|
||||
Returns:
|
||||
tuple: (success: bool, message: str)
|
||||
"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def sync(self) -> Tuple[bool, str]:
|
||||
"""
|
||||
Выполнить основную операцию синхронизации.
|
||||
|
||||
Returns:
|
||||
tuple: (success: bool, message: str)
|
||||
"""
|
||||
pass
|
||||
|
||||
def is_available(self) -> bool:
|
||||
"""
|
||||
Проверить, готова ли интеграция к использованию.
|
||||
|
||||
Returns:
|
||||
bool: True если интеграция активна и настроена
|
||||
"""
|
||||
return (
|
||||
self.config.is_active and
|
||||
self.config.is_configured
|
||||
)
|
||||
11
myproject/integrations/urls.py
Normal file
11
myproject/integrations/urls.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from django.urls import path
|
||||
from .views import IntegrationsListView
|
||||
|
||||
app_name = 'integrations'
|
||||
|
||||
urlpatterns = [
|
||||
path("", IntegrationsListView.as_view(), name="list"),
|
||||
# Здесь будут добавляться endpoint'ы для интеграций
|
||||
# path("test/<int:id>/", views.test_connection, name="test_connection"),
|
||||
# path("webhook/<str:integration_type>/", views.webhook, name="webhook"),
|
||||
]
|
||||
13
myproject/integrations/views.py
Normal file
13
myproject/integrations/views.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from django.views.generic import TemplateView
|
||||
from user_roles.mixins import OwnerRequiredMixin
|
||||
|
||||
|
||||
class IntegrationsListView(OwnerRequiredMixin, TemplateView):
|
||||
"""Страница настроек интеграций (доступна только владельцу)"""
|
||||
template_name = "system_settings/integrations.html"
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
from system_settings.models import IntegrationConfig
|
||||
context['integration_choices'] = IntegrationConfig.INTEGRATION_CHOICES
|
||||
return context
|
||||
Reference in New Issue
Block a user