Ticket #8342: 8342.login.admin.error.diff

File 8342.login.admin.error.diff, 3.5 KB (added by julien, 7 years ago)
  • django/django/contrib/admin/sites.py

     
    230230        """
    231231        Displays the login form for the given HttpRequest.
    232232        """
    233         from django.contrib.auth.models import User
    234233
    235234        # If this isn't already the login page, display it.
    236235        if not request.POST.has_key(LOGIN_FORM_KEY):
     
    250249        password = request.POST.get('password', None)
    251250        user = authenticate(username=username, password=password)
    252251        if user is None:
    253             message = ERROR_MESSAGE
    254             if u'@' in username:
    255                 # Mistakenly entered e-mail address instead of username? Look it up.
    256                 try:
    257                     user = User.objects.get(email=username)
    258                 except (User.DoesNotExist, User.MultipleObjectsReturned):
    259                     message = _("Usernames cannot contain the '@' character.")
    260                 else:
    261                     if user.check_password(password):
    262                         message = _("Your e-mail address is not your username."
    263                                     " Try '%s' instead.") % user.username
    264                     else:
    265                         message = _("Usernames cannot contain the '@' character.")
     252            message = self.get_login_error_message(request, False, username, password)
    266253            return self.display_login_form(request, message)
    267254
    268255        # The user data is correct; log in the user in and continue.
     
    280267                        request.session.delete_test_cookie()
    281268                        return http.HttpResponseRedirect(request.get_full_path())
    282269            else:
    283                 return self.display_login_form(request, ERROR_MESSAGE)
     270                message = self.get_login_error_message(request, True, username, password)
     271                return self.display_login_form(request, message)
    284272    login = never_cache(login)
    285273
    286274    def index(self, request, extra_context=None):
     
    337325        )
    338326    index = never_cache(index)
    339327
     328    def get_login_error_message(self, request, is_authenticated, username, password):
     329        """
     330        Returns a error message when login has failed, and gives suggestions on
     331        how to resolve the error.
     332        """
     333       
     334        from django.contrib.auth.models import User
     335        message = ERROR_MESSAGE
     336        if not is_authenticated:
     337            if u'@' in username:
     338                # Mistakenly entered e-mail address instead of username? Look it up.
     339                try:
     340                    user = User.objects.get(email=username)
     341                except (User.DoesNotExist, User.MultipleObjectsReturned):
     342                    message = _("Usernames cannot contain the '@' character.")
     343                else:
     344                    if user.check_password(password):
     345                        message = _("Your e-mail address is not your username."
     346                                    " Try '%s' instead.") % user.username
     347                    else:
     348                        message = _("Usernames cannot contain the '@' character.")
     349        return message
     350
    340351    def display_login_form(self, request, error_message='', extra_context=None):
    341352        request.session.set_test_cookie()
    342353        if request.POST and request.POST.has_key('post_data'):
Back to Top