add orders RESTfull

This commit is contained in:
Ivan Hörler 2018-02-27 23:56:41 +01:00
parent f170f5b0da
commit a18765b0a6
4 changed files with 98 additions and 39 deletions

View File

@ -27,13 +27,15 @@
<th scope="col">PRICE</th>
</tr>
{% for order, order_positions_count, total, currency_name in order_list_zip %}
<tr class="table_content">
<td scope="col">{{ order.id }}</td>
<td scope="col">{{ order.date }}</td>
<td scope="col">{{ order.status }}</td>
<td scope="col">{{ order_positions_count }}</td>
<td scope="col">{{ total }} {{ currency_name }}</td>
</tr>
<tr class="table_content">
<td scope="col">{{ order.id }}</td>
<td scope="col">{{ order.date }}</td>
<td scope="col">
<a href="{% url 'order' order.id %}">{{ order.status }}</a>
</td>
<td scope="col">{{ order_positions_count }}</td>
<td scope="col">{{ total }} {{ currency_name }}</td>
</tr>
{% endfor %}
</table>
{% else %}

View File

@ -4,4 +4,57 @@
{% block content %}
<h1> Your order was submitted. </h1>
<h3> Thank you for Purchase. </h3>
<h3>List of Items in your Shopping Cart:</h3>
{% if order_position_list %}
<table class="table price-table">
<tr class="table_header">
<th scope="col">POS.</th>
<th scope="col">ART#</th>
<th scope="col">NAME</th>
<th scope="col">STOCK</th>
<th scope="col">AMOUNT</th>
<th scope="col" class="price-label">PRICE p.pce.</th>
<th scope="col" class="price-label">POSITION PRICE</th>
</tr>
{% for order_position in order_position_list %}
<tr class="table_content">
<td scope="col">{{ order_position.id }}</td>
<td scope="col">{{ order_position.article.id }}</td>
<td scope="col">
<a href="{% url 'details' order_position.article.id %}">
{{ order_position.article.name }}
</a>
</td>
<td scope="col">{{ order_position.article.stock }}</td>
<td scope="col">{{ order_position.amount }}</td>
<td scope="col" class="price-value">
{{ order_position.article.price_in_chf }}
{{ currency_name }}
</td>
<td scope="col" class="price-value">
{{ order_position.position_price }} {{ currency_name }}
</td>
</tr>
{% endfor %}
<tr>
<td scope="col" colspan="5"class="text-right">
<td scope="col" class="price-value">
<dl><dt>Total:</dl></dt></td>
<td scope="col" class="price-value">
<dl><dt>{{ total }} {{ currency_name }}</dl></dt>
</td>
</tr>
</table>
{% else %}
<p class="alert alert-danger">
<strong>
This order is empty. Howcome?
<strong>
</p>
{% endif %}
<p class="alert text-danger">
<strong>
{{ message }}
<strong>
</p>
{% endblock %}

View File

@ -23,7 +23,7 @@ urlpatterns = [
url(r'^cart/checkout/$',
views.checkout,
name='checkout'),
url(r'^order/$',
url(r'^order/(?P<order_id>[0-9]+)/$',
views.order,
name='order'),
]

View File

@ -4,6 +4,7 @@ from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
from django.db import transaction
from decimal import Decimal
from webshop.models import (Article,
Category,
Person,
@ -172,6 +173,7 @@ def profile(request):
if orders:
orders_list = list(orders)
for idx1, order in enumerate(orders_list):
print(order.id)
# get all items in the Order:
order_positions = OrderPosition.objects.filter(order=order)
if (order_positions.count()) > 0:
@ -192,8 +194,8 @@ def profile(request):
order_position_list[idx2] = order_position
total = sum(totalprice_list)
currency_list.append(currency_name)
total_list.append(total)
order_positions_count = order_positions.count()
total_list.append(total)
order_positions_count = order_positions.count()
order_positions_count_list.append(order_positions_count)
orders_list[idx1] = order
order_list_zip = zip(orders_list,
@ -417,8 +419,6 @@ def checkout(request):
else:
order = Order.objects.create(user=request.user,
status=orderstatus)
print('order', order, 'created:', order)
for position in cart_positions:
OrderPosition.objects.create(
article=position.article,
@ -426,7 +426,7 @@ def checkout(request):
amount=position.amount,
price_in_chf=position.article.price_in_chf
)
return HttpResponseRedirect('/order/')
return HttpResponseRedirect('/order/%s/' % order.id)
return render(request, 'webshop/checkout.html',
{'cart_position_list': cart_position_list,
@ -436,11 +436,13 @@ def checkout(request):
'article_view': article_view,
'category_list': category_list,
'message': message,
'person': person
'person': person,
})
def order(request):
def order(request, order_id):
totalprice_list = []
order_position_list = []
cart = ShoppingCart.objects.get(user=request.user)
if cart:
# get all items in the cart of this customer:
@ -456,27 +458,29 @@ def order(request):
else:
message = """something whent wrong.
We cold not empty your cart. """
# category_list = get_categories()
# person = Person.objects.get(user=request.user)
# orders = Order.objects.filter(user=request.user)
# for order in orders:
# currency = order.exchange_rate
# # get all items in the Order:
# order_positions = OrderPosition.objects.filter(order=order)
# if (order_positions.count()) > 0:
# order_position_list = list(order_positions)
# for idx, order_position in enumerate(order_position_list):
# # get currencyname to display:
# currency_name = ExchangeRate_name.objects.get(id=currency)
# # get exchange_rate multiplyed:
# cart_position.article.price_in_chf = ExchangeRate.exchange(
# currency,
# order_position.article.price_in_chf
# )
# order_position.calculate_position_price()
# totalprice_list.append(order_position.position_price)
# order_position_list[idx] = order_position
#
# total = sum(totalprice_list)
return render(request, 'webshop/order.html', {})
order = Order.objects.get(id=order_id)
order_positions = OrderPosition.objects.filter(order=order_id)
if (order_positions.count()) > 0:
order_position_list = list(order_positions)
for idx, order_position in enumerate(order_positions):
# get currencyname to display:
if order.exchange_rate is not None:
print('order.exchange_rate', order.exchange_rate, order.exchange_rate.id)
# get price of position in order and append to a list:
rate = ExchangeRate.objects.get(id=order.exchange_rate.id)
order_position.price = round(
rate.exchange_rate_to_chf * order_position.price_in_chf,
2)
currency_name = order.exchange_rate
else:
currency_name = 'CHF'
order_position.price = order_position.price_in_chf
order_position.position_price = order_position.price * Decimal.from_float(order_position.amount)
order_position_list[idx] = order_position
totalprice_list.append(order_position.price)
total = sum(totalprice_list)
return render(request, 'webshop/order.html', {
'order_position_list': order_position_list,
'currency_name': currency_name,
'total': total
})