from django.http import HttpResponseRedirect

from .forms import DonationForm
from .helpers import GoCardlessHelper


class DonationFormMiddleware(object):
    def get_initial(self):
        return {
            'payment_type': 'subscription',
            'amount': 10,
        }

    def form_valid(self, request, form):
        # Add the form info to the session
        request.session['donation_form'] = form.cleaned_data

        # Start the GoCardless process
        gc = GoCardlessHelper(request)

        # Make a customer object at GC's site first.
        redirect_url = gc.get_redirect_url()

        # Redirect to GoCardless
        return HttpResponseRedirect(redirect_url)

    def process_request(self, request):
        form_prefix = "donation_form"
        key_to_check = "{}-amount".format(form_prefix)

        if request.method == 'POST' and key_to_check in request.POST:
            form = DonationForm(data=request.POST, prefix=form_prefix)
            if form.is_valid():
                return self.form_valid(request, form)
        else:
            form = DonationForm(
                initial=self.get_initial(), prefix=form_prefix)
        request.donation_form = form

@@ -108,16 +108,31 @@
 ssHelper
+, PAYMENT_UNITS
 %0A%0A%0Aclass
@@ -188,16 +188,25 @@
 ial(self
+, request
 ):%0A     
@@ -208,22 +208,100 @@
         
-return
+suggested_donation = request.GET.get('suggested_donation', 3)%0A        form_initial =
  %7B%0A     
@@ -351,35 +351,239 @@
     
+%7D%0A
     
-'amount': 10,%0A        %7D
+    if int(suggested_donation) in %5Bx%5B0%5D for x in PAYMENT_UNITS%5D:%0A            form_initial%5B'amount'%5D = suggested_donation%0A        else:%0A            form_initial%5B'other_amount'%5D = suggested_donation%0A%0A        return form_initial
 %0A%0A  
@@ -1441,10 +1441,33 @@
 ial(
-),
+request),%0A               
  pre
@@ -1473,32 +1473,45 @@
 efix=form_prefix
+%0A            
 )%0A        reques

Allow altering the donation amount via a link and default to £3