Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: user form types #764

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/unfold/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from django.contrib.auth.forms import (
AdminPasswordChangeForm as BaseAdminPasswordChangeForm,
)
from django.contrib.auth.models import User

try:
from django.contrib.auth.forms import AdminUserCreationForm as BaseUserCreationForm
Expand Down Expand Up @@ -70,11 +71,10 @@ def __init__(
class UserCreationForm(BaseUserCreationForm):
def __init__(
self,
request: Optional[HttpRequest] = None,
*args,
**kwargs,
) -> None:
super().__init__(request, *args, **kwargs)
super().__init__(*args, **kwargs)

self.fields["password1"].widget = UnfoldAdminPasswordInput(
attrs={"autocomplete": "new-password"}
Expand All @@ -92,11 +92,10 @@ def __init__(
class UserChangeForm(BaseUserChangeForm):
def __init__(
self,
request: Optional[HttpRequest] = None,
*args,
**kwargs,
) -> None:
super().__init__(request, *args, **kwargs)
super().__init__(*args, **kwargs)
self.fields["password"].widget = UnfoldReadOnlyPasswordHashWidget()

self.fields["password"].help_text = _(
Expand All @@ -113,19 +112,19 @@ def __init__(
class AdminPasswordChangeForm(BaseAdminPasswordChangeForm):
def __init__(
self,
request: Optional[HttpRequest] = None,
user: User,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lukasvinclav Sorry, this is wrong: You can have your own User who doesn't inherit from User -> The type-checker will throw an error there, as User will not be the parent.

*args,
**kwargs,
) -> None:
super().__init__(request, *args, **kwargs)
super().__init__(user, *args, **kwargs)

self.fields["password1"].widget.attrs["class"] = " ".join(INPUT_CLASSES)
self.fields["password2"].widget.attrs["class"] = " ".join(INPUT_CLASSES)


class AdminOwnPasswordChangeForm(BaseAdminOwnPasswordChangeForm):
def __init__(self, *args, **kwargs) -> None:
super().__init__(kwargs.pop("user"), *args, **kwargs)
def __init__(self, user: User, *args, **kwargs) -> None:
super().__init__(user, *args, **kwargs)

self.fields["old_password"].widget.attrs["class"] = " ".join(INPUT_CLASSES)
self.fields["new_password1"].widget.attrs["class"] = " ".join(INPUT_CLASSES)
Expand Down