|
|
|
|
@ -6,7 +6,7 @@ from django.http import HttpResponseRedirect |
|
|
|
|
from django import forms |
|
|
|
|
|
|
|
|
|
from .models import ( |
|
|
|
|
Product, Color, Size, Order, OrderItem, GuestUser, Coupon, CouponUsage, |
|
|
|
|
Product, Color, Size, Order, OrderItem, GuestUser, Coupon, CouponUsage, |
|
|
|
|
OrderStatus, ShippingAddress |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
@ -60,6 +60,7 @@ class OrderAdmin(admin.ModelAdmin): |
|
|
|
|
list_filter = ('status', 'payment_status') |
|
|
|
|
readonly_fields = ('shipping_address_details',) |
|
|
|
|
actions = ['change_order_status'] |
|
|
|
|
autocomplete_fields = ['user'] # Add this line for user search functionality |
|
|
|
|
|
|
|
|
|
def get_email(self, obj): |
|
|
|
|
if obj.guest_user: |
|
|
|
|
@ -208,29 +209,29 @@ class OrderAdmin(admin.ModelAdmin): |
|
|
|
|
except Exception as e: |
|
|
|
|
self.message_user(request, f"Error cancelling order: {str(e)}", level='ERROR') |
|
|
|
|
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, |
|
|
|
|
@ -238,7 +239,7 @@ class OrderAdmin(admin.ModelAdmin): |
|
|
|
|
'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): |
|
|
|
|
@ -282,4 +283,4 @@ 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',) |
|
|
|
|
readonly_fields = ('used_at',) |
|
|
|
|
|