2011-10-03 16:36:04 +00:00
|
|
|
from django import forms
|
|
|
|
from django.db import models
|
|
|
|
from regluit.core.models import UserProfile
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2011-10-11 17:03:40 +00:00
|
|
|
from decimal import Decimal as D
|
2011-10-03 16:36:04 +00:00
|
|
|
|
|
|
|
class ProfileForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = UserProfile
|
|
|
|
exclude = 'user'
|
|
|
|
widgets = {
|
|
|
|
'tagline': forms.Textarea(attrs={'cols': 70, 'rows': 2}),
|
|
|
|
}
|
|
|
|
|
|
|
|
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.")
|
|
|
|
}
|
|
|
|
)
|
|
|
|
# oldusername = forms.CharField(max_length=30)
|
|
|
|
|
|
|
|
|
|
|
|
def clean_username(self):
|
|
|
|
username = self.data["username"]
|
|
|
|
oldusername = self.data["oldusername"]
|
|
|
|
if username != oldusername:
|
|
|
|
try:
|
|
|
|
User.objects.get(username=username)
|
|
|
|
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
|
|
|
|
|
|
|
class CampaignPledgeForm(forms.Form):
|
2011-10-18 23:27:20 +00:00
|
|
|
pledge_amount = forms.DecimalField(initial=D('0.00'), min_value=D('0.00'), max_value=D('10000.00'), decimal_places=2)
|
|
|
|
preapproval_amount = forms.DecimalField(initial=D('20.00'), min_value=D('0.00'), max_value=D('10000.00'), decimal_places=2)
|
|
|
|
def clean(self):
|
|
|
|
# force a choice: only one of pledge_amount and pre_approval can be non-zero.
|
|
|
|
cleaned_data = self.cleaned_data
|
|
|
|
pledge_amount = cleaned_data.get("pledge_amount")
|
|
|
|
preapproval_amount = cleaned_data.get("preapproval_amount")
|
|
|
|
if pledge_amount > D('0.00') and preapproval_amount > D('0.00'):
|
|
|
|
raise forms.ValidationError("Only one of pledge_amount and pre_approval can be non-zero.")
|
|
|
|
|
|
|
|
return cleaned_data
|