|
|
|
@ -1,10 +1,14 @@ |
|
|
|
from django.contrib import admin |
|
|
|
from django.contrib import admin |
|
|
|
from django.shortcuts import render |
|
|
|
from django.shortcuts import render, redirect |
|
|
|
from .models import Product, Color, Size, Order, OrderItem, GuestUser, Coupon, CouponUsage, OrderStatus, ShippingAddress |
|
|
|
|
|
|
|
from django.utils.html import format_html |
|
|
|
from django.utils.html import format_html |
|
|
|
from django.urls import path |
|
|
|
from django.urls import path |
|
|
|
from django.contrib import admin |
|
|
|
from django.http import HttpResponseRedirect |
|
|
|
from django.shortcuts import redirect |
|
|
|
from django import forms |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from .models import ( |
|
|
|
|
|
|
|
Product, Color, Size, Order, OrderItem, GuestUser, Coupon, CouponUsage, |
|
|
|
|
|
|
|
OrderStatus, ShippingAddress |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Product) |
|
|
|
@admin.register(Product) |
|
|
|
class ProductAdmin(admin.ModelAdmin): |
|
|
|
class ProductAdmin(admin.ModelAdmin): |
|
|
|
@ -45,18 +49,24 @@ class ShippingAddressAdmin(admin.ModelAdmin): |
|
|
|
list_display = ('street_address', 'city', 'postal_code', 'country') |
|
|
|
list_display = ('street_address', 'city', 'postal_code', 'country') |
|
|
|
search_fields = ('street_address', 'city', 'postal_code', 'country') |
|
|
|
search_fields = ('street_address', 'city', 'postal_code', 'country') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ChangeOrderStatusForm(forms.Form): |
|
|
|
|
|
|
|
_selected_action = forms.CharField(widget=forms.MultipleHiddenInput) |
|
|
|
|
|
|
|
status = forms.ChoiceField(choices=OrderStatus.choices, label="New Status") |
|
|
|
|
|
|
|
|
|
|
|
@admin.register(Order) |
|
|
|
@admin.register(Order) |
|
|
|
class OrderAdmin(admin.ModelAdmin): |
|
|
|
class OrderAdmin(admin.ModelAdmin): |
|
|
|
list_display = ('id', 'get_email', 'date_ordered', 'status', 'total_price', 'get_shipping_address') |
|
|
|
list_display = ('id', 'get_email', 'date_ordered', 'status', 'total_price', 'get_shipping_address') |
|
|
|
inlines = [OrderItemInline] |
|
|
|
inlines = [OrderItemInline] |
|
|
|
list_filter = ('status', 'payment_status') |
|
|
|
list_filter = ('status', 'payment_status') |
|
|
|
readonly_fields = ('shipping_address_details',) |
|
|
|
readonly_fields = ('shipping_address_details',) |
|
|
|
|
|
|
|
actions = ['change_order_status'] |
|
|
|
|
|
|
|
|
|
|
|
def get_email(self, obj): |
|
|
|
def get_email(self, obj): |
|
|
|
if obj.guest_user: |
|
|
|
if obj.guest_user: |
|
|
|
return obj.guest_user.email |
|
|
|
return obj.guest_user.email |
|
|
|
else: |
|
|
|
else: |
|
|
|
return obj.user.email |
|
|
|
return obj.user.email |
|
|
|
|
|
|
|
get_email.short_description = 'Email' |
|
|
|
|
|
|
|
|
|
|
|
def get_shipping_address(self, obj): |
|
|
|
def get_shipping_address(self, obj): |
|
|
|
if obj.shipping_address: |
|
|
|
if obj.shipping_address: |
|
|
|
@ -198,6 +208,38 @@ class OrderAdmin(admin.ModelAdmin): |
|
|
|
except Exception as e: |
|
|
|
except Exception as e: |
|
|
|
self.message_user(request, f"Error cancelling order: {str(e)}", level='ERROR') |
|
|
|
self.message_user(request, f"Error cancelling order: {str(e)}", level='ERROR') |
|
|
|
return redirect('admin:shop_order_changelist') |
|
|
|
return redirect('admin:shop_order_changelist') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def change_order_status(self, request, queryset): |
|
|
|
|
|
|
|
"""Admin action to change the status of selected orders""" |
|
|
|
|
|
|
|
form = None |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if 'apply' in request.POST: |
|
|
|
|
|
|
|
form = ChangeOrderStatusForm(request.POST) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if form.is_valid(): |
|
|
|
|
|
|
|
status = form.cleaned_data['status'] |
|
|
|
|
|
|
|
count = 0 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for order in queryset: |
|
|
|
|
|
|
|
order.status = status |
|
|
|
|
|
|
|
order.save() |
|
|
|
|
|
|
|
count += 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.message_user(request, f"{count} orders have been updated to status '{OrderStatus(status).label}'.") |
|
|
|
|
|
|
|
return HttpResponseRedirect(request.get_full_path()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not form: |
|
|
|
|
|
|
|
form = ChangeOrderStatusForm(initial={'_selected_action': request.POST.getlist('_selected_action')}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context = { |
|
|
|
|
|
|
|
'title': 'Change Order Status', |
|
|
|
|
|
|
|
'orders': queryset, |
|
|
|
|
|
|
|
'form': form, |
|
|
|
|
|
|
|
'action': 'change_order_status' |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return render(request, 'admin/shop/order/change_status.html', context) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
change_order_status.short_description = "Change status for selected orders" |
|
|
|
|
|
|
|
|
|
|
|
class GuestUserOrderInline(admin.TabularInline): |
|
|
|
class GuestUserOrderInline(admin.TabularInline): |
|
|
|
model = Order |
|
|
|
model = Order |
|
|
|
@ -240,4 +282,4 @@ class CouponUsageAdmin(admin.ModelAdmin): |
|
|
|
list_display = ('coupon', 'user', 'guest_email', 'order', 'used_at') |
|
|
|
list_display = ('coupon', 'user', 'guest_email', 'order', 'used_at') |
|
|
|
list_filter = ('used_at',) |
|
|
|
list_filter = ('used_at',) |
|
|
|
search_fields = ('coupon__code', 'user__username', 'user__email', 'guest_email') |
|
|
|
search_fields = ('coupon__code', 'user__username', 'user__email', 'guest_email') |
|
|
|
readonly_fields = ('used_at',) |
|
|
|
readonly_fields = ('used_at',) |