diff --git a/frontend/forms.py b/frontend/forms.py index 54c471df..2b8f0e5f 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -10,9 +10,14 @@ from decimal import Decimal as D from selectable.forms import AutoCompleteSelectMultipleWidget,AutoCompleteSelectMultipleField from selectable.forms import AutoCompleteSelectWidget,AutoCompleteSelectField -from regluit.core.models import UserProfile, RightsHolder, Claim, Campaign +from regluit.core.models import UserProfile, RightsHolder, Claim, Campaign, Premium from regluit.core.lookups import OwnerLookup +import logging + +logger = logging.getLogger(__name__) + + def UserClaimForm ( user_instance, *args, **kwargs ): class ClaimForm(forms.ModelForm): i_agree=forms.BooleanField() @@ -143,6 +148,44 @@ class CampaignPledgeForm(forms.Form): label="Pledge Amount", ) anonymous = forms.BooleanField(required=False, label=_("Don't display my username in the supporters list")) + + premium_id = forms.IntegerField(required=False) + + def __init__(self, *args, **kwargs): + # might want to create the premiums regardless of whether premiums being passed in. + # pull out any premiums keyword + + try: + premiums = kwargs.get('premiums', None) + del kwargs["premiums"] + except: + premiums = None + + super(CampaignPledgeForm,self).__init__(*args, **kwargs) + + # right now we're rendering the premiums in HTML in the template. Might want to create a custom widget + + if premiums is not None: + premium_choices = tuple( [(p.id, "%s (%s)" % (p.description, p.amount)) for p in premiums] ) + self.fields["premium_id"] = forms.ChoiceField(choices=premium_choices, required=False, widget=forms.RadioSelect) + + def clean(self): + cleaned_data = self.cleaned_data + logger.info("cleaned_data: %s " ,cleaned_data) + # check on whether the preapproval amount is < amount for premium tier. If so, put an error message + try: + preapproval_amount = cleaned_data.get("preapproval_amount") + premium_id = int(cleaned_data.get("premium_id")) + premium_amount = Premium.objects.get(id=premium_id).amount + logger.info("amount, id, premium_amount: %s %s %s", preapproval_amount, premium_id, premium_amount ) + if preapproval_amount < premium_amount: + raise forms.ValidationError(_("Sorry, you must pledge at least %s to select that premium." % (premium_amount))) + except Exception, e: + if isinstance(e, forms.ValidationError): + raise e + + return cleaned_data + class GoodreadsShelfLoadingForm(forms.Form): goodreads_shelf_name_number = forms.CharField(widget=forms.Select(choices=( diff --git a/frontend/templates/pledge.html b/frontend/templates/pledge.html index 6758bd4c..e5e32127 100644 --- a/frontend/templates/pledge.html +++ b/frontend/templates/pledge.html @@ -36,19 +36,22 @@ Campaign in Progress: ${{ work.last_campaign.current_total }}/${{ work.last_campaign.target }} - + +
+
{% csrf_token %} - {{form.as_p}} + {{ form.non_field_errors }} + {{ form.preapproval_amount.errors }} + {{ form.preapproval_amount.label_tag }}: {{ form.preapproval_amount }} + {{ form.anonymous.errors }} + {{ form.anonymous.label_tag }}: {{ form.anonymous }} -
-
-