Pārlūkot izejas kodu

HUE-3180 [useradmin] Override duplicate username validation message

Jenny Kim 9 gadi atpakaļ
vecāks
revīzija
06917a0

+ 16 - 1
apps/useradmin/src/useradmin/forms.py

@@ -81,6 +81,9 @@ class UserChangeForm(django.contrib.auth.forms.UserChangeForm):
   This is similar, but not quite the same as djagno.contrib.auth.forms.UserChangeForm
   and UserCreationForm.
   """
+
+  GENERIC_VALIDATION_ERROR = _("Username or password is invalid.")
+
   username = forms.RegexField(
       label=_t("Username"),
       max_length=30,
@@ -103,6 +106,7 @@ class UserChangeForm(django.contrib.auth.forms.UserChangeForm):
                                             initial=True,
                                             required=False)
 
+
   class Meta(django.contrib.auth.forms.UserChangeForm.Meta):
     fields = ["username", "first_name", "last_name", "email", "ensure_home_directory"]
 
@@ -126,6 +130,17 @@ class UserChangeForm(django.contrib.auth.forms.UserChangeForm):
       if 'groups' in self.fields:
         self.fields['groups'].widget.attrs['readonly'] = True
 
+  def clean_username(self):
+    username = self.cleaned_data["username"]
+    if self.instance.username == username:
+      return username
+
+    try:
+      User._default_manager.get(username=username)
+    except User.DoesNotExist:
+      return username
+    raise forms.ValidationError(self.GENERIC_VALIDATION_ERROR, code='duplicate_username')
+
   def clean_password(self):
     return self.cleaned_data["password"]
 
@@ -147,7 +162,7 @@ class UserChangeForm(django.contrib.auth.forms.UserChangeForm):
       password1 = self.cleaned_data.get("password1", "")
       password_old = self.cleaned_data.get("password_old", "")
       if password1 != '' and not self.instance.check_password(password_old):
-        raise forms.ValidationError(_("The old password does not match the current password."))
+        raise forms.ValidationError(self.GENERIC_VALIDATION_ERROR)
     return self.cleaned_data.get("password_old", "")
 
   def save(self, commit=True):

+ 1 - 1
apps/useradmin/src/useradmin/templates/edit_user.mako

@@ -195,4 +195,4 @@ $(document).ready(function(){
 
 ${layout.commons()}
 
-${ commonfooter(request, messages) | n,unicode }
+${ commonfooter(None, messages) | n,unicode }

+ 3 - 2
apps/useradmin/src/useradmin/tests.py

@@ -37,6 +37,7 @@ from desktop.views import home
 from hadoop import pseudo_hdfs4
 
 import useradmin.conf
+from useradmin.forms import UserChangeForm
 import useradmin.ldap_access
 from useradmin.models import HuePermission, GroupPermission, UserProfile
 from useradmin.models import get_profile, get_default_user_group
@@ -581,7 +582,7 @@ class TestUserAdmin(BaseUserAdminTests):
       assert_equal(["Passwords do not match."], response.context["form"]["password2"].errors, "Should have complained about mismatched password")
       # Old password not confirmed
       response = c.post('/useradmin/users/edit/test', dict(username="test", first_name="Tom", last_name="Tester", password1="foo", password2="foo", is_active=True, is_superuser=True))
-      assert_equal(["The old password does not match the current password."], response.context["form"]["password_old"].errors, "Should have complained about old password")
+      assert_equal([UserChangeForm.GENERIC_VALIDATION_ERROR], response.context["form"]["password_old"].errors, "Should have complained about old password")
       # Good now
       response = c.post('/useradmin/users/edit/test', dict(username="test", first_name="Tom", last_name="Tester", password1="foo", password2="foo", password_old="test", is_active=True, is_superuser=True))
       assert_true(User.objects.get(username="test").is_superuser)
@@ -599,7 +600,7 @@ class TestUserAdmin(BaseUserAdminTests):
 
       # Create a new regular user (duplicate name)
       response = c.post('/useradmin/users/new', dict(username="test", password1="test", password2="test"))
-      assert_equal({ 'username': ["User with this Username already exists."]}, response.context["form"].errors)
+      assert_equal({ 'username': [UserChangeForm.GENERIC_VALIDATION_ERROR]}, response.context["form"].errors)
 
       # Create a new regular user (for real)
       response = c.post('/useradmin/users/new', dict(username=FUNNY_NAME,