• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

inventree / inventree-python / 4080124792

pending completion
4080124792

Pull #164

github

GitHub
Merge 16db6b972 into ba6505b32
Pull Request #164: Currency support

47 of 47 new or added lines in 1 file covered. (100.0%)

849 of 979 relevant lines covered (86.72%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.34
/inventree/currency.py
1
"""Manages currency / conversion support for InvenTree"""
2

3
import logging
1✔
4

5

6
logger = logging.getLogger('inventree')
1✔
7

8

9
class CurrencyManager(object):
1✔
10
    """Class for managing InvenTree currency suppport"""
11

12
    # Currency API endpoint
13
    CURRENCY_ENDPOINT = 'currency/exchange/'
1✔
14

15
    def __init__(self, api):
1✔
16
        """Construct a CurrencyManager instance"""
17

18
        # Store internal reference to the API
19
        self.api = api
1✔
20

21
        self.base_currency = None
1✔
22
        self.exchange_rates = None
1✔
23
    
24
    def refreshExchangeRates(self):
1✔
25
        """Request the server update exchange rates from external service"""
26

27
        if self.api.api_version < 93:
×
28
            raise ValueError(f"Server API version ({self.api.api_version}) is older than v93, which is required for manual exchange rate upates")
×
29

30
        return self.api.post('currency/refresh/', {})
×
31

32
    def updateFromServer(self):
1✔
33
        """Retrieve currency data from the server"""
34

35
        if self.api.api_version < 92:
1✔
36
            raise ValueError(f"Server API version ({self.api.api_version}) is older than v92, which is required for currency support")
×
37

38
        response = self.api.get(self.CURRENCY_ENDPOINT)
1✔
39

40
        if response is None:
1✔
41
            logger.error("Could not retrieve currency data from InvenTree server")
×
42
            return
×
43
        
44
        self.base_currency = response.get('base_currency', None)
1✔
45
        self.exchange_rates = response.get('exchange_rates', None)
1✔
46

47
        if self.base_currency is None:
1✔
48
            logger.warning("'base_currency' missing from server response")
×
49
        
50
        if self.exchange_rates is None:
1✔
51
            logger.warning("'exchange_rates' missing from server response")
×
52

53
    def getBaseCurrency(self, cache=True):
1✔
54
        """Return the base currency code (e.g. 'USD') from the server"""
55

56
        if not cache or not self.base_currency:
1✔
57
            self.updateFromServer()
1✔
58
        
59
        return self.base_currency
1✔
60
    
61
    def getExchangeRates(self, cache=True):
1✔
62
        """Return the exchange rate information from the server"""
63

64
        if not cache or not self.exchange_rates:
1✔
65
            self.updateFromServer()
1✔
66
        
67
        return self.exchange_rates
1✔
68

69
    def convertCurrency(self, value, source_currency, target_currency, cache=True):
1✔
70
        """Convert between currencies
71
        
72
        Arguments:
73
            value: The numerical currency value to be converted
74
            source_currency: The source currency code (e.g. 'USD')
75
            target_currency: The target currency code (e.g. 'NZD')
76
        """
77

78
        # Shortcut if the currencies are the same
79
        if source_currency == target_currency:
1✔
80
            return value
×
81
        
82
        base = self.getBaseCurrency(cache=cache)
1✔
83
        rates = self.getExchangeRates(cache=cache)
1✔
84

85
        if base is None:
1✔
86
            raise AttributeError("Base currency information is not available")
×
87
        
88
        if rates is None:
1✔
89
            raise AttributeError("Exchange rate information is not available")
×
90
        
91
        if source_currency not in rates:
1✔
92
            raise NameError(f"Source currency code '{source_currency}' not found in exchange rate data")
×
93
        
94
        if target_currency not in rates:
1✔
95
            raise NameError(f"Target currency code '{target_currency}' not found in exchange rate data")
×
96
        
97
        return value / rates[source_currency] * rates[target_currency]
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc