Skip to content

Commit

Permalink
Added support for differentiating e-mail handling (verification, requ…
Browse files Browse the repository at this point in the history
…ired) between local and social accounts: SOCIALACCOUNT_EMAIL_REQUIRED and SOCIALACCOUNT_EMAIL_VERIFICATION (closes #145)
  • Loading branch information
pennersr committed Apr 25, 2013
1 parent c5f317c commit 74d94c0
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 16 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
2013-04-25 Raymond Penners <[email protected]>

* Added support for differentiating e-mail handling (verification,
required) between local and social accounts:
`SOCIALACCOUNT_EMAIL_REQUIRED` and
`SOCIALACCOUNT_EMAIL_VERIFICATION`.

2013-04-16 Raymond Penners <[email protected]>

* Version 0.10.1 released.
Expand Down
9 changes: 8 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,15 @@ SOCIALACCOUNT_AVATAR_SUPPORT (= 'avatar' in settings.INSTALLED_APPS)
Enable support for django-avatar. When enabled, the profile image of
the user is copied locally into django-avatar at signup.

SOCIALACCOUNT_EMAIL_REQUIRED (=ACCOUNT_EMAIL_REQUIRED)
The user is required to hand over an e-mail address when signing up
using a social account.

SOCIALACCOUNT_EMAIL_VERIFICATION (=ACCOUNT_EMAIL_VERIFICATION)
As `ACCOUNT_EMAIL_VERIFICATION`, but for social accounts.

SOCIALACCOUNT_PROVIDERS (= dict)
Dictionary containing provider specific settings.
Dictionary containing provider specific settings.


Upgrading
Expand Down
14 changes: 7 additions & 7 deletions allauth/account/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from .models import EmailAddress
from .utils import perform_login, send_email_confirmation, setup_user_email
from .app_settings import AuthenticationMethod, EmailVerificationMethod
from .app_settings import AuthenticationMethod
from . import app_settings
from .adapter import get_adapter

Expand Down Expand Up @@ -120,7 +120,9 @@ def clean(self):
return self.cleaned_data

def login(self, request, redirect_url=None):
ret = perform_login(request, self.user, redirect_url=redirect_url)
ret = perform_login(request, self.user,
email_verification=app_settings.EMAIL_VERIFICATION,
redirect_url=redirect_url)
if self.cleaned_data["remember"]:
request.session.set_expiry(60 * 60 * 24 * 7 * 3)
else:
Expand Down Expand Up @@ -171,12 +173,9 @@ class BaseSignupForm(_base_signup_form_class()):
_('E-mail address') }))

def __init__(self, *args, **kwargs):
email_required = kwargs.pop('email_required')
super(BaseSignupForm, self).__init__(*args, **kwargs)
if (app_settings.EMAIL_REQUIRED
or (app_settings.EMAIL_VERIFICATION
== EmailVerificationMethod.MANDATORY)
or (app_settings.AUTHENTICATION_METHOD
== AuthenticationMethod.EMAIL)):
if email_required:
self.fields["email"].label = ugettext("E-mail")
self.fields["email"].required = True
else:
Expand Down Expand Up @@ -230,6 +229,7 @@ class SignupForm(BaseSignupForm):
widget = forms.HiddenInput())

def __init__(self, *args, **kwargs):
kwargs['email_required'] = app_settings.EMAIL_REQUIRED
super(SignupForm, self).__init__(*args, **kwargs)
current_order =self.fields.keyOrder
preferred_order = self.fields.keyOrder = ["username",
Expand Down
4 changes: 2 additions & 2 deletions allauth/account/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def test_username_in_blacklist(self):
'username': 'username',
'email': '[email protected]',
}
form = BaseSignupForm(data)
form = BaseSignupForm(data, email_required=True)
self.assertFalse(form.is_valid())

@override_settings(
Expand All @@ -182,5 +182,5 @@ def test_username_not_in_blacklist(self):
'username': 'theusername',
'email': '[email protected]',
}
form = BaseSignupForm(data)
form = BaseSignupForm(data, email_required=True)
self.assertTrue(form.is_valid())
10 changes: 6 additions & 4 deletions allauth/account/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ def user_email(user, *args):
# return False


