Skip to content

Commit

Permalink
fix: handle empty connection
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabh6790 committed May 7, 2024
1 parent 7c145e0 commit 06e5e25
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 9 deletions.
5 changes: 4 additions & 1 deletion press/api/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,10 @@ def update_partnership_date(team, partnership_date):

@frappe.whitelist()
def get_partner_details(partner_email):
from press.utils.billing import get_frappe_io_connection
from press.utils.billing import get_frappe_io_connection, disabled_frappeio_auth

if disabled_frappeio_auth():
return frappe.dict()

client = get_frappe_io_connection()
data = client.get_doc(
Expand Down
5 changes: 4 additions & 1 deletion press/api/marketplace.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
get_plans_for_app,
)
from press.utils import get_app_tag, get_current_team, get_last_doc, unique
from press.utils.billing import get_frappe_io_connection
from press.utils.billing import get_frappe_io_connection, disabled_frappeio_auth


@frappe.whitelist()
Expand Down Expand Up @@ -972,6 +972,9 @@ def get_discount_percent(plan, discount=0.0):
"Bronze": 30.0,
}

if disabled_frappeio_auth():
return discount

if team.erpnext_partner and frappe.get_value(
"Marketplace App Plan", plan, "partner_discount"
):
Expand Down
15 changes: 14 additions & 1 deletion press/press/doctype/invoice/invoice.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
from frappe.model.document import Document

from press.overrides import get_permission_query_conditions_for_doctype
from press.utils.billing import get_frappe_io_connection, convert_stripe_money
from press.utils.billing import (
get_frappe_io_connection,
convert_stripe_money,
disabled_frappeio_auth,
)


class InvoiceDiscountType(Enum):
Expand Down Expand Up @@ -651,6 +655,9 @@ def on_cancel(self):
doc.submit()

def apply_partner_credits(self):
if disabled_frappeio_auth():
return

client = self.get_frappeio_connection()
response = client.session.post(
f"{client.url}/api/method/consume_credits_against_fc_invoice",
Expand Down Expand Up @@ -789,6 +796,9 @@ def create_invoice_on_frappeio(self):
return

try:
if disabled_frappeio_auth():
return

team = frappe.get_doc("Team", self.team)
address = (
frappe.get_doc("Address", team.billing_address) if team.billing_address else None
Expand Down Expand Up @@ -840,6 +850,9 @@ def fetch_invoice_pdf(self):
if self.frappe_invoice:
from urllib.parse import urlencode

if disabled_frappeio_auth():
return

client = self.get_frappeio_connection()
print_format = frappe.db.get_single_value("Press Settings", "print_format")
params = urlencode(
Expand Down
11 changes: 9 additions & 2 deletions press/press/doctype/team/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from press.utils.billing import (
get_erpnext_com_connection,
get_frappe_io_connection,
disabled_frappeio_auth,
get_stripe,
process_micro_debit_test_charge,
)
Expand Down Expand Up @@ -447,7 +448,7 @@ def create_partner_referral_code(self):
self.save(ignore_permissions=True)

def get_partnership_start_date(self):
if frappe.flags.in_test:
if frappe.flags.in_test or disabled_frappeio_auth():
return frappe.utils.getdate()

client = get_frappe_io_connection()
Expand Down Expand Up @@ -611,7 +612,7 @@ def update_billing_details_on_draft_invoices(self):
frappe.get_doc("Invoice", draft_invoice).save()

def update_billing_details_on_frappeio(self):
if frappe.flags.in_install:
if frappe.flags.in_install or disabled_frappeio_auth():
return

try:
Expand Down Expand Up @@ -805,6 +806,9 @@ def get_balance(self):

@frappe.whitelist()
def get_available_partner_credits(self):
if disabled_frappeio_auth():
return ""

client = get_frappe_io_connection()
response = client.session.post(
f"{client.url}/api/method/partner_relationship_management.api.get_partner_credit_balance",
Expand Down Expand Up @@ -940,6 +944,9 @@ def billing_details(self, timezone=None):

def get_partner_level(self):
# fetch partner level from frappe.io
if disabled_frappeio_auth():
return "", ""

client = get_frappe_io_connection()
response = client.session.get(
f"{client.url}/api/method/get_partner_level",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ def delete_stripe_customer(self):
@handle_exc
def delete_data_on_frappeio(self):
"""Anonymize data on frappe.io"""
from press.utils.billing import get_frappe_io_connection
from press.utils.billing import get_frappe_io_connection, disabled_frappeio_auth

if disabled_frappeio_auth():
return

client = get_frappe_io_connection()
response = client.session.delete(
Expand Down
8 changes: 5 additions & 3 deletions press/utils/billing.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ def get_erpnext_com_connection():
)


def disabled_frappeio_auth():
return frappe.db.get_single_value("Press Settings", "disable_frappe_auth", cache=True)


def get_frappe_io_connection():
if hasattr(frappe.local, "press_frappeio_conn"):
return frappe.local.press_frappeio_conn
Expand All @@ -89,9 +93,7 @@ def get_frappe_io_connection():
"frappeio_api_secret", raise_exception=False
)

if not press_settings.disable_frappe_auth and not (
frappe_api_key and frappe_api_secret and press_settings.frappe_url
):
if not (frappe_api_key and frappe_api_secret and press_settings.frappe_url):
frappe.throw("Frappe.io URL not set up in Press Settings", exc=FrappeioServerNotSet)

frappe.local.press_frappeio_conn = FrappeClient(
Expand Down

0 comments on commit 06e5e25

Please sign in to comment.