Implement currency settings and formatting
This commit is contained in:
parent
e8a54dcbc7
commit
1311fa677d
4 changed files with 62 additions and 2 deletions
|
@ -0,0 +1,28 @@
|
||||||
|
# Generated by Django 5.1.4 on 2025-01-07 18:19
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('inventory', '0007_settings_track_amount'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='settings',
|
||||||
|
name='currency',
|
||||||
|
field=models.CharField(default='Euro', help_text='Currency name', max_length=30),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='settings',
|
||||||
|
name='currency_symbol',
|
||||||
|
field=models.CharField(default='€', help_text='Currency symbol', max_length=20),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='settings',
|
||||||
|
name='currency_symbol_position',
|
||||||
|
field=models.BooleanField(default=True, help_text='Currency symbol after amount'),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,5 +1,7 @@
|
||||||
{% load static %}
|
{% load static %}
|
||||||
{% load admin_urls %}
|
{% load admin_urls %}
|
||||||
|
{% load currency %}
|
||||||
|
|
||||||
<div class="cell{% if hilight == item.id %} search-hilight{% endif %}">
|
<div class="cell{% if hilight == item.id %} search-hilight{% endif %}">
|
||||||
{% if item.name %}
|
{% if item.name %}
|
||||||
{% if item.metadata.package %}
|
{% if item.metadata.package %}
|
||||||
|
@ -26,7 +28,7 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if item.price %}
|
{% if item.price %}
|
||||||
<div class="price">{{ item.price | floatformat:2 }} €</div>
|
<div class="price">{{ item.price | currency:"short" }}</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if item.form_factor %}
|
{% if item.form_factor %}
|
||||||
<div class="form_factor">{{ item.form_factor.name }}</div>
|
<div class="form_factor">{{ item.form_factor.name }}</div>
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
{% load static %}
|
{% load static %}
|
||||||
{% load formatstring %}
|
{% load formatstring %}
|
||||||
|
{% load currency %}
|
||||||
|
|
||||||
{% block title %}{{ item }}{% endblock %}
|
{% block title %}{{ item }}{% endblock %}
|
||||||
|
|
||||||
|
@ -103,7 +104,7 @@
|
||||||
{% if item.price %}
|
{% if item.price %}
|
||||||
<tr>
|
<tr>
|
||||||
<th>Price</th>
|
<th>Price</th>
|
||||||
<td>{{ item.price }} Euro</td>
|
<td>{{ item.price | currency:"detail" }} <span class="small">{% if settings.track_amount %}(sum: {{ item.value | currency:"long" }}){% endif %}</span></td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
|
29
inventory/templatetags/currency.py
Normal file
29
inventory/templatetags/currency.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
from django import template
|
||||||
|
from django.utils.safestring import mark_safe
|
||||||
|
from django.utils.formats import number_format
|
||||||
|
|
||||||
|
from inventory.models import Settings
|
||||||
|
|
||||||
|
register = template.Library()
|
||||||
|
s = Settings.objects.first()
|
||||||
|
|
||||||
|
@register.filter(name='currency')
|
||||||
|
def currency(value, format):
|
||||||
|
value = float(value)
|
||||||
|
|
||||||
|
if format == 'detail':
|
||||||
|
value = number_format(round(value, 3), 3, use_l10n=True)
|
||||||
|
else:
|
||||||
|
value = number_format(round(value, 2), 2, use_l10n=True)
|
||||||
|
|
||||||
|
if format == 'long' or format == 'detail':
|
||||||
|
symbol = s.currency
|
||||||
|
else:
|
||||||
|
symbol = s.currency_symbol
|
||||||
|
|
||||||
|
if s.currency_symbol_position:
|
||||||
|
result = f"{value} {symbol}"
|
||||||
|
else:
|
||||||
|
result = f"{symbol} {value}"
|
||||||
|
|
||||||
|
return mark_safe(result)
|
Loading…
Reference in a new issue