Permalink
Browse files

Fixed #19077, #19079 -- Made USERNAME_FIELD a required field, and mod…

…ified UserAdmin to match.
  • Loading branch information...
1 parent 5fb2232 commit c433fcb3fb34fccd69782979f0e7cd5f2d4a4893 @freakboy3742 freakboy3742 committed Oct 13, 2012
@@ -26,7 +26,7 @@
{% if user.is_active and user.is_staff %}
<div id="user-tools">
{% trans 'Welcome,' %}
- <strong>{% filter force_escape %}{% firstof user.get_short_name user.username %}{% endfilter %}</strong>.
+ <strong>{% filter force_escape %}{% firstof user.get_short_name user.get_username %}{% endfilter %}</strong>.
{% block userlinks %}
{% url 'django-admindocs-docroot' as docsroot %}
{% if docsroot %}
@@ -29,7 +29,7 @@
{% for action in action_list %}
<tr>
<th scope="row">{{ action.action_time|date:"DATETIME_FORMAT" }}</th>
- <td>{{ action.user.username }}{% if action.user.get_full_name %} ({{ action.user.get_full_name }}){% endif %}</td>
+ <td>{{ action.user.get_username }}{% if action.user.get_full_name %} ({{ action.user.get_full_name }}){% endif %}</td>
<td>{{ action.change_message }}</td>
</tr>
{% endfor %}
@@ -5,7 +5,7 @@
{% block reset_link %}
{{ protocol }}://{{ domain }}{% url 'django.contrib.auth.views.password_reset_confirm' uidb36=uid token=token %}
{% endblock %}
-{% trans "Your username, in case you've forgotten:" %} {{ user.username }}
+{% trans "Your username, in case you've forgotten:" %} {{ user.get_username }}
{% trans "Thanks for using our site!" %}
@@ -11,14 +11,13 @@
from django.template.response import TemplateResponse
from django.utils.html import escape
from django.utils.decorators import method_decorator
-from django.utils.safestring import mark_safe
-from django.utils import six
from django.utils.translation import ugettext, ugettext_lazy as _
from django.views.decorators.csrf import csrf_protect
from django.views.decorators.debug import sensitive_post_parameters
csrf_protect_m = method_decorator(csrf_protect)
+
class GroupAdmin(admin.ModelAdmin):
search_fields = ('name',)
ordering = ('name',)
@@ -106,9 +105,10 @@ def add_view(self, request, form_url='', extra_context=None):
raise PermissionDenied
if extra_context is None:
extra_context = {}
+ username_field = self.model._meta.get_field(self.model.USERNAME_FIELD)
defaults = {
'auto_populated_fields': (),
- 'username_help_text': self.model._meta.get_field('username').help_text,
+ 'username_help_text': username_field.help_text,
}
extra_context.update(defaults)
return super(UserAdmin, self).add_view(request, form_url,
@@ -171,4 +171,3 @@ def response_add(self, request, obj, post_url_continue='../%s/'):
admin.site.register(Group, GroupAdmin)
admin.site.register(User, UserAdmin)
-
@@ -105,7 +105,7 @@ def authenticate(self, remote_user):
# built-in safeguards for multiple threads.
if self.create_unknown_user:
user, created = UserModel.objects.get_or_create(**{
- getattr(UserModel, 'USERNAME_FIELD', 'username'): username
+ UserModel.USERNAME_FIELD: username
})
if created:
user = self.configure_user(user)
@@ -52,6 +52,9 @@ def __init__(self, *args, **kwargs):
kwargs.setdefault("required", False)
super(ReadOnlyPasswordHashField, self).__init__(*args, **kwargs)
+ def clean_password(self):
@ptone
ptone Oct 16, 2012 Member

see: https://code.djangoproject.com/ticket/19133

There are a couple things overlooked here - first the method should be def clean(self, value) - but even with that, the value that is passed from the readonly widget is None - so we need to get the password hash from the UserChangeForm's initial data as was being done before.

+ return self.initial
+
class UserCreationForm(forms.ModelForm):
"""
@@ -118,9 +121,6 @@ class UserChangeForm(forms.ModelForm):
"this user's password, but you can change the password "
"using <a href=\"password/\">this form</a>."))
- def clean_password(self):
- return self.initial["password"]
-
class Meta:
model = User
@@ -160,7 +160,7 @@ def __init__(self, request=None, *args, **kwargs):
# Set the label for the "username" field.
UserModel = get_user_model()
- username_field = UserModel._meta.get_field(getattr(UserModel, 'USERNAME_FIELD', 'username'))
+ username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
self.fields['username'].label = capfirst(username_field.verbose_name)
def clean(self):
@@ -34,7 +34,7 @@ def handle(self, *args, **options):
try:
u = UserModel.objects.using(options.get('database')).get(**{
- getattr(UserModel, 'USERNAME_FIELD', 'username'): username
+ UserModel.USERNAME_FIELD: username
})
except UserModel.DoesNotExist:
raise CommandError("user '%s' does not exist" % username)
@@ -42,7 +42,7 @@ def handle(self, *args, **options):
UserModel = get_user_model()
- username_field = UserModel._meta.get_field(getattr(UserModel, 'USERNAME_FIELD', 'username'))
+ username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
other_fields = UserModel.REQUIRED_FIELDS
# If not provided, create the user with an unusable password
@@ -74,7 +74,7 @@ def handle(self, *args, **options):
# Get a username
while username is None:
- username_field = UserModel._meta.get_field(getattr(UserModel, 'USERNAME_FIELD', 'username'))
+ username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
if not username:
input_msg = capfirst(username_field.verbose_name)
if default_username:
@@ -91,7 +91,7 @@ def handle(self, *args, **options):
continue
try:
UserModel.objects.using(database).get(**{
- getattr(UserModel, 'USERNAME_FIELD', 'username'): username
+ UserModel.USERNAME_FIELD: username
})
except UserModel.DoesNotExist:
pass
@@ -55,7 +55,7 @@ def process_request(self, request):
# getting passed in the headers, then the correct user is already
# persisted in the session and we don't need to continue.
if request.user.is_authenticated():
- if request.user.username == self.clean_username(username, request):
+ if request.user.get_username() == self.clean_username(username, request):
return
# We are seeing this user for the first time in this session, attempt
# to authenticate the user.
@@ -75,6 +75,6 @@ def clean_username(self, username, request):
backend = auth.load_backend(backend_str)
try:
username = backend.clean_username(username)
- except AttributeError: # Backend has no clean_username method.
+ except AttributeError: # Backend has no clean_username method.
pass
return username
@@ -165,7 +165,7 @@ def make_random_password(self, length=10,
return get_random_string(length, allowed_chars)
def get_by_natural_key(self, username):
- return self.get(**{getattr(self.model, 'USERNAME_FIELD', 'username'): username})
+ return self.get(**{self.model.USERNAME_FIELD: username})
class UserManager(BaseUserManager):
@@ -227,6 +227,7 @@ def _user_has_module_perms(user, app_label):
return False
+@python_2_unicode_compatible
class AbstractBaseUser(models.Model):
password = models.CharField(_('password'), max_length=128)
last_login = models.DateTimeField(_('last login'), default=timezone.now)
@@ -236,6 +237,16 @@ class AbstractBaseUser(models.Model):
class Meta:
abstract = True
+ def get_username(self):
+ "Return the identifying username for this User"
+ return getattr(self, self.USERNAME_FIELD)
+
+ def __str__(self):
+ return self.get_username()
+
+ def natural_key(self):
+ return (self.get_username(),)
+
def is_anonymous(self):
"""
Always returns False. This is a way of comparing User objects to
@@ -277,7 +288,6 @@ def get_short_name(self):
raise NotImplementedError()
-@python_2_unicode_compatible
class AbstractUser(AbstractBaseUser):
"""
An abstract base class implementing a fully featured User model with
@@ -314,19 +324,14 @@ class AbstractUser(AbstractBaseUser):
objects = UserManager()
+ USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
abstract = True
- def __str__(self):
- return self.username
-
- def natural_key(self):
- return (self.username,)
-
def get_absolute_url(self):
return "/users/%s/" % urlquote(self.username)
@@ -1,11 +1,22 @@
from __future__ import unicode_literals
from django.contrib import admin
+from django.contrib.auth import get_user_model
from django.contrib.comments.models import Comment
from django.utils.translation import ugettext_lazy as _, ungettext
from django.contrib.comments import get_model
from django.contrib.comments.views.moderation import perform_flag, perform_approve, perform_delete
+
+class UsernameSearch(object):
+ """The User object may not be auth.User, so we need to provide
+ a mechanism for issuing the equivalent of a .filter(user__username=...)
+ search in CommentAdmin.
+ """
+ def __str__(self):
+ return 'user__%s' % get_user_model().USERNAME_FIELD
+
+
class CommentsAdmin(admin.ModelAdmin):
fieldsets = (
(None,
@@ -24,7 +35,7 @@ class CommentsAdmin(admin.ModelAdmin):
date_hierarchy = 'submit_date'
ordering = ('-submit_date',)
raw_id_fields = ('user',)
- search_fields = ('comment', 'user__username', 'user_name', 'user_email', 'user_url', 'ip_address')
+ search_fields = ('comment', UsernameSearch(), 'user_name', 'user_email', 'user_url', 'ip_address')
actions = ["flag_comments", "approve_comments", "remove_comments"]
def get_actions(self, request):
@@ -19,14 +19,14 @@ class BaseCommentAbstractModel(models.Model):
"""
# Content-object field
- content_type = models.ForeignKey(ContentType,
+ content_type = models.ForeignKey(ContentType,
verbose_name=_('content type'),
related_name="content_type_set_for_%(class)s")
- object_pk = models.TextField(_('object ID'))
+ object_pk = models.TextField(_('object ID'))
content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
# Metadata about the comment
- site = models.ForeignKey(Site)
+ site = models.ForeignKey(Site)
class Meta:
abstract = True
@@ -50,21 +50,21 @@ class Comment(BaseCommentAbstractModel):
# Who posted this comment? If ``user`` is set then it was an authenticated
# user; otherwise at least user_name should have been set and the comment
# was posted by a non-authenticated user.
- user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'),
+ user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'),
blank=True, null=True, related_name="%(class)s_comments")
- user_name = models.CharField(_("user's name"), max_length=50, blank=True)
- user_email = models.EmailField(_("user's email address"), blank=True)
- user_url = models.URLField(_("user's URL"), blank=True)
+ user_name = models.CharField(_("user's name"), max_length=50, blank=True)
+ user_email = models.EmailField(_("user's email address"), blank=True)
+ user_url = models.URLField(_("user's URL"), blank=True)
comment = models.TextField(_('comment'), max_length=COMMENT_MAX_LENGTH)
# Metadata about the comment
submit_date = models.DateTimeField(_('date/time submitted'), default=None)
- ip_address = models.IPAddressField(_('IP address'), blank=True, null=True)
- is_public = models.BooleanField(_('is public'), default=True,
+ ip_address = models.IPAddressField(_('IP address'), blank=True, null=True)
+ is_public = models.BooleanField(_('is public'), default=True,
help_text=_('Uncheck this box to make the comment effectively ' \
'disappear from the site.'))
- is_removed = models.BooleanField(_('is removed'), default=False,
+ is_removed = models.BooleanField(_('is removed'), default=False,
help_text=_('Check this box if the comment is inappropriate. ' \
'A "This comment has been removed" message will ' \
'be displayed instead.'))
@@ -96,9 +96,9 @@ def _get_userinfo(self):
"""
if not hasattr(self, "_userinfo"):
userinfo = {
- "name" : self.user_name,
- "email" : self.user_email,
- "url" : self.user_url
+ "name": self.user_name,
+ "email": self.user_email,
+ "url": self.user_url
}
if self.user_id:
u = self.user
@@ -111,7 +111,7 @@ def _get_userinfo(self):
if u.get_full_name():
userinfo["name"] = self.user.get_full_name()
elif not self.user_name:
- userinfo["name"] = u.username
+ userinfo["name"] = u.get_username()
self._userinfo = userinfo
return self._userinfo
userinfo = property(_get_userinfo, doc=_get_userinfo.__doc__)
@@ -174,9 +174,9 @@ class CommentFlag(models.Model):
design users are only allowed to flag a comment with a given flag once;
if you want rating look elsewhere.
"""
- user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'), related_name="comment_flags")
- comment = models.ForeignKey(Comment, verbose_name=_('comment'), related_name="flags")
- flag = models.CharField(_('flag'), max_length=30, db_index=True)
+ user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_('user'), related_name="comment_flags")
+ comment = models.ForeignKey(Comment, verbose_name=_('comment'), related_name="flags")
+ flag = models.CharField(_('flag'), max_length=30, db_index=True)
flag_date = models.DateTimeField(_('date'), default=None)
# Constants for flag types
@@ -192,7 +192,7 @@ class Meta:
def __str__(self):
return "%s flag of comment ID %s by %s" % \
- (self.flag, self.comment_id, self.user.username)
+ (self.flag, self.comment_id, self.user.get_username())
def save(self, *args, **kwargs):
if self.flag_date is None:
Oops, something went wrong.

0 comments on commit c433fcb

Please sign in to comment.