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