Repository: svthalia/concrexit

Issue:
User creation in the admin is broken
Sentry Issue: [CONCREXIT-3F](https://sentry.io/organizations/thalia/issues/1844597243/?referrer=github_integration)

```
FieldError: Unknown field(s) (password2, password1) specified for User
  File "django/contrib/admin/options.py", line 702, in get_form
    return modelform_factory(self.model, **defaults)
  File "django/forms/models.py", line 554, in modelform_factory
    return type(form)(class_name, (form,), form_class_attrs)
  File "django/forms/models.py", line 267, in __new__
    raise FieldError(message)

FieldError: Unknown field(s) (password2, password1) specified for User. Check fields/fieldsets/exclude attributes of class UserAdmin.
(15 additional frame(s) were not displayed)
...
  File "django/utils/decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "django/contrib/admin/options.py", line 1522, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "django/contrib/admin/options.py", line 1555, in _changeform_view
    ModelForm = self.get_form(request, obj, change=not add)
  File "django/contrib/auth/admin.py", line 80, in get_form
    return super().get_form(request, obj, **defaults)
  File "django/contrib/admin/options.py", line 704, in get_form
    raise FieldError(
```


Discussion:

Resolving patch:
diff --git a/website/members/forms.py b/website/members/forms.py
--- a/website/members/forms.py
+++ b/website/members/forms.py
@@ -1,13 +1,10 @@
 """Forms defined by the members package."""
 from django import forms
-from django.conf import settings
-from django.contrib.auth import get_user_model
 from django.contrib.auth.forms import UserChangeForm as BaseUserChangeForm
 from django.contrib.auth.forms import UserCreationForm as BaseUserCreationForm
 from django.core.validators import RegexValidator
 from django.utils.translation import gettext_lazy as _
 
-from members import emails
 from .models import Profile
 
 
@@ -47,48 +44,15 @@ def __init__(self, *args, **kwargs):
 
 
 class UserCreationForm(BaseUserCreationForm):
-    """Custom Form that removes the password fields from user creation and sends a welcome message when a user is created."""
-
-    # Don't forget to edit the formset in admin.py!
-    # This is a stupid quirk of the user admin.
-
-    # shadow the password fields to prevent validation errors,
-    #   since we generate the passwords dynamically.
-    password1 = None
-    password2 = None
-
-    def __init__(self, *args, **kwargs):
-        super().__init__(*args, **kwargs)
-        for field in ("email", "first_name", "last_name"):
-            self.fields[field].required = True
-
-    send_welcome_email = forms.BooleanField(
-        label=_("Send welcome email"),
-        help_text=_("This email will include the generated password"),
-        required=False,
-        initial=True,
-    )
+    """Custom Form that lowercases the username on creation."""
 
     def clean(self):
         if "username" in self.cleaned_data:
             self.cleaned_data["username"] = self.cleaned_data["username"].lower()
         super().clean()
 
-    def save(self, commit=True):
-        password = get_user_model().objects.make_random_password(length=15)
-        # pass the password on as if it was filled in, so that save() works
-        self.cleaned_data["password1"] = password
-        user = super().save(commit=False)
-        user.set_password(password)
-        if commit:
-            user.save()
-        if self.cleaned_data["send_welcome_email"]:
-            language = settings.LANGUAGE_CODE
-            emails.send_welcome_message(user, password, language)
-        return user
-
     class Meta:
-        fields = ("username", "first_name", "last_name", "send_welcome_email")
+        fields = ("username", "first_name", "last_name")
 
 
 class UserChangeForm(BaseUserChangeForm):


Test patch: