mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-12-04 14:06:38 +01:00
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.
39 lines
1.2 KiB
Python
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
|