Skip to content

Commit

Permalink
Merge 0477a15 into 82c4000
Browse files Browse the repository at this point in the history
  • Loading branch information
liangliangyy committed Oct 19, 2018
2 parents 82c4000 + 0477a15 commit c61e6c9
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -9,7 +9,7 @@
- 支持文章全文搜索。
- 完整的评论功能,包括发表回复评论,以及评论的邮件提醒,支持`Markdown`
- 侧边栏功能,最新文章,最多阅读,标签云等。
- 支持Oauth登陆,现已有Google,GitHub,facebook,微博登录
- 支持Oauth登陆,现已有Google,GitHub,facebook,微博,QQ登录
- 支持`Memcache`缓存,支持缓存自动刷新。
- 简单的SEO功能,新建文章等会自动通知Google和百度。
- 集成了简单的图床功能。
Expand Down
1 change: 1 addition & 0 deletions oauth/models.py
Expand Up @@ -35,6 +35,7 @@ class OAuthConfig(models.Model):
('google', '谷歌'),
('github', 'GitHub'),
('facebook', 'FaceBook'),
('qq', 'QQ'),
)
type = models.CharField('类型', max_length=10, choices=TYPE, default='a')
appkey = models.CharField(max_length=200, verbose_name='AppKey')
Expand Down
83 changes: 80 additions & 3 deletions oauth/oauthmanager.py
Expand Up @@ -61,10 +61,12 @@ def get_oauth_userinfo(self):

def do_get(self, url, params):
rsp = requests.get(url=url, params=params)
logger.info(rsp.text)
return rsp.text

def do_post(self, url, params):
rsp = requests.post(url, params)
logger.info(rsp.text)
return rsp.text

def get_config(self):
Expand Down Expand Up @@ -272,7 +274,7 @@ def get_oauth_userinfo(self):
user.type = 'github'
user.token = self.access_token
user.matedata = rsp
if datas['email']:
if 'email' in datas and datas['email']:
user.email = datas['email']

return user
Expand Down Expand Up @@ -339,16 +341,91 @@ def get_oauth_userinfo(self):
user.type = 'facebook'
user.token = self.access_token
user.matedata = rsp
if datas['email']:
if 'email' in datas and datas['email']:
user.email = datas['email']
if datas['picture'] and datas['picture']['data'] and datas['picture']['data']['url']:
if 'picture' in datas and datas['picture'] and datas['picture']['data'] and datas['picture']['data']['url']:
user.picture = str(datas['picture']['data']['url'])
return user
except Exception as e:
logger.error(e)
return None


class QQOauthManager(BaseOauthManager):
AUTH_URL = 'https://graph.qq.com/oauth2.0/authorize'
TOKEN_URL = 'https://graph.qq.com/oauth2.0/token'
API_URL = 'https://graph.qq.com/user/get_user_info'
OPEN_ID_URL = 'https://graph.qq.com/oauth2.0/me'
ICON_NAME = 'qq'

def __init__(self, access_token=None, openid=None):
config = self.get_config()
self.client_id = config.appkey if config else ''
self.client_secret = config.appsecret if config else ''
self.callback_url = config.callback_url if config else ''
super(QQOauthManager, self).__init__(access_token=access_token, openid=openid)

def get_authorization_url(self, nexturl='/'):
params = {
'response_type': 'code',
'client_id': self.client_id,
'redirect_uri': self.callback_url + '&next_url=' + nexturl,
}
url = self.AUTH_URL + "?" + urllib.parse.urlencode(params)
return url

def get_access_token_by_code(self, code):
params = {
'grant_type': 'authorization_code',
'client_id': self.client_id,
'client_secret': self.client_secret,
'code': code,
'redirect_uri': self.callback_url
}
rsp = self.do_get(self.TOKEN_URL, params)
if rsp:
d = urllib.parse.parse_qs(rsp)
token = d['access_token']
self.access_token = token
return token

def get_open_id(self):
if self.is_access_token_set:
params = {
'access_token': self.access_token
}
rsp = self.do_get(self.OPEN_ID_URL, params)
if rsp:
rsp = rsp.replace('callback(', '').replace(')', '').replace(';', '')
obj = json.loads(rsp)
openid = str(obj['openid'])
self.openid = openid
return openid

