commit for andreas

This commit is contained in:
Ivan Hörler 2018-02-04 18:32:54 +01:00
parent c1a5627852
commit 1fe10e2185
6 changed files with 73 additions and 10 deletions

View File

@ -100,9 +100,8 @@ class OrderPosition(models.Model):
class ShoppingCart(models.Model):
""" Cart to User Relationships """
name = models.CharField(max_length=200)
name = models.CharField(max_length=200, null=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
article = models.ManyToManyField(Article)
def __str__(self):
return self.name

View File

@ -6,11 +6,15 @@
<p><b>Stock:</b> {{ article.stock }}</p>
<p><b>Status:</b> {{ article.status }}</p>
<p><b>Price:</b> {{ article.price_in_chf }} {{ currency_name }}</p>
<form id="amount" action="" method="POST" novalidate>
{{ amount.as_p }}
<input type="submit" value="Add to Cart" />
{% csrf_token %}
</form>
{% if user.is_authenticated %}
<form id="amount" action="" method="POST" novalidate>
{{ amount.as_p }}
<input type="submit" value="Add to Cart" />
{% csrf_token %}
</form>
{% else %}
<p> please login to fill your basket...</p>
{% endif %}
{% for picture in picture_list %}
<p><img src="{{ MEDIA_URL }}{{ picture.image }}" width="200" /></p>
{% endfor %}

View File

@ -0,0 +1,9 @@
{% extends "webshop/base.html" %}
{% block section_title %}<h1>Cart</h1>{% endblock %}
{% block content %}
<h3>List of Items in your Shopping Cart:</h3>
<p>{{ article.description }}</p>
<p><b>in Stock:</b> {{ article.stock }}</p>
<p><b>Amount you whant:</b> {{ article.amount }}</p>
<p><b>Price:</b> {{ article.price_in_chf }} {{ currency_name }}</p>
{% endblock %}

View File

@ -6,7 +6,7 @@
HOME
</a>
<ul class="nav navbar-nav">
<li><a href="#">CART</a></li>
<li><a href="{% url 'cart' %}">CART</a></li>
{% if user.is_authenticated %}
<li>
<a href="{% url 'profile' %}">PROFILE</a>

View File

@ -17,4 +17,7 @@ urlpatterns = [
url(r'^registration/$',
views.registration,
name='registration'),
url(r'^cart/$',
views.cart,
name='cart'),
]

View File

@ -4,7 +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 webshop.models import (Article, Category, ArticleStatus, Person,
City, Picture)
City, Picture, CartPosition, ShoppingCart)
from webshop.forms import RegistrationForm, AddToCartForm
from currencies.models import ExchangeRate, ExchangeRate_name
@ -144,7 +144,16 @@ def article_details(request, article_id):
if amount.is_valid():
print("is valid")
amount = amount.cleaned_data['amount']
print("amount:", amount, "article_id:", article_id)
currency_id = request.session['currency']
print("amount:", amount,
"article_id:", article_id,
"currency_id:", currency_id)
cart_position = CartPosition.objects.create(
article=article_id,
amount=amount,
cart=ShoppingCart.objects.get(user=request.user)
)
cart_position.save()
amount = AddToCartForm()
else:
amount = AddToCartForm()
@ -203,3 +212,42 @@ def registration(request):
{'profile_form': profile_form,
'category_list': category_list,
'user_form': user_form})
def cart(request):
currencies_form = CurrenciesForm
rate = ExchangeRate
article_view = True
currency_name = "CHF"
if not 'currency' in request.session:
request.session['currency'] = None
currency = request.session['currency']
if request.method == 'POST':
currencies_form = CurrenciesForm(request.POST)
if currencies_form.is_valid():
cf = currencies_form.cleaned_data
if cf['currencies']:
selection = cf['currencies']
request.session['currency'] = selection.id
currency_name = ExchangeRate_name.objects.get(id=selection.id)
else:
request.session['currency'] = None
cart_id = ShoppingCart.objects.get(user=request.user)
articles = CartPosition.objects.filter(cart=cart_id)
articles_list = list(articles)
for idx, article in enumerate(articles_list):
article.price_in_chf = rate.exchange(
currency, article.price_in_chf)
articles_list[idx] = article
currency_name = ExchangeRate_name.objects.get(id=currency)
article.price_in_chf = rate.exchange(currency, article.price_in_chf)
return render(request, 'webshop/cart.html',
{'article': article,
'currencies_form': currencies_form,
'article_view': article_view,
'currency_name': currency_name,
})