mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-07-12 15:52:54 +02:00
feat: filter exchange_rates by pair
This commit is contained in:
@@ -18,6 +18,16 @@ urlpatterns = [
|
|||||||
),
|
),
|
||||||
path("exchange-rates/", views.exchange_rates_index, name="exchange_rates_index"),
|
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/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/add/", views.exchange_rate_add, name="exchange_rate_add"),
|
||||||
path(
|
path(
|
||||||
"exchange-rates/<int:pk>/edit/",
|
"exchange-rates/<int:pk>/edit/",
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.contrib.auth.decorators import login_required
|
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.http import HttpResponse
|
||||||
from django.shortcuts import render, get_object_or_404
|
from django.shortcuts import render, get_object_or_404
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
@@ -24,11 +26,46 @@ def exchange_rates_index(request):
|
|||||||
@login_required
|
@login_required
|
||||||
@require_http_methods(["GET"])
|
@require_http_methods(["GET"])
|
||||||
def exchange_rates_list(request):
|
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(
|
return render(
|
||||||
request,
|
request,
|
||||||
"exchange_rates/fragments/list.html",
|
"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,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,44 +16,22 @@
|
|||||||
{% endspaceless %}
|
{% endspaceless %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="border p-3 rounded-3 table-responsive">
|
<div class="card">
|
||||||
<table class="table table-hover">
|
<div class="card-header">
|
||||||
<thead>
|
<ul class="nav nav-pills card-header-pills" id="myTab" role="tablist">
|
||||||
<tr>
|
<li class="nav-item" role="presentation">
|
||||||
<th scope="col" class="col-auto"></th>
|
<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>
|
||||||
<th scope="col" class="col-auto">{% translate 'Date' %}</th>
|
</li>
|
||||||
<th scope="col" class="col">{% translate 'Pairing' %}</th>
|
{% for pair in pairings %}
|
||||||
<th scope="col" class="col">{% translate 'Rate' %}</th>
|
<li class="nav-item" role="presentation">
|
||||||
</tr>
|
<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>
|
||||||
</thead>
|
</li>
|
||||||
<tbody>
|
{% endfor %}
|
||||||
{% for exchange_rate in exchange_rates %}
|
</ul>
|
||||||
<tr class="exchange-rate">
|
</div>
|
||||||
<td class="col-auto">
|
|
||||||
<a class="text-decoration-none tw-text-gray-400 p-1"
|
<div id="exchange-rates-table"></div>
|
||||||
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>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -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>
|
||||||
@@ -4,5 +4,5 @@
|
|||||||
{% block title %}{% translate 'Exchange Rates' %}{% endblock %}
|
{% block title %}{% translate 'Exchange Rates' %}{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% 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 %}
|
{% endblock %}
|
||||||
|
|||||||
Reference in New Issue
Block a user