Refactoring (rename): UserModel -> User

which I'd say is the idiomatic way
This commit is contained in:
Klaas van Schelven
2025-04-14 10:43:08 +02:00
parent 921d5bd4a3
commit 53db317529
3 changed files with 16 additions and 16 deletions

View File

@@ -22,7 +22,7 @@ def _(x):
return x
UserModel = get_user_model()
User = get_user_model()
class UserCreationForm(BaseUserCreationForm):
@@ -47,11 +47,11 @@ class UserCreationForm(BaseUserCreationForm):
self.fields['password2'].help_text = None # "Confirm password" is descriptive enough
class Meta:
model = UserModel
model = User
fields = ("username",)
def clean_username(self):
if UserModel.objects.filter(username=self.cleaned_data['username'], is_active=False).exists():
if User.objects.filter(username=self.cleaned_data['username'], is_active=False).exists():
raise ValidationError(mark_safe(
'This email is already registered but not yet confirmed. Please check your email for the confirmation '
'link or <b><a href="' + reverse("resend_confirmation") + "?email=" +
@@ -92,11 +92,11 @@ class UserEditForm(ModelForm):
self.fields['username'].help_text = None # "Email" is descriptive enough
class Meta:
model = UserModel
model = User
fields = ("username",)
def clean_username(self):
if UserModel.objects.exclude(pk=self.instance.pk).filter(username=self.cleaned_data['username']).exists():
if User.objects.exclude(pk=self.instance.pk).filter(username=self.cleaned_data['username']).exists():
raise ValidationError(mark_safe("This email is already registered by another user."))
return self.cleaned_data['username']
@@ -118,7 +118,7 @@ class RequestPasswordResetForm(forms.Form):
def clean_email(self):
email = self.cleaned_data['email']
if not UserModel.objects.filter(username=email).exists():
if not User.objects.filter(username=email).exists():
# Many sites say "if the email is registered, we've sent you an email with a password reset link" instead.
# The idea is not to leak information about which emails are registered. But in our setup we're already
# leaking that information in the signup form. At least for now, I'm erring on the side of
@@ -142,5 +142,5 @@ class PreferencesForm(ModelForm):
label=_("Send email alerts"), choices=TRUE_FALSE_CHOICES, required=False, widget=forms.Select())
class Meta:
model = UserModel
model = User
fields = ("send_email_alerts",)

View File

@@ -4,7 +4,7 @@ from django.contrib.auth import get_user_model
from users.models import EmailVerification
from users.tasks import send_welcome_email
UserModel = get_user_model()
User = get_user_model()
class Command(BaseCommand):
@@ -18,7 +18,7 @@ class Command(BaseCommand):
def handle(self, *args, **options):
email = options["email"]
user = UserModel.objects.get(email=email)
user = User.objects.get(email=email)
# copy/paste from views.py (excluding the comments)
verification = EmailVerification.objects.create(user=user, email=user.username)

View File

@@ -17,19 +17,19 @@ from .models import EmailVerification
from .tasks import send_confirm_email, send_reset_email
UserModel = get_user_model()
User = get_user_model()
@atomic_for_request_method
@user_passes_test(lambda u: u.is_superuser)
def user_list(request):
users = UserModel.objects.all().order_by('username')
users = User.objects.all().order_by('username')
if request.method == 'POST':
full_action_str = request.POST.get('action')
action, user_pk = full_action_str.split(":", 1)
if action == "deactivate":
user = UserModel.objects.get(pk=user_pk)
user = User.objects.get(pk=user_pk)
user.is_active = False
user.save()
@@ -37,7 +37,7 @@ def user_list(request):
return redirect('user_list')
if action == "activate":
user = UserModel.objects.get(pk=user_pk)
user = User.objects.get(pk=user_pk)
user.is_active = True
user.save()
@@ -52,7 +52,7 @@ def user_list(request):
@atomic_for_request_method
@user_passes_test(lambda u: u.is_superuser)
def user_edit(request, user_pk):
user = UserModel.objects.get(pk=user_pk)
user = User.objects.get(pk=user_pk)
if request.method == 'POST':
form = UserEditForm(request.POST, instance=user)
@@ -130,7 +130,7 @@ def resend_confirmation(request):
form = ResendConfirmationForm(request.POST)
if form.is_valid():
user = UserModel.objects.get(username=form.cleaned_data['email'])
user = User.objects.get(username=form.cleaned_data['email'])
if user.is_active:
raise Http404("This email is already confirmed.")
@@ -152,7 +152,7 @@ def request_reset_password(request):
form = RequestPasswordResetForm(request.POST)
if form.is_valid():
user = UserModel.objects.get(username=form.cleaned_data['email'])
user = User.objects.get(username=form.cleaned_data['email'])
# if not user.is_active no separate branch for this: password-reset implies email-confirmation
# we reuse the EmailVerification model for password resets; security wise it doesn't matter, because the