Make review and pay button save changes made to order. Also move shop related templates back into shop app.

This commit is contained in:
Víðir Valberg Guðmundsson 2019-07-09 10:07:00 +02:00
parent 9adc2a878f
commit fef09baa3d
6 changed files with 42 additions and 13 deletions

View file

@ -71,8 +71,8 @@
{% if not order.open == None %} {% if not order.open == None %}
<h4>Comment:</h4> <h4>Comment:</h4>
<textarea class="form-control" name="customer_comment" style="width: 100%;" placeholder="Please enter any comments or shipping address here...">{{ order.customer_comment }}</textarea></p> <textarea class="form-control" name="customer_comment" style="width: 100%;" placeholder="Please enter any comments or shipping address here...">{{ order.customer_comment }}</textarea></p>
{% elif order.open == None and order.comment %} {% elif order.open == None and order.customer_comment %}
<div class="alert alert-info">{{ order.comment|linebreaks }}</div> <div class="alert alert-info">{{ order.customer_comment|linebreaks }}</div>
{% endif %} {% endif %}
{% if not order.open == None %} {% if not order.open == None %}
@ -90,9 +90,7 @@
{% bootstrap_button "Cancel order" button_type="submit" button_class="btn-danger" name="cancel_order" %} {% bootstrap_button "Cancel order" button_type="submit" button_class="btn-danger" name="cancel_order" %}
{% endif %} {% endif %}
{% if not order.paid %} {% if not order.paid %}
<a href="{% url "shop:order_review_and_pay" pk=order.pk %}" class="btn btn-success btn-lg pull-right"> {% bootstrap_button "Review and pay" button_type="submit" button_class="btn btn-success btn-lg pull-right" name="review_and_pay" %}
Review and pay
</a>
{% endif %} {% endif %}
</div> </div>

View file

@ -63,9 +63,9 @@
</table> </table>
{% if order.comment %} {% if order.customer_comment %}
<h4>Comment:</h4> <h4>Comment:</h4>
<div class="alert alert-info">{{ order.comment|linebreaks }}</div> <div class="alert alert-info">{{ order.customer_comment|linebreaks }}</div>
{% endif %} {% endif %}
{% if order.invoice_address %} {% if order.invoice_address %}

View file

@ -313,6 +313,32 @@ class TestOrderDetailView(TestCase):
self.assertEquals(response.status_code, 200) self.assertEquals(response.status_code, 200)
self.assertIn("quantity", response.context["order_product_formset"].errors[0]) self.assertIn("quantity", response.context["order_product_formset"].errors[0])
def test_review_and_pay_saves_and_redirects(self):
self.client.force_login(self.user)
opr = OrderProductRelationFactory(order=self.order)
data = self.base_form_data
data["review_and_pay"] = ""
data["form-0-id"] = opr.pk
data["form-0-quantity"] = 5
data["customer_comment"] = "A comment"
data["invoice_address"] = "An invoice address"
response = self.client.post(self.path, data=data)
self.assertRedirects(
response, reverse("shop:order_review_and_pay", kwargs={"pk": self.order.pk})
)
# Get the updated objects
opr.refresh_from_db()
self.order.refresh_from_db()
# Check them
self.assertEqual(opr.quantity, 5)
self.assertEqual(self.order.invoice_address, "An invoice address")
self.assertEqual(self.order.customer_comment, "A comment")
class TestOrderReviewAndPay(TestCase): class TestOrderReviewAndPay(TestCase):
def setUp(self): def setUp(self):

View file

@ -271,7 +271,7 @@ class ProductDetailView(FormView, DetailView):
class OrderListView(LoginRequiredMixin, ListView): class OrderListView(LoginRequiredMixin, ListView):
model = Order model = Order
template_name = "shop/order_list.html" template_name = "order_list.html"
context_object_name = "orders" context_object_name = "orders"
def get_queryset(self): def get_queryset(self):
@ -287,7 +287,7 @@ class OrderDetailView(
DetailView, DetailView,
): ):
model = Order model = Order
template_name = "shop/order_detail.html" template_name = "order_detail.html"
context_object_name = "order" context_object_name = "order"
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
@ -336,8 +336,8 @@ class OrderDetailView(
self.get_context_data(order_product_formset=formset) self.get_context_data(order_product_formset=formset)
) )
# No stock issues, proceed to check if the user is updating the order. # No stock issues, proceed to check if the user is updating or proceeding to review and pay the order.
if "update_order" in request.POST: if "update_order" or "review_and_pay" in request.POST:
# We have already made sure the formset is valid, so just save it to update quantities. # We have already made sure the formset is valid, so just save it to update quantities.
formset.save() formset.save()
@ -345,6 +345,11 @@ class OrderDetailView(
order.invoice_address = request.POST.get("invoice_address") or "" order.invoice_address = request.POST.get("invoice_address") or ""
order.save() order.save()
if "review_and_pay" in request.POST:
return HttpResponseRedirect(
reverse("shop:order_review_and_pay", kwargs={"pk": order.pk})
)
return super(OrderDetailView, self).get(request, *args, **kwargs) return super(OrderDetailView, self).get(request, *args, **kwargs)
@ -355,7 +360,7 @@ class OrderReviewAndPayView(
EnsureOrderIsNotCancelledMixin, EnsureOrderIsNotCancelledMixin,
DetailView, DetailView,
): ):
template_name = "shop/order_review.html" template_name = "order_review.html"
context_object_name = "order" context_object_name = "order"
def post(self, request, *args, **kwargs): def post(self, request, *args, **kwargs):
@ -415,7 +420,7 @@ class DownloadInvoiceView(
class CreditNoteListView(LoginRequiredMixin, ListView): class CreditNoteListView(LoginRequiredMixin, ListView):
model = CreditNote model = CreditNote
template_name = "shop/creditnote_list.html" template_name = "creditnote_list.html"
context_object_name = "creditnotes" context_object_name = "creditnotes"
def get_queryset(self): def get_queryset(self):