Change currency exchange rate api to fixer.io
This commit is contained in:
parent
5cce1e0929
commit
a0f2510b01
@ -66,20 +66,6 @@
|
|||||||
"label": "Disable Rounded Total",
|
"label": "Disable Rounded Total",
|
||||||
"permlevel": 0,
|
"permlevel": 0,
|
||||||
"read_only": 0
|
"read_only": 0
|
||||||
},
|
|
||||||
{
|
|
||||||
"fieldname": "section_break_8",
|
|
||||||
"fieldtype": "Section Break",
|
|
||||||
"permlevel": 0,
|
|
||||||
"precision": ""
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"description": "For automatic exchange rates go to jsonrates.com and signup for an API key",
|
|
||||||
"fieldname": "jsonrates_api_key",
|
|
||||||
"fieldtype": "Data",
|
|
||||||
"label": "jsonrates.com API Key",
|
|
||||||
"permlevel": 0,
|
|
||||||
"precision": ""
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"hide_toolbar": 0,
|
"hide_toolbar": 0,
|
||||||
@ -87,7 +73,7 @@
|
|||||||
"idx": 1,
|
"idx": 1,
|
||||||
"in_create": 1,
|
"in_create": 1,
|
||||||
"issingle": 1,
|
"issingle": 1,
|
||||||
"modified": "2015-05-07 05:43:49.760061",
|
"modified": "2015-06-30 03:00:26.420003",
|
||||||
"modified_by": "Administrator",
|
"modified_by": "Administrator",
|
||||||
"module": "Setup",
|
"module": "Setup",
|
||||||
"name": "Global Defaults",
|
"name": "Global Defaults",
|
||||||
|
|||||||
@ -17,7 +17,6 @@ keydict = {
|
|||||||
'hide_currency_symbol':'hide_currency_symbol',
|
'hide_currency_symbol':'hide_currency_symbol',
|
||||||
'account_url':'account_url',
|
'account_url':'account_url',
|
||||||
'disable_rounded_total': 'disable_rounded_total',
|
'disable_rounded_total': 'disable_rounded_total',
|
||||||
'jsonrates_api_key': 'jsonrates_api_key'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
from frappe.model.document import Document
|
from frappe.model.document import Document
|
||||||
|
|||||||
@ -60,20 +60,21 @@ def before_tests():
|
|||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def get_exchange_rate(from_currency, to_currency):
|
def get_exchange_rate(from_currency, to_currency):
|
||||||
jsonrates_api_key = frappe.conf.jsonrates_api_key or frappe.db.get_default("jsonrates_api_key")
|
try:
|
||||||
|
|
||||||
if jsonrates_api_key:
|
|
||||||
cache = frappe.cache()
|
cache = frappe.cache()
|
||||||
key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency)
|
key = "currency_exchange_rate:{0}:{1}".format(from_currency, to_currency)
|
||||||
value = cache.get(key)
|
value = cache.get(key)
|
||||||
if not value:
|
if not value:
|
||||||
import requests
|
import requests
|
||||||
response = requests.get("http://jsonrates.com/get/?from={0}&to={1}&apiKey={2}".format(from_currency,
|
response = requests.get("http://api.fixer.io/latest", params={
|
||||||
to_currency, jsonrates_api_key))
|
"base": from_currency,
|
||||||
|
"symbols": to_currency
|
||||||
|
})
|
||||||
# expire in 24 hours
|
# expire in 24 hours
|
||||||
value = response.json().get("rate")
|
response.raise_for_status()
|
||||||
|
value = response.json()["rates"][to_currency]
|
||||||
cache.setex(key, value, 24 * 60 * 60)
|
cache.setex(key, value, 24 * 60 * 60)
|
||||||
return flt(value)
|
return flt(value)
|
||||||
else:
|
except:
|
||||||
exchange = "%s-%s" % (from_currency, to_currency)
|
exchange = "%s-%s" % (from_currency, to_currency)
|
||||||
return flt(frappe.db.get_value("Currency Exchange", exchange, "exchange_rate"))
|
return flt(frappe.db.get_value("Currency Exchange", exchange, "exchange_rate"))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user