Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def signup(request, form_class=SignupForm,
- template_name="account/signup.html", success_url=None):
- if success_url is None:
- success_url = get_default_redirect(request)
- if request.method == "POST":
- form = form_class(request.POST)
- if form.is_valid():
- username, password = form.save()
- if settings.ACCOUNT_EMAIL_VERIFICATION:
- return render_to_response("account/verification_sent.html", {
- "email": form.cleaned_data["email"],
- }, context_instance=RequestContext(request))
- else:
- user = authenticate(username=username, password=password)
- auth_login(request, user)
- request.user.message_set.create(
- message=_("Successfully logged in as %(username)s.") % {
- 'username': user.username
- })
- return HttpResponseRedirect(success_url)
- else:
- form = form_class()
- return render_to_response(template_name, {
- "form": form,
- }, context_instance=RequestContext(request))
- class SignupForm(forms.Form):
- username = forms.CharField(label=_("Username"), max_length=30, widget=forms.TextInput())
- password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput(render_value=False))
- password2 = forms.CharField(label=_("Password (again)"), widget=forms.PasswordInput(render_value=False))
- if settings.ACCOUNT_REQUIRED_EMAIL or settings.ACCOUNT_EMAIL_VERIFICATION:
- email = forms.EmailField(
- label = _("Email"),
- required = True,
- widget = forms.TextInput()
- )
- else:
- email = forms.EmailField(
- label = _("Email (optional)"),
- required = False,
- widget = forms.TextInput()
- )
- confirmation_key = forms.CharField(max_length=40, required=False, widget=forms.HiddenInput())
- def clean_username(self):
- if not alnum_re.search(self.cleaned_data["username"]):
- raise forms.ValidationError(_("Usernames can only contain letters, numbers and underscores."))
- try:
- user = User.objects.get(username__iexact=self.cleaned_data["username"])
- except User.DoesNotExist:
- return self.cleaned_data["username"]
- raise forms.ValidationError(_("This username is already taken. Please choose another."))
- def clean(self):
- if "password1" in self.cleaned_data and "password2" in self.cleaned_data:
- if self.cleaned_data["password1"] != self.cleaned_data["password2"]:
- raise forms.ValidationError(_("You must type the same password each time."))
- return self.cleaned_data
- def save(self):
- username = self.cleaned_data["username"]
- email = self.cleaned_data["email"]
- password = self.cleaned_data["password1"]
- if self.cleaned_data["confirmation_key"]:
- from friends.models import JoinInvitation # @@@ temporary fix for issue 93
- try:
- join_invitation = JoinInvitation.objects.get(confirmation_key = self.cleaned_data["confirmation_key"])
- confirmed = True
- except JoinInvitation.DoesNotExist:
- confirmed = False
- else:
- confirmed = False
- # @@@ clean up some of the repetition below -- DRY!
- if confirmed:
- if email == join_invitation.contact.email:
- new_user = User.objects.create_user(username, email, password)
- join_invitation.accept(new_user) # should go before creation of EmailAddress below
- new_user.message_set.create(message=ugettext(u"Your email address has already been verified"))
- # already verified so can just create
- EmailAddress(user=new_user, email=email, verified=True, primary=True).save()
- else:
- new_user = User.objects.create_user(username, "", password)
- join_invitation.accept(new_user) # should go before creation of EmailAddress below
- if email:
- new_user.message_set.create(message=ugettext(u"Confirmation email sent to %(email)s") % {'email': email})
- EmailAddress.objects.add_email(new_user, email)
- else:
- new_user = User.objects.create_user(username, "", password)
- if email:
- new_user.message_set.create(message=ugettext(u"Confirmation email sent to %(email)s") % {'email': email})
- EmailAddress.objects.add_email(new_user, email)
- if settings.ACCOUNT_EMAIL_VERIFICATION:
- new_user.is_active = False
- new_user.save()
- return username, password # required for authenticate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement