Skip to content

Commit 8a8b17d

Browse files
committed
#1480 black format
1 parent 972be90 commit 8a8b17d

5 files changed

Lines changed: 97 additions & 30 deletions

File tree

subscribie/blueprints/admin/__init__.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,13 +1399,23 @@ def _generate_account_link(account_id):
13991399
parameter on their account to see if they've completed the onboarding process.
14001400
"""
14011401
# Stripe requires HTTPS URLs when in live mode
1402-
url_scheme = 'https' if stripe_livemode() else None
1402+
url_scheme = "https" if stripe_livemode() else None
14031403

14041404
account_link = stripe.AccountLink.create(
14051405
type="account_onboarding",
14061406
account=account_id,
1407-
refresh_url=url_for("admin.stripe_connect", refresh="refresh", _external=True, _scheme=url_scheme),
1408-
return_url=url_for("admin.stripe_connect", success="success", _external=True, _scheme=url_scheme),
1407+
refresh_url=url_for(
1408+
"admin.stripe_connect",
1409+
refresh="refresh",
1410+
_external=True,
1411+
_scheme=url_scheme,
1412+
),
1413+
return_url=url_for(
1414+
"admin.stripe_connect",
1415+
success="success",
1416+
_external=True,
1417+
_scheme=url_scheme,
1418+
),
14091419
)
14101420
return account_link.url
14111421

@@ -2405,7 +2415,9 @@ def do_backfill_data(days, backfill_types, app):
24052415
log.info(">>> Completed backfilling transactions")
24062416
sys.stdout.flush()
24072417
except Exception as e:
2408-
error_msg = f"Error backfilling transactions: {e}\n{traceback.format_exc()}"
2418+
error_msg = (
2419+
f"Error backfilling transactions: {e}\n{traceback.format_exc()}"
2420+
)
24092421
log.error(error_msg)
24102422
sys.stdout.flush()
24112423
errors.append(f"transactions: {str(e)}")
@@ -2433,7 +2445,9 @@ def do_backfill_data(days, backfill_types, app):
24332445
log.info(">>> Completed backfilling persons")
24342446
sys.stdout.flush()
24352447
except Exception as e:
2436-
error_msg = f"Error backfilling persons: {e}\n{traceback.format_exc()}"
2448+
error_msg = (
2449+
f"Error backfilling persons: {e}\n{traceback.format_exc()}"
2450+
)
24372451
log.error(error_msg)
24382452
sys.stdout.flush()
24392453
errors.append(f"persons: {str(e)}")
@@ -2447,15 +2461,19 @@ def do_backfill_data(days, backfill_types, app):
24472461
log.info(">>> Completed backfilling invoices")
24482462
sys.stdout.flush()
24492463
except Exception as e:
2450-
error_msg = f"Error backfilling invoices: {e}\n{traceback.format_exc()}"
2464+
error_msg = (
2465+
f"Error backfilling invoices: {e}\n{traceback.format_exc()}"
2466+
)
24512467
log.error(error_msg)
24522468
sys.stdout.flush()
24532469
errors.append(f"invoices: {str(e)}")
24542470

24552471
# Log final results
24562472
log.info("=" * 80)
24572473
if results:
2458-
log.info(f"BACKFILL SUCCESS: {', '.join(results)} for the last {days} days")
2474+
log.info(
2475+
f"BACKFILL SUCCESS: {', '.join(results)} for the last {days} days"
2476+
)
24592477
if errors:
24602478
log.error(f"BACKFILL ERRORS: {len(errors)} error(s) occurred")
24612479
for error in errors:
@@ -2465,7 +2483,9 @@ def do_backfill_data(days, backfill_types, app):
24652483

24662484
except Exception as e:
24672485
# Catch any unexpected errors in the background task itself
2468-
error_msg = f"FATAL ERROR in backfill background task:\n{traceback.format_exc()}"
2486+
error_msg = (
2487+
f"FATAL ERROR in backfill background task:\n{traceback.format_exc()}"
2488+
)
24692489
log.error(error_msg)
24702490
sys.stdout.flush()
24712491
# Re-raise to ensure we see it

subscribie/blueprints/checkout/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def stripe_create_checkout_session():
460460
payment_method_types = ["card"]
461461

462462
# Stripe requires HTTPS URLs when in live mode
463-
url_scheme = 'https' if get_stripe_livemode() else None
463+
url_scheme = "https" if get_stripe_livemode() else None
464464

465465
cancel_url = url_for("checkout.order_summary", _external=True, _scheme=url_scheme)
466466
mode = "payment"
@@ -492,7 +492,10 @@ def stripe_create_checkout_session():
492492
charge["sell_price"] = plan.getSellPrice(currency_code)
493493
charge["interval_amount"] = plan.getIntervalAmount(currency_code)
494494
success_url = url_for(
495-
"checkout.instant_payment_complete", _external=True, plan=plan.uuid, _scheme=url_scheme
495+
"checkout.instant_payment_complete",
496+
_external=True,
497+
plan=plan.uuid,
498+
_scheme=url_scheme,
496499
)
497500

498501
if plan.requirements.subscription:
@@ -568,7 +571,9 @@ def stripe_create_checkout_session():
568571
)
569572

570573
elif is_donation is True:
571-
success_url = url_for("checkout.instant_payment_complete", _external=True, _scheme=url_scheme)
574+
success_url = url_for(
575+
"checkout.instant_payment_complete", _external=True, _scheme=url_scheme
576+
)
572577
donation_amount = int(session["donation_amount"] * 100)
573578
line_items.append(
574579
{

subscribie/blueprints/subscriber/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def account():
266266
)
267267

268268
# Stripe requires HTTPS URLs when in live mode
269-
url_scheme = 'https' if get_stripe_livemode() else None
269+
url_scheme = "https" if get_stripe_livemode() else None
270270

271271
stripe_session = stripe.checkout.Session.create(
272272
stripe_account=stripe_connect_account_id,
@@ -276,9 +276,13 @@ def account():
276276
setup_intent_data={
277277
"metadata": {"subscription_id": stripe_subscription.id}
278278
},
279-
success_url=url_for("subscriber.account", _external=True, _scheme=url_scheme)
279+
success_url=url_for(
280+
"subscriber.account", _external=True, _scheme=url_scheme
281+
)
280282
+ "?stripe_session_id={CHECKOUT_SESSION_ID}",
281-
cancel_url=url_for("subscriber.account", _external=True, _scheme=url_scheme),
283+
cancel_url=url_for(
284+
"subscriber.account", _external=True, _scheme=url_scheme
285+
),
282286
)
283287
if request.args.get("stripe_session_id"):
284288
# Process stripe update payment request

subscribie/utils.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,7 @@ def backfill_transactions(days=30):
653653
if paymentIntent.invoice:
654654
try:
655655
invoice = stripe.Invoice.retrieve(
656-
stripe_account=stripe_connect_account_id,
657-
id=paymentIntent.invoice
656+
stripe_account=stripe_connect_account_id, id=paymentIntent.invoice
658657
)
659658
if invoice.subscription:
660659
subscription_id = invoice.subscription
@@ -677,7 +676,9 @@ def backfill_transactions(days=30):
677676
# Update other fields that may have changed
678677
transaction.amount = paymentIntent.amount
679678
transaction.currency = paymentIntent.currency
680-
transaction.payment_status = "paid" if paymentIntent.status == "succeeded" else paymentIntent.status
679+
transaction.payment_status = (
680+
"paid" if paymentIntent.status == "succeeded" else paymentIntent.status
681+
)
681682

682683
database.session.commit()
683684
updated_count += 1
@@ -689,8 +690,14 @@ def backfill_transactions(days=30):
689690
transaction.external_src = "stripe"
690691
transaction.currency = paymentIntent.currency
691692
transaction.amount = paymentIntent.amount
692-
transaction.payment_status = "paid" if paymentIntent.status == "succeeded" else paymentIntent.status
693-
transaction.comment = invoice.subscription_details.metadata.get("donation_comment") if invoice and invoice.subscription_details else None
693+
transaction.payment_status = (
694+
"paid" if paymentIntent.status == "succeeded" else paymentIntent.status
695+
)
696+
transaction.comment = (
697+
invoice.subscription_details.metadata.get("donation_comment")
698+
if invoice and invoice.subscription_details
699+
else None
700+
)
694701
transaction.created_at = stripe_transaction_created_at
695702

696703
# Try to find subscription in local database
@@ -703,29 +710,45 @@ def backfill_transactions(days=30):
703710
if subscribie_subscription:
704711
transaction.subscription = subscribie_subscription
705712
transaction.person = subscribie_subscription.person
706-
log.info(f"Linked transaction to subscription {subscribie_subscription.id}")
713+
log.info(
714+
f"Linked transaction to subscription {subscribie_subscription.id}"
715+
)
707716
else:
708-
log.warning(f"Subscription {subscription_id} not found in local database")
717+
log.warning(
718+
f"Subscription {subscription_id} not found in local database"
719+
)
709720

710721
# Try to get person from metadata if no subscription found
711722
if transaction.person is None and paymentIntent.invoice:
712-
person_uuid = invoice.subscription_details.metadata.get("person_uuid") if invoice and invoice.subscription_details else None
723+
person_uuid = (
724+
invoice.subscription_details.metadata.get("person_uuid")
725+
if invoice and invoice.subscription_details
726+
else None
727+
)
713728
if person_uuid:
714729
person = Person.query.filter_by(uuid=person_uuid).first()
715730
if person:
716731
transaction.person = person
717-
log.info(f"Linked transaction to person {person.id} via metadata")
732+
log.info(
733+
f"Linked transaction to person {person.id} via metadata"
734+
)
718735

719736
# Check if it's a donation
720-
is_donation = invoice.subscription_details.metadata.get("is_donation", "False") if invoice and invoice.subscription_details else False
737+
is_donation = (
738+
invoice.subscription_details.metadata.get("is_donation", "False")
739+
if invoice and invoice.subscription_details
740+
else False
741+
)
721742
if is_donation == "True":
722743
transaction.is_donation = True
723744

724745
database.session.add(transaction)
725746
database.session.commit()
726747
created_count += 1
727748

728-
log.info(f"Backfill complete: {created_count} transactions created, {updated_count} updated")
749+
log.info(
750+
f"Backfill complete: {created_count} transactions created, {updated_count} updated"
751+
)
729752

730753

731754
def backfill_subscriptions(days=30):

tests/test_admin_backfill.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Tests the user-friendly backfill data form at /admin/backfill
55
introduced in issue #1476.
66
"""
7+
78
import pytest
89
from subscribie.models import User
910
from unittest.mock import patch, MagicMock
@@ -14,6 +15,7 @@
1415
@contextmanager
1516
def user_set(app, user):
1617
"""Context manager to set g.user for testing authenticated routes"""
18+
1719
def handler(sender, **kwargs):
1820
g.user = user
1921

