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

@ -61,6 +61,7 @@ from judge.views import (
course,
email,
)
from judge import authentication
from judge.views.test_formatter import test_formatter
@ -139,9 +140,7 @@ register_patterns = [
url(r"^logout/$", user.UserLogoutView.as_view(), name="auth_logout"),
url(
r"^password/change/$",
auth_views.PasswordChangeView.as_view(
template_name="registration/password_change_form.html",
),
authentication.CustomPasswordChangeView.as_view(),
name="password_change",
),
url(

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,
},
)

File diff suppressed because it is too large Load diff

View file

@ -39,9 +39,6 @@ msgstr "Đăng ký tên"
msgid "Report"
msgstr "Báo cáo tiêu cực"
msgid "Bug Report"
msgstr "Báo cáo lỗi"
msgid "2sat"
msgstr ""
@ -597,6 +594,9 @@ msgstr ""
msgid "z-function"
msgstr ""
#~ msgid "Bug Report"
#~ msgstr "Báo cáo lỗi"
#~ msgid "Insert Image"
#~ msgstr "Chèn hình ảnh"

View file

@ -417,6 +417,11 @@ function onWindowReady() {
$(this).hide().css({ width: 0});
});
});
$('.errorlist').each(function() {
var errorList = $(this);
errorList.nextAll('input, select, textarea').first().after(errorList);
});
}
$(function() {

View file

@ -611,6 +611,7 @@ ul.errorlist {
list-style: none;
padding: 0px;
color: red;
margin-bottom: 3px;
}
.registration-form {

View file

@ -30,11 +30,13 @@
{% block js_media %}{{ form.media.js }}{% endblock %}
{% block body %}
<div id="center-float" class="registration-form">
<form action="" method="post" class="form-area">
{% csrf_token %}
<table border="0" style="text-align:left">{{ form.as_table() }}</table>
<input type="submit" style="float:right;" value="{{ _('Continue >') }}">
</form>
</div>
<div class="map-wrap">
<div class="map-inset">