Compare commits

...

3 commits

Author SHA1 Message Date
a51a7e2099
supporters: Add sustainer confirmation email 2024-10-24 17:51:49 +11:00
48048f349a
supporters: Use data-binding for amount rather than events
Also tidied forms.py a little by consolidating into widget and moving help text
to template.
2024-10-24 17:06:32 +11:00
c843e1c59f
supporters: Apply Black formatter 2024-10-23 18:16:47 +11:00
8 changed files with 108 additions and 45 deletions

View file

@ -3,6 +3,7 @@ from django.utils.safestring import mark_safe
from django.urls import reverse
from .models import SustainerOrder
class SustainerFormRenderer(forms.renderers.DjangoTemplates):
# Customised layout with labels on own row
field_template_name = 'supporters/field.html'
@ -10,6 +11,7 @@ class SustainerFormRenderer(forms.renderers.DjangoTemplates):
class ButtonRadioSelect(forms.widgets.RadioSelect):
"""Radio button styled like a button. BYO CSS."""
# Extra <span> wrappers to support CSS
option_template_name = 'supporters/buttonradio_option.html'
use_fieldset = False
@ -52,11 +54,23 @@ class SustainerForm(forms.ModelForm):
'country',
]
widgets = {
'recurring': ButtonRadioSelect(attrs={
'recurring': ButtonRadioSelect(
attrs={
'x-model': 'recurring',
# Reset the amount field and option when changing monthly/annually.
'x-on:change': '$refs.amount.value = null; amount_option = null',
}),
'x-on:change': 'amount = ""; amount_option = null',
}
),
'amount': forms.widgets.NumberInput(
# Keeping default widget, just neater to add many attrs here.
attrs={
# So we can update the amount field from the amount_option selected.
'x-model': 'amount',
'x-bind:min': 'amount_minimum',
'onblur': 'this.reportValidity()',
'style': 'width: 5rem',
}
),
}
def __init__(self, *args, **kwargs):
@ -66,13 +80,6 @@ class SustainerForm(forms.ModelForm):
self.fields['recurring'].label = ''
self.fields['amount'].initial = self.YEAR_OPTIONS[0]
# So we can write to this field easily from Alpine JS.
self.fields['amount'].widget.attrs['x-ref'] = 'amount'
self.fields['amount'].widget.attrs['style'] = 'width: 5rem'
self.fields['amount'].widget.attrs['onblur'] = 'this.reportValidity()'
self.fields['amount'].widget.attrs['x-bind:min'] = 'amount_minimum'
self.fields['email'].help_text = 'For your payment receipt'
self.fields['tshirt_size'].help_text = mark_safe("""Sizing chart: <a href="/videos/women-2017-to-2020-t-shirt-sizing.jpg" target="_blank" class="black-60">Women's</a>, <a href="/videos/men-2017-to-2020-t-shirt-sizing.jpg" target="_blank" class="black-60">Men's</a>""")
self.fields['tshirt_size'].widget.attrs['x-model'] = 'tshirt_size'
def clean(self):
@ -84,15 +91,16 @@ class SustainerForm(forms.ModelForm):
if amount < minimum:
self.add_error(
'',
mark_safe(f'${minimum:d} is a minimum for Conservancy Sustainers. <a href="{donate_url}">Donate smaller amounts here</a>.')
mark_safe(
f'${minimum:d} is a minimum for Conservancy Sustainers. <a href="{donate_url}">Donate smaller amounts here</a>.'
),
)
tshirt_size = self.cleaned_data.get('tshirt_size')
if tshirt_size and not all([
if tshirt_size and not all(
[
self.cleaned_data.get('street'),
self.cleaned_data.get('city'),
self.cleaned_data.get('country')
]):
self.add_error(
'street',
'No address provided'
)
self.cleaned_data.get('country'),
]
):
self.add_error('street', 'No address provided')

View file

@ -0,0 +1,17 @@
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
def make_stripe_email(order) -> EmailMessage:
subject = 'Thanks for your sustainer payment!'
email_body = render_to_string(
'supporters/mail/sustainer_thanks.txt',
{'order': order},
).strip()
message = EmailMessage(
subject,
email_body,
'Software Freedom Conservancy <sustainers@sfconservancy.org>',
[order.email],
)
return message

View file

