Files
WYGIWYH/app/apps/currencies/exchange_rates/base.py
T
Herculino Trotta 43145be24d feat(automatic-exchange-rates): add provider options field
Add an Options field to exchange rate services. Providers get the raw
text, can validate it on save through clean_options, and describe it
with options_help.

The service form now shows API Key only for providers that require one,
and Options only for providers that read it, with that provider's help.

AllRatesToday reads its bank code from Options instead of API Key.
2026-11-08 02:57:07 -03:00

39 lines
1.2 KiB
Python

from abc import ABC, abstractmethod
from decimal import Decimal
from typing import List, Tuple, Optional
from django.db.models import QuerySet
from apps.currencies.models import Currency
class ExchangeRateProvider(ABC):
rates_inverted = False
# Help shown under the service's Options field. None hides the field
options_help = None
def __init__(self, api_key: Optional[str] = None, options: str = ""):
self.api_key = api_key
self.options = options or ""
@abstractmethod
def get_rates(
self, target_currencies: QuerySet, exchange_currencies: set
) -> List[Tuple[Currency, Currency, Decimal]]:
"""Fetch exchange rates for multiple currency pairs"""
raise NotImplementedError("Subclasses must implement get_rates method")
@classmethod
def requires_api_key(cls) -> bool:
"""Return True if the service requires an API key"""
return True
@classmethod
def clean_options(cls, options: str) -> str:
"""Validate the raw Options text on save. Raise ValidationError if invalid"""
return options
@staticmethod
def invert_rate(rate: Decimal) -> Decimal:
"""Invert the given rate."""
return Decimal("1") / rate