Spaces:
Runtime error
Runtime error
File size: 4,670 Bytes
c68b343 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | from django.contrib import admin
from .models import Product, Category,CustomOrder, CustomOrderImage
from django.urls import path
from django.utils.html import format_html
from .admin_views import send_custom_order_email
from django.urls import reverse
import os
from django.core.mail import send_mail
from django.conf import settings
STATUS_EMAIL_MESSAGES = {
"reviewed": "Your custom order has been reviewed by our team.",
"quoted": "Your custom order has been quoted. We will share pricing shortly.",
"approved": "Your custom order has been approved and will move to production.",
"completed": "Your custom order has been completed. Thank you for choosing us!",
"rejected": "Unfortunately, we are unable to proceed with your custom order.",
}
@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
list_display = ("name", "category", "price", "stock", "featured")
list_filter = ("category", "featured")
search_fields = ("name",)
@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
prepopulated_fields = {"slug": ("name",)}
class CustomOrderImageInline(admin.TabularInline):
model = CustomOrderImage
extra = 0
readonly_fields = ("image",)
@admin.register(CustomOrder)
class CustomOrderAdmin(admin.ModelAdmin):
list_display = (
"id",
"name",
"email",
"email_verified",
"product_type",
"status",
"created_at",
"send_email_button",
)
list_filter = ("email_verified", "status", "product_type", "created_at")
search_fields = ("name", "email", "phone")
readonly_fields = (
"name",
"email",
"phone",
"product_type",
"quantity",
"dimensions",
"preferred_colors",
"timeline",
"budget_range",
"description",
"created_at",
"updated_at",
"email_verified",
"email_verification_token",
)
fieldsets = (
("Customer Info", {
"fields": ("name", "email", "phone")
}),
("Order Details", {
"fields": (
"product_type",
"quantity",
"dimensions",
"preferred_colors",
"timeline",
"budget_range",
"description",
)
}),
("Management (Admin Only)", {
"fields": ("status", "admin_notes")
}),
("Timestamps", {
"fields": ("created_at", "updated_at")
}),
)
inlines = [CustomOrderImageInline]
def send_email_button(self, obj):
if not obj.email_verified:
return format_html(
'<span style="color:red;font-weight:600;">{}</span>',"Email not verified"
)
return format_html(
'<a class="button style="text-decoration:none;" href="{}/send-email/">{}</a>', obj.id,"Send Email"
)
def get_urls(self):
urls = super().get_urls()
custom_urls = [
path(
"<int:order_id>/send-email/",
self.admin_site.admin_view(send_custom_order_email),
name="send-custom-order-email",
),
]
return custom_urls + urls
def save_model(self, request, obj, form, change):
if change:
old_obj = CustomOrder.objects.get(pk=obj.pk)
if old_obj.status != obj.status:
message = STATUS_EMAIL_MESSAGES.get(obj.status)
if message:
send_mail(
subject=f"Update on your custom order #{obj.id}",
message=message,
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[obj.email],
fail_silently=False,
)
super().save_model(request, obj, form, change)
def delete_reference_images(self, request, queryset):
deleted_count = 0
for order in queryset:
for image in order.images.all():
image.delete() # Cloudinary handles deletion
deleted_count += 1
self.message_user(
request,
f"{deleted_count} reference image(s) deleted successfully."
)
actions = ["delete_reference_images"]
delete_reference_images.short_description = "Delete reference images for selected custom orders"
send_email_button.short_description = "Email Customer"
@admin.register(CustomOrderImage)
class CustomOrderImageAdmin(admin.ModelAdmin):
list_display = ("order", "image") |