mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-04-25 10:08:36 +02:00
Merge pull request #317
feat(automatic-exchange-rates): add "Single exchange rate" where only one exchange rate is added and updated to avoid db clutter
This commit is contained in:
@@ -203,21 +203,63 @@ class ExchangeRateFetcher:
|
|||||||
|
|
||||||
if provider.rates_inverted:
|
if provider.rates_inverted:
|
||||||
# If rates are inverted, we need to swap currencies
|
# If rates are inverted, we need to swap currencies
|
||||||
ExchangeRate.objects.create(
|
if service.singleton:
|
||||||
from_currency=to_currency,
|
# Try to get the last automatically created exchange rate
|
||||||
to_currency=from_currency,
|
exchange_rate = (
|
||||||
rate=rate,
|
ExchangeRate.objects.filter(
|
||||||
date=timezone.now(),
|
automatic=True,
|
||||||
)
|
from_currency=to_currency,
|
||||||
|
to_currency=from_currency,
|
||||||
|
)
|
||||||
|
.order_by("-date")
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
exchange_rate = None
|
||||||
|
|
||||||
|
if not exchange_rate:
|
||||||
|
ExchangeRate.objects.create(
|
||||||
|
automatic=True,
|
||||||
|
from_currency=to_currency,
|
||||||
|
to_currency=from_currency,
|
||||||
|
rate=rate,
|
||||||
|
date=timezone.now(),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
exchange_rate.rate = rate
|
||||||
|
exchange_rate.date = timezone.now()
|
||||||
|
exchange_rate.save()
|
||||||
|
|
||||||
processed_pairs.add((to_currency.id, from_currency.id))
|
processed_pairs.add((to_currency.id, from_currency.id))
|
||||||
else:
|
else:
|
||||||
# If rates are not inverted, we can use them as is
|
# If rates are not inverted, we can use them as is
|
||||||
ExchangeRate.objects.create(
|
if service.singleton:
|
||||||
from_currency=from_currency,
|
# Try to get the last automatically created exchange rate
|
||||||
to_currency=to_currency,
|
exchange_rate = (
|
||||||
rate=rate,
|
ExchangeRate.objects.filter(
|
||||||
date=timezone.now(),
|
automatic=True,
|
||||||
)
|
from_currency=from_currency,
|
||||||
|
to_currency=to_currency,
|
||||||
|
)
|
||||||
|
.order_by("-date")
|
||||||
|
.first()
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
exchange_rate = None
|
||||||
|
|
||||||
|
if not exchange_rate:
|
||||||
|
ExchangeRate.objects.create(
|
||||||
|
automatic=True,
|
||||||
|
from_currency=from_currency,
|
||||||
|
to_currency=to_currency,
|
||||||
|
rate=rate,
|
||||||
|
date=timezone.now(),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
exchange_rate.rate = rate
|
||||||
|
exchange_rate.date = timezone.now()
|
||||||
|
exchange_rate.save()
|
||||||
|
|
||||||
processed_pairs.add((from_currency.id, to_currency.id))
|
processed_pairs.add((from_currency.id, to_currency.id))
|
||||||
|
|
||||||
service.last_fetch = timezone.now()
|
service.last_fetch = timezone.now()
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ class ExchangeRateServiceForm(forms.ModelForm):
|
|||||||
"fetch_interval",
|
"fetch_interval",
|
||||||
"target_currencies",
|
"target_currencies",
|
||||||
"target_accounts",
|
"target_accounts",
|
||||||
|
"singleton",
|
||||||
]
|
]
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
@@ -126,6 +127,7 @@ class ExchangeRateServiceForm(forms.ModelForm):
|
|||||||
"name",
|
"name",
|
||||||
"service_type",
|
"service_type",
|
||||||
Switch("is_active"),
|
Switch("is_active"),
|
||||||
|
Switch("singleton"),
|
||||||
"api_key",
|
"api_key",
|
||||||
Row(
|
Row(
|
||||||
Column("interval_type", css_class="form-group col-md-6"),
|
Column("interval_type", css_class="form-group col-md-6"),
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# Generated by Django 5.2.4 on 2025-08-08 02:18
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('currencies', '0014_alter_currency_options'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='exchangerate',
|
||||||
|
name='automatic',
|
||||||
|
field=models.BooleanField(default=False, verbose_name='Automatic'),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='exchangerateservice',
|
||||||
|
name='singleton',
|
||||||
|
field=models.BooleanField(default=False, help_text='Create one exchange rate and keep updating it. Avoids database clutter.', verbose_name='Single exchange rate'),
|
||||||
|
),
|
||||||
|
]
|
||||||
@@ -70,6 +70,8 @@ class ExchangeRate(models.Model):
|
|||||||
)
|
)
|
||||||
date = models.DateTimeField(verbose_name=_("Date and Time"))
|
date = models.DateTimeField(verbose_name=_("Date and Time"))
|
||||||
|
|
||||||
|
automatic = models.BooleanField(verbose_name=_("Automatic"), default=False)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = _("Exchange Rate")
|
verbose_name = _("Exchange Rate")
|
||||||
verbose_name_plural = _("Exchange Rates")
|
verbose_name_plural = _("Exchange Rates")
|
||||||
@@ -148,6 +150,14 @@ class ExchangeRateService(models.Model):
|
|||||||
blank=True,
|
blank=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
singleton = models.BooleanField(
|
||||||
|
verbose_name=_("Single exchange rate"),
|
||||||
|
default=False,
|
||||||
|
help_text=_(
|
||||||
|
"Create one exchange rate and keep updating it. Avoids database clutter."
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = _("Exchange Rate Service")
|
verbose_name = _("Exchange Rate Service")
|
||||||
verbose_name_plural = _("Exchange Rate Services")
|
verbose_name_plural = _("Exchange Rate Services")
|
||||||
|
|||||||
Reference in New Issue
Block a user