First pass at tying the premium selected and the pledge amount

pull/1/head
Raymond Yee 2011-11-29 18:02:51 -08:00
parent dc700b1125
commit b44379e6cd
3 changed files with 62 additions and 12 deletions

View File

@ -10,9 +10,14 @@ from decimal import Decimal as D
from selectable.forms import AutoCompleteSelectMultipleWidget,AutoCompleteSelectMultipleField from selectable.forms import AutoCompleteSelectMultipleWidget,AutoCompleteSelectMultipleField
from selectable.forms import AutoCompleteSelectWidget,AutoCompleteSelectField 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 from regluit.core.lookups import OwnerLookup
import logging
logger = logging.getLogger(__name__)
def UserClaimForm ( user_instance, *args, **kwargs ): def UserClaimForm ( user_instance, *args, **kwargs ):
class ClaimForm(forms.ModelForm): class ClaimForm(forms.ModelForm):
i_agree=forms.BooleanField() i_agree=forms.BooleanField()
@ -143,6 +148,44 @@ class CampaignPledgeForm(forms.Form):
label="Pledge Amount", label="Pledge Amount",
) )
anonymous = forms.BooleanField(required=False, label=_("Don't display my username in the supporters list")) 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): class GoodreadsShelfLoadingForm(forms.Form):
goodreads_shelf_name_number = forms.CharField(widget=forms.Select(choices=( goodreads_shelf_name_number = forms.CharField(widget=forms.Select(choices=(

View File

@ -36,19 +36,22 @@
Campaign in Progress: ${{ work.last_campaign.current_total }}/${{ work.last_campaign.target }} Campaign in Progress: ${{ work.last_campaign.current_total }}/${{ work.last_campaign.target }}
</div> </div>
<!-- Change this to submit to pledge page, not campaign page -->
<div class="jsmod-content">
<form method="POST" action="{% url pledge work_id=work.id %}"> <form method="POST" action="{% url pledge work_id=work.id %}">
{% csrf_token %} {% 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 }}
<input type="submit" value="Pledge" /> <input type="submit" value="Pledge" />
</form>
<div class="jsmod-content">
<form action="#">
<ul class="support menu"> <ul class="support menu">
{% for premium in premiums %} {% for premium in premiums %}
<li class="{% if forloop.first %}first{% else %}{% if forloop.last %}last{% endif %}{% endif %}"> <li class="{% if forloop.first %}first{% else %}{% if forloop.last %}last{% endif %}{% endif %}">
<input type="radio" name="premium_radio" value="{{premium.id}}" {% ifequal request.GET.premium_id premium.id|stringformat:"s" %}checked="checked"{% endifequal %}" /> <input type="radio" name="premium_id" value="{{premium.id}}" {% ifequal request.GET.premium_id premium.id|stringformat:"s" %}checked="checked"{% endifequal %}" />
<span class="menu-item-price">${{ premium.amount }}</span> <span class="menu-item-price">${{ premium.amount }}</span>
<span class="menu-item-desc">{{ premium.description }}</span> <span class="menu-item-desc">{{ premium.description }}</span>
</a></li> </a></li>

View File

@ -2,6 +2,7 @@ import logging
import datetime import datetime
from decimal import Decimal as D from decimal import Decimal as D
from django import forms
from django.db.models import Q, Count from django.db.models import Q, Count
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
@ -146,23 +147,26 @@ class PledgeView(FormView):
work = get_object_or_404(models.Work, id=self.kwargs["work_id"]) work = get_object_or_404(models.Work, id=self.kwargs["work_id"])
campaign = work.last_campaign() campaign = work.last_campaign()
if campaign: if campaign:
premiums = campaign.premiums.all() premiums = campaign.premiums.all()
if premiums.count() == 0: if premiums.count() == 0:
premiums = models.Premium.objects.filter(campaign__isnull=True) premiums = models.Premium.objects.filter(campaign__isnull=True)
premium_id = self.request.GET.get('premium_id', None) premium_id = self.request.REQUEST.get('premium_id', None)
preapproval_amount = self.request.POST.get('preapproval_amount', None) preapproval_amount = self.request.REQUEST.get('preapproval_amount', None)
if premium_id is not None: if premium_id is not None and preapproval_amount is None:
try: try:
preapproval_amount = D(models.Premium.objects.get(id=premium_id).amount) preapproval_amount = D(models.Premium.objects.get(id=premium_id).amount)
except: except:
preapproval_amount = None preapproval_amount = None
data = {'preapproval_amount':preapproval_amount} logger.info("preapproval_amount, premium_id: %s %s ", preapproval_amount, premium_id)
form = CampaignPledgeForm(data) data = {'preapproval_amount':preapproval_amount, 'premium_id':premium_id}
form = CampaignPledgeForm(data, premiums=premiums)
context.update({'work':work,'campaign':campaign, 'premiums':premiums, 'form':form}) context.update({'work':work,'campaign':campaign, 'premiums':premiums, 'form':form})
return context return context