Compare commits
16 Commits
master
...
lht_branch
| Author | SHA1 | Date |
|---|---|---|
|
|
d22b9372cc | 3 months ago |
|
|
dd22fed20a | 4 months ago |
|
|
269cd58ec1 | 4 months ago |
|
|
4ca99ea6df | 4 months ago |
|
|
84a80fdcca | 4 months ago |
|
|
7fe81c8d3f | 4 months ago |
|
|
3d235b6acb | 4 months ago |
|
|
99982317ec | 4 months ago |
|
|
5c87dddf26 | 4 months ago |
|
|
df8aa25c36 | 4 months ago |
|
|
8392c793ce | 4 months ago |
|
|
113f09e91e | 4 months ago |
|
|
d8bdf3afa5 | 4 months ago |
|
|
13f3da23c9 | 4 months ago |
|
|
708dd50998 | 4 months ago |
|
|
161cfbbb02 | 4 months ago |
Binary file not shown.
@ -0,0 +1,43 @@
|
||||
from django.contrib.auth.models import AbstractUser
|
||||
from django.db import models
|
||||
from django.urls import reverse
|
||||
from django.utils.timezone import now
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from djangoblog.utils import get_current_site
|
||||
|
||||
|
||||
#lht: Create your models here.
|
||||
|
||||
class BlogUser(AbstractUser):
|
||||
#lht: 用户昵称字段
|
||||
nickname = models.CharField(_('nick name'), max_length=100, blank=True)
|
||||
#lht: 用户创建时间
|
||||
creation_time = models.DateTimeField(_('creation time'), default=now)
|
||||
#lht: 用户最后修改时间
|
||||
last_modify_time = models.DateTimeField(_('last modify time'), default=now)
|
||||
#lht: 用户来源标识(如通过注册、后台创建等)
|
||||
source = models.CharField(_('create source'), max_length=100, blank=True)
|
||||
|
||||
def get_absolute_url(self):
|
||||
#lht: 返回用户个人页面的URL
|
||||
return reverse(
|
||||
'blog:author_detail', kwargs={
|
||||
'author_name': self.username})
|
||||
|
||||
def __str__(self):
|
||||
#lht: 字符串表示,返回用户邮箱
|
||||
return self.email
|
||||
|
||||
def get_full_url(self):
|
||||
#lht: 获取用户页面的完整URL
|
||||
site = get_current_site().domain
|
||||
url = "https://{site}{path}".format(site=site,
|
||||
path=self.get_absolute_url())
|
||||
return url
|
||||
|
||||
class Meta:
|
||||
#lht: 模型元数据配置
|
||||
ordering = ['-id'] #lht: 默认按ID倒序排列
|
||||
verbose_name = _('user') #lht: 单数名称
|
||||
verbose_name_plural = verbose_name #lht: 复数名称
|
||||
get_latest_by = 'id' #lht: 获取最新记录的字段
|
||||
@ -0,0 +1,35 @@
|
||||
from django.urls import path
|
||||
from django.urls import re_path
|
||||
|
||||
from . import views
|
||||
from .forms import LoginForm
|
||||
|
||||
app_name = "accounts" #lht: 应用命名空间
|
||||
|
||||
urlpatterns = [
|
||||
#lht: 登录URL
|
||||
re_path(r'^login/$',
|
||||
views.LoginView.as_view(success_url='/'),
|
||||
name='login',
|
||||
kwargs={'authentication_form': LoginForm}),
|
||||
#lht: 注册URL
|
||||
re_path(r'^register/$',
|
||||
views.RegisterView.as_view(success_url="/"),
|
||||
name='register'),
|
||||
#lht: 登出URL
|
||||
re_path(r'^logout/$',
|
||||
views.LogoutView.as_view(),
|
||||
name='logout'),
|
||||
#lht: 账户操作结果页面
|
||||
path(r'account/result.html',
|
||||
views.account_result,
|
||||
name='result'),
|
||||
#lht: 忘记密码页面
|
||||
re_path(r'^forget_password/$',
|
||||
views.ForgetPasswordView.as_view(),
|
||||
name='forget_password'),
|
||||
#lht: 获取忘记密码验证码
|
||||
re_path(r'^forget_password_code/$',
|
||||
views.ForgetPasswordEmailCode.as_view(),
|
||||
name='forget_password_code'),
|
||||
]
|
||||
@ -0,0 +1,28 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.backends import ModelBackend
|
||||
|
||||
|
||||
class EmailOrUsernameModelBackend(ModelBackend):
|
||||
#lht: """
|
||||
#lht: 允许使用用户名或邮箱登录
|
||||
#lht: """
|
||||
|
||||
def authenticate(self, request, username=None, password=None, **kwargs):
|
||||
#lht: 根据输入内容判断是邮箱还是用户名
|
||||
if '@' in username:
|
||||
kwargs = {'email': username}
|
||||
else:
|
||||
kwargs = {'username': username}
|
||||
try:
|
||||
user = get_user_model().objects.get(**kwargs)
|
||||
if user.check_password(password):
|
||||
return user
|
||||
except get_user_model().DoesNotExist:
|
||||
return None
|
||||
|
||||
def get_user(self, username):
|
||||
#lht: 根据用户名获取用户对象
|
||||
try:
|
||||
return get_user_model().objects.get(pk=username)
|
||||
except get_user_model().DoesNotExist:
|
||||
return None
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue