diff --git a/app/apps/currencies/utils/convert.py b/app/apps/currencies/utils/convert.py index f1b6b81..caa2a94 100644 --- a/app/apps/currencies/utils/convert.py +++ b/app/apps/currencies/utils/convert.py @@ -1,25 +1,16 @@ -from django.db.models import Func, F, Value, DurationField, Case, When, DecimalField, Q -from django.db.models.functions import Abs, Extract +import datetime + +from django.db.models import Func, F, Value, Case, When, DecimalField, Q +from django.db.models.functions import Extract from django.utils import timezone -from django.core.exceptions import ValidationError -from django.utils.translation import gettext_lazy as _ -from apps.currencies.models import ExchangeRate from apps.currencies.models import Currency +from apps.currencies.models import ExchangeRate -def convert(amount, from_currency: Currency, to_currency: Currency, date=None): - if from_currency == to_currency: - return ( - amount, - to_currency.prefix, - to_currency.suffix, - to_currency.decimal_places, - ) - - if date is None: - date = timezone.localtime(timezone.now()) - +def get_exchange_rate( + from_currency: Currency, to_currency: Currency, date: datetime.date +) -> ExchangeRate | None: try: exchange_rate = ( ExchangeRate.objects.filter( @@ -41,13 +32,37 @@ def convert(amount, from_currency: Currency, to_currency: Currency, date=None): ) if exchange_rate is None: - return None, None, None, None + return None + except ExchangeRate.DoesNotExist: + return None + + else: + return exchange_rate + + +def convert(amount, from_currency: Currency, to_currency: Currency, date=None): + if from_currency == to_currency: return ( - amount * exchange_rate.effective_rate, + amount, to_currency.prefix, to_currency.suffix, to_currency.decimal_places, ) - except ExchangeRate.DoesNotExist: + + if date is None: + date = timezone.localtime(timezone.now()) + + exchange_rate = get_exchange_rate( + from_currency=from_currency, to_currency=to_currency, date=date + ) + + if exchange_rate is None: return None, None, None, None + + return ( + amount * exchange_rate.effective_rate, + to_currency.prefix, + to_currency.suffix, + to_currency.decimal_places, + ) diff --git a/app/apps/dca/models.py b/app/apps/dca/models.py index 0d7d854..f6efcb5 100644 --- a/app/apps/dca/models.py +++ b/app/apps/dca/models.py @@ -7,7 +7,7 @@ from django.template.defaultfilters import date from django.utils import timezone from django.utils.translation import gettext_lazy as _ -from apps.currencies.utils.convert import convert +from apps.currencies.utils.convert import convert, get_exchange_rate class DCAStrategy(models.Model): @@ -133,6 +133,18 @@ class DCAStrategy(models.Model): "amounts_bought": amounts_bought, } + def current_price(self): + exchange_rate = get_exchange_rate( + from_currency=self.payment_currency, + to_currency=self.target_currency, + date=timezone.localtime(timezone.now()), + ) + + if exchange_rate: + return exchange_rate.effective_rate, exchange_rate.date + else: + return None + class DCAEntry(models.Model): strategy = models.ForeignKey( diff --git a/app/apps/dca/views.py b/app/apps/dca/views.py index dccb4c3..94f6ffc 100644 --- a/app/apps/dca/views.py +++ b/app/apps/dca/views.py @@ -145,15 +145,6 @@ def strategy_detail(request, strategy_id): "entries": entries, "entries_data": entries_data, "monthly_data": monthly_data, - "total_invested": strategy.total_invested(), - "total_received": strategy.total_received(), - "average_entry_price": strategy.average_entry_price(), - "total_entries": strategy.total_entries(), - "current_total_value": strategy.current_total_value(), - "total_profit_loss": strategy.total_profit_loss(), - "total_profit_loss_percentage": strategy.total_profit_loss_percentage(), - "investment_frequency": strategy.investment_frequency_data(), - "price_comparison_data": strategy.price_comparison_data(), } return render(request, "dca/fragments/strategy/details.html", context) diff --git a/app/templates/cotton/amount/display.html b/app/templates/cotton/amount/display.html index c8905aa..d33ff1f 100644 --- a/app/templates/cotton/amount/display.html +++ b/app/templates/cotton/amount/display.html @@ -1,7 +1,8 @@ {% load currency_display %}