From f40fbaed3e372e8f8d8493ef2521ff5bfd5ebd7e Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Thu, 21 Dec 2023 14:22:58 +0530 Subject: [PATCH] fix: check country from ip for multicurrency --- lms/lms/utils.py | 16 +++++++++++++++- lms/overrides/user.py | 15 +-------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lms/lms/utils.py b/lms/lms/utils.py index f271811e..59d3ea57 100644 --- a/lms/lms/utils.py +++ b/lms/lms/utils.py @@ -941,9 +941,10 @@ def check_multicurrency(amount, currency, country=None, amount_usd=None): country or frappe.db.get_value("Address", {"email_id": frappe.session.user}, "country") or frappe.db.get_value("User", frappe.session.user, "country") + or get_country_code() ) - if amount_usd and country not in exception_country: + if amount_usd and country and country not in exception_country: return amount_usd, "USD" if not show_usd_equivalent or currency == "USD": @@ -1155,3 +1156,16 @@ def change_currency(amount, currency, country=None): amount = cint(amount) amount, currency = check_multicurrency(amount, currency, country) return fmt_money(amount, 0, currency) + + +def get_country_code(): + ip = frappe.local.request_ip + res = requests.get(f"http://ip-api.com/json/{ip}") + + try: + data = res.json() + if data.get("status") != "fail": + return frappe.db.get_value("Country", {"code": data.get("countryCode")}, "name") + except Exception: + pass + return diff --git a/lms/overrides/user.py b/lms/overrides/user.py index bfc1f49b..3643e473 100644 --- a/lms/overrides/user.py +++ b/lms/overrides/user.py @@ -5,7 +5,7 @@ from frappe import _ from frappe.core.doctype.user.user import User from frappe.utils import cint, escape_html, random_string from frappe.website.utils import is_signup_disabled -from lms.lms.utils import get_average_rating +from lms.lms.utils import get_average_rating, get_country_code from frappe.website.utils import cleanup_page_name from frappe.model.naming import append_number_if_name_exists from lms.widgets import Widgets @@ -260,19 +260,6 @@ def set_country_from_ip(login_manager=None, user=None): return -def get_country_code(): - ip = frappe.local.request_ip - res = requests.get(f"http://ip-api.com/json/{ip}") - - try: - data = res.json() - if data.get("status") != "fail": - return frappe.db.get_value("Country", {"code": data.get("countryCode")}, "name") - except Exception: - pass - return - - def on_session_creation(login_manager): if frappe.db.get_single_value( "System Settings", "setup_complete"