mirror of
				https://github.com/cookiecutter/cookiecutter-django.git
				synced 2025-10-30 23:47:39 +03:00 
			
		
		
		
	Refactored users.forms to make the code more readeable
This commit is contained in:
		
							parent
							
								
									518f179b13
								
							
						
					
					
						commit
						e0cf956fe2
					
				|  | @ -1,6 +1,5 @@ | ||||||
| from django.contrib.auth import forms as admin_forms | from django.contrib.auth import forms as admin_forms | ||||||
| from django.contrib.auth import get_user_model | from django.contrib.auth import get_user_model | ||||||
| from django.core.exceptions import ValidationError |  | ||||||
| from django.utils.translation import gettext_lazy as _ | from django.utils.translation import gettext_lazy as _ | ||||||
| 
 | 
 | ||||||
| User = get_user_model() | User = get_user_model() | ||||||
|  | @ -12,20 +11,9 @@ class UserChangeForm(admin_forms.UserChangeForm): | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class UserCreationForm(admin_forms.UserCreationForm): | class UserCreationForm(admin_forms.UserCreationForm): | ||||||
| 
 |  | ||||||
|     error_message = admin_forms.UserCreationForm.error_messages.update( |  | ||||||
|         {"duplicate_username": _("This username has already been taken.")} |  | ||||||
|     ) |  | ||||||
| 
 |  | ||||||
|     class Meta(admin_forms.UserCreationForm.Meta): |     class Meta(admin_forms.UserCreationForm.Meta): | ||||||
|         model = User |         model = User | ||||||
| 
 | 
 | ||||||
|     def clean_username(self): |         error_messages = { | ||||||
|         username = self.cleaned_data["username"] |             "username": {"unique": _("This username has already been taken.")} | ||||||
| 
 |         } | ||||||
|         try: |  | ||||||
|             User.objects.get(username=username) |  | ||||||
|         except User.DoesNotExist: |  | ||||||
|             return username |  | ||||||
| 
 |  | ||||||
|         raise ValidationError(self.error_messages["duplicate_username"]) |  | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user