land-of-apps / django-oscar

Domain-driven e-commerce for Django
http://oscarcommerce.com
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Offer order export from offer detail view only includes page 1 in export #15

Open kgilpin opened 5 hours ago

kgilpin commented 5 hours ago

If you go to the offer detail view in the dashboard, and this offer has been used in orders, you can generate an export.

See: django-oscar/src/oscar/apps/dashboard/offers/views.py

Lines 407 to 412 in 1d1590f

 def render_to_response(self, context): 
     if self.request.GET.get('format') == 'csv': 
         formatter = OrderDiscountCSVFormatter() 
         return formatter.generate_response(context['order_discounts'], 
                                            offer=self.offer) 
     return super().render_to_response(context) 

However, if you got multiple pages of orders, only the first page will be in the export. It makes more sense when you generate an export, it will include all the orders.

Found in version

1d1590f2

github-actions[bot] commented 5 hours ago

Title:

Fix export to include all orders for an offer in OfferDetailView

Problem:

The export functionality in OfferDetailView only includes the first page of orders when generating the export. This is not the expected behavior. When a user exports data, they expect all relevant orders to be included in the export.

Analysis:

The root cause of this issue is that the render_to_response method in OfferDetailView uses context['order_discounts'], which contains only the orders from the current page. When the export format is CSV, only this subset of orders is being exported.

To resolve this, we need to modify the export functionality to fetch and include all orders associated with the offer, rather than just the orders from the current page.

Proposed Changes:

  1. src/oscar/apps/dashboard/offers/views.py:

    • Update the render_to_response method to fetch all related orders before creating the export, bypassing the pagination.
  2. src/oscar/apps/dashboard/offers/views.py:

    • Create a utility method (or use an existing one) within the OfferDetailView to get all orders linked to the specific offer.

Detailed Changes:

  1. src/oscar/apps/dashboard/offers/views.py:

    • Modify the render_to_response method:
      <!-- file: /home/runner/work/django-oscar/django-oscar/src/oscar/apps/dashboard/offers/views.py -->
      ```python
      def render_to_response(self, context):
       if self.request.GET.get('format') == 'csv':
           formatter = OrderDiscountCSVFormatter() 
           all_order_discounts = self.get_all_order_discounts()
           return formatter.generate_response(all_order_discounts,
                                              offer=self.offer) 
       return super().render_to_response(context)
  2. src/oscar/apps/dashboard/offers/views.py:

    • Add a new method to get all the orders related to the offer:
      <!-- file: /home/runner/work/django-oscar/django-oscar/src/oscar/apps/dashboard/offers/views.py -->
      ```python
      def get_all_order_discounts(self):
       return OrderDiscount.objects.filter(offer=self.offer)

This change ensures that when the export is triggered, it will include all orders related to the offer, thus solving the issue.

kgilpin commented 5 hours ago

Original report: https://github.com/django-oscar/django-oscar/issues/4104