[fix] [exchange-rate] let user defined exchange rate take preceedence

This commit is contained in:
Rushabh Mehta 2015-10-15 11:50:38 +05:30
parent f861856ed9
commit 746fd90625

View File

@ -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