Skip to content
This repository has been archived by the owner on Dec 23, 2018. It is now read-only.

Commit

Permalink
Added setting "USERPROFILES_USE_EMAIL_VERIFICATION".
Browse files Browse the repository at this point in the history
  • Loading branch information
stephrdev committed Mar 11, 2013
1 parent ad10d72 commit 7f6acbc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
8 changes: 7 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Registration settings
This app provides a mechanism to verify user accounts by sending an email
with an activation link. To use the account verification you have to add
`userprofiles.contrib.accountverification` to your `INSTALLED_APPS` in
order toto enable the verification.
order to enable the verification.

`USERPROFILES_ACCOUNT_VERIFICATION_DAYS`
Defines the amount of days a user has to activate his account. Defaults to
Expand Down Expand Up @@ -93,6 +93,12 @@ userprofiles.contrib.emailverification
django-userprofiles provides a simple app to do confirmed email address changes.
(Users have the re-verify their email address after a change)

`USERPROFILES_USE_EMAIL_VERIFICATION`
This app provides a mechanism to verify email changes by sending an email
with an activation link. To use the email verification you have to add
`userprofiles.contrib.emailverification` to your `INSTALLED_APPS` in
order to enable the verification.

`USERPROFILES_EMAIL_VERIFICATION_DAYS`
Defines the number of days a user has time to verify her/his new email
address. Defaults to 2.
Expand Down
15 changes: 6 additions & 9 deletions userprofiles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@

from django import forms
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from django.contrib.auth.models import User

from userprofiles.settings import up_settings


if up_settings.USE_ACCOUNT_VERIFICATION:
from userprofiles.contrib.accountverification.models import AccountVerification

if 'userprofiles.contrib.emailverification' in settings.INSTALLED_APPS:
from userprofiles.contrib.emailverification.models import EmailVerification


class RegistrationForm(forms.Form):
username = forms.RegexField(label=_("Username"), max_length=30,
regex=r'^[\w.-]+$', error_messages={'invalid': _(
Expand Down Expand Up @@ -82,10 +74,13 @@ def clean_email(self):
new_email = self.cleaned_data['email']

emails = User.objects.filter(email__iexact=new_email).count()
if 'userprofiles.contrib.emailverification' in settings.INSTALLED_APPS:

if up_settings.USE_EMAIL_VERIFICATION:
from userprofiles.contrib.emailverification.models import EmailVerification

emails += EmailVerification.objects.filter(
new_email__iexact=new_email, is_expired=False).count()

if emails > 0:
raise forms.ValidationError(
_(u'This email address is already in use. Please supply a different email address.'))
Expand All @@ -107,6 +102,8 @@ def clean(self):

def save(self, *args, **kwargs):
if up_settings.USE_ACCOUNT_VERIFICATION:
from userprofiles.contrib.accountverification.models import AccountVerification

new_user = AccountVerification.objects.create_inactive_user(
username=self.cleaned_data['username'],
password=self.cleaned_data['password'],
Expand Down
15 changes: 10 additions & 5 deletions userprofiles/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ def __getattr__(self, key):
USE_ACCOUNT_VERIFICATION=False,
ACCOUNT_VERIFICATION_DAYS=7,

USE_EMAIL_VERIFICATION=False,
EMAIL_VERIFICATION_DAYS=2,
EMAIL_VERIFICATION_DONE_URL='userprofiles_email_change',

USE_PROFILE=False,
PROFILE_FORM='userprofiles.contrib.profiles.forms.ProfileForm',
INLINE_PROFILE_ADMIN=False,

PROFILE_ALLOW_EMAIL_CHANGE=False,
PROFILE_CHANGE_DONE_URL='userprofiles_profile_change',

INLINE_PROFILE_ADMIN=False,
)


Expand All @@ -60,10 +61,14 @@ def validate_settings():
'USERPROFILES_PROFILE_ALLOW_EMAIL_CHANGE cannot be activated '
'when USERPROFILES_CHECK_UNIQUE_EMAIL is active.')

if (up_settings.PROFILE_ALLOW_EMAIL_CHANGE
and 'userprofiles.contrib.emailverification' in settings.INSTALLED_APPS):
if (up_settings.USE_EMAIL_VERIFICATION and
'userprofiles.contrib.emailverification' not in settings.INSTALLED_APPS):
raise ImproperlyConfigured('You need to add `userprofiles.contrib.emailverification` '
'to INSTALLED_APPS to use emailverification.')

if up_settings.PROFILE_ALLOW_EMAIL_CHANGE and up_settings.USE_EMAIL_VERIFICATION:
raise ImproperlyConfigured(
'USERPROFILES_PROFILE_ALLOW_EMAIL_CHANGE cannot be activated '
'when `userprofiles.contrib.emailverification` is used.')
'when USERPROFILES_USE_EMAIL_VERIFICATION is activated.')

validate_settings()
9 changes: 8 additions & 1 deletion userprofiles/tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ def test_profile(self):
def test_profile_email_check(self):
self.assertRaises(ImproperlyConfigured, validate_settings)

@override_settings(USERPROFILES_PROFILE_ALLOW_EMAIL_CHANGE=True)
@override_settings(USERPROFILES_USE_EMAIL_VERIFICATION=True,
INSTALLED_APPS=list(set(settings.INSTALLED_APPS) - set(
['userprofiles.contrib.emailverification'])))
def test_email_verification(self):
self.assertRaises(ImproperlyConfigured, validate_settings)

@override_settings(USERPROFILES_USE_EMAIL_VERIFICATION=True,
USERPROFILES_PROFILE_ALLOW_EMAIL_CHANGE=True)
def test_email_verification_allow_email_change(self):
self.assertRaises(ImproperlyConfigured, validate_settings)

0 comments on commit 7f6acbc

Please sign in to comment.