refactored campaign management savable constraints out of view and into form

pull/1/head
eric 2011-11-25 21:23:37 -05:00
parent d5bae40c00
commit b398a948dc
2 changed files with 22 additions and 13 deletions

View File

@ -112,6 +112,19 @@ class ManageCampaignForm(forms.ModelForm):
'deadline': SelectDateWidget 'deadline': SelectDateWidget
} }
def clean_target(self):
new_target = self.cleaned_data['target']
if self.instance:
if self.instance.status == 'ACTIVE' and self.instance.target < new_target:
raise forms.ValidationError(_('The fundraising target for an ACTIVE campaign cannot be increased.'))
return new_target
def clean_deadline(self):
new_deadline = self.cleaned_data['deadline']
if self.instance:
if self.instance.status == 'ACTIVE' and self.instance.deadline != new_deadline:
raise forms.ValidationError(_('The closing date for an ACTIVE campaign cannot be changed.'))
return new_deadline
class CampaignPledgeForm(forms.Form): class CampaignPledgeForm(forms.Form):
preapproval_amount = forms.DecimalField( preapproval_amount = forms.DecimalField(

View File

@ -86,28 +86,24 @@ def work(request, work_id, action='display'):
def manage_campaign(request, id): def manage_campaign(request, id):
campaign = get_object_or_404(models.Campaign, id=id) campaign = get_object_or_404(models.Campaign, id=id)
campaign.not_manager=False
campaign.problems=[]
if (not request.user.is_authenticated) or (not request.user in campaign.managers.all()): if (not request.user.is_authenticated) or (not request.user in campaign.managers.all()):
campaign.not_manager=True campaign.not_manager=True
return render(request, 'manage_campaign.html', {'campaign': campaign}) return render(request, 'manage_campaign.html', {'campaign': campaign})
campaign.problems = []
alerts = [] alerts = []
campaign.savable = True
if request.method == 'POST': if request.method == 'POST':
campaign.pretarget=campaign.target
campaign.predeadline=campaign.deadline
form= ManageCampaignForm(instance=campaign, data=request.POST) form= ManageCampaignForm(instance=campaign, data=request.POST)
if form.is_valid(): if form.is_valid():
# might be a good idea to move this code to the model
# general constraints
if campaign.savable:
form.save() form.save()
alerts.append(_('Campaign data has been saved')) alerts.append(_('Campaign data has been saved'))
else: else:
alerts.append(_('Campaign data has NOT been saved')) alerts.append(_('Campaign data has NOT been saved'))
if campaign.launchable and 'launch' in request.POST.keys(): if 'launch' in request.POST.keys():
if campaign.launchable :
campaign.activate() campaign.activate()
alerts.append(_('Campaign has been launched')) alerts.append(_('Campaign has been launched'))
elif 'launch' in request.POST.keys(): else:
alerts.append(_('Campaign has NOT been launched')) alerts.append(_('Campaign has NOT been launched'))
else: else:
form= ManageCampaignForm(instance=campaign) form= ManageCampaignForm(instance=campaign)