chore: code clean up
This commit is contained in:
parent
d56c8d44a6
commit
a86d9c91d0
@ -7,6 +7,7 @@
|
||||
"field_order": [
|
||||
"api_details_section",
|
||||
"api_endpoint",
|
||||
"url",
|
||||
"column_break_3",
|
||||
"result_key",
|
||||
"section_break_2",
|
||||
@ -59,12 +60,18 @@
|
||||
"label": "Result Key",
|
||||
"options": "Currency Exchange Settings Result",
|
||||
"reqd": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "url",
|
||||
"fieldtype": "Data",
|
||||
"label": "URL",
|
||||
"read_only": 1
|
||||
}
|
||||
],
|
||||
"index_web_pages_for_search": 1,
|
||||
"issingle": 1,
|
||||
"links": [],
|
||||
"modified": "2021-09-03 13:21:16.397695",
|
||||
"modified": "2021-09-03 19:09:02.741016",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Setup",
|
||||
"name": "Currency Exchange Settings",
|
||||
|
@ -12,7 +12,7 @@ class CurrencyExchangeSettings(Document):
|
||||
transaction_date = '2021-08-01'
|
||||
from_currency = 'USD'
|
||||
to_currency = 'INR'
|
||||
req_params={
|
||||
req_params = {
|
||||
"transaction_date": transaction_date,
|
||||
"from_currency": from_currency,
|
||||
"to_currency": to_currency
|
||||
@ -25,7 +25,11 @@ class CurrencyExchangeSettings(Document):
|
||||
except:
|
||||
frappe.throw(_("Make sure no mandatory parameters are repeated."))
|
||||
for eparam in self.extra_params:
|
||||
params[eparam.key] = eparam.value
|
||||
params[eparam.key] = eparam.value.format(
|
||||
transaction_date=transaction_date,
|
||||
to_currency=to_currency,
|
||||
from_currency=from_currency
|
||||
)
|
||||
import requests
|
||||
api_url = self.api_endpoint.format(
|
||||
transaction_date=transaction_date,
|
||||
@ -46,7 +50,8 @@ class CurrencyExchangeSettings(Document):
|
||||
from_currency=from_currency
|
||||
)]
|
||||
except KeyError:
|
||||
frappe.throw(_("Invalid result key. Response: ") + response.text)
|
||||
frappe.throw("Invalid result key. Response: " + response.text)
|
||||
if not isinstance(value, (int, float)):
|
||||
frappe.throw(_("Returned exchange rate is neither integer not float."))
|
||||
frappe.msgprint(_("Exchange rate of USD to INR on 01-08-2021 is ") + str(value))
|
||||
self.url = response.url
|
||||
frappe.msgprint("Exchange rate of USD to INR on 01-08-2021 is " + str(value))
|
||||
|
@ -14,7 +14,8 @@
|
||||
"fieldtype": "Data",
|
||||
"in_list_view": 1,
|
||||
"label": "Key",
|
||||
"reqd": 1
|
||||
"reqd": 1,
|
||||
"unique": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "value",
|
||||
@ -28,7 +29,7 @@
|
||||
"index_web_pages_for_search": 1,
|
||||
"istable": 1,
|
||||
"links": [],
|
||||
"modified": "2021-09-02 15:24:24.675019",
|
||||
"modified": "2021-09-03 18:50:47.145457",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Setup",
|
||||
"name": "Currency Exchange Settings Details",
|
||||
|
@ -14,7 +14,8 @@
|
||||
"fieldtype": "Data",
|
||||
"in_list_view": 1,
|
||||
"label": "Key",
|
||||
"reqd": 1
|
||||
"reqd": 1,
|
||||
"unique": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "value",
|
||||
@ -27,7 +28,7 @@
|
||||
"index_web_pages_for_search": 1,
|
||||
"istable": 1,
|
||||
"links": [],
|
||||
"modified": "2021-09-02 15:18:17.888667",
|
||||
"modified": "2021-09-03 18:50:28.482851",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Setup",
|
||||
"name": "Currency Exchange Settings Extra Details",
|
||||
|
@ -101,7 +101,7 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No
|
||||
if not value:
|
||||
import requests
|
||||
settings = frappe.get_single('Currency Exchange Settings')
|
||||
req_params={
|
||||
req_params = {
|
||||
"transaction_date": transaction_date,
|
||||
"from_currency": from_currency,
|
||||
"to_currency": to_currency
|
||||
@ -110,21 +110,13 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No
|
||||
for row in settings.req_params:
|
||||
params[row.key] = req_params[row.value]
|
||||
for eparam in settings.extra_params:
|
||||
params[eparam.key] = eparam.value
|
||||
response = requests.get(settings.api_endpoint.format(
|
||||
transaction_date=transaction_date,
|
||||
to_currency=to_currency,
|
||||
from_currency=from_currency
|
||||
), params=params)
|
||||
params[eparam.key] = format_ces_api(eparam.value, req_params)
|
||||
response = requests.get(format_ces_api(settings.api_endpoint, req_params), params=params)
|
||||
# expire in 6 hours
|
||||
response.raise_for_status()
|
||||
value = response.json()
|
||||
for res_key in settings.result_key:
|
||||
value = value[str(res_key.key).format(
|
||||
transaction_date=transaction_date,
|
||||
to_currency=to_currency,
|
||||
from_currency=from_currency
|
||||
)]
|
||||
value = value[format_ces_api(str(res_key.key), req_params)]
|
||||
cache.setex(name=key, time=21600, value=flt(value))
|
||||
return flt(value)
|
||||
except Exception:
|
||||
@ -132,6 +124,13 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No
|
||||
frappe.msgprint(_("Unable to find exchange rate for {0} to {1} for key date {2}. Please create a Currency Exchange record manually").format(from_currency, to_currency, transaction_date))
|
||||
return 0.0
|
||||
|
||||
def format_ces_api(data="", param={}):
|
||||
return data.format(
|
||||
transaction_date=param["transaction_date"],
|
||||
to_currency=param["to_currency"],
|
||||
from_currency=param["from_currency"]
|
||||
)
|
||||
|
||||
def enable_all_roles_and_domains():
|
||||
""" enable all roles and domain for testing """
|
||||
# add all roles to users
|
||||
|
Loading…
x
Reference in New Issue
Block a user