@ -5,7 +5,9 @@ class Supporter(models.Model):
"""Conservancy Supporter listing"""
display_name = models.CharField(max_length=200, blank=False)
display_until_date = models.DateTimeField("date until which this supporter name is displayed")
display_until_date = models.DateTimeField(
"date until which this supporter name is displayed"
)
ledger_entity_id = models.CharField(max_length=200, blank=False)
def test(self):
@ -65,13 +67,17 @@ class SustainerOrder(models.Model):
name = models.CharField(max_length=255)
email = models.EmailField()
amount = models.PositiveIntegerField()
recurring = models.CharField(max_length=10, choices=RENEW_CHOICES, blank=True, default='')
recurring = models.CharField(
max_length=10, choices=RENEW_CHOICES, blank=True, default=''
)
payment_method = models.CharField(max_length=10, default='Stripe')
payment_id = models.CharField(max_length=255, blank=True)
paid_time = models.DateTimeField(null=True, blank=True)
acknowledge_publicly = models.BooleanField(default=True)
add_to_mailing_list = models.BooleanField(default=True)
tshirt_size = models.CharField('T-shirt size', max_length=50, choices=TSHIRT_CHOICES, blank=True, default='')
tshirt_size = models.CharField(
'T-shirt size', max_length=50, choices=TSHIRT_CHOICES, blank=True, default=''
)
street = models.CharField(max_length=255, blank=True)
city = models.CharField(max_length=255, blank=True)
state = models.CharField(max_length=255, blank=True)

View file

