Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Support custom user model with USERNAME_FIELD != "username" #534

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions userena/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from django.conf import settings
from django.contrib.auth import get_user_model as _get_user_model
from django.shortcuts import get_object_or_404
from django.utils.encoding import smart_bytes
from django.utils.functional import allow_lazy
from django.utils.http import urlencode
Expand Down Expand Up @@ -194,3 +196,9 @@ def get_user_model():
"django-userena.", DeprecationWarning)
from django.contrib.auth import get_user_model
return get_user_model()


def get_user_or_404(username):
user_model = _get_user_model()
kwargs = {user_model.USERNAME_FIELD + '__iexact': username}
return get_object_or_404(user_model, **kwargs)
17 changes: 8 additions & 9 deletions userena/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.core.urlresolvers import reverse
from django.shortcuts import redirect, get_object_or_404
from django.shortcuts import redirect
from django.contrib.auth import authenticate, login, logout, REDIRECT_FIELD_NAME
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import PasswordChangeForm
Expand All @@ -15,7 +15,7 @@
ChangeEmailForm, EditProfileForm)
from userena.models import UserenaSignup
from userena.decorators import secure_required
from userena.utils import signin_redirect, get_profile_model, get_user_profile
from userena.utils import signin_redirect, get_profile_model, get_user_profile, get_user_or_404
from userena import signals as userena_signals
from userena import settings as userena_settings

Expand Down Expand Up @@ -340,7 +340,7 @@ def direct_to_user_template(request, username, template_name,
The currently :class:`User` that is viewed.

"""
user = get_object_or_404(get_user_model(), username__iexact=username)
user = get_user_or_404(username)

if not extra_context: extra_context = dict()
extra_context['viewed_user'] = user
Expand Down Expand Up @@ -375,7 +375,7 @@ def disabled_account(request, username, template_name, extra_context=None):
Profile of the viewed user.

"""
user = get_object_or_404(get_user_model(), username__iexact=username)
user = get_user_or_404(username)

if user.is_active:
raise Http404
Expand Down Expand Up @@ -536,7 +536,7 @@ def email_change(request, username, email_form=ChangeEmailForm,
permissions to alter the email address of others.

"""
user = get_object_or_404(get_user_model(), username__iexact=username)
user = get_user_or_404(username)
prev_email = user.email
form = email_form(user)

Expand Down Expand Up @@ -603,8 +603,7 @@ def password_change(request, username, template_name='userena/password_form.html
Form used to change the password.

"""
user = get_object_or_404(get_user_model(),
username__iexact=username)
user = get_user_or_404(username)

form = pass_form(user=user)

Expand Down Expand Up @@ -673,7 +672,7 @@ def profile_edit(request, username, edit_profile_form=EditProfileForm,
Instance of the ``Profile`` that is edited.

"""
user = get_object_or_404(get_user_model(), username__iexact=username)
user = get_user_or_404(username)

profile = get_user_profile(user=user)

Expand Down Expand Up @@ -729,7 +728,7 @@ def profile_detail(request, username,
Instance of the currently viewed ``Profile``.

"""
user = get_object_or_404(get_user_model(), username__iexact=username)
user = get_user_or_404(username)
profile = get_user_profile(user=user)
if not profile.can_view_profile(request.user):
raise PermissionDenied
Expand Down