Implement currency settings and formatting

This commit is contained in:
Johannes Schriewer 2025-01-07 21:40:51 +01:00
parent e8a54dcbc7
commit 1311fa677d
4 changed files with 62 additions and 2 deletions

View file

@ -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'),
),
]

View file

@ -1,5 +1,7 @@
{% load static %}
{% load admin_urls %}
{% load currency %}
<div class="cell{% if hilight == item.id %} search-hilight{% endif %}">
{% if item.name %}
{% if item.metadata.package %}
@ -26,7 +28,7 @@
{% endif %}
{% endif %}
{% if item.price %}
<div class="price">{{ item.price | floatformat:2 }} &euro;</div>
<div class="price">{{ item.price | currency:"short" }}</div>
{% endif %}
{% if item.form_factor %}
<div class="form_factor">{{ item.form_factor.name }}</div>

View file

@ -1,6 +1,7 @@
{% extends "base.html" %}
{% load static %}
{% load formatstring %}
{% load currency %}
{% block title %}{{ item }}{% endblock %}
@ -103,7 +104,7 @@
{% if item.price %}
<tr>
<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>
{% endif %}

View 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)