Repository: saleor/saleor

Issue:
404 not supported for "Packing slips" and "Invoices" views
When accessing "Packing slips" or "Invoices" views with IDs of non-existing objects:
```
ERROR django.request Internal Server Error: /dashboard/orders/123/packing-slips/ 
saleor.order.models.DoesNotExist: DeliveryGroup matching query does not exist.
```
Looks like missing `get_object_or_404`.


Discussion:

Resolving patch:
diff --git a/saleor/dashboard/order/utils.py b/saleor/dashboard/order/utils.py
--- a/saleor/dashboard/order/utils.py
+++ b/saleor/dashboard/order/utils.py
@@ -2,8 +2,6 @@
 from django.contrib.sites.shortcuts import get_current_site
 from django.template.loader import get_template
 
-from ...order.models import DeliveryGroup, Order
-
 INVOICE_TEMPLATE = 'dashboard/order/pdf/invoice.html'
 PACKING_SLIP_TEMPLATE = 'dashboard/order/pdf/packing_slip.html'
 
@@ -25,10 +23,7 @@ def _create_pdf(rendered_template, absolute_url):
     return pdf_file
 
 
-def create_invoice_pdf(order_pk, absolute_url):
-    order = (Order.objects.prefetch_related(
-        'user', 'shipping_address', 'billing_address', 'voucher',
-        'groups').get(pk=order_pk))
+def create_invoice_pdf(order, absolute_url):
     shipping_methods = [
         {'name': d.shipping_method_name} for d in order.groups.all()]
     ctx = {'order': order, 'shipping_methods': shipping_methods}
@@ -37,10 +32,7 @@ def create_invoice_pdf(order_pk, absolute_url):
     return pdf_file, order
 
 
-def create_packing_slip_pdf(group_pk, absolute_url):
-    group = (DeliveryGroup.objects.prefetch_related(
-        'lines', 'order', 'order__user', 'order__shipping_address',
-        'order__billing_address').get(pk=group_pk))
+def create_packing_slip_pdf(group, absolute_url):
     ctx = {'group': group}
     rendered_template = get_template(PACKING_SLIP_TEMPLATE).render(ctx)
     pdf_file = _create_pdf(rendered_template, absolute_url)
diff --git a/saleor/dashboard/order/views.py b/saleor/dashboard/order/views.py
--- a/saleor/dashboard/order/views.py
+++ b/saleor/dashboard/order/views.py
@@ -24,7 +24,7 @@
 from ..views import staff_member_required
 from ...core.utils import get_paginator_items
 from ...order import GroupStatus
-from ...order.models import Order, OrderLine, OrderNote
+from ...order.models import DeliveryGroup, Order, OrderLine, OrderNote
 
 
 @staff_member_required
@@ -406,8 +406,11 @@ def remove_order_voucher(request, order_pk):
 @staff_member_required
 @permission_required('order.edit_order')
 def order_invoice(request, order_pk):
+    orders = Order.objects.prefetch_related(
+        'user', 'shipping_address', 'billing_address', 'voucher', 'groups')
+    order = get_object_or_404(orders, pk=order_pk)
     absolute_url = get_statics_absolute_url(request)
-    pdf_file, order = create_invoice_pdf(order_pk, absolute_url)
+    pdf_file, order = create_invoice_pdf(order, absolute_url)
     response = HttpResponse(pdf_file, content_type='application/pdf')
     name = "invoice-%s" % order.id
     response['Content-Disposition'] = 'filename=%s' % name
@@ -417,8 +420,12 @@ def order_invoice(request, order_pk):
 @staff_member_required
 @permission_required('order.edit_order')
 def order_packing_slip(request, group_pk):
+    groups = DeliveryGroup.objects.prefetch_related(
+        'lines', 'order', 'order__user', 'order__shipping_address',
+        'order__billing_address')
+    group = get_object_or_404(groups, pk=group_pk)
     absolute_url = get_statics_absolute_url(request)
-    pdf_file, group = create_packing_slip_pdf(group_pk, absolute_url)
+    pdf_file, group = create_packing_slip_pdf(group, absolute_url)
     response = HttpResponse(pdf_file, content_type='application/pdf')
     name = "packing-slip-%s-%s" % (group.order.id, group.id)
     response['Content-Disposition'] = 'filename=%s' % name


Test patch: