85 lines
2.7 KiB
Python
85 lines
2.7 KiB
Python
from django import forms
|
|
from django.urls import reverse_lazy
|
|
from django.views.generic import DetailView, ListView, UpdateView
|
|
|
|
from backoffice.mixins import BackofficeCRUDMixin, BackofficeHtmxMixin
|
|
from shop.models import Order
|
|
|
|
SECTION = 'orders'
|
|
|
|
|
|
class ShippingStatusForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Order
|
|
fields = ('shipping_status',)
|
|
widgets = {'shipping_status': forms.Select(attrs={'class': 'select select-bordered w-full'})}
|
|
|
|
|
|
class OrderListView(BackofficeCRUDMixin, BackofficeHtmxMixin, ListView):
|
|
model = Order
|
|
permission_required = 'shop.view_order'
|
|
section = SECTION
|
|
paginate_by = 20
|
|
template_name = 'backoffice/generic/list.html'
|
|
fragment_template_name = 'backoffice/generic/_list_fragment.html'
|
|
|
|
def get_queryset(self):
|
|
queryset = super().get_queryset()
|
|
status = self.request.GET.get('status')
|
|
|
|
if status:
|
|
queryset = queryset.filter(status=status)
|
|
|
|
return queryset
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context.update(
|
|
{
|
|
'title': 'Pedidos',
|
|
'columns': [('Código', 'code'), ('Cliente', 'email'), ('Estado', 'get_status_display'), ('Total', 'total')],
|
|
'detail_url_name': 'backoffice:order_detail',
|
|
}
|
|
)
|
|
return context
|
|
|
|
|
|
class OrderDetailView(BackofficeCRUDMixin, DetailView):
|
|
model = Order
|
|
permission_required = 'shop.view_order'
|
|
section = SECTION
|
|
template_name = 'backoffice/orders/order_detail.html'
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context.update(
|
|
{
|
|
'lines': self.object.lines.all(),
|
|
'payments': self.object.payments.all(),
|
|
'shipping_status_form': ShippingStatusForm(instance=self.object),
|
|
}
|
|
)
|
|
return context
|
|
|
|
|
|
class OrderUpdateShippingStatusView(BackofficeCRUDMixin, UpdateView):
|
|
model = Order
|
|
form_class = ShippingStatusForm
|
|
permission_required = 'shop.change_order'
|
|
section = SECTION
|
|
|
|
def get_success_url(self):
|
|
return reverse_lazy('backoffice:order_detail', args=[self.object.pk])
|
|
|
|
def form_invalid(self, form):
|
|
return self.render_to_response(self.get_context_data(shipping_status_form=form))
|
|
|
|
def get_template_names(self):
|
|
return ['backoffice/orders/order_detail.html']
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context.update({'lines': self.object.lines.all(), 'payments': self.object.payments.all()})
|
|
context.setdefault('shipping_status_form', ShippingStatusForm(instance=self.object))
|
|
return context
|