diff --git a/registrasion/forms.py b/registrasion/forms.py index 7770e9be..ecb889fa 100644 --- a/registrasion/forms.py +++ b/registrasion/forms.py @@ -414,6 +414,15 @@ def staff_products_formset_factory(user): class InvoiceNagForm(forms.Form): + + ACTION_PREVIEW = 1 + ACTION_SEND = 2 + + ACTION_CHOICES = ( + (ACTION_PREVIEW, "Preview"), + (ACTION_SEND, "Send emails"), + ) + invoice = forms.ModelMultipleChoiceField( widget=forms.CheckboxSelectMultiple, queryset=commerce.Invoice.objects.all(), @@ -423,6 +432,12 @@ class InvoiceNagForm(forms.Form): body = forms.CharField( widget=forms.Textarea, ) + action = forms.TypedChoiceField( + widget=forms.RadioSelect, + coerce=int, + choices=ACTION_CHOICES, + initial=ACTION_PREVIEW, + ) def __init__(self, *a, **k): category = k.pop('category', None) or [] diff --git a/registrasion/views.py b/registrasion/views.py index 5f57eb20..b3aae73d 100644 --- a/registrasion/views.py +++ b/registrasion/views.py @@ -920,6 +920,11 @@ def extend_reservation(request, user_id, days=7): return redirect(request.META["HTTP_REFERER"]) +Email = namedtuple( + "Email", + ("subject", "body", "from_email", "recipient_list"), +) + @user_passes_test(_staff_only) def nag_unpaid(request): ''' Allows staff to nag users with unpaid invoices. ''' @@ -933,6 +938,8 @@ def nag_unpaid(request): product=product, ) + emails = [] + if form.is_valid(): emails = [] for invoice in form.cleaned_data["invoice"]: @@ -945,12 +952,16 @@ def nag_unpaid(request): }) ) recipient_list = [invoice.user.email] - emails.append((subject, body, from_email, recipient_list)) - send_mass_mail(emails) - messages.info(request, "The e-mails have been sent.") + emails.append(Email(subject, body, from_email, recipient_list)) + + if form.cleaned_data["action"] == forms.InvoiceNagForm.ACTION_SEND: + # Send e-mails *ONLY* if we're sending. + send_mass_mail(emails) + messages.info(request, "The e-mails have been sent.") data = { "form": form, + "emails": emails, } return render(request, "registrasion/nag_unpaid.html", data)