refactored campaign management savable constraints out of view and into form
parent
d5bae40c00
commit
b398a948dc
|
@ -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(
|
||||||
|
|
|
@ -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
|
form.save()
|
||||||
# general constraints
|
alerts.append(_('Campaign data has been saved'))
|
||||||
if campaign.savable:
|
else:
|
||||||
form.save()
|
alerts.append(_('Campaign data has NOT been saved'))
|
||||||
alerts.append(_('Campaign data has been saved'))
|
if 'launch' in request.POST.keys():
|
||||||
else:
|
if campaign.launchable :
|
||||||
alerts.append(_('Campaign data has NOT been saved'))
|
|
||||||
if campaign.launchable and 'launch' in request.POST.keys():
|
|
||||||
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)
|
||||||
|
|
Loading…
Reference in New Issue