Skip to content

Commit

Permalink
Fix #1419 as a subscriber I can edit the subscriber name last name an…
Browse files Browse the repository at this point in the history
…d first name
  • Loading branch information
chrisjsimpson committed Nov 3, 2024
1 parent a316cdc commit 18bd172
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 6 deletions.
96 changes: 91 additions & 5 deletions subscribie/blueprints/admin/subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,40 @@ def show_subscriber_email(subscriber_id):
return f"""
<span hx-target="this" hx-swap="outerHTML">
<li class="list-group-item">📧 {person.email}
<button hx-get="{url_for('admin.edit_subscriber', subscriber_id=person.id)}">
<button class="btn btn-primary" hx-get="{url_for('admin.edit_subscriber_email', subscriber_id=person.id)}">
Edit
</button>
</li>
</span>
"""


def show_subscriber_full_name(subscriber_id):
person = Person.query.execution_options(include_archived=True).get(subscriber_id)

return f"""
<span hx-target="this" hx-swap="outerHTML">
<h3 class="card-title">👤 {person.full_name}</h3>
<button hx-get="{url_for('admin.edit_subscriber_full_name', subscriber_id=person.id)}" class="btn btn-primary">Edit</button>
</span>
"""


@admin.route("/subscriber/<subscriber_id>/email", methods=["GET"])
@login_required
def get_subscriber_email_address(subscriber_id):
return show_subscriber_email(subscriber_id)


@admin.route("/subscriber/<subscriber_id>/edit", methods=["GET", "PUT"])
@admin.route("/subscriber/<subscriber_id>/full_name", methods=["GET"])
@login_required
def get_subscriber_full_name(subscriber_id):
return show_subscriber_full_name(subscriber_id)


@admin.route("/subscriber/<subscriber_id>/email/edit", methods=["GET", "PUT"])
@login_required
def edit_subscriber(subscriber_id):
def edit_subscriber_email(subscriber_id):
person = Person.query.execution_options(include_archived=True).get(subscriber_id)

if request.method == "PUT":
Expand Down Expand Up @@ -111,18 +128,87 @@ def edit_subscriber(subscriber_id):
return show_subscriber_email(subscriber_id)

return f"""
<form hx-put="{url_for('admin.edit_subscriber', subscriber_id=subscriber_id)}" hx-target="this" hx-swap="outerHTML">
<form hx-put="{url_for('admin.edit_subscriber_email', subscriber_id=subscriber_id)}" hx-target="this" hx-swap="outerHTML">
<div class="form-group">
<label>Email Address</label>
<input type="email" name="email" value="{person.email}">
</div>
<button class="btn btn-primary">Submit</button>
<button class="btn btn-secondary"
<button class="btn"
hx-get="{url_for('admin.get_subscriber_email_address', subscriber_id=subscriber_id)}">Cancel</button>
</form>
"""


@admin.route("/subscriber/<subscriber_id>/full_name/edit", methods=["GET", "PUT"])
@login_required
def edit_subscriber_full_name(subscriber_id):
person = Person.query.execution_options(include_archived=True).get(subscriber_id)

if request.method == "PUT":
old_given_name = person.given_name
old_family_name = person.family_name
new_given_name = request.form.get("given_name")
new_family_name = request.form.get("family_name")
person.given_name = new_given_name
person.family_name = new_family_name

database.session.commit()

# Also update Stripe customer email record
try:
if len(person.subscriptions) > 0:
# Verify we have a Stripe customer record for
# this person (a person may have subscriptions,
# however they may not all be associated with Stripe)
# e.g. Free subscriptions, other payment providers etc.
log.debug("Verifying if person has associated Stripe record or not")
if len(person.subscriptions[0].stripe_invoices) > 0:
new_name = f"{new_given_name} {new_family_name}"
stripe_customer_id = json.loads(
person.subscriptions[0]
.stripe_invoices[0]
.stripe_invoice_raw_json
)["customer"]
stripe.api_key = get_stripe_secret_key()
stripe_connect_account_id = get_stripe_connect_account_id()
stripe.Customer.modify(
id=stripe_customer_id,
stripe_account=stripe_connect_account_id,
name=new_name,
)
else:
msg = f"""Updating Person {person.id} full_name however no Stripe
record found. This may be OK if no associated Stripe payments"""
log.warning(msg)
except Exception as e:
log.error(f"Failure updating person '{person.id}' full_name to. {e}")
log.debug(
"Switching person given_name / family_name address back to previous values"
)
person.given_name = old_given_name
person.family_name = old_family_name
database.session.commit()

return show_subscriber_full_name(subscriber_id)

return f"""
<form hx-put="{url_for('admin.edit_subscriber_full_name', subscriber_id=subscriber_id)}" hx-target="this" hx-swap="outerHTML">
<div class="form-group">
<label>First name:</label>
<input type="text" name="given_name" value="{person.given_name}">
</div>
<div class="form-group">
<label>Last name:</label>
<input type="text" name="family_name" value="{person.family_name}">
</div>
<button class="btn btn-primary">Submit</button>
<button class="btn"
hx-get="{url_for('admin.get_subscriber_full_name', subscriber_id=subscriber_id)}">Cancel</button>
</form>
"""


@admin.route("/charge/subscriber/<person_id>", methods=["GET"])
@login_required
def charge_subscriber(person_id):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ <h2 class="text-center text-dark mb-3">Subscriber: {{ person.full_name | title }
<div class="row">
<div class="card col-md-12">
<div class="card-body">
<span hx-target="this" hx-swap="outerHTML">
<h3 class="card-title">👤 {{ person.full_name | title }}</h3>
<button hx-get="{{ url_for('admin.edit_subscriber_full_name', subscriber_id = person.id) }}" class="btn btn-primary">Edit</button>
</span>
</div>
<ul class="list-group list-group-flush">
<span hx-target="this" hx-swap="outerHTML">
<li class="list-group-item">📧 {{ person.email }}
<button hx-get="{{ url_for('admin.edit_subscriber', subscriber_id = person.id) }}">
<button hx-get="{{ url_for('admin.edit_subscriber_email', subscriber_id = person.id) }}" class="btn btn-primary" >
Edit
</button>
</li>
Expand Down

0 comments on commit 18bd172

Please sign in to comment.