Refining the interaction of Premium choice and pledge amount

pull/1/head
Raymond Yee 2011-11-30 08:58:26 -08:00
parent b44379e6cd
commit b8cebfa0dd
4 changed files with 17 additions and 27 deletions

View File

@ -181,6 +181,12 @@ class Campaign(models.Model):
def supporters(self):
translist = self.transactions().values_list('user', flat=True).distinct()
return translist
def effective_premiums(self):
"""returns either the custom premiums for Campaign or any default premiums"""
premiums = self.premiums.all()
if premiums.count() == 0:
premiums = Premium.objects.filter(campaign__isnull=True)
return premiums
class Work(models.Model):
created = models.DateTimeField(auto_now_add=True)

View File

@ -150,36 +150,16 @@ class CampaignPledgeForm(forms.Form):
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)))
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

View File

@ -38,6 +38,12 @@
<div class="jsmod-content">
{% comment %}
Even though we have in frontend/forms.py a CampaignPledgeForm, we
{% endcomment %}
<form method="POST" action="{% url pledge work_id=work.id %}">
{% csrf_token %}
@ -51,7 +57,7 @@
<ul class="support menu">
{% for premium in premiums %}
<li class="{% if forloop.first %}first{% else %}{% if forloop.last %}last{% endif %}{% endif %}">
<input type="radio" name="premium_id" 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.REQUEST.premium_id premium.id|stringformat:"s" %}checked="checked"{% endifequal %}" />
<span class="menu-item-price">${{ premium.amount }}</span>
<span class="menu-item-desc">{{ premium.description }}</span>
</a></li>

View File

@ -149,9 +149,7 @@ class PledgeView(FormView):
campaign = work.last_campaign()
if campaign:
premiums = campaign.premiums.all()
if premiums.count() == 0:
premiums = models.Premium.objects.filter(campaign__isnull=True)
premiums = campaign.effective_premiums()
premium_id = self.request.REQUEST.get('premium_id', None)
preapproval_amount = self.request.REQUEST.get('preapproval_amount', None)
@ -165,9 +163,9 @@ class PledgeView(FormView):
logger.info("preapproval_amount, premium_id: %s %s ", preapproval_amount, premium_id)
data = {'preapproval_amount':preapproval_amount, 'premium_id':premium_id}
form = CampaignPledgeForm(data, premiums=premiums)
form = CampaignPledgeForm(data)
context.update({'work':work,'campaign':campaign, 'premiums':premiums, 'form':form})
context.update({'work':work,'campaign':campaign, 'premiums':premiums, 'form':form, 'premium_id':premium_id})
return context
def form_valid(self, form):