Merge branch 'cart' into andreas

This commit is contained in:
Andreas Zweili 2018-02-04 20:54:15 +01:00
commit 711e73a8b2
9 changed files with 195 additions and 48 deletions

View File

@ -8,7 +8,7 @@ class RegistrationForm(forms.Form):
first_name = forms.CharField()
last_name = forms.CharField()
street_name = forms.CharField()
street_number = forms.CharField()
street_number = forms.CharField(max_length=4)
zip_code = forms.IntegerField(min_value=1000, max_value=9999)
city = forms.CharField()
@ -51,3 +51,10 @@ class PictureForm(forms.ModelForm):
class Meta:
model = Picture
fields = ['name', 'article', 'image']
class AddToCartForm(forms.Form):
amount = forms.IntegerField(
label='Amount in piece.',
help_text="Enter a Value between 1 and 99.",
initial=1)

View File

@ -3,6 +3,7 @@ from django.core.validators import MinValueValidator
from django.db import models
from django.contrib.auth.models import User
from django.utils import timezone
from currencies.models import ExchangeRate
class Option(models.Model):
@ -22,7 +23,6 @@ class ArticleStatus(models.Model):
return self.name
# Create your models here.
class Category(models.Model):
name = models.CharField(max_length=200, unique=True)
parent_category = models.ForeignKey('self', null=True, blank=True)
@ -50,6 +50,7 @@ class Article(models.Model):
class OrderStatus(models.Model):
""" Warehouse Items have Status like ordered or out of Stock """
name = models.CharField(max_length=200, unique=True)
def __str__(self):
@ -57,6 +58,7 @@ class OrderStatus(models.Model):
class OrderOfGoods(models.Model):
""" Warehouse operations """
article = models.ForeignKey(Article)
amount = models.FloatField(max_length=5)
delivery_date = models.DateField()
@ -68,6 +70,7 @@ class OrderOfGoods(models.Model):
class Picture(models.Model):
""" Pictures in relationship to Articles """
name = models.CharField(max_length=200)
article = models.ForeignKey(Article)
image = models.ImageField(upload_to="images")
@ -77,13 +80,15 @@ class Picture(models.Model):
class Order(models.Model):
""" Submitted Orders """
user = models.ForeignKey(User, on_delete=models.CASCADE)
article = models.ManyToManyField(Article, through='OrderPosition')
status = models.ForeignKey(OrderStatus)
date = models.DateTimeField(default=timezone.now)
exchange_rate = models.ForeignKey(ExchangeRate)
class OrderPosition(models.Model):
""" Items in Submitted Orders"""
article = models.ForeignKey(Article, on_delete=models.CASCADE)
order = models.ForeignKey(Order, on_delete=models.CASCADE)
amount = models.FloatField(max_length=5)
@ -94,14 +99,21 @@ class OrderPosition(models.Model):
class ShoppingCart(models.Model):
name = models.CharField(max_length=200)
""" Cart to User Relationships """
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
class CartPosition(models.Model):
""" Items in Cart """
article = models.ForeignKey(Article, on_delete=models.CASCADE)
amount = models.FloatField(max_length=5)
cart = models.ForeignKey(ShoppingCart, on_delete=models.CASCADE)
class City(models.Model):
name = models.CharField(max_length=200)
zip_code = models.PositiveSmallIntegerField()

View File

