Skip to content

Commit

Permalink
refactor: clean up code to py310 supported features (#17367)
Browse files Browse the repository at this point in the history
refactor: clean up code to py39+ supported syntax

- f-strings instead of format
- latest typing support instead of pre 3.9 TitleCase
- remove UTF-8 declarations.
- many more changes

Powered by https://github.com/asottile/pyupgrade/ + manual cleanups
  • Loading branch information
ankush authored Jul 1, 2022
1 parent 088050b commit b613fd1
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018, Frappe Technologies and contributors
# License: MIT. See LICENSE

Expand Down Expand Up @@ -184,7 +183,7 @@ def validate_transaction_currency(self, currency):
)

def get_payment_url(self, **kwargs):
return get_url("./integrations/braintree_checkout?{0}".format(urlencode(kwargs)))
return get_url(f"./integrations/braintree_checkout?{urlencode(kwargs)}")

def create_payment_request(self, data):
self.data = frappe._dict(data)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018, Frappe Technologies and Contributors
# License: MIT. See LICENSE
import unittest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2015, Frappe Technologies and contributors
# License: MIT. See LICENSE

Expand Down Expand Up @@ -193,7 +192,7 @@ def execute_set_express_checkout(self, **kwargs):
params.update(
{
"METHOD": "SetExpressCheckout",
"returnUrl": get_url("{0}.get_express_checkout_details".format(api_path)),
"returnUrl": get_url(f"{api_path}.get_express_checkout_details"),
"cancelUrl": get_url("/payment-cancel"),
"PAYMENTREQUEST_0_PAYMENTACTION": "SALE",
"PAYMENTREQUEST_0_AMT": kwargs["amount"],
Expand Down Expand Up @@ -331,7 +330,7 @@ def confirm_payment(token):
).run_method("on_payment_authorized", "Completed")
frappe.db.commit()

redirect_url = "/integrations/payment-success?doctype={0}&docname={1}".format(
redirect_url = "/integrations/payment-success?doctype={}&docname={}".format(
data.get("reference_doctype"), data.get("reference_docname")
)
else:
Expand Down Expand Up @@ -404,7 +403,7 @@ def create_recurring_profile(token, payerid):
).run_method("on_payment_authorized", status_changed_to)
frappe.db.commit()

redirect_url = "/integrations/payment-success?doctype={0}&docname={1}".format(
redirect_url = "/integrations/payment-success?doctype={}&docname={}".format(
data.get("reference_doctype"), data.get("reference_docname")
)
else:
Expand All @@ -427,11 +426,9 @@ def get_redirect_uri(doc, token, payerid):
data = json.loads(doc.data)

if data.get("subscription_details") or data.get("subscription_id"):
return get_url(
"{0}.create_recurring_profile?token={1}&payerid={2}".format(api_path, token, payerid)
)
return get_url(f"{api_path}.create_recurring_profile?token={token}&payerid={payerid}")
else:
return get_url("{0}.confirm_payment?token={1}".format(api_path, token))
return get_url(f"{api_path}.confirm_payment?token={token}")


def manage_recurring_payment_profile_status(profile_id, action, args, url):
Expand Down Expand Up @@ -474,7 +471,7 @@ def ipn_handler():
queue="long",
timeout=600,
is_async=True,
**{"doctype": "Integration Request", "docname": doc.name}
**{"doctype": "Integration Request", "docname": doc.name},
)

except frappe.InvalidStatusError:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020, Frappe Technologies and contributors
# License: MIT. See LICENSE

Expand Down Expand Up @@ -37,7 +36,7 @@ def get_payment_url(self, **kwargs):
integration_request = create_request_log(kwargs, service_name="Paytm")
kwargs.update(dict(order_id=integration_request.name))

return get_url("./integrations/paytm_checkout?{0}".format(urlencode(kwargs)))
return get_url(f"./integrations/paytm_checkout?{urlencode(kwargs)}")


def get_paytm_config():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2020, Frappe Technologies and Contributors
# License: MIT. See LICENSE
# import frappe
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2015, Frappe Technologies and contributors
# License: MIT. See LICENSE

Expand Down Expand Up @@ -124,9 +123,7 @@ def setup_addon(self, settings, **kwargs):
"quantity": 1 (The total amount is calculated as item.amount * quantity)
}
"""
url = "https://api.razorpay.com/v1/subscriptions/{0}/addons".format(
kwargs.get("subscription_id")
)
url = "https://api.razorpay.com/v1/subscriptions/{}/addons".format(kwargs.get("subscription_id"))

try:
if not frappe.conf.converted_rupee_to_paisa:
Expand Down Expand Up @@ -195,7 +192,7 @@ def prepare_subscription_details(self, settings, **kwargs):

def get_payment_url(self, **kwargs):
integration_request = create_request_log(kwargs, service_name="Razorpay")
return get_url("./integrations/razorpay_checkout?token={0}".format(integration_request.name))
return get_url(f"./integrations/razorpay_checkout?token={integration_request.name}")

def create_order(self, **kwargs):
# Creating Orders https://razorpay.com/docs/api/orders/
Expand Down Expand Up @@ -257,7 +254,7 @@ def authorize_payment(self):

try:
resp = make_get_request(
"https://api.razorpay.com/v1/payments/{0}".format(self.data.razorpay_payment_id),
f"https://api.razorpay.com/v1/payments/{self.data.razorpay_payment_id}",
auth=(settings.api_key, settings.api_secret),
)

Expand Down Expand Up @@ -303,7 +300,7 @@ def authorize_payment(self):
if custom_redirect_to:
redirect_to = custom_redirect_to

redirect_url = "payment-success?doctype={0}&docname={1}".format(
redirect_url = "payment-success?doctype={}&docname={}".format(
self.data.reference_doctype, self.data.reference_docname
)
else:
Expand Down Expand Up @@ -339,7 +336,7 @@ def cancel_subscription(self, subscription_id):

try:
resp = make_post_request(
"https://api.razorpay.com/v1/subscriptions/{0}/cancel".format(subscription_id),
f"https://api.razorpay.com/v1/subscriptions/{subscription_id}/cancel",
auth=(settings.api_key, settings.api_secret),
)
except Exception:
Expand Down Expand Up @@ -383,14 +380,14 @@ def capture_payment(is_sandbox=False, sanbox_response=None):
settings = controller.get_settings(data)

resp = make_get_request(
"https://api.razorpay.com/v1/payments/{0}".format(data.get("razorpay_payment_id")),
"https://api.razorpay.com/v1/payments/{}".format(data.get("razorpay_payment_id")),
auth=(settings.api_key, settings.api_secret),
data={"amount": data.get("amount")},
)

if resp.get("status") == "authorized":
resp = make_post_request(
"https://api.razorpay.com/v1/payments/{0}/capture".format(data.get("razorpay_payment_id")),
"https://api.razorpay.com/v1/payments/{}/capture".format(data.get("razorpay_payment_id")),
auth=(settings.api_key, settings.api_secret),
data={"amount": data.get("amount")},
)
Expand All @@ -403,7 +400,7 @@ def capture_payment(is_sandbox=False, sanbox_response=None):
doc.status = "Failed"
doc.error = frappe.get_traceback()
doc.save()
frappe.log_error(doc.error, "{0} Failed".format(doc.name))
frappe.log_error(doc.error, f"{doc.name} Failed")


@frappe.whitelist(allow_guest=True)
Expand Down Expand Up @@ -498,7 +495,7 @@ def razorpay_subscription_callback():
queue="long",
timeout=600,
is_async=True,
**{"doctype": "Integration Request", "docname": doc.name}
**{"doctype": "Integration Request", "docname": doc.name},
)

except frappe.InvalidStatusError:
Expand All @@ -521,7 +518,7 @@ def _throw():
settings = controller.get_settings(data)

resp = make_get_request(
"https://api.razorpay.com/v1/subscriptions/{0}".format(subscription_id),
f"https://api.razorpay.com/v1/subscriptions/{subscription_id}",
auth=(settings.api_key, settings.api_secret),
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2017, Frappe Technologies and contributors
# License: MIT. See LICENSE

Expand Down Expand Up @@ -163,7 +162,7 @@ def on_update(self):
def validate_stripe_credentails(self):
if self.publishable_key and self.secret_key:
header = {
"Authorization": "Bearer {0}".format(
"Authorization": "Bearer {}".format(
self.get_password(fieldname="secret_key", raise_exception=False)
)
}
Expand All @@ -190,7 +189,7 @@ def validate_minimum_transaction_amount(self, currency, amount):
)

def get_payment_url(self, **kwargs):
return get_url("./integrations/stripe_checkout?{0}".format(urlencode(kwargs)))
return get_url(f"./integrations/stripe_checkout?{urlencode(kwargs)}")

def create_request(self, data):
import stripe
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2018, Frappe Technologies and Contributors
# License: MIT. See LICENSE
import unittest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and contributors
# License: MIT. See LICENSE

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
import unittest
Expand Down

0 comments on commit b613fd1

Please sign in to comment.