[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() @frappe.whitelist()
def get_exchange_rate(from_currency, to_currency): def get_exchange_rate(from_currency, to_currency):
try: exchange = "%s-%s" % (from_currency, to_currency)
cache = frappe.cache() value = flt(frappe.db.get_value("Currency Exchange", exchange, "exchange_rate"))
key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency)
value = cache.get(key) if not value:
if not value: try:
import requests cache = frappe.cache()
response = requests.get("http://api.fixer.io/latest", params={ key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency)
"base": from_currency, value = cache.get(key)
"symbols": to_currency if not value:
}) import requests
# expire in 24 hours response = requests.get("http://api.fixer.io/latest", params={
response.raise_for_status() "base": from_currency,
value = response.json()["rates"][to_currency] "symbols": to_currency
cache.setex(key, value, 24 * 60 * 60) })
return flt(value) # expire in 24 hours
except: response.raise_for_status()
exchange = "%s-%s" % (from_currency, to_currency) value = response.json()["rates"][to_currency]
return flt(frappe.db.get_value("Currency Exchange", exchange, "exchange_rate")) 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