Fixes for when there is no order + adding a new product to the order

This commit is contained in:
Víðir Valberg Guðmundsson 2016-05-16 00:20:09 +02:00
parent 5ed1d3075b
commit 68fce085e7

View file

@ -11,6 +11,7 @@ from django.views.generic import (
FormView,
)
from camps.models import Camp
from shop.models import (
Order,
Product,
@ -114,7 +115,7 @@ class ProductDetailView(LoginRequiredMixin, FormView, DetailView):
def form_valid(self, form):
product = self.get_object()
quantity = form.cleaned_data.get('quantity'),
quantity = form.cleaned_data.get('quantity')
# do we have an open order?
try:
@ -124,7 +125,10 @@ class ProductDetailView(LoginRequiredMixin, FormView, DetailView):
)
except Order.DoesNotExist:
# no open order - open a new one
order = Order.objects.create(user=self.request.user)
order = Order.objects.create(
user=self.request.user,
camp=Camp.objects.current()
)
# get product from kwargs
if product in order.products.all():
@ -135,7 +139,10 @@ class ProductDetailView(LoginRequiredMixin, FormView, DetailView):
order=order
).update(quantity=F('quantity') + quantity)
else:
order.products.add(product)
order.orderproductrelation_set.create(
product=product,
quantity=quantity,
)
messages.info(
self.request,