From 746fd9062524a644b3f8735a72b3e076d5dbec3c Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Thu, 15 Oct 2015 11:50:38 +0530 Subject: [PATCH] [fix] [exchange-rate] let user defined exchange rate take preceedence --- erpnext/setup/utils.py | 42 ++++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py index 5480613b4e..8b84cc2cc7 100644 --- a/erpnext/setup/utils.py +++ b/erpnext/setup/utils.py @@ -63,21 +63,27 @@ def before_tests(): @frappe.whitelist() def get_exchange_rate(from_currency, to_currency): - try: - cache = frappe.cache() - key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency) - value = cache.get(key) - if not value: - import requests - response = requests.get("http://api.fixer.io/latest", params={ - "base": from_currency, - "symbols": to_currency - }) - # expire in 24 hours - response.raise_for_status() - value = response.json()["rates"][to_currency] - cache.setex(key, value, 24 * 60 * 60) - return flt(value) - except: - exchange = "%s-%s" % (from_currency, to_currency) - return flt(frappe.db.get_value("Currency Exchange", exchange, "exchange_rate")) + exchange = "%s-%s" % (from_currency, to_currency) + value = flt(frappe.db.get_value("Currency Exchange", exchange, "exchange_rate")) + + if not value: + try: + cache = frappe.cache() + key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency) + value = cache.get(key) + if not value: + import requests + response = requests.get("http://api.fixer.io/latest", params={ + "base": from_currency, + "symbols": to_currency + }) + # expire in 24 hours + response.raise_for_status() + value = response.json()["rates"][to_currency] + cache.setex(key, value, 24 * 60 * 60) + return flt(value) + except: + frappe.msgprint(_("Unable to find exchange rate")) + return 0.0 + else: + return 0.0