chore: clean up layout and code

This commit is contained in:
rtdany10 2021-09-04 14:04:56 +05:30
parent a86d9c91d0
commit 227466c365
8 changed files with 29 additions and 89 deletions

View File

@ -9,11 +9,11 @@
"api_endpoint",
"url",
"column_break_3",
"result_key",
"help",
"section_break_2",
"req_params",
"column_break_4",
"extra_params"
"result_key"
],
"fields": [
{
@ -35,16 +35,10 @@
{
"fieldname": "req_params",
"fieldtype": "Table",
"label": "Mandatory Parameters",
"label": "Parameters",
"options": "Currency Exchange Settings Details",
"reqd": 1
},
{
"fieldname": "extra_params",
"fieldtype": "Table",
"label": "Additional Parameters",
"options": "Currency Exchange Settings Extra Details"
},
{
"fieldname": "api_details_section",
"fieldtype": "Section Break",
@ -64,14 +58,20 @@
{
"fieldname": "url",
"fieldtype": "Data",
"label": "URL",
"label": "Example URL",
"read_only": 1
},
{
"fieldname": "help",
"fieldtype": "HTML",
"label": "Help",
"options": "<h3>Currency Exchange Settings Help</h3>\n<p>There are 3 variables that could be used within the endpoint, result key and in values of the parameter.</p>\n<p>Exchange rate between {from_currency} and {to_currency} on {transaction_date} is fetched by the API.</p>\n<p>Example: If your endpoint is exchange.com/2021-08-01, then, you will have to input exchange.com/{transaction_date}</p>"
}
],
"index_web_pages_for_search": 1,
"issingle": 1,
"links": [],
"modified": "2021-09-03 19:09:02.741016",
"modified": "2021-09-04 11:41:34.375637",
"modified_by": "Administrator",
"module": "Setup",
"name": "Currency Exchange Settings",

View File

@ -3,29 +3,17 @@
import frappe
from frappe import _
from frappe.utils import nowdate
from frappe.model.document import Document
class CurrencyExchangeSettings(Document):
def validate(self):
if len(self.req_params) > 3:
frappe.throw(_("Make sure no mandatory parameters are repeated."))
transaction_date = '2021-08-01'
transaction_date = nowdate()
from_currency = 'USD'
to_currency = 'INR'
req_params = {
"transaction_date": transaction_date,
"from_currency": from_currency,
"to_currency": to_currency
}
params = {}
for row in self.req_params:
try:
params[row.key] = req_params[row.value]
req_params.pop(row.value)
except:
frappe.throw(_("Make sure no mandatory parameters are repeated."))
for eparam in self.extra_params:
params[eparam.key] = eparam.value.format(
params[row.key] = row.value.format(
transaction_date=transaction_date,
to_currency=to_currency,
from_currency=from_currency
@ -54,4 +42,4 @@ class CurrencyExchangeSettings(Document):
if not isinstance(value, (int, float)):
frappe.throw(_("Returned exchange rate is neither integer not float."))
self.url = response.url
frappe.msgprint("Exchange rate of USD to INR on 01-08-2021 is " + str(value))
frappe.msgprint("Exchange rate of USD to INR is " + str(value))

View File

@ -19,17 +19,16 @@
},
{
"fieldname": "value",
"fieldtype": "Select",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Value",
"options": "\ntransaction_date\nfrom_currency\nto_currency",
"reqd": 1
}
],
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2021-09-03 18:50:47.145457",
"modified": "2021-09-04 11:24:21.944002",
"modified_by": "Administrator",
"module": "Setup",
"name": "Currency Exchange Settings Details",

View File

@ -1,40 +0,0 @@
{
"actions": [],
"creation": "2021-09-02 15:18:17.888667",
"doctype": "DocType",
"editable_grid": 1,
"engine": "InnoDB",
"field_order": [
"key",
"value"
],
"fields": [
{
"fieldname": "key",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Key",
"reqd": 1,
"unique": 1
},
{
"fieldname": "value",
"fieldtype": "Data",
"in_list_view": 1,
"label": "Value",
"reqd": 1
}
],
"index_web_pages_for_search": 1,
"istable": 1,
"links": [],
"modified": "2021-09-03 18:50:28.482851",
"modified_by": "Administrator",
"module": "Setup",
"name": "Currency Exchange Settings Extra Details",
"owner": "Administrator",
"permissions": [],
"sort_field": "modified",
"sort_order": "DESC",
"track_changes": 1
}

View File

@ -1,8 +0,0 @@
# Copyright (c) 2021, Wahni Green Technologies Pvt. Ltd. and contributors
# For license information, please see license.txt
# import frappe
from frappe.model.document import Document
class CurrencyExchangeSettingsExtraDetails(Document):
pass

View File

@ -59,15 +59,18 @@ def set_single_defaults():
pass
except frappe.ValidationError:
pass
ces = frappe.get_single('Currency Exchange Settings')
ces.api_endpoint = "https://api.exchangerate.host/convert"
ces.append('result_key', {'key': 'result'})
ces.append('req_params', {'key': 'date', 'value': 'transaction_date'})
ces.append('req_params', {'key': 'from', 'value': 'from_currency'})
ces.append('req_params', {'key': 'to', 'value': 'to_currency'})
ces.save()
frappe.db.set_default("date_format", "dd-mm-yyyy")
frappe.db.set_default("date_format", "dd-mm-yyyy")
ces = frappe.get_single('Currency Exchange Settings')
try:
ces.api_endpoint = "https://api.exchangerate.host/convert"
ces.append('result_key', {'key': 'result'})
ces.append('req_params', {'key': 'date', 'value': '{transaction_date}'})
ces.append('req_params', {'key': 'from', 'value': '{from_currency}'})
ces.append('req_params', {'key': 'to', 'value': '{to_currency}'})
ces.save()
except frappe.ValidationError:
pass
def create_compact_item_print_custom_field():
create_custom_field('Print Settings', {

View File

@ -108,9 +108,7 @@ def get_exchange_rate(from_currency, to_currency, transaction_date=None, args=No
}
params = {}
for row in settings.req_params:
params[row.key] = req_params[row.value]
for eparam in settings.extra_params:
params[eparam.key] = format_ces_api(eparam.value, req_params)
params[row.key] = format_ces_api(row.value, req_params)
response = requests.get(format_ces_api(settings.api_endpoint, req_params), params=params)
# expire in 6 hours
response.raise_for_status()