diff --git a/frontend/forms.py b/frontend/forms.py index 25744d2f..b7a03750 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -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' diff --git a/frontend/templates/manage_campaign.html b/frontend/templates/manage_campaign.html index 4178a3ed..d6b29da5 100644 --- a/frontend/templates/manage_campaign.html +++ b/frontend/templates/manage_campaign.html @@ -3,17 +3,24 @@ {% block title %}Campaign Management{% endblock %} {% block doccontent %} -{% if not request.user in campaign.managers.all %} -

You're not a manager for this campaign

+{% if campaign.not_manager %} +

You're not a manager for campaign: {{ campaign.name }}

{% else %} +{% for alert in alerts %} +

{{ alert }}

+{% empty %}

Manage the campaign

+{% endfor %}

The work

Title: {{ campaign.work.title }}

Authors: {{ campaign.work.author }}

Published: {{ campaign.work.editions.all.0.publication_date }}

Language: {{ campaign.work.editions.all.0.language }}

+

Target Price: {{ campaign.pretarget }}

+

End Date: {{ campaign.predeadline }}

+

Description of the offering

{% csrf_token %} @@ -36,13 +43,18 @@

Ending date

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.

+ {{ form.deadline }}{{ form.deadline.errors }} +

Paypal collection address

If your campaign succeeds, the funds raised (less commission and fees) will be deposited in a paypal account bearing this email address.

{{ form.paypal_receiver }}{{ form.paypal_receiver.errors }}

To save your work, or to launch the campaign, click a button below.

-{{ campaign.errors }} + +{% for problem in problems %} +

{{ problem }}

+{% endfor %}
diff --git a/frontend/views.py b/frontend/views.py index 8992cf51..707e97da 100755 --- a/frontend/views.py +++ b/frontend/views.py @@ -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)