def perform_login(request, user, redirect_url=None):
def perform_login(request, user, email_verification, redirect_url=None):
from .models import EmailAddress

# not is_active: social users are redirected to a template
# local users are stopped due to form validation checking is_active
assert user.is_active
if (app_settings.EMAIL_VERIFICATION == EmailVerificationMethod.MANDATORY
if (email_verification == EmailVerificationMethod.MANDATORY
and not EmailAddress.objects.filter(user=user,
verified=True).exists()):
send_email_confirmation(request, user)
Expand All @@ -125,12 +125,14 @@ def perform_login(request, user, redirect_url=None):
return HttpResponseRedirect(get_login_redirect_url(request, redirect_url))


def complete_signup(request, user, success_url, signal_kwargs={}):
def complete_signup(request, user, email_verification, success_url, signal_kwargs={}):
signals.user_signed_up.send(sender=user.__class__,
request=request,
user=user,
**signal_kwargs)
return perform_login(request, user, redirect_url=success_url)
return perform_login(request, user,
email_verification=email_verification,
redirect_url=success_url)


def cleanup_email_addresses(request, addresses):
Expand Down
4 changes: 3 additions & 1 deletion allauth/account/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ def get_success_url(self):

def form_valid(self, form):
user = form.save(self.request)
return complete_signup(self.request, user, self.get_success_url())
return complete_signup(self.request, user,
app_settings.EMAIL_VERIFICATION,
self.get_success_url())

def get_context_data(self, **kwargs):
ret = super(SignupView, self).get_context_data(**kwargs)
Expand Down
17 changes: 17 additions & 0 deletions allauth/socialaccount/app_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ def PROVIDERS(self):
"""
return self._setting("PROVIDERS", {})

@property
def EMAIL_REQUIRED(self):
"""
The user is required to hand over an e-mail address when signing up
"""
from allauth.account import app_settings as account_settings
return self._setting("EMAIL_REQUIRED", account_settings.EMAIL_REQUIRED)

@property
def EMAIL_VERIFICATION(self):
"""
See e-mail verification method
"""
from allauth.account import app_settings as account_settings
return self._setting("EMAIL_VERIFICATION",
account_settings.EMAIL_VERIFICATION)

@property
def ADAPTER(self):
return self._setting \
Expand Down
3 changes: 3 additions & 0 deletions allauth/socialaccount/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from .models import SocialAccount

from . import app_settings

class SignupForm(BaseSignupForm):

def __init__(self, *args, **kwargs):
Expand All @@ -18,6 +20,7 @@ def __init__(self, *args, **kwargs):
'first_name': user.first_name or '',
'last_name': user.last_name or '' }
kwargs['initial'] = initial
kwargs['email_required'] = app_settings.EMAIL_REQUIRED
super(SignupForm, self).__init__(*args, **kwargs)

def save(self, request):
Expand Down
4 changes: 3 additions & 1 deletion allauth/socialaccount/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _process_signup(request, sociallogin):
# FIXME: We redirect to signup form -- user will
# see email address conflict only after posting
# whereas we detected it here already.
elif account_settings.EMAIL_REQUIRED:
elif app_settings.EMAIL_REQUIRED:
# Nope, email is required and we don't have it yet...
auto_signup = False
if not auto_signup:
Expand Down Expand Up @@ -87,6 +87,7 @@ def _login_social_account(request, sociallogin):
context_instance=RequestContext(request))
else:
ret = perform_login(request, user,
email_verification=app_settings.EMAIL_VERIFICATION,
redirect_url=sociallogin.get_redirect_url(request))
return ret

Expand Down Expand Up @@ -194,6 +195,7 @@ def complete_social_signup(request, sociallogin):
_copy_avatar(request, sociallogin.account.user, sociallogin.account)
return complete_signup(request,
sociallogin.account.user,
app_settings.EMAIL_VERIFICATION,
sociallogin.get_redirect_url(request),
signal_kwargs={'sociallogin': sociallogin})

Expand Down

0 comments on commit 74d94c0

Please sign in to comment.