class UpgradeForm(forms.Form, SavesPayment): #TODO: Fill in first name if available first_name = forms.CharField( label="First name", min_length=2, max_length=30, required=False, ) last_name = forms.CharField( label="Last name", min_length=2, max_length=30, required=False, ) card_number = forms.CharField( label="Card number", required=False, ) card_expiration = forms.DateField( label="Expiration", required=False, ) def __init__(self, requires_payment, *args, **kwargs): self.requires_payment = requires_payment forms.Form.__init__(self, *args, **kwargs)
def formfield(self, **kwargs): defaults = { 'required': not self.blank, 'label': capfirst(self.verbose_name), 'help_text': self.help_text } defaults.update(kwargs) return forms.DateField(**defaults)
class PaymentForm(forms.Form, SavesPayment): first_name = forms.CharField( label="First name", min_length=2, max_length=30, ) last_name = forms.CharField( label="Last name", min_length=2, max_length=30, ) card_number = forms.CharField(label="Card number", ) card_expiration = forms.DateField(label="Expiration", )
class ExtendedContactInfoForm(ContactInfoForm): """Contact form which includes birthday and newsletter.""" dob = forms.DateField(required=False) newsletter = forms.BooleanField(label=_('Newsletter'), widget=forms.CheckboxInput(), required=False)
class FilterHistoryForm(forms.Form): date_from = forms.DateField(required=False) date_to = forms.DateField(required=False) keywords = forms.CharField(required=False)
class SignupForm(forms.Form, SavesPayment): def __init__(self, requires_payment, *args, **kwargs): self.requires_payment = requires_payment forms.Form.__init__(self, *args, **kwargs) if not requires_payment: del self.fields['card_number'] del self.fields['card_expiration'] first_name = forms.CharField( label="First name", min_length=2, max_length=30, ) last_name = forms.CharField( label="Last name", min_length=2, max_length=30, ) email = forms.EmailField(label="Email", ) username = forms.CharField( label="Username", help_text="You'll use this to log in", min_length=4, max_length=30, ) password = forms.CharField(label="Password", min_length=6, max_length=20, widget=forms.PasswordInput()) password2 = forms.CharField( label="Password again", help_text="Confirm your password by entering it again", min_length=6, max_length=20, widget=forms.PasswordInput()) group = forms.CharField( label="Company / Organization", min_length=2, max_length=30, ) timezone = forms.ChoiceField( label="Time zone", choices=settings.ACCOUNT_TIME_ZONES, initial=settings.ACCOUNT_DEFAULT_TIME_ZONE, ) # Domain must come BEFORE subdomain. Cleaning order is important. domain = forms.ChoiceField(choices=settings.ACCOUNT_DOMAINS) subdomain = forms.CharField( min_length=2, max_length=30, ) #card_type = forms.ChoiceField( #label = "Card type", #choices = enumerate(['Visa', 'Mastercard', 'AmericanExpress']), #required = False, #) card_number = forms.CharField( label="Card number", required=False, ) card_expiration = forms.DateField( label="Expiration", required=False, ) terms_of_service = forms.BooleanField( label="I agree to the Terms of Service, Refund, and Privacy policies") def clean_password2(self): if self.cleaned_data['password'] != self.cleaned_data['password2']: raise forms.ValidationError( "The two passwords didn't match. Please try again.") return self.cleaned_data['password2'] def clean_subdomain(self): if not self.cleaned_data['subdomain'].isalnum(): raise forms.ValidationError( "Your subdomain can only include letters and numbers.") try: Account.objects.get( subdomain=self.cleaned_data['subdomain'], domain=self.data['domain'], ) raise forms.ValidationError( "The domain %s.%s has already been taken" % (self.cleaned_data['subdomain'], self.cleaned_data['domain'])) except Account.DoesNotExist: pass return self.cleaned_data['subdomain'] def clean_terms_of_service(self): if not self.cleaned_data['terms_of_service']: raise forms.ValidationError( "Sorry, but we can't create your account unless you accept the terms of service." ) def save_account(self, level): account = Account( domain=self.cleaned_data['domain'], subdomain=self.cleaned_data['subdomain'], timezone=self.cleaned_data['timezone'], name=self.cleaned_data['group'], subscription_level_id=level, ) if not account.validate(): account.save() return account else: raise ValueError def save_person(self, account): person = Person( account=account, username=self.cleaned_data['username'], first_name=self.cleaned_data['first_name'], last_name=self.cleaned_data['last_name'], email=self.cleaned_data['email'], ) person.set_password(self.cleaned_data['password']) if not person.validate(): person.save() return person else: raise ValueError