Though the form is ugly, a campaign manager can now launch a campaign and set info

I also learned what it means when a form is bound to a model object
pull/1/head
eric 2011-11-22 00:28:06 -05:00
parent bd9084ffea
commit 0bad939bfb
3 changed files with 67 additions and 8 deletions

View File

@ -91,7 +91,7 @@ class ManageCampaignForm(forms.ModelForm):
label=_("email address to collect Paypal funds"),
max_length=100,
)
target = forms.DecimalField( min_value=1000.00 )
target = forms.DecimalField( min_value= D('1000.00') )
class Meta:
model = Campaign
fields = 'description', 'details', 'target', 'deadline', 'paypal_receiver'

View File

@ -3,17 +3,24 @@
{% block title %}Campaign Management{% endblock %}
{% block doccontent %}
{% if not request.user in campaign.managers.all %}
<h2>You're not a manager for this campaign</h2>
{% if campaign.not_manager %}
<h2>You're not a manager for campaign: {{ campaign.name }}</h2>
{% else %}
{% for alert in alerts %}
<h1 class="alert">{{ alert }}</h1>
{% empty %}
<h1>Manage the campaign</h1>
{% endfor %}
<h2>The work</h2>
<div class="book-detail-info">
<h2 class="book-name">Title: {{ campaign.work.title }}</h2>
<h3 class="book-author">Authors: {{ campaign.work.author }}</h3>
<h3 class="book-year">Published: {{ campaign.work.editions.all.0.publication_date }}</h3>
<h3 class="book-author">Language: {{ campaign.work.editions.all.0.language }}</h3>
<p>Target Price: {{ campaign.pretarget }}</p>
<p>End Date: {{ campaign.predeadline }}</p>
</div>
<h3>Description of the offering</h2>
<form action="#" method="POST">
{% csrf_token %}
@ -36,13 +43,18 @@
<h3>Ending date</h2>
<p> This is the ending date of your campaign. Once you launch the campaign, you won't be able to change it.
The ending date can't be more than six months away- that's a practical limit for credit card authorizations.</p>
{{ form.deadline }}{{ form.deadline.errors }}
<h3>Paypal collection address</h2>
<p> If your campaign succeeds, the funds raised (less commission and fees) will be deposited in a paypal account bearing this email address.</p>
{{ form.paypal_receiver }}{{ form.paypal_receiver.errors }}
<p> To save your work, or to launch the campaign, click a button below.</p>
{{ campaign.errors }}
{% for problem in problems %}
<p>{{ problem }}</p>
{% endfor %}
<input type="submit" name="save" value="Save Campaign" />
<input type="submit" name="launch" value="Launch Campaign" />
</form>

View File

@ -1,4 +1,5 @@
import logging
import datetime
from decimal import Decimal as D
from django.db.models import Q, Count
@ -16,6 +17,7 @@ from django.views.generic.edit import FormView
from django.views.generic.base import TemplateView
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, render_to_response, get_object_or_404
from django.utils.translation import ugettext_lazy as _
import oauth2 as oauth
from itertools import islice
@ -80,8 +82,53 @@ def work(request, work_id, action='display'):
def manage_campaign(request, id):
campaign = get_object_or_404(models.Campaign, id=id)
form= ManageCampaignForm(instance=campaign)
return render(request, 'manage_campaign.html', {'campaign': campaign, 'form':form})
if (not request.user.is_authenticated) or (not request.user in campaign.managers.all()):
campaign.not_manager=True
return render(request, 'manage_campaign.html', {'campaign': campaign})
problems = []
alerts = []
campaign.savable = True
if campaign.status == 'INITIALIZED':
campaign.launchable = True
else:
campaign.launchable = False
if request.method == 'POST':
campaign.pretarget=campaign.target
campaign.predeadline=campaign.deadline
form= ManageCampaignForm(instance=campaign, data=request.POST)
if form.is_valid():
# might be a good idea to move this code to the model
# general constraints
if form.cleaned_data['target'] < D('1000'):
problems.append(_('The minimum target to launch a campaign is 1000'))
campaign.launchable = False
if form.cleaned_data['deadline'].date()-datetime.date.today() > datetime.timedelta(days=180):
problems.append(_('The chosen closing date is more than 6 months away'))
campaign.launchable = False
if campaign.status == 'ACTIVE':
#special constraints on active campaigns
# can't increase target
if campaign.pretarget < campaign.target:
problems.append(_('The fundraising target for an ACTIVE campaign cannot be increased.'))
campaign.savable = False
# can't change deadline
if campaign.deadline != campaign.predeadline:
problems.append(_('The closing date for an ACTIVE campaign cannot be changed.'))
campaign.savable = False
if campaign.savable:
form.save()
alerts.append(_('Campaign data has been saved'))
else:
alerts.append(_('Campaign data has NOT been saved'))
if campaign.launchable and 'launch' in request.POST.keys():
campaign.activate()
alerts.append(_('Campaign has been launched'))
elif 'launch' in request.POST.keys():
alerts.append(_('Campaign has NOT been launched'))
else:
form= ManageCampaignForm(instance=campaign)
return render(request, 'manage_campaign.html', {'campaign': campaign, 'form':form, 'problems': problems, 'alerts': alerts})
def googlebooks(request, googlebooks_id):
try:
@ -150,8 +197,8 @@ def rh_tools(request):
claim.campaigns= claim.work.campaigns.all()
claim.can_open_new=True
for campaign in claim.campaigns:
if campaign.status in ['ACTIVE','INITIALIZED']:
claim.can_open_new=False
if campaign.status in ['ACTIVE','INITIALIZED']:
claim.can_open_new=False
if claim.status == 'active' and claim.can_open_new:
if request.method == 'POST' and int(request.POST['work']) == claim.work.id :
claim.campaign_form = OpenCampaignForm(request.POST)