feat: filter exchange_rates by pair

This commit is contained in:
Herculino Trotta
2024-10-24 23:47:08 -03:00
parent c513bda3fd
commit a650b19547
5 changed files with 117 additions and 42 deletions

View File

@@ -18,6 +18,16 @@ urlpatterns = [
),
path("exchange-rates/", views.exchange_rates_index, name="exchange_rates_index"),
path("exchange-rates/list/", views.exchange_rates_list, name="exchange_rates_list"),
path(
"exchange-rates/pair/",
views.exchange_rates_list_pair,
name="exchange_rates_list_pair",
),
path(
"exchange-rates/pair/<str:from_currency>/<str:to_currency>",
views.exchange_rates_list_pair,
name="exchange_rates_list_pair",
),
path("exchange-rates/add/", views.exchange_rate_add, name="exchange_rate_add"),
path(
"exchange-rates/<int:pk>/edit/",

View File

@@ -1,5 +1,7 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.db.models import F, CharField, Value
from django.db.models.functions import Concat
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
from django.utils.translation import gettext_lazy as _
@@ -24,11 +26,46 @@ def exchange_rates_index(request):
@login_required
@require_http_methods(["GET"])
def exchange_rates_list(request):
exchange_rates = ExchangeRate.objects.all().order_by("-date")
pairings = (
ExchangeRate.objects.values("from_currency__code", "to_currency__code")
.distinct()
.annotate(
pair=Concat(
"from_currency__code",
Value(" x "),
"to_currency__code",
output_field=CharField(),
)
)
.values_list("pair", "from_currency__code", "to_currency__code")
)
return render(
request,
"exchange_rates/fragments/list.html",
{"exchange_rates": exchange_rates},
{"pairings": pairings},
)
@only_htmx
@login_required
@require_http_methods(["GET"])
def exchange_rates_list_pair(request, from_currency=None, to_currency=None):
if from_currency and to_currency:
exchange_rates = ExchangeRate.objects.filter(
from_currency__code=from_currency, to_currency__code=to_currency
).order_by("-date")
else:
exchange_rates = ExchangeRate.objects.all().order_by("-date")
return render(
request,
"exchange_rates/fragments/table.html",
{
"exchange_rates": exchange_rates,
"from_currency": from_currency,
"to_currency": to_currency,
},
)

View File

@@ -16,44 +16,22 @@
{% endspaceless %}
</div>
<div class="border p-3 rounded-3 table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th scope="col" class="col-auto"></th>
<th scope="col" class="col-auto">{% translate 'Date' %}</th>
<th scope="col" class="col">{% translate 'Pairing' %}</th>
<th scope="col" class="col">{% translate 'Rate' %}</th>
</tr>
</thead>
<tbody>
{% for exchange_rate in exchange_rates %}
<tr class="exchange-rate">
<td class="col-auto">
<a class="text-decoration-none tw-text-gray-400 p-1"
role="button"
data-bs-toggle="tooltip"
data-bs-title="{% translate "Edit" %}"
hx-get="{% url 'exchange_rate_edit' pk=exchange_rate.id %}"
hx-target="#generic-offcanvas">
<i class="fa-solid fa-pencil fa-fw"></i></a>
<a class="text-danger text-decoration-none p-1"
role="button"
data-bs-toggle="tooltip"
data-bs-title="{% translate "Delete" %}"
hx-delete="{% url 'exchange_rate_delete' pk=exchange_rate.id %}"
hx-trigger='confirmed'
data-bypass-on-ctrl="true"
data-title="{% translate "Are you sure?" %}"
data-text="{% translate "You won't be able to revert this!" %}"
data-confirm-text="{% translate "Yes, delete it!" %}"
_="install prompt_swal"><i class="fa-solid fa-trash fa-fw"></i></a></td>
<td class="col-auto">{{ exchange_rate.date|date:"SHORT_DATETIME_FORMAT" }}</td>
<td class="col"><span class="badge rounded-pill text-bg-secondary">{{ exchange_rate.from_currency.code }}</span> x <span class="badge rounded-pill text-bg-secondary">{{ exchange_rate.to_currency.code }}</span></td>
<td class="col">1 {{ exchange_rate.from_currency.code }} ≅ {% currency_display amount=exchange_rate.rate prefix=exchange_rate.to_currency.prefix suffix=exchange_rate.to_currency.suffix decimal_places=exchange_rate.to_currency.decimal_places%}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="card">
<div class="card-header">
<ul class="nav nav-pills card-header-pills" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" data-bs-toggle="tab" type="button" role="tab" aria-controls="home-tab-pane" aria-selected="true" hx-get="{% url 'exchange_rates_list_pair' %}" hx-trigger="load, click" hx-target="#exchange-rates-table">{% translate 'All' %}</button>
</li>
{% for pair in pairings %}
<li class="nav-item" role="presentation">
<button class="nav-link" hx-get="{% url 'exchange_rates_list_pair' from_currency=pair.1 to_currency=pair.2 %}" hx-target="#exchange-rates-table" data-bs-toggle="tab" type="button" role="tab" aria-controls="profile-tab-pane" aria-selected="false">{{ pair.0 }}</button>
</li>
{% endfor %}
</ul>
</div>
<div id="exchange-rates-table"></div>
</div>
</div>

View File

@@ -0,0 +1,50 @@
{% load currency_display %}
{% load i18n %}
{% if from_currency and to_currency %}
<div class="card-body show-loading" hx-get="{% url 'exchange_rates_list_pair' from_currency=from_currency to_currency=to_currency %}" hx-trigger="updated from:window" hx-swap="outerHTML">
{% else %}
<div class="card-body show-loading" hx-get="{% url 'exchange_rates_list_pair' %}" hx-trigger="updated from:window" hx-swap="outerHTML">
{% endif %}
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th scope="col" class="col-auto"></th>
<th scope="col" class="col-auto">{% translate 'Date' %}</th>
<th scope="col" class="col">{% translate 'Pairing' %}</th>
<th scope="col" class="col">{% translate 'Rate' %}</th>
</tr>
</thead>
<tbody>
{% for exchange_rate in exchange_rates %}
<tr class="exchange-rate">
<td class="col-auto">
<a class="text-decoration-none tw-text-gray-400 p-1"
role="button"
data-bs-toggle="tooltip"
data-bs-title="{% translate "Edit" %}"
hx-get="{% url 'exchange_rate_edit' pk=exchange_rate.id %}"
hx-target="#generic-offcanvas"
hx-swap="innerHTML">
<i class="fa-solid fa-pencil fa-fw"></i></a>
<a class="text-danger text-decoration-none p-1"
role="button"
data-bs-toggle="tooltip"
data-bs-title="{% translate "Delete" %}"
hx-delete="{% url 'exchange_rate_delete' pk=exchange_rate.id %}"
hx-trigger='confirmed'
hx-swap="innerHTML"
data-bypass-on-ctrl="true"
data-title="{% translate "Are you sure?" %}"
data-text="{% translate "You won't be able to revert this!" %}"
data-confirm-text="{% translate "Yes, delete it!" %}"
_="install prompt_swal"><i class="fa-solid fa-trash fa-fw"></i></a></td>
<td class="col-auto">{{ exchange_rate.date|date:"SHORT_DATETIME_FORMAT" }}</td>
<td class="col"><span class="badge rounded-pill text-bg-secondary">{{ exchange_rate.from_currency.code }}</span> x <span class="badge rounded-pill text-bg-secondary">{{ exchange_rate.to_currency.code }}</span></td>
<td class="col">1 {{ exchange_rate.from_currency.code }} ≅ {% currency_display amount=exchange_rate.rate prefix=exchange_rate.to_currency.prefix suffix=exchange_rate.to_currency.suffix decimal_places=exchange_rate.to_currency.decimal_places%}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>

View File

@@ -4,5 +4,5 @@
{% block title %}{% translate 'Exchange Rates' %}{% endblock %}
{% block content %}
<div hx-get="{% url 'exchange_rates_list' %}" hx-trigger="load, updated from:window" class="show-loading"></div>
<div hx-get="{% url 'exchange_rates_list' %}" hx-trigger="load" class="show-loading"></div>
{% endblock %}