You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
3.0 KiB
85 lines
3.0 KiB
from django.contrib import admin
|
|
from .models import Product, Color, Size, Order, OrderItem, GuestUser, Coupon, CouponUsage
|
|
from django.utils.html import format_html
|
|
|
|
@admin.register(Product)
|
|
class ProductAdmin(admin.ModelAdmin):
|
|
list_display = ("title", "ordering_value", "price", "cut")
|
|
|
|
@admin.register(Color)
|
|
class ColorAdmin(admin.ModelAdmin):
|
|
list_display = ("color_preview", "name", "ordering", "colorHex", "secondary_hex_color")
|
|
list_editable = ("ordering",)
|
|
ordering = ["ordering"]
|
|
search_fields = ["name"]
|
|
list_per_page = 20
|
|
|
|
def color_preview(self, obj):
|
|
if obj.secondary_hex_color:
|
|
return format_html(
|
|
'<div style="background-image: linear-gradient(to right, {} 50%, {} 50%); '
|
|
'width: 60px; height: 30px; border-radius: 15px; border: 1px solid #ddd;"></div>',
|
|
obj.colorHex, obj.secondary_hex_color
|
|
)
|
|
return format_html(
|
|
'<div style="background-color: {}; width: 60px; height: 30px; '
|
|
'border-radius: 15px; border: 1px solid #ddd;"></div>',
|
|
obj.colorHex
|
|
)
|
|
|
|
@admin.register(Size)
|
|
class SizeAdmin(admin.ModelAdmin):
|
|
list_display = ("name",)
|
|
|
|
class OrderItemInline(admin.TabularInline):
|
|
model = OrderItem
|
|
extra = 0
|
|
readonly_fields = ('product', 'quantity', 'color', 'size', 'price')
|
|
|
|
@admin.register(Order)
|
|
class OrderAdmin(admin.ModelAdmin):
|
|
list_display = ('id', 'date_ordered', 'status', 'total_price')
|
|
inlines = [OrderItemInline]
|
|
|
|
class GuestUserOrderInline(admin.TabularInline):
|
|
model = Order
|
|
extra = 0
|
|
readonly_fields = ('date_ordered', 'total_price')
|
|
can_delete = False
|
|
show_change_link = True
|
|
exclude = ('user',) # Exclude the user field from the inline display
|
|
|
|
@admin.register(GuestUser)
|
|
class GuestUserAdmin(admin.ModelAdmin):
|
|
list_display = ('email', 'phone')
|
|
inlines = [GuestUserOrderInline]
|
|
|
|
@admin.register(Coupon)
|
|
class CouponAdmin(admin.ModelAdmin):
|
|
list_display = ('code', 'discount_amount', 'discount_percent', 'is_active',
|
|
'valid_from', 'valid_to', 'current_uses', 'max_uses')
|
|
list_filter = ('is_active', 'valid_from', 'valid_to')
|
|
search_fields = ('code', 'description')
|
|
readonly_fields = ('current_uses', 'created_at', 'stripe_coupon_id')
|
|
fieldsets = (
|
|
('Basic Information', {
|
|
'fields': ('code', 'description', 'is_active')
|
|
}),
|
|
('Discount', {
|
|
'fields': ('discount_amount', 'discount_percent')
|
|
}),
|
|
('Validity', {
|
|
'fields': ('valid_from', 'valid_to', 'max_uses', 'current_uses')
|
|
}),
|
|
('Stripe Information', {
|
|
'fields': ('stripe_coupon_id',),
|
|
'classes': ('collapse',)
|
|
}),
|
|
)
|
|
|
|
@admin.register(CouponUsage)
|
|
class CouponUsageAdmin(admin.ModelAdmin):
|
|
list_display = ('coupon', 'user', 'guest_email', 'order', 'used_at')
|
|
list_filter = ('used_at',)
|
|
search_fields = ('coupon__code', 'user__username', 'user__email', 'guest_email')
|
|
readonly_fields = ('used_at',)
|
|
|