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
|
||||
@@ -93,6 +93,7 @@ TENANT_APPS = [
|
||||
'pos', # POS Terminal
|
||||
'discounts', # Скидки и промокоды
|
||||
'system_settings', # Системные настройки компании (только для владельца)
|
||||
'integrations', # Интеграции с внешними сервисами
|
||||
# TODO: 'simple_history' - вернуть позже для истории изменений
|
||||
]
|
||||
|
||||
|
||||
30
myproject/system_settings/migrations/0001_initial.py
Normal file
30
myproject/system_settings/migrations/0001_initial.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# Generated by Django 5.0.10 on 2026-01-11 19:36
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='IntegrationConfig',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('integration_id', models.CharField(choices=[('woocommerce', 'WooCommerce')], max_length=50, unique=True, verbose_name='Интеграция')),
|
||||
('is_enabled', models.BooleanField(default=False, help_text='Глобальное включение интеграции для тенанта', verbose_name='Включена')),
|
||||
('last_sync_at', models.DateTimeField(blank=True, null=True, verbose_name='Последняя синхронизация')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Дата создания')),
|
||||
('updated_at', models.DateTimeField(auto_now=True, verbose_name='Дата обновления')),
|
||||
],
|
||||
options={
|
||||
'verbose_name': 'Настройка интеграции',
|
||||
'verbose_name_plural': 'Настройки интеграций',
|
||||
'ordering': ['integration_id'],
|
||||
},
|
||||
),
|
||||
]
|
||||
3
myproject/system_settings/models/__init__.py
Normal file
3
myproject/system_settings/models/__init__.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from .integration_config import IntegrationConfig
|
||||
|
||||
__all__ = ['IntegrationConfig']
|
||||
53
myproject/system_settings/models/integration_config.py
Normal file
53
myproject/system_settings/models/integration_config.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from django.db import models
|
||||
|
||||
|
||||
class IntegrationConfig(models.Model):
|
||||
"""
|
||||
Глобальные тумблеры для включения/выключения интеграций.
|
||||
Одна запись на доступную интеграцию.
|
||||
"""
|
||||
|
||||
INTEGRATION_CHOICES = [
|
||||
('woocommerce', 'WooCommerce'),
|
||||
# Здесь добавлять новые интеграции:
|
||||
# ('shopify', 'Shopify'),
|
||||
# ('telegram', 'Telegram'),
|
||||
]
|
||||
|
||||
integration_id = models.CharField(
|
||||
max_length=50,
|
||||
choices=INTEGRATION_CHOICES,
|
||||
unique=True,
|
||||
verbose_name="Интеграция"
|
||||
)
|
||||
|
||||
is_enabled = models.BooleanField(
|
||||
default=False,
|
||||
verbose_name="Включена",
|
||||
help_text="Глобальное включение интеграции для тенанта"
|
||||
)
|
||||
|
||||
last_sync_at = models.DateTimeField(
|
||||
null=True,
|
||||
blank=True,
|
||||
verbose_name="Последняя синхронизация"
|
||||
)
|
||||
|
||||
created_at = models.DateTimeField(
|
||||
auto_now_add=True,
|
||||
verbose_name="Дата создания"
|
||||
)
|
||||
|
||||
updated_at = models.DateTimeField(
|
||||
auto_now=True,
|
||||
verbose_name="Дата обновления"
|
||||
)
|
||||
|
||||
class Meta:
|
||||
verbose_name = "Настройка интеграции"
|
||||
verbose_name_plural = "Настройки интеграций"
|
||||
ordering = ['integration_id']
|
||||
|
||||
def __str__(self):
|
||||
status = "вкл" if self.is_enabled else "выкл"
|
||||
return f"{self.get_integration_id_display()}: {status}"
|
||||
@@ -25,7 +25,12 @@
|
||||
<i class="bi bi-tag"></i> Скидки
|
||||
</a>
|
||||
</li>
|
||||
<!-- Здесь в будущем добавятся: Категории, Статусы заказов, Часовой пояс, Интеграции и т.д. -->
|
||||
<li class="nav-item" role="presentation">
|
||||
<a class="nav-link {% if 'integrations' in request.resolver_match.namespaces %}active{% endif %}"
|
||||
href="{% url 'system_settings:integrations:list' %}">
|
||||
Интеграции
|
||||
</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<!-- Контент конкретной страницы настроек -->
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
{% extends "system_settings/base_settings.html" %}
|
||||
|
||||
{% block title %}Интеграции{% endblock %}
|
||||
|
||||
{% block settings_content %}
|
||||
<div class="row">
|
||||
<!-- Левая колонка: список интеграций с тумблерами -->
|
||||
<div class="col-md-5">
|
||||
<div class="card shadow-sm">
|
||||
<div class="card-header bg-light">
|
||||
<h5 class="mb-0">Доступные интеграции</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
{% for value, label in integration_choices %}
|
||||
<div class="d-flex align-items-center justify-content-between py-2 border-bottom">
|
||||
<div>
|
||||
<span class="fw-medium">{{ label }}</span>
|
||||
<small class="text-muted d-block">Маркетплейс</small>
|
||||
</div>
|
||||
<div class="form-check form-switch">
|
||||
<input class="form-check-input integration-toggle"
|
||||
type="checkbox"
|
||||
data-integration="{{ value }}"
|
||||
id="integration-{{ value }}">
|
||||
<label class="form-check-label" for="integration-{{ value }}"></label>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Правая колонка: placeholder для настроек -->
|
||||
<div class="col-md-7">
|
||||
<div class="card shadow-sm">
|
||||
<div class="card-header bg-light">
|
||||
<h5 class="mb-0">Настройки интеграции</h5>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<div class="text-center text-muted py-5">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" fill="currentColor" class="bi bi-plug mb-3" viewBox="0 0 16 16">
|
||||
<path d="M6 0a.5.5 0 0 1 .5.5V3h3V.5a.5.5 0 0 1 1 0V3h1v2.5a1.5 1.5 0 0 1-1 1.25v4.5a.5.5 0 0 1-1 0v-4.25c-.286.14-.6.25-1 .25a2.5 2.5 0 0 1-1-.25v4.25a.5.5 0 0 1-1 0v-4.5a1.5 1.5 0 0 1-1-1.25V3h1V.5A.5.5 0 0 1 6 0Zm0 3a.5.5 0 0 1 .5.5v2a.5.5 0 0 1-1 0v-2A.5.5 0 0 1 6 3Zm3.5-.5a.5.5 0 0 1 1 0v2a.5.5 0 0 1-1 0v-2Z"/>
|
||||
</svg>
|
||||
<p class="mb-0">Выберите интеграцию слева для настройки</p>
|
||||
<small class="text-muted">Здесь появится форма с настройками выбранной интеграции</small>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- JavaScript для переключения интеграций (placeholder) -->
|
||||
<script>
|
||||
document.querySelectorAll('.integration-toggle').forEach(toggle => {
|
||||
toggle.addEventListener('change', function() {
|
||||
const integration = this.dataset.integration;
|
||||
const isEnabled = this.checked;
|
||||
|
||||
// TODO: отправить состояние на сервер
|
||||
console.log(`Интеграция ${integration}: ${isEnabled ? 'включена' : 'выключена'}`);
|
||||
|
||||
// TODO: показать/скрыть блок настроек справа
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
@@ -8,4 +8,5 @@ urlpatterns = [
|
||||
path("", SystemSettingsView.as_view(), name="settings"),
|
||||
path("roles/", include('user_roles.urls', namespace='user_roles')),
|
||||
path("discounts/", include('discounts.urls', namespace='discounts')),
|
||||
path("integrations/", include('integrations.urls', namespace='integrations')),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user