fix: trigger tenant activation when status changed to approved

Added save_model override to TenantRegistrationAdmin to catch status
changes from PENDING to APPROVED and trigger the full activation flow.

Previously, changing status via admin form only saved the status field
without creating the tenant, sending emails, etc.

Now both methods work:
- Click "Activate" button (via GET parameter)
- Change status dropdown and save (via save_model)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-12-01 22:15:43 +03:00
parent fb4bcf37ec
commit 4da6d6922e

View File

@@ -204,6 +204,36 @@ class TenantRegistrationAdmin(admin.ModelAdmin):
resend_password_setup_email.short_description = "📧 Отправить письмо повторно"
def save_model(self, request, obj, form, change):
"""
Отлавливаем изменение статуса на APPROVED и запускаем процесс активации
"""
# Если это изменение существующего объекта
if change:
# Получаем старое значение из БД
try:
old_obj = TenantRegistration.objects.get(pk=obj.pk)
old_status = old_obj.status
except TenantRegistration.DoesNotExist:
old_status = None
# Если статус изменился на APPROVED
if old_status == TenantRegistration.STATUS_PENDING and obj.status == TenantRegistration.STATUS_APPROVED:
# Не сохраняем пока - сначала активируем
try:
self._approve_registration(obj, request.user)
messages.success(request, f"Заявка '{obj.shop_name}' успешно активирована!")
return # _approve_registration уже сохраняет объект
except Exception as e:
messages.error(request, f"Ошибка при активации: {str(e)}")
# Откатываем статус обратно
obj.status = old_status
super().save_model(request, obj, form, change)
return
# Обычное сохранение для всех остальных случаев
super().save_model(request, obj, form, change)
def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
"""
Обработка действий активации/отклонения через GET параметры