Skip to content

Commit

Permalink
refactor: add import guard for erpnext
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Sep 27, 2023
1 parent 9fa9025 commit 3307d25
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from payments.payment_gateways.doctype.mpesa_settings.mpesa_custom_fields import (
create_custom_pos_fields,
)
from payments.utils import erpnext_app_import_guard


class MpesaSettings(Document):
Expand Down Expand Up @@ -354,7 +355,8 @@ def fetch_param_value(response, key, key_field):


def create_mode_of_payment(gateway, payment_type="General"):
from erpnext import get_default_company
with erpnext_app_import_guard():
from erpnext import get_default_company

payment_gateway_account = frappe.db.get_value(
"Payment Gateway Account", {"payment_gateway": gateway}, ["payment_account"]
Expand Down
1 change: 1 addition & 0 deletions payments/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
delete_custom_fields,
get_payment_gateway_controller,
make_custom_fields,
erpnext_app_import_guard,
)
14 changes: 14 additions & 0 deletions payments/utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click
import frappe
from frappe import _
from contextlib import contextmanager
from frappe.custom.doctype.custom_field.custom_field import create_custom_fields


Expand Down Expand Up @@ -188,3 +189,16 @@ def before_install():
# a lot of apis don;t exist in v10 and this is a (at the moment) required app for erpnext.
if not frappe.get_meta("Module Def").has_field("custom"):
return False


@contextmanager
def erpnext_app_import_guard():
marketplace_link = '<a href="https://frappecloud.com/marketplace/apps/erpnext">Marketplace</a>'
github_link = '<a href="https://github.com/frappe/erpnext">GitHub</a>'
msg = _("erpnext app is not installed. Please install it from {} or {}").format(
marketplace_link, github_link
)
try:
yield
except ImportError:
frappe.throw(msg, title=_("Missing ERPNext App"))

0 comments on commit 3307d25

Please sign in to comment.