No require password if register via social auth

This commit is contained in:
cuom1999 2024-01-30 19:47:06 -06:00
parent 9fd93a3b53
commit 2ff1ed0f54
8 changed files with 410 additions and 353 deletions

View file

@ -1,5 +1,8 @@
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.views import PasswordChangeView
from django.urls import reverse_lazy
class CustomModelBackend(ModelBackend):
@ -13,3 +16,32 @@ class CustomModelBackend(ModelBackend):
if user and user.check_password(password):
return user
class CustomPasswordChangeForm(PasswordChangeForm):
def __init__(self, *args, **kwargs):
super(CustomPasswordChangeForm, self).__init__(*args, **kwargs)
if not self.user.has_usable_password():
self.fields.pop("old_password")
def clean_old_password(self):
if "old_password" not in self.cleaned_data:
return
return super(CustomPasswordChangeForm, self).clean_old_password()
def clean(self):
cleaned_data = super(CustomPasswordChangeForm, self).clean()
if "old_password" not in self.cleaned_data and not self.errors:
cleaned_data["old_password"] = ""
return cleaned_data
class CustomPasswordChangeView(PasswordChangeView):
form_class = CustomPasswordChangeForm
success_url = reverse_lazy("password_change_done")
template_name = "registration/password_change_form.html"
def get_form_kwargs(self):
kwargs = super(CustomPasswordChangeView, self).get_form_kwargs()
kwargs["user"] = self.request.user
return kwargs

View file

@ -9,6 +9,7 @@ from django.db import transaction
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.utils.translation import gettext as _
from requests import HTTPError
from reversion import revisions
from social_core.backends.github import GithubOAuth2
@ -65,13 +66,13 @@ class UsernameForm(forms.Form):
max_length=30,
label="Username",
error_messages={
"invalid": "A username must contain letters, numbers, or underscores"
"invalid": _("A username must contain letters, numbers, or underscores")
},
)
def clean_username(self):
if User.objects.filter(username=self.cleaned_data["username"]).exists():
raise forms.ValidationError("Sorry, the username is taken.")
raise forms.ValidationError(_("Sorry, the username is taken."))
return self.cleaned_data["username"]
@ -89,7 +90,7 @@ def choose_username(backend, user, username=None, *args, **kwargs):
request,
"registration/username_select.html",
{
"title": "Choose a username",
"title": _("Choose a username"),
"form": form,
},
)
@ -118,7 +119,7 @@ def make_profile(backend, user, response, is_new=False, *args, **kwargs):
backend.strategy.request,
"registration/profile_creation.html",
{
"title": "Create your profile",
"title": _("Create your profile"),
"form": form,
},
)