@ -6,6 +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>
{% 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,32 @@
{% extends "webshop/base.html" %}
{% block section_title %}<h1>Cart</h1>{% endblock %}
{% block content %}
<h3>List of Items in your Shopping Cart:</h3>
{% if articles_list %}
<table class="table">
<tr class="table_header">
<th scope="col">ID</th>
<th scope="col">NAME</th>
<th scope="col">STOCK</th>
<th scope="col">AMOUNT</th>
<th scope="col">PRICE</th>
</tr>
{% for article in articles_list %}
<tr class="table_content">
<td scope="col">{{ article.id }}</td>
<td scope="col">
<a href="{% url 'details' article.id %}">
{{ article.article.name }}
</a></td>
<td scope="col">{{ article.article.stock }}</td>
<td scope="col">{{ article.amount }}</td>
<td scope="col">{{ article.article.price_in_chf }} {{ currency_name }}</td>
</tr>
{% endfor %}
</table>
{% else %}
<p class="alert">
There are currently no articles in your cart.
</p>
{% endif %}
{% endblock %}

View File

@ -6,26 +6,18 @@
HOME
</a>
<ul class="nav navbar-nav">
<li><a href="#">CART</a></li>
{% if user.is_authenticated %}
<li>
<a href="{% url 'profile' %}">PROFILE</a>
</li>
<li>
<a href="{% url 'logout' %}">LOGOUT</a>
</li>
<li><a href="{% url 'cart' %}">CART</a></li>
<li><a href="{% url 'profile' %}">PROFILE</a></li>
<li><a href="{% url 'logout' %}">LOGOUT</a></li>
{% else %}
<li>
<a href="{% url 'login' %}">LOGIN</a>
</li>
<li><a href="{% url 'login' %}">LOGIN</a></li>
{% endif %}
<li>
<li class="dropdown">
{% if article_view %}
<form id="currency" name="currency" action="" method="POST" novalidate>
<form id="currency" action="" method="POST" novalidate>
{{ currencies_form.as_ul }}
<li>
<input type="submit" value="Select">
</li>
<li><input type="submit" value="Select"></li>
{% csrf_token %}
</form>
{% endif %}

View File

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

View File

@ -4,8 +4,8 @@ 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)
from webshop.forms import RegistrationForm
City, Picture, CartPosition, ShoppingCart)
from webshop.forms import RegistrationForm, AddToCartForm
from currencies.models import ExchangeRate, ExchangeRate_name
from currencies.forms import CurrenciesForm
@ -33,7 +33,7 @@ def index(request):
articles = Article.objects.all().exclude(status=get_hidden_status_id())
articles_list = list(articles)
currencies_form = CurrenciesForm
rate=ExchangeRate
rate = ExchangeRate
article_view = True
currency_name = "CHF"
@ -47,24 +47,26 @@ def index(request):
if cf['currencies']:
selection = cf['currencies']
request.session['currency'] = selection.id
currency_name=ExchangeRate_name.objects.get(id=selection.id)
currency_name = ExchangeRate_name.objects.get(id=selection.id)
else:
request.session['currency'] = None
if request.session['currency']:
currency = request.session['currency']
for idx, article in enumerate(articles_list):
article.price_in_chf = rate.exchange(currency, article.price_in_chf)
article.price_in_chf = rate.exchange(
currency, article.price_in_chf
)
articles_list[idx] = article
currency_name=ExchangeRate_name.objects.get(id=currency)
currency_name = ExchangeRate_name.objects.get(id=currency)
return render(request,
'webshop/index.html',
{'category_list': category_list,
'articles_list': articles_list,
'currencies_form': currencies_form,
'article_view': article_view,
'currency_name': currency_name})
'webshop/index.html',
{'category_list': category_list,
'articles_list': articles_list,
'currencies_form': currencies_form,
'article_view': article_view,
'currency_name': currency_name})
def articles_in_category(request, category_id):
@ -74,7 +76,7 @@ def articles_in_category(request, category_id):
category=selected_category.id).exclude(status=get_hidden_status_id())
articles_list = list(articles)
currencies_form = CurrenciesForm
rate=ExchangeRate
rate = ExchangeRate
article_view = True
currency_name = "CHF"
@ -88,16 +90,17 @@ def articles_in_category(request, category_id):
if cf['currencies']:
selection = cf['currencies']
request.session['currency'] = selection.id
currency_name=ExchangeRate_name.objects.get(id=selection.id)
currency_name = ExchangeRate_name.objects.get(id=selection.id)
else:
request.session['currency'] = None
if request.session['currency']:
currency = request.session['currency']
for idx, article in enumerate(articles_list):
article.price_in_chf = rate.exchange(currency, article.price_in_chf)
article.price_in_chf = rate.exchange(
currency, article.price_in_chf)
articles_list[idx] = article
currency_name=ExchangeRate_name.objects.get(id=currency)
currency_name = ExchangeRate_name.objects.get(id=currency)
return render(request, 'webshop/category.html',
{'articles_list': articles_list,
@ -111,7 +114,8 @@ def articles_in_category(request, category_id):
def article_details(request, article_id):
category_list = get_categories()
currencies_form = CurrenciesForm
rate=ExchangeRate
amount = AddToCartForm
rate = ExchangeRate
article_view = True
currency_name = "CHF"
@ -122,20 +126,53 @@ def article_details(request, article_id):
picture_list = Picture.objects.filter(article=article_id)
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
print(request.POST)
# hier wird das Currency dropdown bearbeitet:
if 'currencies' in request.POST:
currencies_form = CurrenciesForm(request.POST)
print("currencies_form")
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
# hier wird der Artikel in den Wahrenkorb transferiert:
if 'amount' in request.POST:
amount = AddToCartForm(request.POST)
print("add_to_cart_form")
if amount.is_valid():
print("is valid")
amount = amount.cleaned_data['amount']
currency_id = request.session['currency']
print("amount:", amount,
"article_id:", article_id,
"currency_id:", currency_id)
article = Article.objects.get(id=article_id)
try:
cart_id = ShoppingCart.objects.get(user=request.user)
except:
cart_id = ShoppingCart.objects.create(user=request.user)
cart_id.save()
if cart_id:
cart_position = CartPosition.objects.create(
article=article,
amount=amount,
cart=ShoppingCart.objects.get(user=request.user)
)
cart_position.save()
amount = AddToCartForm()
else:
amount = AddToCartForm()
if request.session['currency']:
currency = request.session['currency']
article.price_in_chf = rate.exchange(currency, article.price_in_chf)
currency_name=ExchangeRate_name.objects.get(id=currency)
currency_name = ExchangeRate_name.objects.get(id=currency)
return render(request, 'webshop/article_details.html',
{'article': article,
@ -143,7 +180,10 @@ def article_details(request, article_id):
'currencies_form': currencies_form,
'article_view': article_view,
'currency_name': currency_name,
'picture_list': picture_list})
'picture_list': picture_list,
'amount': amount
})
@login_required
def profile(request):
@ -183,3 +223,55 @@ def registration(request):
{'profile_form': profile_form,
'category_list': category_list,
'user_form': user_form})
def cart(request):
category_list = get_categories()
currencies_form = CurrenciesForm
rate = ExchangeRate
article_view = True
currency_name = "CHF"
message = ""
cart_id = False
articles_list = ""
if not 'currency' in request.session:
request.session['currency'] = None
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
try:
cart_id = ShoppingCart.objects.get(user=request.user)
except Exception as e:
message = "You have no items in the Basket"
if cart_id and request.session['currency']:
articles = CartPosition.objects.filter(cart=cart_id)
articles_list = list(articles)
currency = request.session['currency']
for idx, article in enumerate(articles_list):
article.price_in_chf = rate.exchange(
currency, article.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)
else:
articles = CartPosition.objects.filter(cart=cart_id)
articles_list = list(articles)
return render(request, 'webshop/cart.html',
{'articles_list': articles_list,
'currencies_form': currencies_form,
'article_view': article_view,
'currency_name': currency_name,
'category_list': category_list,
'message': message,
})

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 121 KiB