mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-11-03 22:55:43 +00:00 
			
		
		
		
	Merge pull request #2604 from SchrodingersGat/currency-timeout
Handle errors when connecting to currency exchange
This commit is contained in:
		@@ -100,7 +100,7 @@ class InvenTreeConfig(AppConfig):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        try:
 | 
					        try:
 | 
				
			||||||
            from djmoney.contrib.exchange.models import ExchangeBackend
 | 
					            from djmoney.contrib.exchange.models import ExchangeBackend
 | 
				
			||||||
            from datetime import datetime, timedelta
 | 
					
 | 
				
			||||||
            from InvenTree.tasks import update_exchange_rates
 | 
					            from InvenTree.tasks import update_exchange_rates
 | 
				
			||||||
            from common.settings import currency_code_default
 | 
					            from common.settings import currency_code_default
 | 
				
			||||||
        except AppRegistryNotReady:
 | 
					        except AppRegistryNotReady:
 | 
				
			||||||
@@ -115,23 +115,18 @@ class InvenTreeConfig(AppConfig):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            last_update = backend.last_update
 | 
					            last_update = backend.last_update
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            if last_update is not None:
 | 
					            if last_update is None:
 | 
				
			||||||
                delta = datetime.now().date() - last_update.date()
 | 
					 | 
				
			||||||
                if delta > timedelta(days=1):
 | 
					 | 
				
			||||||
                    print(f"Last update was {last_update}")
 | 
					 | 
				
			||||||
                    update = True
 | 
					 | 
				
			||||||
            else:
 | 
					 | 
				
			||||||
                # Never been updated
 | 
					                # Never been updated
 | 
				
			||||||
                print("Exchange backend has never been updated")
 | 
					                logger.info("Exchange backend has never been updated")
 | 
				
			||||||
                update = True
 | 
					                update = True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # Backend currency has changed?
 | 
					            # Backend currency has changed?
 | 
				
			||||||
            if not base_currency == backend.base_currency:
 | 
					            if not base_currency == backend.base_currency:
 | 
				
			||||||
                print(f"Base currency changed from {backend.base_currency} to {base_currency}")
 | 
					                logger.info(f"Base currency changed from {backend.base_currency} to {base_currency}")
 | 
				
			||||||
                update = True
 | 
					                update = True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        except (ExchangeBackend.DoesNotExist):
 | 
					        except (ExchangeBackend.DoesNotExist):
 | 
				
			||||||
            print("Exchange backend not found - updating")
 | 
					            logger.info("Exchange backend not found - updating")
 | 
				
			||||||
            update = True
 | 
					            update = True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        except:
 | 
					        except:
 | 
				
			||||||
@@ -139,4 +134,7 @@ class InvenTreeConfig(AppConfig):
 | 
				
			|||||||
            return
 | 
					            return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if update:
 | 
					        if update:
 | 
				
			||||||
            update_exchange_rates()
 | 
					            try:
 | 
				
			||||||
 | 
					                update_exchange_rates()
 | 
				
			||||||
 | 
					            except Exception as e:
 | 
				
			||||||
 | 
					                logger.error(f"Error updating exchange rates: {e}")
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,3 +1,7 @@
 | 
				
			|||||||
 | 
					import certifi
 | 
				
			||||||
 | 
					import ssl
 | 
				
			||||||
 | 
					from urllib.request import urlopen
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from common.settings import currency_code_default, currency_codes
 | 
					from common.settings import currency_code_default, currency_codes
 | 
				
			||||||
from urllib.error import URLError
 | 
					from urllib.error import URLError
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -24,6 +28,22 @@ class InvenTreeExchange(SimpleExchangeBackend):
 | 
				
			|||||||
        return {
 | 
					        return {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def get_response(self, **kwargs):
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					        Custom code to get response from server.
 | 
				
			||||||
 | 
					        Note: Adds a 5-second timeout
 | 
				
			||||||
 | 
					        """
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        url = self.get_url(**kwargs)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try:
 | 
				
			||||||
 | 
					            context = ssl.create_default_context(cafile=certifi.where())
 | 
				
			||||||
 | 
					            response = urlopen(url, timeout=5, context=context)
 | 
				
			||||||
 | 
					            return response.read()
 | 
				
			||||||
 | 
					        except:
 | 
				
			||||||
 | 
					            # Returning None here will raise an error upstream
 | 
				
			||||||
 | 
					            return None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def update_rates(self, base_currency=currency_code_default()):
 | 
					    def update_rates(self, base_currency=currency_code_default()):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        symbols = ','.join(currency_codes())
 | 
					        symbols = ','.join(currency_codes())
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -269,10 +269,13 @@ def update_exchange_rates():
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    logger.info(f"Using base currency '{base}'")
 | 
					    logger.info(f"Using base currency '{base}'")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    backend.update_rates(base_currency=base)
 | 
					    try:
 | 
				
			||||||
 | 
					        backend.update_rates(base_currency=base)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Remove any exchange rates which are not in the provided currencies
 | 
					        # Remove any exchange rates which are not in the provided currencies
 | 
				
			||||||
    Rate.objects.filter(backend="InvenTreeExchange").exclude(currency__in=currency_codes()).delete()
 | 
					        Rate.objects.filter(backend="InvenTreeExchange").exclude(currency__in=currency_codes()).delete()
 | 
				
			||||||
 | 
					    except Exception as e:
 | 
				
			||||||
 | 
					        logger.error(f"Error updating exchange rates: {e}")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def send_email(subject, body, recipients, from_email=None, html_message=None):
 | 
					def send_email(subject, body, recipients, from_email=None, html_message=None):
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user