@@ -62,7 +64,7 @@ def test_backfill_form_renders_successfully(
6264
assert 'id="backfill_subscriptions"' in response_data
6365
assert 'id="backfill_persons"' in response_data
6466
assert 'id="backfill_invoices"' in response_data
65-
assert 'checked' in response_data # At least some checkboxes are checked
67+
assert "checked" in response_data # At least some checkboxes are checked
6668

6769
@patch("subscribie.blueprints.admin.backfill_transactions")
6870
@patch("subscribie.blueprints.admin.backfill_subscriptions")
@@ -95,7 +97,12 @@ def test_backfill_form_submission_with_all_types(
9597
"/admin/backfill",
9698
data={
9799
"days": "30",
98-
"backfill_types": ["transactions", "subscriptions", "persons", "invoices"],
100+
"backfill_types": [
101+
"transactions",
102+
"subscriptions",
103+
"persons",
104+
"invoices",
105+
],
99106
},
100107
follow_redirects=True,
101108
)
@@ -105,7 +112,10 @@ def test_backfill_form_submission_with_all_types(
105112
response_data = response.data.decode("utf-8")
106113

107114
# Should show the backfill form again after redirect
108-
assert "Backfill Data from Stripe" in response_data or "Synchronise Data from Stripe" in response_data
115+
assert (
116+
"Backfill Data from Stripe" in response_data
117+
or "Synchronise Data from Stripe" in response_data
118+
)
109119

110120
# Give background thread a moment to start
111121
time.sleep(0.5)
@@ -146,7 +156,10 @@ def test_backfill_form_submission_with_single_type(
146156
response_data = response.data.decode("utf-8")
147157

148158
# Should show the backfill form again after redirect
149-
assert "Backfill Data from Stripe" in response_data or "Synchronise Data from Stripe" in response_data
159+
assert (
160+
"Backfill Data from Stripe" in response_data
161+
or "Synchronise Data from Stripe" in response_data
162+
)
150163

151164
# Give background thread a moment to start
152165
time.sleep(0.5)
@@ -271,7 +284,9 @@ def test_backfill_requires_authentication(
271284

272285
# Should redirect to login
273286
assert response.status_code == 302
274-
assert "/auth/login" in response.location or "login" in response.location.lower()
287+
assert (
288+
"/auth/login" in response.location or "login" in response.location.lower()
289+
)
275290

276291
@patch("subscribie.blueprints.admin.backfill_transactions")
277292
def test_legacy_api_endpoint_preserved(

0 commit comments

Comments
 (0)