Merge branch 'master' into production
This commit is contained in:
commit
097e46988a
|
@ -0,0 +1,8 @@
|
|||
from django import forms
|
||||
from currencies.models import ExchangeRate_name
|
||||
|
||||
|
||||
class CurrenciesForm(forms.Form):
|
||||
currencies = forms.ModelChoiceField(
|
||||
queryset=ExchangeRate_name.objects.all(),
|
||||
required=False)
|
|
@ -23,5 +23,9 @@ class ExchangeRate(models.Model):
|
|||
exchange_rate_to_chf = models.DecimalField(max_digits=12,
|
||||
decimal_places=5)
|
||||
|
||||
def exchange(_currency_id, _base_currency):
|
||||
rate = ExchangeRate.objects.filter(name=_currency_id).latest('date')
|
||||
return round(rate.exchange_rate_to_chf * _base_currency,2)
|
||||
|
||||
def __str__(self):
|
||||
return str(self.name)
|
||||
|
|
|
@ -3,10 +3,10 @@
|
|||
{% block section_title %}Login{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<form method="post">
|
||||
<form id="login" name="login" method="post">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<button type="submit">Login</button>
|
||||
<button name="button_login" type="submit">Login</button>
|
||||
<p><a href="{% url 'registration' %}">Go to registration.</a></p>
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
|
|
@ -8,12 +8,12 @@
|
|||
Please correct the error{{ form.errors|pluralize }} below.
|
||||
</p>
|
||||
{% endif %}
|
||||
<form action="" method="post" novalidate>
|
||||
<form id="register" name="register" action="" method="post" novalidate>
|
||||
<table>
|
||||
{{ user_form.as_table }}
|
||||
{{ profile_form.as_table }}
|
||||
</table>
|
||||
{% csrf_token %}
|
||||
<button type="submit">Register</button>
|
||||
<button name="button_register" type="submit">Register</button>
|
||||
</form>
|
||||
{% endblock %}
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
<h3>Description</h3>
|
||||
<p>{{ article.description }}</p>
|
||||
<p><b>Stock:</b> {{ article.stock }}</p>
|
||||
<p><b>Status:</b> {{ article.status}}</p>
|
||||
<p><b>Price:</b> {{ article.price_in_chf }}</p>
|
||||
<p><b>Status:</b> {{ article.status }}</p>
|
||||
<p><b>Price:</b> {{ article.price_in_chf }} {{ currency_name }}</p>
|
||||
{% for picture in picture_list %}
|
||||
<p><img src="{{ MEDIA_URL }}{{ picture.image }}" width="200" /></p>
|
||||
{% endfor %}
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
|
||||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
|
||||
|
||||
<!-- Add additional CSS in static file -->
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
{% extends "webshop/base.html" %}
|
||||
{% block section_title %}Category Overview{% endblock %}
|
||||
{% block content %}
|
||||
{% if article_list %}
|
||||
{% if articles_list %}
|
||||
<table class="table">
|
||||
<tr class="table_header">
|
||||
<th scope="col">ID</th>
|
||||
|
@ -10,7 +10,7 @@
|
|||
<th scope="col">STOCK</th>
|
||||
<th scope="col">PRICE</th>
|
||||
</tr>
|
||||
{% for article in article_list %}
|
||||
{% for article in articles_list %}
|
||||
<tr class="table_content">
|
||||
<td scope="col">{{ article.id }}</td>
|
||||
<td scope="col">
|
||||
|
@ -19,7 +19,7 @@
|
|||
</a></td>
|
||||
<td scope="col">{{ article.category }}</td>
|
||||
<td scope="col">{{ article.stock }}</td>
|
||||
<td scope="col">{{ article.price_in_chf }}</td>
|
||||
<td scope="col">{{ article.price_in_chf }} {{ currency_name }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
</td>
|
||||
<td scope="col">{{ article.category }}</td>
|
||||
<td scope="col">{{ article.stock }}</td>
|
||||
<td scope="col">{{ article.price_in_chf }}</td>
|
||||
<td scope="col">{{ article.price_in_chf }} {{ currency_name }}</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
|
|
|
@ -19,24 +19,19 @@
|
|||
<a href="{% url 'login' %}">LOGIN</a>
|
||||
</li>
|
||||
{% endif %}
|
||||
<!-- https://pypi.python.org/pypi/django-select-multiple-field -->
|
||||
<form method="POST" novalidate>
|
||||
<select name="currency_update" id="id_currency_update">
|
||||
<option value="CHF">CHF</option>
|
||||
<option value="USD">USD</option>
|
||||
<option value="GBP">GBP</option>
|
||||
<option value="JPY">JPY</option>
|
||||
</select>
|
||||
<li>
|
||||
{% if article_view %}
|
||||
<form id="currency" name="currency" action="" method="POST" novalidate>
|
||||
{{ currencies_form.as_ul }}
|
||||
<li>
|
||||
<input type="submit" value="Select">
|
||||
</li>
|
||||
{% csrf_token %}
|
||||
</form>
|
||||
</form>
|
||||
{% endif %}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<!--
|
||||
Es wird auf id im app.js file gemached.
|
||||
die URL wird im app.js gesetzt
|
||||
und mit urls.py weitergereicht.
|
||||
dann auf name im views.py gemached und ausgeführt.
|
||||
-->
|
||||
</div>
|
||||
</nav>
|
||||
{% endblock %}
|
||||
|
|
|
@ -3,77 +3,159 @@ from django.shortcuts import get_object_or_404, render
|
|||
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.models import (Article, Category, ArticleStatus, Person,
|
||||
City, Picture)
|
||||
from webshop.forms import RegistrationForm
|
||||
|
||||
from currencies.models import ExchangeRate, ExchangeRate_name
|
||||
from currencies.forms import CurrenciesForm
|
||||
|
||||
|
||||
# Create your views here.
|
||||
|
||||
|
||||
def index(request):
|
||||
def get_categories():
|
||||
parent_category_list = Category.objects.filter(parent_category=None)
|
||||
category_list = {}
|
||||
hidden = ArticleStatus.objects.get(name="hidden")
|
||||
articles_list = Article.objects.all().exclude(status=hidden.id)
|
||||
|
||||
for i in parent_category_list:
|
||||
category_list.update(
|
||||
{i: Category.objects.filter(parent_category=i.id)})
|
||||
return category_list
|
||||
|
||||
|
||||
def get_hidden_status_id():
|
||||
hidden_status = ArticleStatus.objects.get(name="hidden")
|
||||
return hidden_status.id
|
||||
|
||||
|
||||
def index(request):
|
||||
category_list = get_categories()
|
||||
articles = Article.objects.all().exclude(status=get_hidden_status_id())
|
||||
articles_list = list(articles)
|
||||
currencies_form = CurrenciesForm
|
||||
rate=ExchangeRate
|
||||
article_view = True
|
||||
currency_name = "CHF"
|
||||
|
||||
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
|
||||
|
||||
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)
|
||||
articles_list[idx] = article
|
||||
currency_name=ExchangeRate_name.objects.get(id=currency)
|
||||
|
||||
return render(request,
|
||||
'webshop/index.html',
|
||||
{'category_list': category_list,
|
||||
'articles_list': articles_list})
|
||||
'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):
|
||||
category_list = get_categories()
|
||||
selected_category = Category.objects.get(id=category_id)
|
||||
hidden = ArticleStatus.objects.get(name="hidden")
|
||||
articles = Article.objects.filter(
|
||||
category=selected_category.id).exclude(status=get_hidden_status_id())
|
||||
articles_list = list(articles)
|
||||
currencies_form = CurrenciesForm
|
||||
rate=ExchangeRate
|
||||
article_view = True
|
||||
currency_name = "CHF"
|
||||
|
||||
article_list = Article.objects.filter(
|
||||
category=selected_category.id).exclude(status=hidden.id)
|
||||
if not 'currency' in request.session:
|
||||
request.session['currency'] = None
|
||||
|
||||
parent_category_list = Category.objects.filter(parent_category=None)
|
||||
category_list = {}
|
||||
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
|
||||
|
||||
for i in parent_category_list:
|
||||
category_list.update(
|
||||
{i: Category.objects.filter(parent_category=i.id)})
|
||||
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)
|
||||
articles_list[idx] = article
|
||||
currency_name=ExchangeRate_name.objects.get(id=currency)
|
||||
|
||||
return render(request, 'webshop/category.html',
|
||||
{'article_list': article_list,
|
||||
{'articles_list': articles_list,
|
||||
'category_list': category_list,
|
||||
'currencies_form': currencies_form,
|
||||
'article_view': article_view,
|
||||
'currency_name': currency_name,
|
||||
'category': selected_category})
|
||||
|
||||
|
||||
def article_details(request, article_id):
|
||||
parent_category_list = Category.objects.filter(parent_category=None)
|
||||
category_list = {}
|
||||
category_list = get_categories()
|
||||
currencies_form = CurrenciesForm
|
||||
rate=ExchangeRate
|
||||
article_view = True
|
||||
currency_name = "CHF"
|
||||
|
||||
for i in parent_category_list:
|
||||
category_list.update(
|
||||
{i: Category.objects.filter(parent_category=i.id)})
|
||||
if not 'currency' in request.session:
|
||||
request.session['currency'] = None
|
||||
|
||||
article = get_object_or_404(Article, pk=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
|
||||
|
||||
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)
|
||||
|
||||
return render(request, 'webshop/article_details.html',
|
||||
{'article': article,
|
||||
'category_list': category_list,
|
||||
'currencies_form': currencies_form,
|
||||
'article_view': article_view,
|
||||
'currency_name': currency_name,
|
||||
'picture_list': picture_list})
|
||||
|
||||
@login_required
|
||||
def profile(request):
|
||||
category_list = get_categories()
|
||||
person = Person.objects.get(user=request.user)
|
||||
return render(request, 'registration/profile.html',
|
||||
{'person': person})
|
||||
{'person': person,
|
||||
'category_list': category_list})
|
||||
|
||||
|
||||
def registration(request):
|
||||
category_list = get_categories()
|
||||
if request.method == 'POST':
|
||||
profile_form = RegistrationForm(request.POST)
|
||||
user_form = UserCreationForm(request.POST)
|
||||
|
@ -99,4 +181,5 @@ def registration(request):
|
|||
user_form = UserCreationForm
|
||||
return render(request, 'registration/register.html',
|
||||
{'profile_form': profile_form,
|
||||
'category_list': category_list,
|
||||
'user_form': user_form})
|
||||
|
|
Loading…
Reference in New Issue