修复用户名可能为空的错误,修复后台不能修改密码的问题

sh_branch
liangliangyy 7 years ago
parent 6f4df53401
commit 260244c511

@ -5,6 +5,8 @@ from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.forms import ReadOnlyPasswordHashField
# Register your models here.
from .models import BlogUser
from django.utils.translation import gettext, gettext_lazy as _
from django.contrib.auth.forms import UsernameField
class BlogUserCreationForm(forms.ModelForm):
@ -28,23 +30,29 @@ class BlogUserCreationForm(forms.ModelForm):
user = super().save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.source = 'adminsite'
user.save()
return user
class BlogUserChangeForm(forms.ModelForm):
password = ReadOnlyPasswordHashField
class BlogUserChangeForm(UserChangeForm):
password = ReadOnlyPasswordHashField(
label=_("Password"),
help_text=_(
"Raw passwords are not stored, so there is no way to see this "
"user's password, but you can change the password using "
"<a href=\"{}\">this form</a>."
),
)
email = forms.EmailField(label="Email", widget=forms.EmailInput)
class Meta:
model = BlogUser
fields = ('email', 'password', 'is_active')
fields = '__all__'
field_classes = {'username': UsernameField}
def clean_password(self):
# Regardless of what the user provides, return the initial value.
# This is done here, rather than on the field, because the
# field does not have access to the initial value
return self.initial["password"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
class BlogUserAdmin(UserAdmin):

@ -11,6 +11,7 @@ class BlogUser(AbstractUser):
nickname = models.CharField('昵称', max_length=100, blank=True)
created_time = models.DateTimeField('创建时间', default=now)
last_mod_time = models.DateTimeField('修改时间', default=now)
source = models.CharField("创建涞源", max_length=100, blank=True)
# objects = BlogUserManager()

@ -33,6 +33,7 @@ class RegisterView(FormView):
if form.is_valid():
user = form.save(False)
user.is_active = False
user.source = 'Register'
user.save(True)
site = get_current_site().domain
sign = get_md5(get_md5(settings.SECRET_KEY + str(user.id)))
@ -106,7 +107,7 @@ class LoginView(FormView):
if cache and cache is not None:
cache.clear()
logger.info(self.redirect_field_name)
redirect_to = self.request.GET.get(self.redirect_field_name)
auth.login(self.request, form.get_user())
return super(LoginView, self).form_valid(form)
# return HttpResponseRedirect('/')

@ -2,6 +2,7 @@ from django.shortcuts import render
# Create your views here.
from urllib.parse import urlparse
import datetime
from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth import get_user_model
@ -70,7 +71,7 @@ def authorize(request):
return HttpResponseRedirect(manager.get_authorization_url(nexturl))
user = manager.get_oauth_userinfo()
if user:
if not user.nikename:
if not user.nikename.strip():
import datetime
user.nikename = "djangoblog" + datetime.datetime.now().strftime('%y%m%d%I%M%S')
try:
@ -96,6 +97,7 @@ def authorize(request):
author = result[0]
if result[1]:
author.username = user.nikename
author.source = 'authorize'
author.save()
user.author = author
@ -127,7 +129,9 @@ def emailconfirm(request, id, sign):
result = get_user_model().objects.get_or_create(email=oauthuser.email)
author = result[0]
if result[1]:
author.username = oauthuser.nikename
author.source = 'emailconfirm'
author.username = oauthuser.nikename.strip() if oauthuser.nikename.strip() else "djangoblog" + datetime.datetime.now().strftime(
'%y%m%d%I%M%S')
author.save()
oauthuser.author = author
oauthuser.save()

Loading…
Cancel
Save