add the currency calculation to the index page
This commit is contained in:
parent
e76b3cb80d
commit
1481727a51
|
@ -22,7 +22,7 @@
|
||||||
</td>
|
</td>
|
||||||
<td scope="col">{{ article.category }}</td>
|
<td scope="col">{{ article.category }}</td>
|
||||||
<td scope="col">{{ article.stock }}</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>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
|
|
|
@ -19,24 +19,19 @@
|
||||||
<a href="{% url 'login' %}">LOGIN</a>
|
<a href="{% url 'login' %}">LOGIN</a>
|
||||||
</li>
|
</li>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
<!-- https://pypi.python.org/pypi/django-select-multiple-field -->
|
<li>
|
||||||
<form method="POST" novalidate>
|
{% if article_view %}
|
||||||
<select name="currency_update" id="id_currency_update">
|
<form id="currency" name="currency" action="" method="POST" novalidate>
|
||||||
<option value="CHF">CHF</option>
|
{{ currencies_form.as_ul }}
|
||||||
<option value="USD">USD</option>
|
<li>
|
||||||
<option value="GBP">GBP</option>
|
<input type="submit" value="Select">
|
||||||
<option value="JPY">JPY</option>
|
</li>
|
||||||
</select>
|
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
</form>
|
</form>
|
||||||
|
{% endif %}
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</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>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
@ -7,6 +7,8 @@ from webshop.models import (Article, Category, ArticleStatus, Person,
|
||||||
City, Picture)
|
City, Picture)
|
||||||
from webshop.forms import RegistrationForm
|
from webshop.forms import RegistrationForm
|
||||||
|
|
||||||
|
from currencies.models import ExchangeRate, ExchangeRate_name
|
||||||
|
from currencies.forms import CurrenciesForm
|
||||||
|
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
@ -26,11 +28,40 @@ def get_hidden_status_id():
|
||||||
return hidden_status.id
|
return hidden_status.id
|
||||||
|
|
||||||
|
|
||||||
|
def index(request):
|
||||||
category_list = get_categories()
|
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 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,
|
return render(request,
|
||||||
'webshop/index.html',
|
'webshop/index.html',
|
||||||
{'category_list': category_list,
|
{'category_list': category_list,
|
||||||
'articles_list': articles_list})
|
'articles_list': articles_list,
|
||||||
|
'currencies_form': currencies_form,
|
||||||
|
'article_view': article_view,
|
||||||
|
'currency_name': currency_name})
|
||||||
|
|
||||||
|
|
||||||
def articles_in_category(request, category_id):
|
def articles_in_category(request, category_id):
|
||||||
|
|
Loading…
Reference in New Issue