Files
octopus/myproject/customers/admin.py
Andrey Smakotin fac3d55083 Удалена система лояльности из модели Customer
Удалены поля loyalty_tier, is_vip, get_loyalty_discount(), increment_total_spent().
Поле total_spent оставлено для будущего расчёта по заказам.
Обновлены admin, forms, views и шаблоны.

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-22 17:05:18 +03:00

88 lines
3.2 KiB
Python

from django.contrib import admin
from django.db import models
from .models import Customer
class IsSystemCustomerFilter(admin.SimpleListFilter):
title = 'Системный клиент'
parameter_name = 'is_system_customer'
def lookups(self, request, model_admin):
return (
('yes', 'Системный'),
('no', 'Обычный'),
)
def queryset(self, request, queryset):
if self.value() == 'yes':
return queryset.filter(is_system_customer=True)
if self.value() == 'no':
return queryset.filter(is_system_customer=False)
return queryset
@admin.register(Customer)
class CustomerAdmin(admin.ModelAdmin):
"""Административный интерфейс для управления клиентами цветочного магазина"""
list_display = (
'full_name',
'email',
'phone',
'total_spent',
'is_system_customer',
'created_at'
)
list_filter = (
IsSystemCustomerFilter,
'created_at'
)
search_fields = (
'name',
'email',
'phone'
)
date_hierarchy = 'created_at'
ordering = ('-created_at',)
readonly_fields = ('created_at', 'updated_at', 'total_spent', 'is_system_customer')
fieldsets = (
('Основная информация', {
'fields': ('name', 'email', 'phone', 'is_system_customer')
}),
('Статистика покупок', {
'fields': ('total_spent',),
'classes': ('collapse',)
}),
('Заметки', {
'fields': ('notes',)
}),
('Даты', {
'fields': ('created_at', 'updated_at'),
'classes': ('collapse',)
}),
)
def get_readonly_fields(self, request, obj=None):
"""Делаем все поля read-only для системного клиента"""
if obj and obj.is_system_customer:
# Для системного клиента все поля только для чтения
return ['name', 'email', 'phone', 'total_spent', 'is_system_customer', 'notes', 'created_at', 'updated_at']
return self.readonly_fields
def has_delete_permission(self, request, obj=None):
"""Запрет на удаление системного клиента"""
if obj and obj.is_system_customer:
return False
return super().has_delete_permission(request, obj)
def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
"""Добавляем предупреждение для системного клиента"""
extra_context = extra_context or {}
if object_id:
obj = self.get_object(request, object_id)
if obj and obj.is_system_customer:
extra_context['readonly'] = True
from django.contrib import messages
messages.warning(request, 'Это системный клиент. Редактирование запрещено для обеспечения корректной работы системы.')
return super().changeform_view(request, object_id, form_url, extra_context)