Commit 460fa25d authored by MOREAU Ulysse's avatar MOREAU Ulysse
Browse files

Add download button

parent 26008a7a
......@@ -40,6 +40,8 @@
</button>
<button class="delivery_list tooltip see-orders"><i class="fas fa-eye"></i> <span class="tooltiptext">Voir les commandes</span>
</button>
<button class="delivery_list tooltip dl-orders" onclick="window.location.href = '{% url 'boutique:csvexport' pk=item.id %}'"><i class="fas fa-download"></i> <span class="tooltiptext">Voir les commandes</span>
</button>
</div>
</article>
......
......@@ -13,6 +13,7 @@ urlpatterns = [
path("create", views.ItemCreateView.as_view(), name="item_create"),
path("<int:pk>", views.ItemDetailView.as_view(), name="item_detail"),
path("manage", views.ItemManageView.as_view(), name="item_manage"),
path("csvexport/<int:pk>", views.CsvExportView.as_view(), name="csvexport"),
path("sell", views.ItemSellView.as_view(), name="item_sell"),
path("edit/<int:pk>", views.ItemEditView.as_view(), name="item_edit"),
]
......
import json
import csv
from typing import Any
from typing import Dict
from typing import Type
......@@ -159,6 +160,33 @@ class ItemEditView(PermissionRequiredMixin, UpdateView):
return response
class CsvExportView(PermissionRequiredMixin, ListView, SingleObjectMixin):
model = Item
permission_required = "boutique.management"
object: Item
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
self.object = self.get_object()
item_qs = self.get_queryset().filter(id=self.object.id)
item = item_qs.prefetch_related(
"order_items__order__payment", "order_items__order__user__profile"
).get()
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = f'attachment; filename="{self.object.name}.csv"'
writer = csv.writer(response)
writer.writerow(['Username', 'Quantity', 'Paid', 'Customization', 'Option', 'Second Option', 'Quantity delivered'])
for oi in item.order_items.all():
writer.writerow([str(oi.order.user.profile),
oi.quantity,
oi.order.paid,
oi.customization,
oi.option,
oi.second_option,
oi.quantity_delivered])
return response
class ItemManageView(PermissionRequiredMixin, ListView):
template_name = "boutique/item_manage.html"
permission_required = "boutique.management"
......@@ -198,7 +226,6 @@ class ItemManageView(PermissionRequiredMixin, ListView):
],
}
)
else:
raise SuspiciousOperation()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment