2011-11-29 15:48:37 +00:00
|
|
|
import datetime
|
2011-10-03 16:36:04 +00:00
|
|
|
from django import forms
|
|
|
|
from django.db import models
|
|
|
|
from django.contrib.auth.models import User
|
2011-11-29 15:48:37 +00:00
|
|
|
from django.conf import settings
|
2011-10-03 16:36:04 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
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
|
|
|
|
2011-11-30 02:02:51 +00:00
|
|
|
from regluit.core.models import UserProfile, RightsHolder, Claim, Campaign, Premium
|
2011-11-18 14:22:21 +00:00
|
|
|
from regluit.core.lookups import OwnerLookup
|
2011-11-15 23:20:29 +00:00
|
|
|
|
2011-11-30 02:02:51 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2011-11-23 16:06:48 +00:00
|
|
|
def UserClaimForm ( user_instance, *args, **kwargs ):
|
|
|
|
class ClaimForm(forms.ModelForm):
|
|
|
|
i_agree=forms.BooleanField()
|
|
|
|
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,
|
|
|
|
)
|
2011-11-21 03:23:51 +00:00
|
|
|
email = forms.EmailField(
|
|
|
|
label=_("notification email address for rights holder"),
|
|
|
|
max_length=100,
|
|
|
|
)
|
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)
|
2011-10-03 16:36:04 +00:00
|
|
|
class Meta:
|
|
|
|
model = UserProfile
|
2011-11-14 18:23:14 +00:00
|
|
|
fields = 'tagline', 'librarything_id', 'home_url', 'clear_facebook', 'clear_twitter'
|
2011-10-03 16:36:04 +00:00
|
|
|
widgets = {
|
2011-12-16 17:17:08 +00:00
|
|
|
'tagline': forms.Textarea(attrs={'cols': 25, 'rows': 5}),
|
2011-10-03 16:36:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class UserData(forms.Form):
|
|
|
|
username = forms.RegexField(
|
|
|
|
label=_("New Username"),
|
|
|
|
max_length=30,
|
|
|
|
regex=r'^[\w.@+-]+$',
|
|
|
|
help_text = _("30 characters or less."),
|
|
|
|
error_messages = {
|
|
|
|
'invalid': _("This value may contain only letters, numbers and @/./+/-/_ characters.")
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def clean_username(self):
|
|
|
|
username = self.data["username"]
|
|
|
|
oldusername = self.data["oldusername"]
|
|
|
|
if username != oldusername:
|
|
|
|
try:
|
2011-11-17 19:35:29 +00:00
|
|
|
User.objects.get(username__iexact=username)
|
2011-10-03 16:36:04 +00:00
|
|
|
except User.DoesNotExist:
|
|
|
|
return username
|
|
|
|
raise forms.ValidationError(_("Another user with that username already exists."))
|
2011-10-09 19:17:43 +00:00
|
|
|
raise forms.ValidationError(_("Your username is already "+oldusername))
|
2011-10-11 17:03:40 +00:00
|
|
|
|
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),
|
2011-11-20 02:12:18 +00:00
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
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
|
|
|
|
2011-11-21 03:23:51 +00:00
|
|
|
class ManageCampaignForm(forms.ModelForm):
|
|
|
|
paypal_receiver = forms.EmailField(
|
|
|
|
label=_("email address to collect Paypal funds"),
|
|
|
|
max_length=100,
|
|
|
|
)
|
2011-11-24 02:41:06 +00:00
|
|
|
target = forms.DecimalField( min_value= D('0.00') )
|
2011-11-21 03:23:51 +00:00
|
|
|
class Meta:
|
|
|
|
model = Campaign
|
|
|
|
fields = 'description', 'details', 'target', 'deadline', 'paypal_receiver'
|
|
|
|
widgets = {
|
|
|
|
'description': forms.Textarea(attrs={'cols': 80, 'rows': 20}),
|
|
|
|
'details': forms.Textarea(attrs={'cols': 80, 'rows': 20}),
|
|
|
|
'deadline': SelectDateWidget
|
|
|
|
}
|
|
|
|
|
2011-11-26 02:23:37 +00:00
|
|
|
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.'))
|
2011-11-29 15:48:37 +00:00
|
|
|
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))
|
2011-11-26 02:23:37 +00:00
|
|
|
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.'))
|
2011-11-29 15:48:37 +00:00
|
|
|
if new_deadline-datetime.datetime.today() > datetime.timedelta(days=int(settings.UNGLUEIT_LONGEST_DEADLINE)):
|
|
|
|
raise forms.ValidationError(_('The chosen closing date is more than %s days from now' % settings.UNGLUEIT_LONGEST_DEADLINE))
|
|
|
|
elif new_deadline-datetime.datetime.today() < datetime.timedelta(days=int(settings.UNGLUEIT_SHORTEST_DEADLINE)):
|
|
|
|
raise forms.ValidationError(_('The chosen closing date is less than %s days from now' % settings.UNGLUEIT_SHORTEST_DEADLINE))
|
2011-11-26 02:23:37 +00:00
|
|
|
return new_deadline
|
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(
|
2011-11-28 23:39:06 +00:00
|
|
|
required=False,
|
|
|
|
min_value=D('1.00'),
|
2011-11-20 02:12:18 +00:00
|
|
|
max_value=D('10000.00'),
|
|
|
|
decimal_places=2,
|
|
|
|
label="Pledge Amount",
|
|
|
|
)
|
2011-11-21 03:23:51 +00:00
|
|
|
anonymous = forms.BooleanField(required=False, label=_("Don't display my username in the supporters list"))
|
2011-11-30 02:02:51 +00:00
|
|
|
|
|
|
|
premium_id = forms.IntegerField(required=False)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = self.cleaned_data
|
|
|
|
# check on whether the preapproval amount is < amount for premium tier. If so, put an error message
|
|
|
|
try:
|
|
|
|
preapproval_amount = cleaned_data.get("preapproval_amount")
|
|
|
|
premium_id = int(cleaned_data.get("premium_id"))
|
|
|
|
premium_amount = Premium.objects.get(id=premium_id).amount
|
|
|
|
if preapproval_amount < premium_amount:
|
2011-11-30 16:58:26 +00:00
|
|
|
raise forms.ValidationError(_("Sorry, you must pledge at least $%s to select that premium." % (premium_amount)))
|
2011-11-30 02:02:51 +00:00
|
|
|
except Exception, e:
|
|
|
|
if isinstance(e, forms.ValidationError):
|
|
|
|
raise e
|
|
|
|
|
|
|
|
return cleaned_data
|
|
|
|
|
2011-12-03 00:37:27 +00:00
|
|
|
class DonateForm(forms.Form):
|
|
|
|
donation_amount = forms.DecimalField(
|
|
|
|
required=False,
|
|
|
|
min_value=D('1.00'),
|
|
|
|
max_value=D('100000.00'),
|
|
|
|
decimal_places=2,
|
|
|
|
label="Donation",
|
|
|
|
)
|
|
|
|
anonymous = forms.BooleanField(required=False, label=_("Don't display my username in the donors' list"))
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = self.cleaned_data
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
|
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)
|