Editing of an order (remove/update products)

This commit is contained in:
Víðir Valberg Guðmundsson 2016-05-16 15:25:12 +02:00
parent 2287e6dda5
commit 373a4cf65b
6 changed files with 89 additions and 32 deletions

View file

@ -3,5 +3,5 @@ from .models import Order
class AddToOrderForm(forms.Form): class AddToOrderForm(forms.Form):
quantity = forms.IntegerField() quantity = forms.IntegerField(initial=1)

View file

@ -0,0 +1,29 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-05-16 09:54
from __future__ import unicode_literals
import datetime
from django.db import migrations, models
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
('shop', '0007_auto_20160515_2157'),
]
operations = [
migrations.AddField(
model_name='orderproductrelation',
name='created',
field=models.DateTimeField(auto_now_add=True, default=datetime.datetime(2016, 5, 16, 9, 53, 59, 584701, tzinfo=utc)),
preserve_default=False,
),
migrations.AddField(
model_name='orderproductrelation',
name='updated',
field=models.DateTimeField(auto_now=True, default=datetime.datetime(2016, 5, 16, 9, 54, 3, 23885, tzinfo=utc)),
preserve_default=False,
),
]

View file

@ -131,7 +131,7 @@ class Product(CreatedUpdatedModel, UUIDModel):
return now in self.available_in return now in self.available_in
class OrderProductRelation(models.Model): class OrderProductRelation(CreatedUpdatedModel):
order = models.ForeignKey('shop.Order') order = models.ForeignKey('shop.Order')
product = models.ForeignKey('shop.Product') product = models.ForeignKey('shop.Product')
quantity = models.PositiveIntegerField() quantity = models.PositiveIntegerField()

View file

@ -5,6 +5,11 @@
<h1>Order #{{ order.id }}</h1> <h1>Order #{{ order.id }}</h1>
{% if not order.open == None %}
<form method="POST" class="form-inline">
{% csrf_token %}
{% endif %}
<table class="table table-bordered table-hover"> <table class="table table-bordered table-hover">
<thead> <thead>
@ -24,7 +29,18 @@
<td> <td>
{{ order_product.product.name }} {{ order_product.product.name }}
<td> <td>
{% if not order.open == None %}
<input type="number"
class="form-control"
style="width: 75px;"
min=1
name="{{ order_product.id }}"
value="{{ order_product.quantity }}" />
{% bootstrap_button '<i class="glyphicon glyphicon-remove"></i>' button_type="submit" button_class="btn-danger" name="remove_product" value=order_product.pk %}
</form>
{% else %}
{{ order_product.quantity }} {{ order_product.quantity }}
{% endif %}
<td> <td>
{{ order_product.product.price }} {{ order_product.product.price }}
<td> <td>
@ -36,12 +52,17 @@
</table> </table>
{% if order.open %} {% if not order.open == None %}
{% bootstrap_button "Update order" button_type="submit" button_class="btn-primary" name="update_order" %}
<hr />
<h3>Checkout</h3>
<form method="POST"> <form method="POST">
{% csrf_token %} {% csrf_token %}
{% bootstrap_button "Credit card" button_type="submit" button_class="btn-primary" name="payment_method" value="credit_card" %} {% bootstrap_button "<i class='glyphicon glyphicon-credit-card'></i> Credit card" button_type="submit" button_class="btn-primary" name="payment_method" value="credit_card" %}
{% bootstrap_button "Blockchain" button_type="submit" button_class="btn-primary" name="payment_method" value="blockchain" %} {% bootstrap_button "<i class='glyphicon glyphicon-bitcoin'></i> Blockchain" button_type="submit" button_class="btn-primary" name="payment_method" value="blockchain" %}
{% bootstrap_button "Bank transfer" button_type="submit" button_class="btn-primary" name="payment_method" value="bank_transfer" %} {% bootstrap_button "<i class='glyphicon glyphicon-piggy-bank'></i> Bank transfer" button_type="submit" button_class="btn-primary" name="payment_method" value="bank_transfer" %}
</form> </form>
{% endif %} {% endif %}

View file

@ -3,7 +3,7 @@ from views import *
urlpatterns = [ urlpatterns = [
url(r'orders/(?P<pk>[0-9]+)/pay/creditcard/$', EpayFormView.as_view(), name='epay_form'), url(r'orders/(?P<pk>[0-9]+)/pay/creditcard/$', EpayFormView.as_view(), name='epay_form'),
url(r'epay_callback/', EpayCallbackViev.as_view(), name='epay_callback'), url(r'epay_callback/', EpayCallbackView.as_view(), name='epay_callback'),
url(r'^$', ShopIndexView.as_view(), name='index'), url(r'^$', ShopIndexView.as_view(), name='index'),
url(r'products/(?P<slug>[-_\w+]+)/$', ProductDetailView.as_view(), name='product_detail'), url(r'products/(?P<slug>[-_\w+]+)/$', ProductDetailView.as_view(), name='product_detail'),
url(r'orders/$', OrderListView.as_view(), name='order_list'), url(r'orders/$', OrderListView.as_view(), name='order_list'),

View file

@ -5,6 +5,7 @@ from django.core.urlresolvers import reverse_lazy
from django.db.models import Count, F from django.db.models import Count, F
from django.http import HttpResponseRedirect, Http404 from django.http import HttpResponseRedirect, Http404
from django.views.generic import ( from django.views.generic import (
View,
TemplateView, TemplateView,
ListView, ListView,
DetailView, DetailView,
@ -78,17 +79,9 @@ class OrderDetailView(LoginRequiredMixin, DetailView):
if payment_method in order.PAYMENT_METHODS: if payment_method in order.PAYMENT_METHODS:
order.payment_method = payment_method order.payment_method = payment_method
else:
# unknown submit button
messages.error(self.request, 'Unknown submit button :(')
return reverse_lazy(
'shop:checkout',
kwargs={'orderid': self.get_object.id}
)
# Mark the order as closed # Mark the order as closed
order.open = None order.open = None
order.save()
reverses = { reverses = {
Order.CREDIT_CARD: reverse_lazy( Order.CREDIT_CARD: reverse_lazy(
@ -107,6 +100,20 @@ class OrderDetailView(LoginRequiredMixin, DetailView):
return HttpResponseRedirect(reverses[payment_method]) return HttpResponseRedirect(reverses[payment_method])
if 'update_order' in request.POST:
for order_product in order.orderproductrelation_set.all():
order_product_id = str(order_product.pk)
if order_product_id in request.POST:
new_quantity = int(request.POST.get(order_product_id))
order_product.quantity = new_quantity
order_product.save()
product_remove = request.POST.get('remove_product')
if product_remove:
order.orderproductrelation_set.filter(pk=product_remove).delete()
return super(OrderDetailView, self).get(request, *args, **kwargs)
class ProductDetailView(LoginRequiredMixin, FormView, DetailView): class ProductDetailView(LoginRequiredMixin, FormView, DetailView):
model = Product model = Product