Skip to content

Commit

Permalink
Merge pull request #27 from m3brown/usermodel
Browse files Browse the repository at this point in the history
Use custom user model to allow for longer usernames
  • Loading branch information
sephcoster committed Jul 3, 2014
2 parents 46b0dac + a1e8b91 commit 7fa584b
Show file tree
Hide file tree
Showing 23 changed files with 902 additions and 57 deletions.
4 changes: 2 additions & 2 deletions collab/django_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import factory

from django.conf import settings
from django.contrib.auth import models
from django.contrib.auth import models, get_user_model
from django.contrib.contenttypes import models as ctmodels
from django.db.models import get_model, Model
from django.utils import timezone
Expand Down Expand Up @@ -30,7 +30,7 @@ def _setup_next_sequence(cls):


class UserF(factory.Factory):
FACTORY_FOR = models.User
FACTORY_FOR = get_user_model()

@classmethod
def _setup_next_sequence(cls):
Expand Down
2 changes: 1 addition & 1 deletion collab/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
USE_TZ = True

# Default user model is 'auth.User', override here
AUTH_USER_MODEL = 'auth.User'
AUTH_USER_MODEL = 'core.CollabUser'

# List of finder classes that know how to find static files in
# various locations.
Expand Down
15 changes: 10 additions & 5 deletions core/admin.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from django.contrib import admin
from django.contrib.auth.models import User, Permission
from django.contrib.auth.models import Permission
from django.contrib.auth.admin import UserAdmin
from core.models import App, Person, OrgGroup, OfficeLocation
from core.models import CollabUser, App, Person, OrgGroup, OfficeLocation
from core.models import Alert
from core.forms import CollabUserChangeForm, CollabUserCreationForm
from actions import export_as_csv_action


admin.site.register(OfficeLocation)
admin.site.register(App)
admin.site.register(OrgGroup)
admin.site.register(Permission)
admin.site.unregister(User)


class CollabUserAdmin(UserAdmin):
form = CollabUserChangeForm
add_form = CollabUserCreationForm


class PersonAdmin(admin.ModelAdmin):
Expand All @@ -34,8 +39,8 @@ class UserProfileInline(admin.StackedInline):
exclude = ('tags',)


class UserProfileAdmin(UserAdmin):
class UserProfileAdmin(CollabUserAdmin):
inlines = [UserProfileInline]

admin.site.register(User, UserProfileAdmin)
admin.site.register(CollabUser, UserProfileAdmin)
admin.site.register(Alert)
8 changes: 4 additions & 4 deletions core/fixtures/core-test-fixtures.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
},
{
"pk": 1,
"model": "auth.user",
"model": "core.collabuser",
"fields": {
"username": "[email protected]",
"first_name": "John",
Expand All @@ -76,7 +76,7 @@
},
{
"pk": 2,
"model": "auth.user",
"model": "core.collabuser",
"fields": {
"username": "[email protected]",
"first_name": "Sara",
Expand All @@ -94,7 +94,7 @@
},
{
"pk": 3,
"model": "auth.user",
"model": "core.collabuser",
"fields": {
"username": "[email protected]",
"first_name": "Beth",
Expand All @@ -112,7 +112,7 @@
},
{
"pk": 4,
"model": "auth.user",
"model": "core.collabuser",
"fields": {
"username": "[email protected]",
"first_name": "Admin",
Expand Down
54 changes: 50 additions & 4 deletions core/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm, UserChangeForm, ReadOnlyPasswordHashField
from core.models import CollabUser
from core.models import OrgGroup, OfficeLocation
from collab.settings import VALID_DOMAINS
from django.conf import settings

required_validator = {
'first_name': 'Your first name is required.',
Expand All @@ -17,15 +18,15 @@

def valid_domain(email):
valid_domain = False
for domain in VALID_DOMAINS:
for domain in settings.VALID_DOMAINS:
if email.endswith(domain):
valid_domain = True

return valid_domain


def email_exists(email):
if len(User.objects.filter(email=email)) > 0:
if len(CollabUser.objects.filter(email=email)) > 0:
return True
else:
return False
Expand Down Expand Up @@ -114,3 +115,48 @@ def clean_email(self):
if email_changed and email_exists(email):
raise forms.ValidationError(required_validator['email_dupe'])
return email


class CollabUserCreationForm(UserCreationForm):
"""
Modify the Django UserCreationForm with upated char limits for username
"""
username = forms.RegexField(max_length=75, regex=r'^[\w.@+-]+$',
help_text="Required. 75 characters or fewer. Letters, digits and " +
"@/./+/-/_ only.",
error_messages={
'invalid': "This value may contain only letters, numbers and " +
"@/./+/-/_ characters."})

class Meta:
model = CollabUser
fields = ("username",)

def clean_username(self):
# Since User.username is unique, this check is redundant,
# but it sets a nicer error message than the ORM. See #13147.
username = self.cleaned_data["username"]
try:
CollabUser._default_manager.get(username=username)
except CollabUser.DoesNotExist:
return username
raise forms.ValidationError(self.error_messages['duplicate_username'])


class CollabUserChangeForm(UserChangeForm):
"""
Modify the Django UserChangeForm with upated char limits for username
"""
username = forms.RegexField(
max_length=75, regex=r"^[\w.@+-]+$",
help_text="Required. 75 characters or fewer. Letters, digits and " +
"@/./+/-/_ only.",
error_messages={
'invalid': "This value may contain only letters, numbers and " +
"@/./+/-/_ characters."})

class Meta:
model = CollabUser

def __init__(self, *args, **kwargs):
super(CollabUserChangeForm, self).__init__(*args, **kwargs)
6 changes: 3 additions & 3 deletions core/management/commands/create_users.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
from core.models import Person, OfficeLocation, OrgGroup
import random

Expand All @@ -25,7 +25,7 @@ def handle(self, *args, **options):
last_name = last_names[int(random.random() * len(last_names))]
username = last_name + first_name[0]
email = username + '@exmaple.com'
if not User.objects.filter(username=email).exists():
if not get_user_model().objects.filter(username=email).exists():
break

user_attr = {
Expand All @@ -40,7 +40,7 @@ def handle(self, *args, **options):
'username': email
}

user = User(**user_attr)
user = get_user_model(**user_attr)
user.save()

person_attr = {
Expand Down
Loading

0 comments on commit 7fa584b

Please sign in to comment.