2012-03-16 20:24:55 +00:00
|
|
|
from datetime import timedelta
|
2011-10-03 16:36:04 +00:00
|
|
|
from django import forms
|
|
|
|
from django.db import models
|
|
|
|
from django.contrib.auth.models import User
|
2013-03-12 18:26:40 +00:00
|
|
|
from django.contrib.auth.forms import AuthenticationForm
|
2011-11-29 15:48:37 +00:00
|
|
|
from django.conf import settings
|
2012-05-11 18:13:09 +00:00
|
|
|
from django.conf.global_settings import LANGUAGES
|
2011-12-29 15:40:35 +00:00
|
|
|
from django.core.validators import validate_email
|
2011-10-03 16:36:04 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2012-05-01 14:56:19 +00:00
|
|
|
from django.forms.widgets import RadioSelect
|
2011-11-21 03:23:51 +00:00
|
|
|
from django.forms.extras.widgets import SelectDateWidget
|
2011-11-18 14:22:21 +00:00
|
|
|
|
2011-10-11 17:03:40 +00:00
|
|
|
from decimal import Decimal as D
|
2011-11-20 19:02:09 +00:00
|
|
|
from selectable.forms import AutoCompleteSelectMultipleWidget,AutoCompleteSelectMultipleField
|
2011-11-20 19:22:51 +00:00
|
|
|
from selectable.forms import AutoCompleteSelectWidget,AutoCompleteSelectField
|
2011-11-18 14:22:21 +00:00
|
|
|
|
2012-12-13 03:35:35 +00:00
|
|
|
from regluit.core.models import UserProfile, RightsHolder, Claim, Campaign, Premium, Ebook, Edition, PledgeExtra, Work
|
|
|
|
from regluit.core.lookups import OwnerLookup, WorkLookup
|
2011-11-15 23:20:29 +00:00
|
|
|
|
2012-03-23 16:29:38 +00:00
|
|
|
from regluit.utils.localdatetime import now
|
2012-03-16 20:24:55 +00:00
|
|
|
|
2011-11-30 02:02:51 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2012-05-11 18:13:09 +00:00
|
|
|
class EditionForm(forms.ModelForm):
|
|
|
|
add_author = forms.CharField(max_length=500, required=False)
|
|
|
|
add_subject = forms.CharField(max_length=200, required=False)
|
|
|
|
isbn_13 = forms.RegexField(
|
|
|
|
label=_("ISBN"),
|
|
|
|
max_length=13,
|
|
|
|
regex=r'^97[89]\d\d\d\d\d\d\d\d\d\d$',
|
|
|
|
required = True,
|
|
|
|
help_text = _("13 digits, no dash."),
|
|
|
|
error_messages = {
|
|
|
|
'invalid': _("This value must be 13 digits, starting with 978 or 979."),
|
|
|
|
'required': _("Yes, we need an ISBN."),
|
|
|
|
}
|
|
|
|
)
|
2012-09-25 01:51:57 +00:00
|
|
|
oclcnum = forms.RegexField(
|
|
|
|
label=_("OCLCnum"),
|
|
|
|
regex=r'^\d\d\d\d\d\d\d\d\d*$',
|
|
|
|
required = False,
|
|
|
|
help_text = _("8 or more digits."),
|
|
|
|
error_messages = {
|
|
|
|
'invalid': _("This value must be 8 or more digits."),
|
|
|
|
}
|
|
|
|
)
|
2012-05-11 18:13:09 +00:00
|
|
|
language = forms.ChoiceField(choices=LANGUAGES)
|
|
|
|
description = forms.CharField( required=False, widget= forms.Textarea(attrs={'cols': 80, 'rows': 2}))
|
|
|
|
class Meta:
|
|
|
|
model = Edition
|
|
|
|
exclude = 'created', 'work'
|
|
|
|
widgets = {
|
|
|
|
'title': forms.TextInput(attrs={'size': 40}),
|
|
|
|
'add_author': forms.TextInput(attrs={'size': 30}),
|
|
|
|
'add_subject': forms.TextInput(attrs={'size': 30}),
|
2012-09-10 19:18:40 +00:00
|
|
|
'unglued': forms.CheckboxInput(),
|
2012-05-11 18:13:09 +00:00
|
|
|
}
|
|
|
|
|
2012-02-28 22:28:33 +00:00
|
|
|
class EbookForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = Ebook
|
2012-09-10 19:18:40 +00:00
|
|
|
exclude =( 'created',)
|
2012-02-28 22:28:33 +00:00
|
|
|
widgets = {
|
|
|
|
'edition': forms.HiddenInput,
|
|
|
|
'user': forms.HiddenInput,
|
|
|
|
'provider': forms.HiddenInput,
|
|
|
|
'url': forms.TextInput(attrs={'size' : 60}),
|
|
|
|
}
|
|
|
|
def clean_provider(self):
|
2012-04-27 21:14:57 +00:00
|
|
|
new_provider= Ebook.infer_provider(self.data[self.prefix + '-url'])
|
2012-02-28 22:28:33 +00:00
|
|
|
if not new_provider:
|
|
|
|
raise forms.ValidationError(_("At this time, ebook URLs must point at Internet Archive, Wikisources, Hathitrust, Project Gutenberg, or Google Books."))
|
|
|
|
return new_provider
|
|
|
|
|
2012-03-05 21:03:57 +00:00
|
|
|
def clean_url(self):
|
2012-04-27 21:14:57 +00:00
|
|
|
url = self.data[self.prefix + '-url']
|
2012-03-05 21:03:57 +00:00
|
|
|
try:
|
|
|
|
Ebook.objects.get(url=url)
|
|
|
|
except Ebook.DoesNotExist:
|
|
|
|
return url
|
|
|
|
raise forms.ValidationError(_("There's already an ebook with that url."))
|
|
|
|
|
2011-11-23 16:06:48 +00:00
|
|
|
def UserClaimForm ( user_instance, *args, **kwargs ):
|
|
|
|
class ClaimForm(forms.ModelForm):
|
2012-04-16 19:28:06 +00:00
|
|
|
i_agree=forms.BooleanField(error_messages={'required': 'You must agree to the Terms in order to claim a work.'})
|
2011-11-23 16:06:48 +00:00
|
|
|
rights_holder=forms.ModelChoiceField(queryset=user_instance.rights_holder.all(), empty_label=None)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Claim
|
|
|
|
exclude = 'status'
|
|
|
|
widgets = {
|
|
|
|
'user': forms.HiddenInput,
|
|
|
|
'work': forms.HiddenInput,
|
|
|
|
}
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(ClaimForm, self).__init__(*args, **kwargs)
|
2011-11-15 04:28:55 +00:00
|
|
|
|
2011-11-23 16:06:48 +00:00
|
|
|
return ClaimForm()
|
|
|
|
|
2011-11-15 04:28:55 +00:00
|
|
|
class RightsHolderForm(forms.ModelForm):
|
2011-11-18 14:22:21 +00:00
|
|
|
owner = AutoCompleteSelectField(
|
|
|
|
OwnerLookup,
|
|
|
|
label='Owner',
|
|
|
|
widget=AutoCompleteSelectWidget(OwnerLookup),
|
|
|
|
required=True,
|
2012-05-08 20:15:23 +00:00
|
|
|
error_messages={'required': 'Please ensure the owner is a valid Unglue.it account.'},
|
2011-11-18 14:22:21 +00:00
|
|
|
)
|
2011-11-21 03:23:51 +00:00
|
|
|
email = forms.EmailField(
|
|
|
|
label=_("notification email address for rights holder"),
|
2012-04-16 19:28:06 +00:00
|
|
|
max_length=100,
|
|
|
|
error_messages={'required': 'Please enter an email address for the rights holder.'},
|
2011-11-21 03:23:51 +00:00
|
|
|
)
|
2011-11-15 04:28:55 +00:00
|
|
|
class Meta:
|
|
|
|
model = RightsHolder
|
|
|
|
|
2011-11-17 19:35:29 +00:00
|
|
|
def clean_rights_holder_name(self):
|
|
|
|
rights_holder_name = self.data["rights_holder_name"]
|
|
|
|
try:
|
|
|
|
RightsHolder.objects.get(rights_holder_name__iexact=rights_holder_name)
|
2011-11-18 14:22:21 +00:00
|
|
|
except RightsHolder.DoesNotExist:
|
2011-11-17 19:35:29 +00:00
|
|
|
return rights_holder_name
|
|
|
|
raise forms.ValidationError(_("Another rights holder with that name already exists."))
|
2011-11-18 14:22:21 +00:00
|
|
|
|
2011-10-03 16:36:04 +00:00
|
|
|
class ProfileForm(forms.ModelForm):
|
2011-11-14 18:23:14 +00:00
|
|
|
clear_facebook=forms.BooleanField(required=False)
|
|
|
|
clear_twitter=forms.BooleanField(required=False)
|
2012-01-24 17:36:45 +00:00
|
|
|
clear_goodreads=forms.BooleanField(required=False)
|
2011-10-03 16:36:04 +00:00
|
|
|
class Meta:
|
|
|
|
model = UserProfile
|
2012-01-24 17:36:45 +00:00
|
|
|
fields = 'tagline', 'librarything_id', 'home_url', 'clear_facebook', 'clear_twitter', 'clear_goodreads'
|
2011-10-03 16:36:04 +00:00
|
|
|
widgets = {
|
2012-02-02 18:55:21 +00:00
|
|
|
'tagline': forms.Textarea(attrs={'rows': 5, 'onKeyUp': "counter(this, 140)", 'onBlur': "counter(this, 140)"}),
|
2011-10-03 16:36:04 +00:00
|
|
|
}
|
|
|
|
|
2012-04-25 02:20:10 +00:00
|
|
|
class UserEmail(forms.Form):
|
|
|
|
email = forms.EmailField(
|
|
|
|
label=_("new email address"),
|
|
|
|
max_length=100,
|
|
|
|
error_messages={'required': 'Please enter an email address.'},
|
|
|
|
)
|
2012-05-26 21:28:57 +00:00
|
|
|
oldemail = None
|
|
|
|
|
|
|
|
def clean_email(self):
|
|
|
|
email = self.data["email"]
|
|
|
|
if email != self.oldemail:
|
|
|
|
users = User.objects.filter(email__iexact=email)
|
|
|
|
for user in users:
|
|
|
|
raise forms.ValidationError(_("Another user with that email already exists."))
|
|
|
|
return email
|
|
|
|
raise forms.ValidationError(_("Your email is already "+ email))
|
2012-04-25 02:20:10 +00:00
|
|
|
|
2011-10-03 16:36:04 +00:00
|
|
|
class UserData(forms.Form):
|
|
|
|
username = forms.RegexField(
|
|
|
|
label=_("New Username"),
|
|
|
|
max_length=30,
|
|
|
|
regex=r'^[\w.@+-]+$',
|
2012-01-04 16:34:31 +00:00
|
|
|
help_text = _("30 characters or fewer."),
|
2011-10-03 16:36:04 +00:00
|
|
|
error_messages = {
|
|
|
|
'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")
|
|
|
|
}
|
|
|
|
)
|
2012-05-26 21:30:12 +00:00
|
|
|
oldusername = None
|
2011-10-03 16:36:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def clean_username(self):
|
|
|
|
username = self.data["username"]
|
2012-05-26 21:30:12 +00:00
|
|
|
if username != self.oldusername:
|
|
|
|
users = User.objects.filter(username__iexact=username)
|
|
|
|
for user in users:
|
|
|
|
raise forms.ValidationError(_("Another user with that username already exists."))
|
|
|
|
return username
|
|
|
|
raise forms.ValidationError(_("Your username is already "+username))
|
2011-10-11 17:03:40 +00:00
|
|
|
|
2012-09-21 16:10:13 +00:00
|
|
|
class CloneCampaignForm(forms.Form):
|
|
|
|
campaign_id = forms.IntegerField(required = True, widget = forms.HiddenInput)
|
|
|
|
|
2011-11-20 02:12:18 +00:00
|
|
|
class OpenCampaignForm(forms.ModelForm):
|
2011-11-21 03:23:51 +00:00
|
|
|
managers = AutoCompleteSelectMultipleField(
|
2011-11-20 02:12:18 +00:00
|
|
|
OwnerLookup,
|
2011-11-21 03:23:51 +00:00
|
|
|
label='Campaign Managers',
|
2011-11-20 19:02:09 +00:00
|
|
|
widget=AutoCompleteSelectMultipleWidget(OwnerLookup),
|
2012-04-03 12:20:46 +00:00
|
|
|
required=True,
|
2012-04-09 15:14:31 +00:00
|
|
|
error_messages = {'required': "You must have at least one manager for a campaign."},
|
2011-11-20 02:12:18 +00:00
|
|
|
)
|
|
|
|
userid = forms.IntegerField( required = True, widget = forms.HiddenInput )
|
|
|
|
class Meta:
|
|
|
|
model = Campaign
|
2011-11-29 04:37:17 +00:00
|
|
|
fields = 'name', 'work', 'managers'
|
|
|
|
widgets = { 'work': forms.HiddenInput }
|
2011-11-20 02:12:18 +00:00
|
|
|
|
2012-08-14 01:00:28 +00:00
|
|
|
def getTransferCreditForm(maximum, data=None, *args, **kwargs ):
|
|
|
|
class TransferCreditForm(forms.Form):
|
|
|
|
recipient = AutoCompleteSelectField(
|
|
|
|
OwnerLookup,
|
|
|
|
label='Recipient',
|
|
|
|
widget=AutoCompleteSelectWidget(OwnerLookup),
|
|
|
|
required=True,
|
|
|
|
error_messages={'required': 'Please ensure the recipient is a valid Unglue.it account.'},
|
|
|
|
)
|
|
|
|
amount = forms.IntegerField(
|
|
|
|
required=True,
|
|
|
|
min_value=1,
|
|
|
|
max_value=maximum,
|
|
|
|
label="Transfer Amount",
|
|
|
|
error_messages={'min_value': 'Transfer amount must be positive', 'max_value': 'You only have %(limit_value)s available for transfer'},
|
|
|
|
)
|
|
|
|
return TransferCreditForm( data=data )
|
|
|
|
|
2012-12-13 03:35:35 +00:00
|
|
|
|
|
|
|
class WorkForm(forms.Form):
|
|
|
|
other_work = forms.ModelChoiceField(queryset=Work.objects.all(),
|
|
|
|
widget=forms.HiddenInput(),
|
|
|
|
required=True,
|
|
|
|
error_messages={'required': 'Missing work to merge with.'},
|
|
|
|
)
|
|
|
|
work=None
|
|
|
|
|
|
|
|
def clean_other_work(self):
|
|
|
|
if self.cleaned_data["other_work"].id== self.work.id:
|
|
|
|
raise forms.ValidationError(_("You can't merge a work into itself"))
|
|
|
|
return self.cleaned_data["other_work"]
|
|
|
|
|
|
|
|
def __init__(self, work=None, *args, **kwargs):
|
|
|
|
super(WorkForm, self).__init__(*args, **kwargs)
|
|
|
|
self.work=work
|
|
|
|
|
|
|
|
class OtherWorkForm(WorkForm):
|
|
|
|
other_work = AutoCompleteSelectField(
|
|
|
|
WorkLookup,
|
|
|
|
label='Other Work',
|
|
|
|
widget=AutoCompleteSelectWidget(WorkLookup),
|
|
|
|
required=True,
|
|
|
|
error_messages={'required': 'Missing work to merge with.'},
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(OtherWorkForm, self).__init__(*args, **kwargs)
|
|
|
|
self.fields['other_work'].widget.update_query_parameters({'language':self.work.language})
|
2012-08-14 01:00:28 +00:00
|
|
|
|
2012-04-03 19:50:02 +00:00
|
|
|
class EditManagersForm(forms.ModelForm):
|
|
|
|
managers = AutoCompleteSelectMultipleField(
|
|
|
|
OwnerLookup,
|
|
|
|
label='Campaign Managers',
|
|
|
|
widget=AutoCompleteSelectMultipleWidget(OwnerLookup),
|
|
|
|
required=True,
|
2012-04-09 15:14:31 +00:00
|
|
|
error_messages = {'required': "You must have at least one manager for a campaign."},
|
2012-04-03 19:50:02 +00:00
|
|
|
)
|
|
|
|
class Meta:
|
|
|
|
model = Campaign
|
|
|
|
fields = ('id', 'managers')
|
|
|
|
widgets = { 'id': forms.HiddenInput }
|
|
|
|
|
2012-03-26 19:31:41 +00:00
|
|
|
class CustomPremiumForm(forms.ModelForm):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Premium
|
|
|
|
fields = 'campaign', 'amount', 'description', 'type', 'limit'
|
|
|
|
widgets = {
|
2012-10-04 16:08:40 +00:00
|
|
|
'description': forms.Textarea(attrs={'cols': 80, 'rows': 4}),
|
2012-03-26 19:31:41 +00:00
|
|
|
'campaign': forms.HiddenInput,
|
|
|
|
'type': forms.HiddenInput(attrs={'value':'XX'}),
|
|
|
|
'limit': forms.TextInput(attrs={'value':'0'}),
|
|
|
|
}
|
2012-05-01 14:56:19 +00:00
|
|
|
def getManageCampaignForm ( instance, data=None, *args, **kwargs ):
|
|
|
|
def get_queryset():
|
|
|
|
work=instance.work
|
|
|
|
return Edition.objects.filter(work = work)
|
|
|
|
|
|
|
|
class ManageCampaignForm(forms.ModelForm):
|
|
|
|
paypal_receiver = forms.EmailField(
|
2012-09-27 16:35:23 +00:00
|
|
|
label=_("contact email address for this campaign"),
|
2012-05-01 14:56:19 +00:00
|
|
|
max_length=100,
|
2012-09-27 16:35:23 +00:00
|
|
|
error_messages={'required': 'You must enter the email we should contact you at for this campaign.'},
|
2012-05-01 14:56:19 +00:00
|
|
|
)
|
2012-05-07 22:00:52 +00:00
|
|
|
target = forms.DecimalField( min_value= D(settings.UNGLUEIT_MINIMUM_TARGET), error_messages={'required': 'Please specify a target price.'} )
|
2012-05-14 05:05:14 +00:00
|
|
|
edition = forms.ModelChoiceField(get_queryset(), widget=RadioSelect(),empty_label='no edition selected',required = False,)
|
2012-05-07 22:00:52 +00:00
|
|
|
minimum_target = settings.UNGLUEIT_MINIMUM_TARGET
|
|
|
|
latest_ending = (timedelta(days=int(settings.UNGLUEIT_LONGEST_DEADLINE)) + now()).date
|
2012-05-01 14:56:19 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Campaign
|
|
|
|
fields = 'description', 'details', 'license', 'target', 'deadline', 'paypal_receiver', 'edition'
|
|
|
|
widgets = {
|
|
|
|
'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.'))
|
|
|
|
if new_target < D(settings.UNGLUEIT_MINIMUM_TARGET):
|
|
|
|
raise forms.ValidationError(_('A campaign may not be launched with a target less than $%s' % settings.UNGLUEIT_MINIMUM_TARGET))
|
|
|
|
return new_target
|
2012-07-07 02:10:55 +00:00
|
|
|
|
2012-05-01 14:56:19 +00:00
|
|
|
def clean_deadline(self):
|
2012-07-07 02:10:55 +00:00
|
|
|
new_deadline_date = self.cleaned_data['deadline']
|
|
|
|
new_deadline= new_deadline_date + timedelta(hours=23,minutes=59)
|
2012-05-01 14:56:19 +00:00
|
|
|
if self.instance:
|
2012-07-07 02:10:55 +00:00
|
|
|
if self.instance.status == 'ACTIVE' and self.instance.deadline.date() != new_deadline.date():
|
|
|
|
raise forms.ValidationError(_('The closing date for an ACTIVE campaign cannot be changed.'))
|
|
|
|
if new_deadline_date - now() > timedelta(days=int(settings.UNGLUEIT_LONGEST_DEADLINE)):
|
2012-05-01 14:56:19 +00:00
|
|
|
raise forms.ValidationError(_('The chosen closing date is more than %s days from now' % settings.UNGLUEIT_LONGEST_DEADLINE))
|
2012-05-16 03:42:31 +00:00
|
|
|
elif new_deadline - now() < timedelta(days=0):
|
|
|
|
raise forms.ValidationError(_('The chosen closing date is in the past'))
|
2012-05-01 14:56:19 +00:00
|
|
|
return new_deadline
|
|
|
|
|
|
|
|
def clean_license(self):
|
|
|
|
new_license = self.cleaned_data['license']
|
|
|
|
if self.instance:
|
|
|
|
if self.instance.status == 'ACTIVE' and self.instance.license != new_license:
|
2012-05-20 04:13:12 +00:00
|
|
|
# should only allow change to a less restrictive license
|
2012-05-21 13:25:38 +00:00
|
|
|
if self.instance.license == 'CC BY-ND' and new_license in ['CC BY-NC-ND','CC BY-NC-SA','CC BY-NC']:
|
2012-05-20 04:13:12 +00:00
|
|
|
raise forms.ValidationError(_('The proposed license for an ACTIVE campaign may not add restrictions.'))
|
|
|
|
elif self.instance.license == 'CC BY' and new_license != 'CC0':
|
|
|
|
raise forms.ValidationError(_('The proposed license for an ACTIVE campaign may not add restrictions.'))
|
|
|
|
elif self.instance.license == 'CC BY-NC' and new_license in ['CC BY-NC-ND','CC BY-NC-SA','CC BY-SA','CC BY-ND']:
|
|
|
|
raise forms.ValidationError(_('The proposed license for an ACTIVE campaign may not add restrictions.'))
|
|
|
|
elif self.instance.license == 'CC BY-ND' and new_license in ['CC BY-NC-ND','CC BY-NC-SA','CC BY-SA','CC BY-NC']:
|
|
|
|
raise forms.ValidationError(_('The proposed license for an ACTIVE campaign may not add restrictions.'))
|
|
|
|
elif self.instance.license == 'CC BY-SA' and new_license in ['CC BY-NC-ND','CC BY-NC-SA','CC BY-ND','CC BY-NC']:
|
|
|
|
raise forms.ValidationError(_('The proposed license for an ACTIVE campaign may not add restrictions.'))
|
|
|
|
elif self.instance.license == 'CC BY-NC-SA' and new_license in ['CC BY-NC-ND','CC BY-ND']:
|
|
|
|
raise forms.ValidationError(_('The proposed license for an ACTIVE campaign may not add restrictions.'))
|
|
|
|
elif self.instance.license == 'CC0' :
|
|
|
|
raise forms.ValidationError(_('The proposed license for an ACTIVE campaign may not add restrictions.'))
|
2012-05-01 14:56:19 +00:00
|
|
|
return new_license
|
|
|
|
return ManageCampaignForm(instance = instance, data=data)
|
2011-11-20 02:12:18 +00:00
|
|
|
|
2011-10-11 17:03:40 +00:00
|
|
|
class CampaignPledgeForm(forms.Form):
|
2011-11-20 02:12:18 +00:00
|
|
|
preapproval_amount = forms.DecimalField(
|
2012-03-15 21:54:38 +00:00
|
|
|
required = False,
|
|
|
|
min_value=D('1.00'),
|
2012-10-13 14:13:46 +00:00
|
|
|
max_value=D('2000.00'),
|
2011-11-20 02:12:18 +00:00
|
|
|
decimal_places=2,
|
|
|
|
label="Pledge Amount",
|
|
|
|
)
|
2012-10-03 20:02:51 +00:00
|
|
|
anonymous = forms.BooleanField(required=False, label=_("Make this pledge anonymous, please"))
|
2012-08-01 12:52:39 +00:00
|
|
|
ack_name = forms.CharField(required=False, max_length=64, label=_("What name should we display?"))
|
|
|
|
ack_dedication = forms.CharField(required=False, max_length=140, label=_("Your dedication:"))
|
2011-11-30 02:02:51 +00:00
|
|
|
|
|
|
|
premium_id = forms.IntegerField(required=False)
|
2012-09-07 13:46:38 +00:00
|
|
|
premium=None
|
2012-03-15 21:54:38 +00:00
|
|
|
|
2012-09-07 13:46:38 +00:00
|
|
|
@property
|
|
|
|
def pledge_extra(self):
|
|
|
|
return PledgeExtra( anonymous=self.cleaned_data['anonymous'],
|
|
|
|
ack_name=self.cleaned_data['ack_name'],
|
|
|
|
ack_dedication=self.cleaned_data['ack_dedication'],
|
|
|
|
premium=self.premium)
|
|
|
|
|
2012-03-15 21:54:38 +00:00
|
|
|
def clean_preapproval_amount(self):
|
2012-08-31 07:16:04 +00:00
|
|
|
preapproval_amount = self.cleaned_data['preapproval_amount']
|
|
|
|
if preapproval_amount is None:
|
2012-03-15 21:54:38 +00:00
|
|
|
raise forms.ValidationError(_("Please enter a pledge amount."))
|
2012-08-31 07:16:04 +00:00
|
|
|
return preapproval_amount
|
2012-03-21 21:53:33 +00:00
|
|
|
|
2012-09-07 13:46:38 +00:00
|
|
|
def clean_premium_id(self):
|
|
|
|
premium_id = self.cleaned_data['premium_id']
|
|
|
|
try:
|
|
|
|
self.premium= Premium.objects.get(id=premium_id)
|
|
|
|
if self.premium.limit>0:
|
|
|
|
if self.premium.limit<=self.premium.premium_count:
|
|
|
|
raise forms.ValidationError(_("Sorry, that premium is fully subscribed."))
|
|
|
|
except Premium.DoesNotExist:
|
|
|
|
raise forms.ValidationError(_("Sorry, that premium is not valid."))
|
2011-11-30 02:02:51 +00:00
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
# check on whether the preapproval amount is < amount for premium tier. If so, put an error message
|
2012-10-13 14:13:46 +00:00
|
|
|
preapproval_amount = self.cleaned_data.get("preapproval_amount")
|
|
|
|
if preapproval_amount is None:
|
|
|
|
# preapproval_amount failed validation, that error is the relevant one
|
|
|
|
return self.cleaned_data
|
2012-10-14 15:33:13 +00:00
|
|
|
elif self.premium is None:
|
|
|
|
raise forms.ValidationError(_("Please select a premium." ))
|
2012-10-13 14:13:46 +00:00
|
|
|
elif preapproval_amount < self.premium.amount:
|
|
|
|
logger.info("raising form validating error")
|
|
|
|
raise forms.ValidationError(_("Sorry, you must pledge at least $%s to select that premium." % (self.premium.amount)))
|
2012-09-07 13:46:38 +00:00
|
|
|
return self.cleaned_data
|
2011-11-30 02:02:51 +00:00
|
|
|
|
2012-10-15 03:41:17 +00:00
|
|
|
class PlainCCForm(forms.Form):
|
|
|
|
stripe_token = forms.CharField(required=False, widget=forms.HiddenInput())
|
|
|
|
|
|
|
|
class CCForm(PlainCCForm):
|
2012-10-02 13:32:33 +00:00
|
|
|
username = forms.CharField(max_length=30, required=True, widget=forms.HiddenInput())
|
|
|
|
work_id = forms.IntegerField(required=False, widget=forms.HiddenInput())
|
2012-09-10 23:47:36 +00:00
|
|
|
preapproval_amount= forms.DecimalField(
|
2011-12-03 00:37:27 +00:00
|
|
|
required=False,
|
|
|
|
min_value=D('1.00'),
|
|
|
|
max_value=D('100000.00'),
|
|
|
|
decimal_places=2,
|
2012-09-10 23:47:36 +00:00
|
|
|
label="Pledge",
|
2011-12-03 00:37:27 +00:00
|
|
|
)
|
|
|
|
|
2012-09-06 05:01:17 +00:00
|
|
|
class DonateForm(forms.Form):
|
|
|
|
preapproval_amount = forms.DecimalField( widget=forms.HiddenInput() )
|
|
|
|
username = forms.CharField(max_length=30, required=True, widget=forms.HiddenInput() )
|
|
|
|
work_id = forms.IntegerField(required=False, widget=forms.HiddenInput() )
|
2012-10-05 16:08:06 +00:00
|
|
|
title = forms.CharField(max_length=200, required=False, widget=forms.HiddenInput() )
|
2012-09-06 05:01:17 +00:00
|
|
|
|
|
|
|
|
2011-11-01 00:26:05 +00:00
|
|
|
class GoodreadsShelfLoadingForm(forms.Form):
|
2011-11-15 20:51:38 +00:00
|
|
|
goodreads_shelf_name_number = forms.CharField(widget=forms.Select(choices=(
|
2011-11-01 00:26:05 +00:00
|
|
|
('all','all'),
|
|
|
|
)))
|
2011-11-16 18:20:10 +00:00
|
|
|
|
|
|
|
class LibraryThingForm(forms.Form):
|
2011-11-16 19:58:39 +00:00
|
|
|
lt_username = forms.CharField(max_length=30, required=True)
|
2012-05-21 14:56:18 +00:00
|
|
|
|
|
|
|
class PledgeCancelForm(forms.Form):
|
2012-05-23 14:22:48 +00:00
|
|
|
# which campaign whose active transaction to cancel?
|
2012-05-21 14:56:18 +00:00
|
|
|
campaign_id = forms.IntegerField(required=True, widget=forms.HiddenInput())
|
|
|
|
|
2012-05-23 14:22:48 +00:00
|
|
|
|
2011-12-20 22:42:06 +00:00
|
|
|
class CampaignAdminForm(forms.Form):
|
2012-05-21 14:56:18 +00:00
|
|
|
campaign_id = forms.IntegerField()
|
2011-12-29 01:43:52 +00:00
|
|
|
|
|
|
|
class EmailShareForm(forms.Form):
|
2012-04-16 19:28:06 +00:00
|
|
|
recipient = forms.EmailField(error_messages={'required': 'Please specify a recipient.'})
|
|
|
|
subject = forms.CharField(max_length=100, error_messages={'required': 'Please specify a subject.'})
|
|
|
|
message = forms.CharField(widget=forms.Textarea(), error_messages={'required': 'Please include a message.'})
|
2012-02-28 22:28:33 +00:00
|
|
|
# allows us to return user to original page by passing it as hidden form input
|
|
|
|
# we can't rely on POST or GET since the emailshare view handles both
|
|
|
|
# and may iterate several times as it catches user errors, losing URL info
|
|
|
|
next = forms.CharField(widget=forms.HiddenInput())
|
|
|
|
|
2012-01-09 20:53:09 +00:00
|
|
|
class FeedbackForm(forms.Form):
|
2012-04-16 19:28:06 +00:00
|
|
|
sender = forms.EmailField(widget=forms.TextInput(attrs={'size':50}), label="Your email", error_messages={'required': 'Please specify your email address.'})
|
|
|
|
subject = forms.CharField(max_length=500, widget=forms.TextInput(attrs={'size':50}), error_messages={'required': 'Please specify a subject.'})
|
|
|
|
message = forms.CharField(widget=forms.Textarea(), error_messages={'required': 'Please specify a message.'})
|
2012-02-28 22:28:33 +00:00
|
|
|
page = forms.CharField(widget=forms.HiddenInput())
|
2012-04-16 19:28:06 +00:00
|
|
|
notarobot = forms.IntegerField(label="Please prove you're not a robot", error_messages={'required': "You must do the sum to prove you're not a robot."})
|
2012-02-28 22:28:33 +00:00
|
|
|
answer = forms.IntegerField(widget=forms.HiddenInput())
|
|
|
|
num1 = forms.IntegerField(widget=forms.HiddenInput())
|
|
|
|
num2 = forms.IntegerField(widget=forms.HiddenInput())
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = self.cleaned_data
|
|
|
|
notarobot = str(cleaned_data.get("notarobot"))
|
|
|
|
answer = str(cleaned_data.get("answer"))
|
2012-04-16 19:28:06 +00:00
|
|
|
if notarobot != answer:
|
2012-02-28 22:28:33 +00:00
|
|
|
raise forms.ValidationError(_("Whoops, try that sum again."))
|
|
|
|
|
|
|
|
return cleaned_data
|
2013-03-12 18:26:40 +00:00
|
|
|
|
|
|
|
class AuthForm(AuthenticationForm):
|
|
|
|
def __init__(self, request=None, *args, **kwargs):
|
|
|
|
if request.method == 'GET':
|
|
|
|
saved_un= request.COOKIES.get('un', None)
|
|
|
|
super(AuthForm, self).__init__(initial={"username":saved_un},*args, **kwargs)
|
|
|
|
else:
|
|
|
|
super(AuthForm, self).__init__(*args, **kwargs)
|