def get_oauth_userinfo(self):
openid = self.get_open_id()
if openid:
params = {
'access_token': self.access_token,
'oauth_consumer_key': self.client_id,
'openid': self.openid
}
rsp = self.do_get(self.API_URL, params)
logger.info(rsp)
obj = json.loads(rsp)
user = OAuthUser()
user.nikename = obj['nickname']
user.openid = openid
user.type = 'qq'
user.token = self.access_token
user.matedata = rsp
if 'email' in obj:
user.email = obj['email']
if 'figureurl' in obj:
user.picture = str(obj['figureurl'])
return user


def get_oauth_apps():
configs = OAuthConfig.objects.filter(is_enable=True).all()
if not configs:
Expand Down
10 changes: 5 additions & 5 deletions requirements.txt
Expand Up @@ -2,7 +2,7 @@ appdirs==1.4.3
asn1crypto==0.24.0
astroid==2.0.4
bottle==0.12.13
certifi==2018.8.24
certifi==2018.10.15
cffi==1.11.5
chardet==3.0.4
coverage==4.5.1
Expand All @@ -23,11 +23,11 @@ jsonpickle==1.0
lazy-object-proxy==1.3.1
markdown2==2.3.6
mccabe==0.6.1
mistune==0.8.3
mistune==0.8.4
olefile==0.46
packaging==18.0
Pillow==5.3.0
pycparser==2.18
pycparser==2.19
Pygments==2.2.0
pylint==2.1.1
PyMySQL==0.9.2
Expand All @@ -37,12 +37,12 @@ python-slugify==1.2.6
pytz==2018.5
raven==6.9.0
rcssmin==1.0.6
requests==2.19.1
requests==2.20.0
rjsmin==1.0.12
six==1.11.0
sqlparse==0.2.4
Unidecode==1.0.22
urllib3==1.23
urllib3==1.24
webencodings==0.5.1
WeRoBot==1.6.0
Whoosh==2.7.4
Expand Down
4 changes: 2 additions & 2 deletions templates/oauth/bindsuccess.html
Expand Up @@ -13,9 +13,9 @@ <h2 class="archive-title"> {{ content }}</h2>
<br/>
<header class="archive-header" style="text-align: center">

<a href="{% url "account:login" %}">Sign In</a>
<a href="{% url "account:login" %}">登录</a>
|
<a href="/">Home Page</a>
<a href="/">回到首页</a>
</header><!-- .archive-header -->
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions templates/oauth/require_email.html
Expand Up @@ -4,7 +4,7 @@
{% block content %}
<div class="container">

<h2 class="form-signin-heading text-center">Binding E-mail account</h2>
<h2 class="form-signin-heading text-center">绑定您的邮箱账号</h2>

<div class="card card-signin">
{% if picture %}
Expand All @@ -25,7 +25,7 @@ <h2 class="form-signin-heading text-center">Binding E-mail account</h2>
{% endfor %}


<button class="btn btn-lg btn-primary btn-block" type="submit">Submit</button>
<button class="btn btn-lg btn-primary btn-block" type="submit">提交</button>

{% comment %}
<div class="checkbox">
Expand All @@ -39,7 +39,7 @@ <h2 class="form-signin-heading text-center">Binding E-mail account</h2>
</div>

<p class="text-center">
<a href="{% url "account:login" %}">Sign In</a>
<a href="{% url "account:login" %}">登录</a>
</p>

</div> <!-- /container -->
Expand Down
11 changes: 5 additions & 6 deletions travis_test/requirements.txt
Expand Up @@ -2,7 +2,7 @@ appdirs==1.4.3
asn1crypto==0.24.0
astroid==2.0.4
bottle==0.12.13
certifi==2018.8.24
certifi==2018.10.15
cffi==1.11.5
chardet==3.0.4
coverage==4.5.1
Expand All @@ -23,26 +23,25 @@ jsonpickle==1.0
lazy-object-proxy==1.3.1
markdown2==2.3.6
mccabe==0.6.1
mistune==0.8.3
mistune==0.8.4
olefile==0.46
packaging==18.0
Pillow==5.3.0
pycparser==2.18
pycparser==2.19
Pygments==2.2.0
pylint==2.1.1
PyMySQL==0.9.2
pyparsing==2.2.2
python-memcached==1.59
python-slugify==1.2.6
pytz==2018.5
raven==6.9.0
rcssmin==1.0.6
requests==2.19.1
requests==2.20.0
rjsmin==1.0.12
six==1.11.0
sqlparse==0.2.4
Unidecode==1.0.22
urllib3==1.23
urllib3==1.24
webencodings==0.5.1
WeRoBot==1.6.0
Whoosh==2.7.4
Expand Down

0 comments on commit c61e6c9

Please sign in to comment.