add action in admin shop order panel

sync_v2
Raz 6 months ago
parent 97a7543f9e
commit 3b3cf56896
  1. 52
      shop/admin.py
  2. 2
      shop/signals.py
  3. 32
      shop/templates/admin/shop/order/change_status.html

@ -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',)

@ -238,7 +238,7 @@ Montant payé: {final_price}€"""
} }
# Order status update email # Order status update email
elif status in [OrderStatus.SHIPPED, OrderStatus.DELIVERED, OrderStatus.CANCELED, OrderStatus.PREPARED, OrderStatus.REFUNDED]: elif status in [OrderStatus.SHIPPED, OrderStatus.DELIVERED, OrderStatus.CANCELED, OrderStatus.PREPARED, OrderStatus.REFUNDED, OrderStatus.READY]:
status_message = { status_message = {
OrderStatus.PREPARED: "Votre commande est en cours de préparation.", OrderStatus.PREPARED: "Votre commande est en cours de préparation.",
OrderStatus.READY: "Votre commande est prête.", OrderStatus.READY: "Votre commande est prête.",

@ -0,0 +1,32 @@
{% extends "admin/base_site.html" %}
{% load i18n l10n admin_urls %}
{% block content %}
<div class="module">
<h2>{{ title }}</h2>
<div>
<p>Change status for the following {{ orders|length }} orders:</p>
<ul>
{% for order in orders %}
<li>{{ order }}</li>
{% endfor %}
</ul>
</div>
<form action="{% url 'admin:shop_order_changelist' %}" method="post">
{% csrf_token %}
<input type="hidden" name="action" value="{{ action }}" />
{{ form.as_p }}
{% for obj in orders %}
<input type="hidden" name="_selected_action" value="{{ obj.pk }}" />
{% endfor %}
<div class="actions">
<input type="submit" name="apply" value="Change Status" class="default" />
<a href="{% url 'admin:shop_order_changelist' %}" class="button cancel-link">Cancel</a>
</div>
</form>
</div>
{% endblock %}
Loading…
Cancel
Save