123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- from django import forms
- from django.contrib.auth import authenticate, login
- from django.contrib.auth.models import User
- from django.contrib.sites.models import Site
- from django.utils.translation import ugettext_lazy as _
- from . import models
- from . import profile as profile_utils
- from . import constants
- from . import utils
- REQUIRED = (
- u"There was missing information on your order. Please correct " u"the errors below."
- )
- class BasePersonForm(forms.Form):
- """ There's a common theme of form data for person information. """
- first_name = forms.CharField(
- label=_(u"First name"),
- max_length=30,
- required=True,
- widget=forms.TextInput(attrs={"type": "text", "size": 25}),
- )
- last_name = forms.CharField(
- label=_(u"Last name"),
- max_length=30,
- required=True,
- widget=forms.TextInput(attrs={"type": "text", "size": 25}),
- )
- company = forms.CharField(
- label=_(u"Company"),
- max_length=30,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 50}),
- )
- address1 = forms.CharField(
- label=_(u"Address 1"),
- max_length=30,
- required=True,
- widget=forms.TextInput(attrs={"type": "text", "size": 50}),
- )
- address2 = forms.CharField(
- label=_(u"Address 2"),
- max_length=30,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 50}),
- )
- city = forms.CharField(
- label=_(u"City"),
- max_length=30,
- required=True,
- widget=forms.TextInput(attrs={"type": "text", "size": 50}),
- )
- region = forms.CharField(
- label=_(u"Region"),
- max_length=30,
- required=True,
- widget=forms.Select(
- choices=constants.STATE_CHOICES,
- attrs={"width": "100px", "onchange": "calculate_price();"},
- ),
- )
- country = forms.CharField(
- label=_(u"Country"),
- max_length=50,
- required=True,
- widget=forms.Select(
- choices=constants.COUNTRIES,
- attrs={"onchange": "check_state(); calculate_price();"},
- ),
- )
- country_other = forms.CharField(
- label=_(u"Other"),
- max_length=30,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 25}),
- )
- postal_code = forms.CharField(
- label=_(u"Postal Code"),
- max_length=15,
- required=True,
- widget=forms.TextInput(
- attrs={"type": "text", "size": 15, "onchange": "calculate_price();"}
- ),
- )
- plus_four = forms.CharField(
- label=_(u"Plus Four"),
- max_length=15,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 15}),
- )
- phone = forms.CharField(
- label=_(u"Phone"),
- max_length=30,
- required=True,
- widget=forms.TextInput(attrs={"type": "text", "size": 50}),
- )
- class EmailRequiredPersonForm(BasePersonForm):
- """ In some cases, we need the email address to be required """
- email = forms.EmailField(
- label=_(u"Email"),
- max_length=30,
- required=True,
- widget=forms.TextInput(attrs={"type": "text", "size": 50}),
- )
- class EmailNotRequiredPersonForm(BasePersonForm):
- """ In some cases, we need the email address to not be required """
- email = forms.EmailField(
- label=_(u"Email"),
- max_length=30,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 50}),
- )
- class NonRequiredBasePersonForm(forms.Form):
- first_name = forms.CharField(
- label=_(u"First name"),
- max_length=30,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 25}),
- )
- last_name = forms.CharField(
- label=_(u"Last name"),
- max_length=30,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 25}),
- )
- company = forms.CharField(
- label=_(u"Company"),
- max_length=30,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 50}),
- )
- address1 = forms.CharField(
- label=_(u"Address 1"),
- max_length=30,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 50}),
- )
- address2 = forms.CharField(
- label=_(u"Address 2"),
- max_length=30,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 50}),
- )
- city = forms.CharField(
- label=_(u"City"),
- max_length=30,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 50}),
- )
- region = forms.ChoiceField(
- label=_(u"Region"),
- required=False,
- choices=constants.STATE_CHOICES,
- widget=forms.Select(
- choices=constants.STATE_CHOICES, attrs={"onchange": "calculate_price();"}
- ),
- )
- country = forms.ChoiceField(
- label=_(u"Country"),
- required=False,
- choices=(("", "----------"),) + constants.COUNTRIES,
- widget=forms.Select(
- choices=constants.STATE_CHOICES,
- attrs={"onchange": "check_state(); calculate_price();"},
- ),
- )
- country_other = forms.CharField(
- label=_(u"Other"),
- max_length=30,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 25}),
- )
- postal_code = forms.CharField(
- label=_(u"Postal Code"),
- max_length=15,
- required=False,
- widget=forms.TextInput(
- attrs={"type": "text", "size": 15, "onchange": "calculate_price();"}
- ),
- )
- plus_four = forms.CharField(
- label=_(u"Plus Four"),
- max_length=4,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 15}),
- )
- phone = forms.CharField(
- label=_(u"Phone"),
- max_length=30,
- required=False,
- widget=forms.TextInput(attrs={"type": "text", "size": 50}),
- )
- class ProfileCreateForm(EmailRequiredPersonForm):
- """ Form used for creating a new user """
- password1 = forms.CharField(
- label=_("Password"), widget=forms.PasswordInput, required=True
- )
- password2 = forms.CharField(
- label=_("Password confirmation"), widget=forms.PasswordInput, required=True
- )
- def save(self, request=None):
- """ Creates a new user and account. Returns the newly created user. """
- password = self.cleaned_data["password1"]
- username, email, password = (
- self.cleaned_data["email"],
- self.cleaned_data["email"],
- password,
- )
- user = User.objects.create_user(username, email, password)
- user.first_name = self.cleaned_data["first_name"]
- user.last_name = self.cleaned_data["last_name"]
- user.save()
- profile = profile_utils.get_profile(user)
- profile.company = self.cleaned_data["company"]
- profile.address1 = self.cleaned_data["address1"]
- profile.address2 = self.cleaned_data["address2"]
- profile.city = self.cleaned_data["city"]
- profile.region = self.cleaned_data["region"]
- profile.phone = self.cleaned_data["phone"]
- profile.country = self.cleaned_data["country"]
- profile.postal_code = self.cleaned_data["postal_code"]
- profile.plus_four = self.cleaned_data["plus_four"]
- profile.save()
- user = authenticate(username=username, password=password)
- if request:
- login(request, user)
- site = Site.objects.get_current()
- utils.send_new_account_email(email, password, user, profile, site)
- return user
- def clean(self):
- # We want a single error message. We don't display the validation
- # errors for each part of the form
- for field in self.fields:
- if self.fields[field].required and not self.cleaned_data.get(field, None):
- raise forms.ValidationError(_(REQUIRED))
- self.clean_email()
- return self.cleaned_data
- def clean_email(self):
- """ Validate that the e-mail address is unique. """
- if User.objects.filter(
- email__iexact=self.cleaned_data["email"]
- ) or User.objects.filter(username__iexact=self.cleaned_data["email"]):
- raise forms.ValidationError(
- _("This email is already in use. " "Please supply a different email.")
- )
- return self.cleaned_data["email"]
- def clean_password2(self):
- password1 = self.cleaned_data.get("password1", "")
- password2 = self.cleaned_data["password2"]
- if password1 != password2:
- raise forms.ValidationError(
- "Your passwords did not match. Please re-enter your passwords."
- )
- return password2
- class ProfileUpdateForm(EmailRequiredPersonForm):
- """
- A form to demonstrate how to add extra fields to the signup form, in this
- case adding the first and last name.
- """
- def __init__(self, user, *args, **kwargs):
- self.user = user
- super(ProfileUpdateForm, self).__init__(*args, **kwargs)
- def clean_email(self):
- """ Validate that the email is not already registered with another user
- """
- if User.objects.filter(email__iexact=self.cleaned_data["email"]).exclude(
- email__iexact=self.user.email
- ):
- raise forms.ValidationError(
- _(u"This email is already in use. Please supply a different " u"email.")
- )
- return self.cleaned_data["email"]
- def save(self, request=None):
- """
- Override the save method to save the first and last name to the user
- field.
- """
- user = self.user
- user.first_name = self.cleaned_data["first_name"]
- user.last_name = self.cleaned_data["last_name"]
- user.email = self.cleaned_data["email"]
- user.save()
- profile = profile_utils.get_profile(user)
- profile.company = self.cleaned_data["company"]
- profile.address1 = self.cleaned_data["address1"]
- profile.address2 = self.cleaned_data["address2"]
- profile.city = self.cleaned_data["city"]
- profile.region = self.cleaned_data["region"]
- profile.phone = self.cleaned_data["phone"]
- profile.country = self.cleaned_data["country"]
- profile.country_other = self.cleaned_data["country_other"]
- profile.postal_code = self.cleaned_data["postal_code"]
- profile.plus_four = self.cleaned_data["plus_four"]
- profile.save()
- # Userena expects to get the new user from this form, so return the new
- # user.
- return user
- class ProfileSecondaryAddressForm(NonRequiredBasePersonForm):
- address_from = forms.DateField(required=False)
- address_to = forms.DateField(required=False)
- class Meta:
- model = models.SecondaryProfileAddress
- exclude = "profile"
- def __init__(self, *args, **kw):
- super(ProfileSecondaryAddressForm, self).__init__(*args, **kw)
- def save(self, *args, **kwargs):
- user = kwargs.pop("user", None)
- profile_address = profile_utils.get_profile_secondary_address(user)
- profile_address.profile = profile_utils.get_profile(user)
- for k, v in self.cleaned_data.iteritems():
- setattr(profile_address, k, v)
- profile_address.save()
- return profile_address
|