Files
octopus/myproject/customers/admin.py

121 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from django.contrib import admin
from django.db import models
from .models import Customer, Address
class IsVipFilter(admin.SimpleListFilter):
title = 'VIP статус'
parameter_name = 'is_vip'
def lookups(self, request, model_admin):
return (
('yes', 'VIP'),
('no', 'Не VIP'),
)
def queryset(self, request, queryset):
if self.value() == 'yes':
return queryset.filter(loyalty_tier__in=['gold', 'platinum'])
if self.value() == 'no':
return queryset.exclude(loyalty_tier__in=['gold', 'platinum'])
return queryset
class AddressInline(admin.TabularInline):
"""Inline для управления адресами клиента в интерфейсе администратора"""
model = Address
extra = 1
verbose_name = "Адрес доставки"
verbose_name_plural = "Адреса доставки"
@admin.register(Customer)
class CustomerAdmin(admin.ModelAdmin):
"""Административный интерфейс для управления клиентами цветочного магазина"""
list_display = (
'full_name',
'email',
'phone',
'loyalty_tier',
'total_spent',
'is_vip',
'created_at'
)
list_filter = (
'loyalty_tier',
IsVipFilter,
'created_at'
)
search_fields = (
'name',
'email',
'phone'
)
date_hierarchy = 'created_at'
ordering = ('-created_at',)
readonly_fields = ('created_at', 'updated_at', 'total_spent', 'is_vip')
fieldsets = (
('Основная информация', {
'fields': ('name', 'email', 'phone')
}),
('Программа лояльности', {
'fields': ('loyalty_tier', 'total_spent', 'is_vip'),
'classes': ('collapse',)
}),
('Даты', {
'fields': ('created_at', 'updated_at'),
'classes': ('collapse',)
}),
)
inlines = [AddressInline]
@admin.register(Address)
class AddressAdmin(admin.ModelAdmin):
"""Административный интерфейс для управления адресами доставки"""
list_display = (
'recipient_name',
'full_address',
'customer',
'district',
'is_default'
)
list_filter = (
'is_default',
'district',
'created_at'
)
search_fields = (
'recipient_name',
'street',
'building_number',
'customer__name',
'customer__email'
)
ordering = ('-is_default', '-created_at')
readonly_fields = ('created_at', 'updated_at')
fieldsets = (
('Информация о получателе', {
'fields': ('customer', 'recipient_name')
}),
('Адрес доставки', {
'fields': ('street', 'building_number', 'apartment_number', 'district')
}),
('Дополнительная информация', {
'fields': ('delivery_instructions',),
'classes': ('collapse',)
}),
('Статус', {
'fields': ('is_default',),
'classes': ('collapse',)
}),
('Даты', {
'fields': ('created_at', 'updated_at'),
'classes': ('collapse',)
}),
)