From 249fdf10f367799cfdabfed1010e867d1055ba84 Mon Sep 17 00:00:00 2001 From: liangliang Date: Sat, 22 Jul 2017 12:21:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84oauth=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .coveragerc | 1 + README.md | 17 +++++++++++++++-- oauth/oauthmanager.py | 12 ++++++------ oauth/templatetags/oauth_tags.py | 11 ++++++++--- oauth/urls.py | 1 + oauth/views.py | 24 +++++++++++++++++++++--- templates/account/login.html | 2 +- templates/blog/article_detail.html | 2 +- 8 files changed, 54 insertions(+), 16 deletions(-) diff --git a/.coveragerc b/.coveragerc index 1668be1..c47ac35 100644 --- a/.coveragerc +++ b/.coveragerc @@ -8,3 +8,4 @@ omit = *whoosh_cn_backend* *apps* *commands* + *oauth* diff --git a/README.md b/README.md index 5452e67..a429cd7 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,19 @@ [![Build Status](https://travis-ci.org/liangliangyy/DjangoBlog.svg?branch=master)](https://travis-ci.org/liangliangyy/DjangoBlog) [![Coverage Status](https://coveralls.io/repos/github/liangliangyy/DjangoBlog/badge.svg?branch=master)](https://coveralls.io/github/liangliangyy/DjangoBlog?branch=master) [![Requirements Status](https://requires.io/github/liangliangyy/DjangoBlog/requirements.svg?branch=master)](https://requires.io/github/liangliangyy/DjangoBlog/requirements/?branch=master) [![license](https://img.shields.io/github/license/liangliangyy/djangoblog.svg)]() [![GitHub release](https://img.shields.io/github/release/liangliangyy/djangoblog.svg)]() [![python3.5](https://img.shields.io/badge/python-3.5-brightgreen.svg)]() [![django1.10](https://img.shields.io/badge/django-1.10-brightgreen.svg)]() +## 主要功能: +- 文章,页面,分类目录,标签的添加,删除,编辑等。文章及页面支持`Markdown`,支持代码高亮。 +- 支持文章全文搜索。 +- 完整的评论功能,包括发表回复评论,以及评论的邮件提醒,支持`Markdown`。 +- 侧边栏功能,最新文章,最多阅读,标签云等。 +- 支持Oauth登陆,现已有Google,GitHub,微博登录。 +- 支持`Memcache`缓存,支持缓存自动刷新。 +- 简单的SEO功能,新建文章等会自动通知Google和百度。 +- 集成了简单的图床功能。 +- 集成`django-compressor`,自动压缩`css`,`js`。 +- 基于`python3`,支持`Django`多版本。`Django`的1.8,1.9,1.10,1.11均测试通过。 +- 网站异常邮件提醒,若有未捕捉到的异常会自动发送提醒邮件。 + ## 安装 使用pip安装: `pip install -r requirements.txt` @@ -70,7 +83,7 @@ windows电脑: 浏览器打开: http://127.0.0.1:8000/ 就可以看到效果了。 - + ## 问题相关 - + 有任何问题欢迎提Issue,或者将问题描述发送至我邮箱 `liangliangyy#gmail.com`.我会尽快解答.推荐提交Issue方式. diff --git a/oauth/oauthmanager.py b/oauth/oauthmanager.py index 4edbf0c..13d5937 100644 --- a/oauth/oauthmanager.py +++ b/oauth/oauthmanager.py @@ -45,7 +45,7 @@ class BaseOauthManager(metaclass=ABCMeta): return self.is_access_token_set and self.access_token is not None and self.openid is not None @abstractmethod - def get_authorization_url(self): + def get_authorization_url(self, nexturl='/'): pass @abstractmethod @@ -77,11 +77,11 @@ class WBOauthManager(BaseOauthManager): self.callback_url = settings.OAHUTH['sina']['callbackurl'] super(WBOauthManager, self).__init__(access_token=access_token, openid=openid) - def get_authorization_url(self): + def get_authorization_url(self, nexturl='/'): params = { 'client_id': self.client_id, 'response_type': 'code', - 'redirect_uri': self.callback_url + 'redirect_uri': self.callback_url + '&next_url=' + nexturl } url = self.AUTH_URL + "?" + urllib.parse.urlencode(params) return url @@ -142,7 +142,7 @@ class GoogleOauthManager(BaseOauthManager): self.callback_url = settings.OAHUTH['google']['callbackurl'] super(GoogleOauthManager, self).__init__(access_token=access_token, openid=openid) - def get_authorization_url(self): + def get_authorization_url(self, nexturl='/'): params = { 'client_id': self.client_id, 'response_type': 'code', @@ -210,11 +210,11 @@ class GitHubOauthManager(BaseOauthManager): self.callback_url = settings.OAHUTH['github']['callbackurl'] super(GitHubOauthManager, self).__init__(access_token=access_token, openid=openid) - def get_authorization_url(self): + def get_authorization_url(self, nexturl='/'): params = { 'client_id': self.client_id, 'response_type': 'code', - 'redirect_uri': self.callback_url, + 'redirect_uri': self.callback_url + '&next_url=' + nexturl, 'scope': 'user' } # url = self.AUTH_URL + "?" + urllib.parse.urlencode(params, quote_via=urllib.parse.quote) diff --git a/oauth/templatetags/oauth_tags.py b/oauth/templatetags/oauth_tags.py index eb37aab..96b74cd 100644 --- a/oauth/templatetags/oauth_tags.py +++ b/oauth/templatetags/oauth_tags.py @@ -13,7 +13,7 @@ @time: 2017/3/4 下午3:22 """ from oauth.oauthmanager import get_oauth_apps - +from django.core.urlresolvers import reverse from django import template from django.conf import settings @@ -21,9 +21,14 @@ register = template.Library() @register.inclusion_tag('oauth/oauth_applications.html') -def load_oauth_applications(): +def load_oauth_applications(request): applications = get_oauth_apps() - apps = list(map(lambda x: (x.ICON_NAME, x.get_authorization_url()), applications)) + baseurl = reverse('oauth:oauthlogin') + path = request.get_full_path() + + apps = list(map(lambda x: (x.ICON_NAME, + '{baseurl}?type={type}&next_url={next}' + .format(baseurl=baseurl, type=x.ICON_NAME, next=path)), applications)) return { 'apps': apps } diff --git a/oauth/urls.py b/oauth/urls.py index f54db3d..a10fc7e 100644 --- a/oauth/urls.py +++ b/oauth/urls.py @@ -22,6 +22,7 @@ urlpatterns = [ url(r'^oauth/requireemail/(?P\d+).html', views.RequireEmailView.as_view(), name='require_email'), url(r'^oauth/emailconfirm/(?P\d+)/(?P\S+).html', views.emailconfirm, name='email_confirm'), url(r'^oauth/bindsuccess/(?P\d+).html', views.bindsuccess, name='bindsuccess'), + url(r'^oauth/oauthlogin$', views.oauthlogin,name='oauthlogin') ] """ diff --git a/oauth/views.py b/oauth/views.py index 42e5b8f..d721284 100644 --- a/oauth/views.py +++ b/oauth/views.py @@ -18,6 +18,21 @@ from django.http import HttpResponseForbidden from .oauthmanager import get_manager_by_type +def oauthlogin(request): + type = request.GET.get('type', None) + if not type: + return HttpResponseRedirect('/') + manager = get_manager_by_type(type) + if not manager: + return HttpResponseRedirect('/') + nexturl = request.GET.get('next_url', None) + print(nexturl) + if not nexturl or nexturl == '/login/': + nexturl = '/' + authorizeurl = manager.get_authorization_url(nexturl) + return HttpResponseRedirect(authorizeurl) + + def authorize(request): manager = None type = request.GET.get('type', None) @@ -28,8 +43,11 @@ def authorize(request): return HttpResponseRedirect('/') code = request.GET.get('code', None) rsp = manager.get_access_token_by_code(code) + nexturl = request.GET.get('next_url', None) + if not nexturl: + nexturl = '/' if not rsp: - return HttpResponseRedirect(manager.get_authorization_url()) + return HttpResponseRedirect(manager.get_authorization_url(nexturl)) user = manager.get_oauth_userinfo() if user: @@ -60,7 +78,7 @@ def authorize(request): user.author = author user.save() login(request, author) - return HttpResponseRedirect('/') + return HttpResponseRedirect(nexturl) if not email: # todo # 未避免用户名重复,暂时使用oauth用户名+openid这种方式来创建用户 @@ -74,7 +92,7 @@ def authorize(request): return HttpResponseRedirect(url) else: - return HttpResponseRedirect('/') + return HttpResponseRedirect(nexturl) def emailconfirm(request, id, sign): diff --git a/templates/account/login.html b/templates/account/login.html index 3a7e364..f758b7a 100644 --- a/templates/account/login.html +++ b/templates/account/login.html @@ -29,7 +29,7 @@ {% load oauth_tags %} - {% load_oauth_applications %} + {% load_oauth_applications request%} diff --git a/templates/blog/article_detail.html b/templates/blog/article_detail.html index 7e45d5e..13f8d1b 100755 --- a/templates/blog/article_detail.html +++ b/templates/blog/article_detail.html @@ -53,7 +53,7 @@ href="{% url "account:login" %}?next={{ request.get_full_path }}">登录后发表评论。 {% load oauth_tags %} - {% load_oauth_applications %} + {% load_oauth_applications request%} {% endif %}