Skip to content

Commit

Permalink
Merge branch 'AMeng-fix-issue-27'
Browse files Browse the repository at this point in the history
  • Loading branch information
macropin committed May 16, 2015
2 parents c73536c + c13141c commit 4cc13e5
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 8 deletions.
7 changes: 4 additions & 3 deletions registration/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
from django.utils.translation import ugettext_lazy as _

from registration.models import RegistrationProfile
from registration.users import UsernameField


class RegistrationAdmin(admin.ModelAdmin):
actions = ['activate_users', 'resend_activation_email']
list_display = ('user', 'activation_key_expired')
raw_id_fields = ['user']
search_fields = ('user__username', 'user__first_name', 'user__last_name')
search_fields = ('user__{0}'.format(UsernameField()), 'user__first_name', 'user__last_name')

def activate_users(self, request, queryset):
"""
Activates the selected users, if they are not already
activated.
"""
for profile in queryset:
RegistrationProfile.objects.activate_user(profile.activation_key)
Expand All @@ -30,7 +31,7 @@ def resend_activation_email(self, request, queryset):
who are eligible to activate; emails will not be sent to users
whose activation keys have expired or who have already
activated.
"""
if Site._meta.installed:
site = Site.objects.get_current()
Expand Down
9 changes: 5 additions & 4 deletions registration/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.forms import UserCreationForm

from registration.users import UserModel
from registration.users import UserModel, UsernameField

User = UserModel()


class RegistrationForm(UserCreationForm):
"""
Form for registering a new user account.
Expand All @@ -33,10 +34,10 @@ class RegistrationForm(UserCreationForm):
"""
required_css_class = 'required'
email = forms.EmailField(label=_("E-mail"))

class Meta:
model = User
fields = (getattr(User, 'USERNAME_FIELD', 'username'), "email")
model = User
fields = (UsernameField(), "email")


class RegistrationFormTermsOfService(RegistrationForm):
Expand Down
6 changes: 5 additions & 1 deletion registration/tests/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
from registration.users import UserModel


DJANGO_VERSION = StrictVersion(get_version())


class RegistrationFormTests(TestCase):
"""
Test the default registration forms.
"""

def test_registration_form(self):
"""
Test that ``RegistrationForm`` enforces username constraints
Expand All @@ -24,7 +28,7 @@ def test_registration_form(self):
UserModel().objects.create_user('alice', '[email protected]', 'secret')

bad_username_error = 'This value may contain only letters, numbers and @/./+/-/_ characters.'
if StrictVersion(get_version()) >= StrictVersion('1.8'):
if DJANGO_VERSION >= StrictVersion('1.8'):
bad_username_error = 'Enter a valid username. ' + bad_username_error

invalid_data_dicts = [
Expand Down
64 changes: 64 additions & 0 deletions registration/tests/forms_custom_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from __future__ import unicode_literals
from distutils.version import StrictVersion
try:
from importlib import reload # Python 3.4+ reload()
except:
try:
from imp import reload # Python 3.0+ reload()
except:
pass # Python 2 reload()

from django import get_version
from django.conf import settings
from django.test import TestCase

from registration import forms
from registration.users import UsernameField
from test_app.models import CustomUser


DJANGO_VERSION = StrictVersion(get_version())


class RegistrationFormTests(TestCase):
"""
Test the default registration forms.
"""
def setUp(self):
self.old_auth_model = getattr(settings, 'AUTH_USER_MODEL', None)
settings.AUTH_USER_MODEL = 'test_app.CustomUser'
# The form's Meta class is created on import. We have to reload()
# to apply the new AUTH_USER_MODEL to the Meta class.
reload(forms)

def tearDown(self):
settings.AUTH_USER_MODEL = self.old_auth_model

def test_registration_form_adds_custom_user_name_field(self):
"""
Test that ``RegistrationForm`` adds custom username
field and does not raise errors
"""

form = forms.RegistrationForm()

self.assertTrue(UsernameField() in form.fields)


def test_registration_form_subclass_is_valid_for_django_18(self):
"""
Test that ``RegistrationForm`` subclasses can save in
Django > 1.8
"""
if DJANGO_VERSION >= StrictVersion('1.8'):
data = {'new_field': 'custom username',
'email': '[email protected]',
'password1': 'foo',
'password2': 'foo'}

form = forms.RegistrationForm(data=data)

self.assertTrue(form.is_valid())
4 changes: 4 additions & 0 deletions registration/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ def UserModelString():
return settings.AUTH_USER_MODEL
except AttributeError:
return 'auth.User'


def UsernameField():
return getattr(UserModel(), 'USERNAME_FIELD', 'username')
14 changes: 14 additions & 0 deletions test_app/models.py
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
# coding: utf-8

try:
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
except ImportError:
from django.contrib.auth.models import User as AbstractBaseUser, UserManager as BaseUserManager

from django.db import models


class CustomUser(AbstractBaseUser):
new_field = models.CharField(max_length=25)
objects = BaseUserManager()

USERNAME_FIELD = 'new_field'

0 comments on commit 4cc13e5

Please sign in to comment.