diff --git a/DjangoBlog/settings.py b/DjangoBlog/settings.py index f847e5c..a304d85 100644 --- a/DjangoBlog/settings.py +++ b/DjangoBlog/settings.py @@ -39,8 +39,8 @@ INSTALLED_APPS = [ 'pagedown', 'blog', 'accounts', - 'comments' - + 'comments', + 'oauth' ] MIDDLEWARE_CLASSES = [ @@ -165,3 +165,11 @@ CACHES = { } } """ + +OAHUTH = { + 'sina': { + 'appkey':'3161614143', + 'appsecret':'ee17c099317f872eeddb25204ea46721', + 'callbackurl':'http://blog.lylinux.org/oauth/weibo' + } +} diff --git a/DjangoBlog/urls.py b/DjangoBlog/urls.py index 03725d3..623518e 100644 --- a/DjangoBlog/urls.py +++ b/DjangoBlog/urls.py @@ -21,5 +21,6 @@ urlpatterns = [ url(r'', include('blog.urls', namespace='blog', app_name='blog')), url(r'', include('comments.urls', namespace='comment', app_name='comments')), - url(r'', include('accounts.urls', namespace='account', app_name='accounts')) + url(r'', include('accounts.urls', namespace='account', app_name='accounts')), + url(r'', include('oauth.urls', namespace='oauth', app_name='oauth')) ] diff --git a/bin/django_start b/bin/django_start index 40b1141..c65d59b 100755 --- a/bin/django_start +++ b/bin/django_start @@ -5,7 +5,7 @@ DJANGODIR=//var/www/DjangoBlog # Django project directory SOCKFILE=/var/www/DjangoBlog/run/gunicorn.sock # we will communicte using this unix socket USER=www-data # the user to run as GROUP=www-data # the group to run as -NUM_WORKERS=2 # how many worker processes should Gunicorn spawn +NUM_WORKERS=3 # how many worker processes should Gunicorn spawn DJANGO_SETTINGS_MODULE=DjangoBlog.settings # which settings file should Django use DJANGO_WSGI_MODULE=DjangoBlog.wsgi # WSGI module name diff --git a/blog/context_processors.py b/blog/context_processors.py index 34874f9..8a86b16 100644 --- a/blog/context_processors.py +++ b/blog/context_processors.py @@ -20,5 +20,6 @@ def seo_processor(requests): return { 'SITE_NAME': settings.SITE_NAME, 'SITE_DESCRIPTION': settings.SITE_DESCRIPTION, - 'SITE_BASE_URL':'http://'+ requests.get_host() + '/', + 'SITE_BASE_URL': 'http://' + requests.get_host() + '/', + 'nav_category_list': Category.objects.all() } diff --git a/blog/templatetags/blog_tags.py b/blog/templatetags/blog_tags.py index a7f816a..f1f891e 100644 --- a/blog/templatetags/blog_tags.py +++ b/blog/templatetags/blog_tags.py @@ -115,6 +115,15 @@ def load_article_metas(article): } +""" +@register.inclusion_tag('nav.html') +def load_nav_info(): + category_list = Category.objects.all() + return { + 'nav_category_list': category_list + } +""" + @register.inclusion_tag('blog/tags/article_info.html') def load_article_detail(article, isindex): return { @@ -156,8 +165,6 @@ def query(qs, **kwargs): return qs.filter(**kwargs) - - """ article = Article.objects.get(pk=4) comments = Comment.objects.filter(article=article) diff --git a/oauth/__init__.py b/oauth/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/oauth/admin.py b/oauth/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/oauth/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/oauth/apps.py b/oauth/apps.py new file mode 100644 index 0000000..17fcea2 --- /dev/null +++ b/oauth/apps.py @@ -0,0 +1,5 @@ +from django.apps import AppConfig + + +class OauthConfig(AppConfig): + name = 'oauth' diff --git a/oauth/models.py b/oauth/models.py new file mode 100644 index 0000000..db2fa82 --- /dev/null +++ b/oauth/models.py @@ -0,0 +1,29 @@ +from django.db import models + +# Create your models here. +from django.conf import settings + + +class BaseModel(models.Model): + author = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name='用户') + openid = models.CharField(max_length=50) + nikename = models.CharField(max_length=50, verbose_name='昵称') + token = models.CharField(max_length=50) + + def __str__(self): + return self.nikename + + class Meta: + abstract = True + + +class SinaWBUserInfo(BaseModel): + class Meta: + verbose_name = "新浪微博" + verbose_name_plural = verbose_name + + +class QQUserInfo(BaseModel): + class Meta: + verbose_name = "QQ" + verbose_name_plural = verbose_name diff --git a/oauth/oauthmanager.py b/oauth/oauthmanager.py new file mode 100644 index 0000000..42bbc3c --- /dev/null +++ b/oauth/oauthmanager.py @@ -0,0 +1,117 @@ +#!/usr/bin/env python +# encoding: utf-8 + + +""" +@version: ?? +@author: liangliangyy +@license: MIT Licence +@contact: liangliangyy@gmail.com +@site: https://www.lylinux.org/ +@software: PyCharm +@file: oauthmanager.py +@time: 2016/11/26 下午5:09 +""" + +from abc import ABCMeta, abstractmethod, abstractproperty +import requests +import json +import urllib.parse + + +class BaseManager(metaclass=ABCMeta): + """获取用户授权""" + AUTH_URL = None + """获取token""" + TOKEN_URL = None + """获取用户信息""" + API_URL = None + + def __init__(self, client_id, client_secret, callback_url, access_token=None, openid=None): + self.client_id = client_id + self.client_secret = client_secret + self.callback_url = callback_url + self.access_token = access_token + self.openid = openid + + @property + def is_access_token_set(self): + return self.access_token != None + + @property + def is_authorized(self): + return self.is_access_token_set and self.access_token != None and self.openid != None + + @abstractmethod + def get_authorization_url(self): + pass + + @abstractmethod + def get_access_token_by_code(self, code): + pass + + @abstractmethod + def get_oauth_userinfo(self): + pass + + def do_get(self, url, params): + rsp = requests.get(url=url, params=params) + return rsp.text + + def do_post(self, url, params): + rsp = requests.post(url, params) + return rsp.text + + +class WBOauthManager(BaseManager): + AUTH_URL = 'https://api.weibo.com/oauth2/authorize' + TOKEN_URL = 'https://api.weibo.com/oauth2/access_token' + API_URL = 'https://api.weibo.com/2/users/show.json' + + def __init__(self, client_id, client_secret, callback_url, access_token=None, openid=None): + super(WBOauthManager, self).__init__(client_id=client_id, client_secret=client_secret, + callback_url=callback_url, access_token=access_token, openid=openid) + + def get_authorization_url(self): + params = { + 'client_id': self.client_id, + 'response_type': 'code', + 'redirect_uri': self.callback_url + } + url = self.AUTH_URL + "?" + urllib.parse.urlencode(params) + return url + + def get_access_token_by_code(self, code): + print(code) + params = { + 'client_id': self.client_id, + 'client_secret': self.client_secret, + 'grant_type': 'authorization_code', + 'code': code, + 'redirect_uri': self.callback_url + } + rsp = self.do_post(self.TOKEN_URL, params) + print(rsp) + # return rsp + + obj = json.loads(rsp) + self.access_token = str(obj['access_token']) + self.openid = str(obj['uid']) + return self.get_oauth_userinfo() + + try: + self.access_token = str(obj['access_token']) + self.openid = str(obj['uid']) + return self.get_oauth_userinfo() + except: + return None + + def get_oauth_userinfo(self): + if not self.is_authorized: + return None + params = { + 'uid': self.openid, + 'access_token': self.access_token + } + rsp = self.do_get(self.API_URL, params) + print(rsp) diff --git a/oauth/tests.py b/oauth/tests.py new file mode 100644 index 0000000..d65f3fd --- /dev/null +++ b/oauth/tests.py @@ -0,0 +1,13 @@ +from django.test import TestCase + +# Create your tests here. + +class OAuthTet(TestCase): + def setUp(self): + pass + + +from oauth.oauthmanager import WBOauthManager +from django.conf import settings +settings.OAHUTH['sina'] +manager=WBOauthManager(client_id=settings.OAHUTH['sina']['appkey'],client_secret=settings.OAHUTH['sina']['appsecret'],callback_url=settings.OAHUTH['sina']['callbackurl']) \ No newline at end of file diff --git a/oauth/urls.py b/oauth/urls.py new file mode 100644 index 0000000..a5d4906 --- /dev/null +++ b/oauth/urls.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python +# encoding: utf-8 + + +""" +@version: ?? +@author: liangliangyy +@license: MIT Licence +@contact: liangliangyy@gmail.com +@site: https://www.lylinux.org/ +@software: PyCharm +@file: urls.py +@time: 2016/11/26 下午5:25 +""" + +from django.conf.urls import url +from django.views.decorators.cache import cache_page +from . import views + +urlpatterns = [ + url(r'^oauth/wbauthorize/(?P\w+)$', views.wbauthorize), + + url(r'^oauth/wboauthurl$', views.wboauthurl), +] diff --git a/oauth/views.py b/oauth/views.py new file mode 100644 index 0000000..560b2b6 --- /dev/null +++ b/oauth/views.py @@ -0,0 +1,24 @@ +from django.shortcuts import render + +# Create your views here. +from .oauthmanager import WBOauthManager +from django.conf import settings +from django.http import HttpResponse + + +def wbauthorize(request, sitename): + manager = WBOauthManager(client_id=settings.OAHUTH['sina']['appkey'], + client_secret=settings.OAHUTH['sina']['appsecret'], + callback_url=settings.OAHUTH['sina']['callbackurl']) + code = request.GET.get('code', None) + rsp = manager.get_access_token_by_code(code) + print(rsp) + return HttpResponse(rsp) + + +def wboauthurl(request): + manager = WBOauthManager(client_id=settings.OAHUTH['sina']['appkey'], + client_secret=settings.OAHUTH['sina']['appsecret'], + callback_url=settings.OAHUTH['sina']['callbackurl']) + url = manager.get_authorization_url() + return HttpResponse(url) diff --git a/templates/base.html b/templates/base.html index 28c157a..a3246bd 100644 --- a/templates/base.html +++ b/templates/base.html @@ -18,7 +18,7 @@ - + @@ -52,64 +52,8 @@

大巧无工,重剑无锋

- + {% include 'nav.html' %}
{% block content %} diff --git a/templates/nav.html b/templates/nav.html new file mode 100644 index 0000000..009b075 --- /dev/null +++ b/templates/nav.html @@ -0,0 +1,77 @@ +{% comment %}{% load blog_tags %} +{% query nav_category_list parent_category=None as root_categorys %} +{% for root in root_categorys %} + + {% query nav_category_list parent_category=root as chind_categorys %} + {% if chind_categorys %} + + {% endif %} +{% endfor %}{% endcomment %} + + + \ No newline at end of file diff --git a/templates/nav_node.html b/templates/nav_node.html new file mode 100644 index 0000000..f91338f --- /dev/null +++ b/templates/nav_node.html @@ -0,0 +1,40 @@ + + +