diff --git a/app/apps/common/templatetags/decimal.py b/app/apps/common/templatetags/decimal.py new file mode 100644 index 0000000..587a3d9 --- /dev/null +++ b/app/apps/common/templatetags/decimal.py @@ -0,0 +1,37 @@ +from django import template +from decimal import Decimal, DecimalException + +from django.utils.formats import number_format +from django.utils.translation import to_locale, get_language + +register = template.Library() + + +@register.filter +def drop_trailing_zeros(value): + if not isinstance(value, (float, Decimal, str)): + return value + + try: + decimal_value = Decimal(str(value)) + return decimal_value.normalize() + except Exception: + return value + + +@register.filter +def localize_number(value): + if value is None: + return value + + try: + value = Decimal(str(value)) + except (TypeError, ValueError, DecimalException): + return value + + return number_format( + value, + decimal_pos=abs(value.as_tuple().exponent), + use_l10n=True, + force_grouping=True, + ) diff --git a/app/apps/mini_tools/forms.py b/app/apps/mini_tools/forms.py new file mode 100644 index 0000000..429c770 --- /dev/null +++ b/app/apps/mini_tools/forms.py @@ -0,0 +1,29 @@ +from crispy_forms.helper import FormHelper +from crispy_forms.layout import Layout, Row, Column +from django import forms +from apps.currencies.models import Currency +from apps.common.widgets.tom_select import TomSelect + + +class CurrencyConverterForm(forms.Form): + from_currency = forms.ModelChoiceField( + queryset=Currency.objects.all(), + label="", + widget=TomSelect(clear_button=False), + ) + + to_currency = forms.ModelChoiceField( + queryset=Currency.objects.all(), + label="", + widget=TomSelect(clear_button=False), + ) + + def __init__(self, *args, **kwargs): + super(CurrencyConverterForm, self).__init__(*args, **kwargs) + + self.helper = FormHelper() + self.helper.form_tag = False + self.helper.layout = Layout( + "from_currency", + "to_currency", + ) diff --git a/app/apps/mini_tools/urls.py b/app/apps/mini_tools/urls.py index e872f6c..e474042 100644 --- a/app/apps/mini_tools/urls.py +++ b/app/apps/mini_tools/urls.py @@ -8,4 +8,14 @@ urlpatterns = [ views.unit_price_calculator, name="unit_price_calculator", ), + path( + "tools/currency-converter/", + views.currency_converter, + name="currency_converter", + ), + path( + "tools/currency-converter/convert/", + views.currency_converter_convert, + name="currency_converter_convert", + ), ] diff --git a/app/apps/mini_tools/views.py b/app/apps/mini_tools/views.py index 93c6ecf..40ef0c8 100644 --- a/app/apps/mini_tools/views.py +++ b/app/apps/mini_tools/views.py @@ -1,7 +1,59 @@ from django.contrib.auth.decorators import login_required from django.shortcuts import render +from apps.common.widgets.decimal import convert_to_decimal +from apps.currencies.models import Currency +from apps.currencies.utils.convert import convert +from apps.mini_tools.forms import CurrencyConverterForm + @login_required def unit_price_calculator(request): return render(request, "mini_tools/unit_price_calculator.html") + + +@login_required +def currency_converter(request): + form = CurrencyConverterForm() + return render( + request, + "mini_tools/currency_converter/currency_converter.html", + context={"form": form}, + ) + + +@login_required +def currency_converter_convert(request): + from_value = request.GET.get("from_value") + from_currency = request.GET.get("from_currency") + to_currency = request.GET.get("to_currency") + + if from_currency: + from_currency = Currency.objects.filter(id=from_currency).first() + if to_currency: + to_currency = Currency.objects.filter(id=to_currency).first() + if from_value: + from_value = convert_to_decimal(from_value) + + if from_currency and to_currency and from_value: + converted_amount, prefix, suffix, decimal_places = convert( + amount=from_value, + from_currency=from_currency, + to_currency=to_currency, + ) + else: + converted_amount = None + prefix = "" + suffix = "" + decimal_places = 0 + + return render( + request, + "mini_tools/currency_converter/converted_value.html", + context={ + "converted_amount": converted_amount, + "prefix": prefix, + "suffix": suffix, + "decimal_places": decimal_places, + }, + ) diff --git a/app/templates/includes/navbar.html b/app/templates/includes/navbar.html index fc5ccf4..66aafa4 100644 --- a/app/templates/includes/navbar.html +++ b/app/templates/includes/navbar.html @@ -65,7 +65,7 @@
  • {% translate 'Unit Price Calculator' %}
  • +
  • {% translate 'Currency Converter' %}
  • +