From 3545575c8b4be38d1c959572691525463f4383fa Mon Sep 17 00:00:00 2001 From: Rushabh Mehta Date: Thu, 7 May 2015 15:16:28 +0530 Subject: [PATCH] [jsonrates api] add from global defaults --- .../doctype/global_defaults/global_defaults.json | 16 +++++++++++++++- .../doctype/global_defaults/global_defaults.py | 1 + erpnext/setup/utils.py | 6 ++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/erpnext/setup/doctype/global_defaults/global_defaults.json b/erpnext/setup/doctype/global_defaults/global_defaults.json index 1216442894..777ae3b78b 100644 --- a/erpnext/setup/doctype/global_defaults/global_defaults.json +++ b/erpnext/setup/doctype/global_defaults/global_defaults.json @@ -66,6 +66,20 @@ "label": "Disable Rounded Total", "permlevel": 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, @@ -73,7 +87,7 @@ "idx": 1, "in_create": 1, "issingle": 1, - "modified": "2015-03-02 02:23:20.688406", + "modified": "2015-05-07 05:43:49.760061", "modified_by": "Administrator", "module": "Setup", "name": "Global Defaults", diff --git a/erpnext/setup/doctype/global_defaults/global_defaults.py b/erpnext/setup/doctype/global_defaults/global_defaults.py index efdc875249..a3fa3a9c5f 100644 --- a/erpnext/setup/doctype/global_defaults/global_defaults.py +++ b/erpnext/setup/doctype/global_defaults/global_defaults.py @@ -17,6 +17,7 @@ keydict = { 'hide_currency_symbol':'hide_currency_symbol', 'account_url':'account_url', 'disable_rounded_total': 'disable_rounded_total', + 'jsonrates_api_key': 'jsonrates_api_key' } from frappe.model.document import Document diff --git a/erpnext/setup/utils.py b/erpnext/setup/utils.py index 2923f67a63..cef60799a1 100644 --- a/erpnext/setup/utils.py +++ b/erpnext/setup/utils.py @@ -60,14 +60,16 @@ def before_tests(): @frappe.whitelist() def get_exchange_rate(from_currency, to_currency): - if frappe.conf.jsonrates_api_key: + jsonrates_api_key = frappe.conf.jsonrates_api_key or frappe.db.get_default("jsonrates_api_key") + + if jsonrates_api_key: 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://jsonrates.com/get/?from={0}&to={1}&apiKey={2}".format(from_currency, - to_currency, frappe.conf.jsonrates_api_key)) + to_currency, jsonrates_api_key)) # expire in 24 hours value = response.json().get("rate") cache.setex(key, value, 24 * 60 * 60)