Skip to content

Commit

Permalink
added route guard stripe_connect_id_required
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsimpson committed Feb 27, 2022
1 parent 4efcac3 commit 722c51f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
36 changes: 36 additions & 0 deletions subscribie/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
url_for,
current_app,
render_template_string,
Markup,
)
from subscribie.email import EmailMessageQueue
from subscribie.utils import get_stripe_secret_key, get_stripe_connect_account
from base64 import urlsafe_b64encode
import os
from .forms import (
Expand All @@ -32,6 +34,7 @@
from functools import wraps
from py_auth_header_parser import parse_auth_header
import datetime
import stripe

log = logging.getLogger(__name__)
bp = Blueprint("auth", __name__, url_prefix="/auth")
Expand Down Expand Up @@ -410,6 +413,39 @@ def wrapped_view(**kwargs):
return wrapped_view


def stripe_connect_id_required(view):
"""Redirect away from route if requires a Stripe Connect id
NOTE:
- Does *not* require Stripe Connect process is completed
- Does require that a Stripe Connect id has been generated
- e.g. The shop owner may have started the process but not yet
finished Stripe onboarding
Used to redirect views when Stripe Connect is required
but there is a request to visit a page which needs Stripe
connect to be completed.
"""

@functools.wraps(view)
def wrapped_view(**kwargs):
stripe.api_key = get_stripe_secret_key()
connect_account = get_stripe_connect_account()
if connect_account is None:
stripe_connect_url = url_for("admin.stripe_connect")
flash(
Markup(
f"You must <a href='{ stripe_connect_url }'>connect Stripe first.</a>" # noqa: E501
)
)
return redirect(url_for("admin.dashboard"))

return view(**kwargs)

return wrapped_view


def protected_download(view):
"""Only allow authenticated users to download (Shop owners or subscribers)"""

Expand Down
15 changes: 6 additions & 9 deletions subscribie/blueprints/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
SetReplyToEmailForm,
UploadFilesForm,
)
from subscribie.auth import login_required, protected_download
from subscribie.auth import (
login_required,
protected_download,
stripe_connect_id_required,
)
from flask_uploads import UploadSet, IMAGES
import os
from pathlib import Path
Expand Down Expand Up @@ -1090,17 +1094,10 @@ def upcoming_invoices():

@admin.route("/invoices")
@login_required
@stripe_connect_id_required
def invoices():
stripe.api_key = get_stripe_secret_key()
connect_account = get_stripe_connect_account()
if connect_account is None:
stripe_connect_url = url_for("admin.stripe_connect")
flash(
Markup(
f"You must <a href='{ stripe_connect_url }'>connect Stripe first.</a>"
)
)
return redirect(url_for("admin.dashboard"))
invoices = stripe.Invoice.list(stripe_account=connect_account.id)

return render_template("admin/invoices.html", invoices=invoices, datetime=datetime)
Expand Down

0 comments on commit 722c51f

Please sign in to comment.