commit for andreas
This commit is contained in:
parent
c1a5627852
commit
1fe10e2185
|
@ -100,9 +100,8 @@ class OrderPosition(models.Model):
|
||||||
|
|
||||||
class ShoppingCart(models.Model):
|
class ShoppingCart(models.Model):
|
||||||
""" Cart to User Relationships """
|
""" 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)
|
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||||
article = models.ManyToManyField(Article)
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
|
@ -6,11 +6,15 @@
|
||||||
<p><b>Stock:</b> {{ article.stock }}</p>
|
<p><b>Stock:</b> {{ article.stock }}</p>
|
||||||
<p><b>Status:</b> {{ article.status }}</p>
|
<p><b>Status:</b> {{ article.status }}</p>
|
||||||
<p><b>Price:</b> {{ article.price_in_chf }} {{ currency_name }}</p>
|
<p><b>Price:</b> {{ article.price_in_chf }} {{ currency_name }}</p>
|
||||||
<form id="amount" action="" method="POST" novalidate>
|
{% if user.is_authenticated %}
|
||||||
{{ amount.as_p }}
|
<form id="amount" action="" method="POST" novalidate>
|
||||||
<input type="submit" value="Add to Cart" />
|
{{ amount.as_p }}
|
||||||
{% csrf_token %}
|
<input type="submit" value="Add to Cart" />
|
||||||
</form>
|
{% csrf_token %}
|
||||||
|
</form>
|
||||||
|
{% else %}
|
||||||
|
<p> please login to fill your basket...</p>
|
||||||
|
{% endif %}
|
||||||
{% for picture in picture_list %}
|
{% for picture in picture_list %}
|
||||||
<p><img src="{{ MEDIA_URL }}{{ picture.image }}" width="200" /></p>
|
<p><img src="{{ MEDIA_URL }}{{ picture.image }}" width="200" /></p>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
@ -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 %}
|
|
@ -6,7 +6,7 @@
|
||||||
HOME
|
HOME
|
||||||
</a>
|
</a>
|
||||||
<ul class="nav navbar-nav">
|
<ul class="nav navbar-nav">
|
||||||
<li><a href="#">CART</a></li>
|
<li><a href="{% url 'cart' %}">CART</a></li>
|
||||||
{% if user.is_authenticated %}
|
{% if user.is_authenticated %}
|
||||||
<li>
|
<li>
|
||||||
<a href="{% url 'profile' %}">PROFILE</a>
|
<a href="{% url 'profile' %}">PROFILE</a>
|
||||||
|
|
|
@ -17,4 +17,7 @@ urlpatterns = [
|
||||||
url(r'^registration/$',
|
url(r'^registration/$',
|
||||||
views.registration,
|
views.registration,
|
||||||
name='registration'),
|
name='registration'),
|
||||||
|
url(r'^cart/$',
|
||||||
|
views.cart,
|
||||||
|
name='cart'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -4,7 +4,7 @@ from django.contrib.auth.decorators import login_required
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.contrib.auth.forms import UserCreationForm
|
from django.contrib.auth.forms import UserCreationForm
|
||||||
from webshop.models import (Article, Category, ArticleStatus, Person,
|
from webshop.models import (Article, Category, ArticleStatus, Person,
|
||||||
City, Picture)
|
City, Picture, CartPosition, ShoppingCart)
|
||||||
from webshop.forms import RegistrationForm, AddToCartForm
|
from webshop.forms import RegistrationForm, AddToCartForm
|
||||||
|
|
||||||
from currencies.models import ExchangeRate, ExchangeRate_name
|
from currencies.models import ExchangeRate, ExchangeRate_name
|
||||||
|
@ -144,7 +144,16 @@ def article_details(request, article_id):
|
||||||
if amount.is_valid():
|
if amount.is_valid():
|
||||||
print("is valid")
|
print("is valid")
|
||||||
amount = amount.cleaned_data['amount']
|
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()
|
amount = AddToCartForm()
|
||||||
else:
|
else:
|
||||||
amount = AddToCartForm()
|
amount = AddToCartForm()
|
||||||
|
@ -203,3 +212,42 @@ def registration(request):
|
||||||
{'profile_form': profile_form,
|
{'profile_form': profile_form,
|
||||||
'category_list': category_list,
|
'category_list': category_list,
|
||||||
'user_form': user_form})
|
'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,
|
||||||
|
})
|
||||||
|
|
Loading…
Reference in New Issue