diff --git a/django/didgeridoo/webshop/forms.py b/django/didgeridoo/webshop/forms.py
index 5e431a9..680f7a8 100644
--- a/django/didgeridoo/webshop/forms.py
+++ b/django/didgeridoo/webshop/forms.py
@@ -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)
diff --git a/django/didgeridoo/webshop/models.py b/django/didgeridoo/webshop/models.py
index a17e7dc..a17cb65 100644
--- a/django/didgeridoo/webshop/models.py
+++ b/django/didgeridoo/webshop/models.py
@@ -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()
diff --git a/django/didgeridoo/webshop/templates/webshop/article_details.html b/django/didgeridoo/webshop/templates/webshop/article_details.html
index 8703063..a16a642 100644
--- a/django/didgeridoo/webshop/templates/webshop/article_details.html
+++ b/django/didgeridoo/webshop/templates/webshop/article_details.html
@@ -6,6 +6,15 @@
Stock: {{ article.stock }}
Status: {{ article.status }}
Price: {{ article.price_in_chf }} {{ currency_name }}
+ {% if user.is_authenticated %}
+
+ {% else %}
+ please login to fill your basket...
+ {% endif %}
{% for picture in picture_list %}
{% endfor %}
diff --git a/django/didgeridoo/webshop/templates/webshop/cart.html b/django/didgeridoo/webshop/templates/webshop/cart.html
new file mode 100644
index 0000000..637d524
--- /dev/null
+++ b/django/didgeridoo/webshop/templates/webshop/cart.html
@@ -0,0 +1,32 @@
+{% extends "webshop/base.html" %}
+{% block section_title %}Cart
{% endblock %}
+{% block content %}
+ List of Items in your Shopping Cart:
+ {% if articles_list %}
+
+
+ {% for article in articles_list %}
+
+ {{ article.id }} |
+
+
+ {{ article.article.name }}
+ |
+ {{ article.article.stock }} |
+ {{ article.amount }} |
+ {{ article.article.price_in_chf }} {{ currency_name }} |
+
+ {% endfor %}
+
+ {% else %}
+
+ There are currently no articles in your cart.
+
+ {% endif %}
+{% endblock %}
diff --git a/django/didgeridoo/webshop/templates/webshop/nav.html b/django/didgeridoo/webshop/templates/webshop/nav.html
index 9b01f06..f65151f 100644
--- a/django/didgeridoo/webshop/templates/webshop/nav.html
+++ b/django/didgeridoo/webshop/templates/webshop/nav.html
@@ -6,26 +6,18 @@
HOME
- - CART
{% if user.is_authenticated %}
- -
- PROFILE
-
- -
- LOGOUT
-
+ - CART
+ - PROFILE
+ - LOGOUT
{% else %}
- -
- LOGIN
-
+ - LOGIN
{% endif %}
- -
+
-
{% if article_view %}
-
{% endif %}
diff --git a/django/didgeridoo/webshop/urls.py b/django/didgeridoo/webshop/urls.py
index 5a4685e..e59619a 100644
--- a/django/didgeridoo/webshop/urls.py
+++ b/django/didgeridoo/webshop/urls.py
@@ -17,4 +17,7 @@ urlpatterns = [
url(r'^registration/$',
views.registration,
name='registration'),
+ url(r'^cart/$',
+ views.cart,
+ name='cart'),
]
diff --git a/django/didgeridoo/webshop/views.py b/django/didgeridoo/webshop/views.py
index 84ab9e4..32fab77 100644
--- a/django/didgeridoo/webshop/views.py
+++ b/django/didgeridoo/webshop/views.py
@@ -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,
+ })
diff --git a/docs/diagrammes/180102_erd.dia b/docs/diagrammes/180102_erd.dia
new file mode 100644
index 0000000..7372b52
Binary files /dev/null and b/docs/diagrammes/180102_erd.dia differ
diff --git a/docs/diagrammes/180102_erd.png b/docs/diagrammes/180102_erd.png
new file mode 100644
index 0000000..b8193aa
Binary files /dev/null and b/docs/diagrammes/180102_erd.png differ