You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tentest/doc/DjangoBlog/oauth/models.py

94 lines
4.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#zwz: Create your models here.
#zwz: 导入Django配置、异常、数据库模型等核心模块
from django.conf import settings
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
#zwz: OAuth用户表存储第三方登录用户的关联信息
class OAuthUser(models.Model):
#zwz: 关联Django系统用户可为空未绑定本地用户时用
author = models.ForeignKey(
settings.AUTH_USER_MODEL,
verbose_name=_('author'), # 字段显示名称(多语言支持)
blank=True,
null=True,
on_delete=models.CASCADE) # 关联用户删除时,此记录也删除
#zwz: 第三方平台的用户唯一标识如GitHub的ID
openid = models.CharField(max_length=50)
#zwz: 第三方平台的用户昵称
nickname = models.CharField(max_length=50, verbose_name=_('nick name'))
#zwz: 第三方平台的授权令牌(可选)
token = models.CharField(max_length=150, null=True, blank=True)
#zwz: 第三方平台的用户头像链接
picture = models.CharField(max_length=350, blank=True, null=True)
#zwz: 第三方平台类型如github/weibo
type = models.CharField(blank=False, null=False, max_length=50)
#zwz: 第三方平台的用户邮箱(可选)
email = models.CharField(max_length=50, null=True, blank=True)
#zwz: 第三方平台返回的额外元数据存JSON等
metadata = models.TextField(null=True, blank=True)
#zwz: 记录创建时间(默认当前时间)
creation_time = models.DateTimeField(_('creation time'), default=now)
#zwz: 记录最后修改时间
last_modify_time = models.DateTimeField(_('last modify time'), default=now)
#zwz: 打印对象时显示昵称
def __str__(self):
return self.nickname
#zwz: 模型元信息配置
class Meta:
verbose_name = _('oauth user') # 模型显示名称
verbose_name_plural = verbose_name # 复数显示名称
ordering = ['-creation_time'] # 默认按创建时间倒序排列
#zwz: OAuth平台配置表存储第三方登录的AppKey、密钥等信息
class OAuthConfig(models.Model):
#zwz: 支持的第三方平台类型(固定选项)
TYPE = (
('weibo', _('weibo')),
('google', _('google')),
('github', 'GitHub'),
('facebook', 'FaceBook'),
('qq', 'QQ'),
)
#zwz: 平台类型关联上面的TYPE选项
type = models.CharField(_('type'), max_length=10, choices=TYPE, default='a')
#zwz: 第三方平台的AppKey
appkey = models.CharField(max_length=200, verbose_name='AppKey')
#zwz: 第三方平台的AppSecret
appsecret = models.CharField(max_length=200, verbose_name='AppSecret')
#zwz: 第三方平台的授权回调地址
callback_url = models.CharField(
max_length=200,
verbose_name=_('callback url'),
blank=False,
default='')
#zwz: 是否启用该平台的登录功能
is_enable = models.BooleanField(
_('is enable'), default=True, blank=False, null=False)
#zwz: 配置创建时间
creation_time = models.DateTimeField(_('creation time'), default=now)
#zwz: 配置最后修改时间
last_modify_time = models.DateTimeField(_('last modify time'), default=now)
#zwz: 数据验证:同一平台只能有一条配置
def clean(self):
#zwz: 排除当前记录后,检查是否已有同类型配置
if OAuthConfig.objects.filter(
type=self.type).exclude(id=self.id).count():
raise ValidationError(_(self.type + _('already exists')))
#zwz: 打印对象时显示平台类型
def __str__(self):
return self.type
#zwz: 模型元信息配置
class Meta:
verbose_name = 'oauth配置' # 模型显示名称(中文)
verbose_name_plural = verbose_name # 复数显示名称
ordering = ['-creation_time'] # 默认按创建时间倒序排列