@ -1,2 +1,2 @@
<!-- Custom <span> wrapper around the label to enable radio fields to be styled like buttons. -->
{# Custom <span> wrapper around the label to enable radio fields to be styled like buttons. #}
{% if widget.wrap_label %}<label onclick="click()"{% if widget.attrs.id %} for="{{ widget.attrs.id }}"{% endif %}>{% endif %}{% include "django/forms/widgets/input.html" %}{% if widget.wrap_label %} <span>{{ widget.label }}</span></label>{% endif %}

View file

@ -1,4 +1,4 @@
<!-- Labels on a separate line, custom help text layout -->
{# Labels on a separate line, custom help text layout #}
{% if field.use_fieldset %}
<fieldset{% if field.help_text and field.auto_id and "aria-describedby" not in field.field.widget.attrs %} aria-describedby="{{ field.auto_id }}_helptext"{% endif %}>
{% if field.label %}{{ field.legend_tag }}{% endif %}

View file

@ -0,0 +1,18 @@
Hi {{ order.name }},
Thanks so much for being a sustainer! Your support is what makes our work possible.
Order: #{{ order.id }}
Payment: ${{ order.amount }}{% if order.recurring %} {{ order.get_recurring_display }}{% endif %}
Acknowledge me on the list of sustainers: {{ order.acknowledge_publicly|yesno }}
Add me to the announcements email list: {{ order.add_to_mailing_list|yesno }}
T-shirt: {{ order.get_tshirt_size_display }}{% if order.tshirt_size %}
Postal address:
{{ order.street }}
{{ order.city }} {{ order.state }} {{ order.zip_code }}
{{ order.country }}{% endif %}
{% if order.recurring == 'month' and order.tshirt_size %}
Please note that you may not receive the T-shirt until you've paid at least $60.{% endif %}
Kind regards,
Software Freedom Conservancy

View file

@ -52,8 +52,9 @@
</div>
</noscript>
<!-- Alpine JS is used to show different payments amounts for monthly/annual, write the selected payment amount into the "amount" field, reset the seleted amount when you change monthly/annual and pop out the address when you select a T-shirt. -->
{# Alpine JS is used to show different payments amounts for monthly/annual, write the selected payment amount into the "amount" field, reset the seleted amount when you change monthly/annual and pop out the address when you select a T-shirt. #}
<form method="post" action="."
{# Pre-fill field defaults in case of server-side validation error. Otherwise Alpine JS will override them. #}
x-data="{
recurring: '{{ form.recurring.value|escapejs }}',
amount: parseInt('{{ form.amount.value|escapejs }}'),
@ -82,34 +83,40 @@
<template x-for="m in amount_options">
{# Additional click handler ensures a click-drag activates the radio (similar to a real button). #}
<label onclick="this.click()">
{# It seems to be important that all radios have a unique value to avoid UI glitches. #}
<input type="radio" name="amount_option" x-bind:value="m" x-on:change="$refs.amount.value = m" x-model="amount_option" required>
{# All radios have a unique value to avoid UI glitches (even though the value isn't actually used). #}
<input type="radio" name="amount_option" x-bind:value="m" x-on:change="amount = m" x-model="amount_option" required>
<span>$<span x-text="m.toLocaleString()"></span></span>
</label>
</template>
<!-- Hide if no JS -->
<template x-if="true">
<label onclick="this.click()">
<input type="radio" name="amount_option" value="other" x-on:change="$refs.amount.value = ''" x-model="amount_option" required>
<input type="radio" name="amount_option" value="other" x-on:change="amount = ''" x-model="amount_option" required>
<span>Other</span>
</label>
</template>
</div>
<div class="mt2" x-show="amount_option === 'other'">
{{ form.amount.as_field_group }}
<p class="f7 black-60 mt1">Minimum $<span x-text="amount_minimum"></span>. <a href="/donate" class="black-60">Donate smaller amounts here</a>.</p>
</div>
</div>
<div class="mt3">{{ form.name.as_field_group }}</div>
<div class="mt2">{{ form.email.as_field_group }}</div>
<div class="mt2">
{{ form.email.as_field_group }}
</div>
<div class="mt3"><label class="lh-title">{{ form.acknowledge_publicly }} Acknowledge me on the <a href="/sponsors#sustainers" target="_blank">list of sustainers</a></label></div>
<div class="mt3"><label class="lh-title">{{ form.add_to_mailing_list }} Add me to the <a href="https://lists.sfconservancy.org/pipermail/announce/">announcements</a> email list</label></div>
<div class="mt3"><label class="lh-title">{{ form.add_to_mailing_list }} Add me to the <a href="https://lists.sfconservancy.org/pipermail/announce/">announcements email list</a></label></div>
<div class="mt3">
{{ form.tshirt_size.as_field_group }}
<p class="f7 black-60 mt1">Sizing chart:
<a href="/videos/women-2017-to-2020-t-shirt-sizing.jpg" target="_blank" class="black-60">Women's</a>,
<a href="/videos/men-2017-to-2020-t-shirt-sizing.jpg" target="_blank" class="black-60">Men's</a></p>
<figure class="mt2">
<img src="/static/img/tshirt-2023.png" alt="Software Freedom Conservancy T-shirt" width="200">
</figure>
@ -130,7 +137,7 @@
</fieldset>
</form>
<p class="f7 mt3">Credit card and ACH payments are processed with Stripe. We also accept payment by PayPal, paper check and wire transfer (see below). Our sustainer program has a minimum of $120 USD per year, but we also accept <a href="/donate/">donations of smaller amounts</a>.</p>
<p class="f7 mt3">Credit card and ACH payments are processed with Stripe. We also accept payment by PayPal, paper check and wire transfer.</p>
<details id="paypal">
<summary class="f6">PayPal</summary>

View file

@ -8,11 +8,12 @@ from django.utils import timezone
import stripe
from .. import ParameterValidator
from . import forms
from . import forms, mail
from .models import Supporter, SustainerOrder
logger = logging.getLogger(__name__)
def sustainers(request):
with ParameterValidator(request.GET, 'upgrade_id') as validator:
try:
@ -39,14 +40,16 @@ def sponsors(request):
anonymous_count = len(supporters.filter(display_name='Anonymous'))
supporters = supporters.exclude(display_name='Anonymous').order_by('ledger_entity_id')
c = {
'supporters' : supporters,
'supporters_count' : supporters_count,
'anonymous_count' : anonymous_count
'supporters': supporters,
'supporters_count': supporters_count,
'anonymous_count': anonymous_count,
}
return render(request, "supporters/sponsors.html", c)
def create_checkout_session(reference_id, email: str, amount: int, recurring: str, base_url: str):
def create_checkout_session(
reference_id, email: str, amount: int, recurring: str, base_url: str
):
# https://docs.stripe.com/payments/accept-a-payment
# https://docs.stripe.com/api/checkout/sessions
YOUR_DOMAIN = base_url
@ -85,7 +88,9 @@ def sustainers_stripe(request):
if form.is_valid():
order = form.save()
base_url = f'{request.scheme}://{request.get_host()}'
stripe_checkout_url = create_checkout_session(order.id, order.email, order.amount, order.recurring, base_url)
stripe_checkout_url = create_checkout_session(
order.id, order.email, order.amount, order.recurring, base_url
)
return redirect(stripe_checkout_url)
else:
form = forms.SustainerForm()
@ -121,7 +126,9 @@ def fulfill_checkout(session_id):
# Checkout Session
logger.info(f'Session ID {session_id} PAID!')
try:
order = SustainerOrder.objects.get(id=checkout_session['client_reference_id'], paid_time=None)
order = SustainerOrder.objects.get(
id=checkout_session['client_reference_id'], paid_time=None
)
order.paid_time = timezone.now()
if checkout_session['payment_intent']:
# Payments get a payment intent directly
@ -131,6 +138,8 @@ def fulfill_checkout(session_id):
order.payment_id = checkout_session['invoice']['payment_intent']
order.save()
logger.info(f'Marked sustainer order {order.id} (order.email) as paid')
email = mail.make_stripe_email(order)
email.send()
except SustainerOrder.DoesNotExist:
logger.info('No action')
@ -151,9 +160,7 @@ def webhook(request):
logger.warning('Missing STRIPE_ENDPOINT_SECRET')
try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
event = stripe.Webhook.construct_event(payload, sig_header, endpoint_secret)
except ValueError:
# Invalid payload
return HttpResponse(status=400)