From 0db6a2805e904006b6f5ea99d23cee03284037f8 Mon Sep 17 00:00:00 2001 From: liangliangyy Date: Tue, 28 Feb 2017 17:43:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E6=B6=88=E5=85=A8=E7=AB=99=E7=BC=93?= =?UTF-8?q?=E5=AD=98.=E5=AE=8C=E5=96=84=E7=BC=93=E5=AD=98=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DjangoBlog/settings.py | 4 +- DjangoBlog/utils.py | 5 + blog/models.py | 16 +- blog/static/blog/css/style.css | 307 +++++++++++++++++++++++++++++- blog/static/blog/img/icon-sn.svg | 1 + blog/urls.py | 7 - blog/views.py | 113 ++++++++++- comments/views.py | 6 +- oauth/oauthmanager.py | 3 + templates/blog/article_index.html | 6 +- 10 files changed, 442 insertions(+), 26 deletions(-) create mode 100644 blog/static/blog/img/icon-sn.svg diff --git a/DjangoBlog/settings.py b/DjangoBlog/settings.py index 437ae92..569679d 100644 --- a/DjangoBlog/settings.py +++ b/DjangoBlog/settings.py @@ -51,9 +51,9 @@ MIDDLEWARE_CLASSES = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.gzip.GZipMiddleware', - 'django.middleware.cache.UpdateCacheMiddleware', + #'django.middleware.cache.UpdateCacheMiddleware', 'django.middleware.common.CommonMiddleware', - 'django.middleware.cache.FetchFromCacheMiddleware', + #'django.middleware.cache.FetchFromCacheMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', diff --git a/DjangoBlog/utils.py b/DjangoBlog/utils.py index 6d6e3f2..cdfcfda 100644 --- a/DjangoBlog/utils.py +++ b/DjangoBlog/utils.py @@ -33,6 +33,11 @@ def get_max_articleid_commentid(): return (Article.objects.latest().pk, Comment.objects.latest().pk) +def get_md5(str): + m = md5(str.encode('utf-8')) + return m.hexdigest() + + def cache_decorator(expiration=3 * 60): def wrapper(func): def news(*args, **kwargs): diff --git a/blog/models.py b/blog/models.py index 1abb117..8331a9a 100644 --- a/blog/models.py +++ b/blog/models.py @@ -4,7 +4,7 @@ from django.conf import settings from uuslug import slugify from DjangoBlog.spider_notify import spider_notify from django.contrib.sites.models import Site -from DjangoBlog.utils import cache_decorator, logger +from DjangoBlog.utils import cache_decorator, logger, cache from django.utils.functional import cached_property @@ -114,11 +114,17 @@ class Article(BaseModel): self.views += 1 self.save(update_fields=['views']) - """ def comment_list(self): - comments = self.comment_set.all() - parent_comments = comments.filter(parent_comment=None) - """ + cache_key = 'article_comments_{id}'.format(id=self.id) + value = cache.get(cache_key) + if value: + logger.info('get article comments:{id}'.format(id=self.id)) + return value + else: + comments = self.comment_set.all() + cache.set(cache_key, comments) + logger.info('set article comments:{id}'.format(id=self.id)) + return comments def get_admin_url(self): info = (self._meta.app_label, self._meta.model_name) diff --git a/blog/static/blog/css/style.css b/blog/static/blog/css/style.css index 2328292..fe85ae8 100755 --- a/blog/static/blog/css/style.css +++ b/blog/static/blog/css/style.css @@ -923,7 +923,7 @@ article.sticky .featured-post { font-size: 12px; font-size: 0.857142857rem; line-height: 2; - background-color: rgba(0,0,0,0.04); + background-color: rgba(0, 0, 0, 0.04); } .entry-content pre, @@ -2358,3 +2358,308 @@ li #reply-title { font: inherit; vertical-align: baseline; } + +.icon-sn-google { + background-position: 0 -28px; +} + +.icon-sn-bg-google { + background-color: #4285f4; + background-position: 0 0; +} + +.fa-sn-google { + color: #4285f4; +} + +.icon-sn-github { + background-position: -28px -28px; +} + +.icon-sn-bg-github { + background-color: #333; + background-position: -28px 0; +} + +.fa-sn-github { + color: #333; +} + +.icon-sn-weibo { + background-position: -56px -28px; +} + +.icon-sn-bg-weibo { + background-color: #e90d24; + background-position: -56px 0; +} + +.fa-sn-weibo { + color: #e90d24; +} + +.icon-sn-qq { + background-position: -84px -28px; +} + +.icon-sn-bg-qq { + background-color: #0098e6; + background-position: -84px 0; +} + +.fa-sn-qq { + color: #0098e6; +} + +.icon-sn-twitter { + background-position: -112px -28px; +} + +.icon-sn-bg-twitter { + background-color: #50abf1; + background-position: -112px 0; +} + +.fa-sn-twitter { + color: #50abf1; +} + +.icon-sn-facebook { + background-position: -140px -28px; +} + +.icon-sn-bg-facebook { + background-color: #4862a3; + background-position: -140px 0; +} + +.fa-sn-facebook { + color: #4862a3; +} + +.icon-sn-renren { + background-position: -168px -28px; +} + +.icon-sn-bg-renren { + background-color: #197bc8; + background-position: -168px 0; +} + +.fa-sn-renren { + color: #197bc8; +} + +.icon-sn-tqq { + background-position: -196px -28px; +} + +.icon-sn-bg-tqq { + background-color: #1f9ed2; + background-position: -196px 0; +} + +.fa-sn-tqq { + color: #1f9ed2; +} + +.icon-sn-douban { + background-position: -224px -28px; +} + +.icon-sn-bg-douban { + background-color: #279738; + background-position: -224px 0; +} + +.fa-sn-douban { + color: #279738; +} + +.icon-sn-weixin { + background-position: -252px -28px; +} + +.icon-sn-bg-weixin { + background-color: #00b500; + background-position: -252px 0; +} + +.fa-sn-weixin { + color: #00b500; +} + +.icon-sn-dotted { + background-position: -280px -28px; +} + +.icon-sn-bg-dotted { + background-color: #eee; + background-position: -280px 0; +} + +.fa-sn-dotted { + color: #eee; +} + +.icon-sn-site { + background-position: -308px -28px; +} + +.icon-sn-bg-site { + background-color: #00b500; + background-position: -308px 0; +} + +.fa-sn-site { + color: #00b500; +} + +.icon-sn-linkedin { + background-position: -336px -28px; +} + +.icon-sn-bg-linkedin { + background-color: #0077b9; + background-position: -336px 0; +} + +.fa-sn-linkedin { + color: #0077b9; +} + +[class*=icon-sn-] { + display: inline-block; + background-image: url('../img/icon-sn.svg'); + background-repeat: no-repeat; + width: 28px; + height: 28px; + vertical-align: middle; + background-size: auto 56px; +} + +[class*=icon-sn-]:hover { + opacity: .8; + filter: alpha(opacity=80); +} + +.btn-sn-google { + background: #4285f4; +} + +.btn-sn-google:active, .btn-sn-google:focus, .btn-sn-google:hover { + background: #2a75f3; +} + +.btn-sn-github { + background: #333; +} + +.btn-sn-github:active, .btn-sn-github:focus, .btn-sn-github:hover { + background: #262626; +} + +.btn-sn-weibo { + background: #e90d24; +} + +.btn-sn-weibo:active, .btn-sn-weibo:focus, .btn-sn-weibo:hover { + background: #d10c20; +} + +.btn-sn-qq { + background: #0098e6; +} + +.btn-sn-qq:active, .btn-sn-qq:focus, .btn-sn-qq:hover { + background: #0087cd; +} + +.btn-sn-twitter { + background: #50abf1; +} + +.btn-sn-twitter:active, .btn-sn-twitter:focus, .btn-sn-twitter:hover { + background: #38a0ef; +} + +.btn-sn-facebook { + background: #4862a3; +} + +.btn-sn-facebook:active, .btn-sn-facebook:focus, .btn-sn-facebook:hover { + background: #405791; +} + +.btn-sn-renren { + background: #197bc8; +} + +.btn-sn-renren:active, .btn-sn-renren:focus, .btn-sn-renren:hover { + background: #166db1; +} + +.btn-sn-tqq { + background: #1f9ed2; +} + +.btn-sn-tqq:active, .btn-sn-tqq:focus, .btn-sn-tqq:hover { + background: #1c8dbc; +} + +.btn-sn-douban { + background: #279738; +} + +.btn-sn-douban:active, .btn-sn-douban:focus, .btn-sn-douban:hover { + background: #228330; +} + +.btn-sn-weixin { + background: #00b500; +} + +.btn-sn-weixin:active, .btn-sn-weixin:focus, .btn-sn-weixin:hover { + background: #009c00; +} + +.btn-sn-dotted { + background: #eee; +} + +.btn-sn-dotted:active, .btn-sn-dotted:focus, .btn-sn-dotted:hover { + background: #e1e1e1; +} + +.btn-sn-site { + background: #00b500; +} + +.btn-sn-site:active, .btn-sn-site:focus, .btn-sn-site:hover { + background: #009c00; +} + +.btn-sn-linkedin { + background: #0077b9; +} + +.btn-sn-linkedin:active, .btn-sn-linkedin:focus, .btn-sn-linkedin:hover { + background: #0067a0; +} + +[class*=btn-sn-], [class*=btn-sn-]:active, [class*=btn-sn-]:focus, [class*=btn-sn-]:hover { + border: none; + color: #fff; +} + +.btn-sn-more { + padding: 0; +} + +.btn-sn-more, .btn-sn-more:active, .btn-sn-more:hover { + box-shadow: none; +} + +[class*=btn-sn-] [class*=icon-sn-] { + background-color: transparent; +} \ No newline at end of file diff --git a/blog/static/blog/img/icon-sn.svg b/blog/static/blog/img/icon-sn.svg new file mode 100644 index 0000000..a1df619 --- /dev/null +++ b/blog/static/blog/img/icon-sn.svg @@ -0,0 +1 @@ +icon-sn \ No newline at end of file diff --git a/blog/urls.py b/blog/urls.py index cd757be..87471fb 100644 --- a/blog/urls.py +++ b/blog/urls.py @@ -20,13 +20,6 @@ from haystack.forms import ModelSearchForm from haystack.query import SearchQuerySet from haystack.views import SearchView - -def key_prefixer(request): - # if it's not there, don't cache - # return request.GET.get('number') - return "123" - - urlpatterns = [ url(r'^$', views.IndexView.as_view(), name='index'), url(r'^page/(?P\d+)$', views.IndexView.as_view(), name='index_page'), diff --git a/blog/views.py b/blog/views.py index cc6bd9d..b53a9c2 100644 --- a/blog/views.py +++ b/blog/views.py @@ -23,7 +23,7 @@ import datetime from django.views.decorators.csrf import csrf_exempt import os from django.contrib.auth.decorators import login_required -from DjangoBlog.utils import cache, cache_decorator +from DjangoBlog.utils import cache, cache_decorator, logger, get_md5 from django.utils.cache import get_cache_key from django.utils.decorators import classonlymethod from django.utils.decorators import method_decorator @@ -72,12 +72,69 @@ class ArticleListView(ListView): def get_view_cache_key(self): return self.request.get['pages'] + @property + def page_number(self): + page_kwarg = self.page_kwarg + page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1 + return page + + def get_queryset_cache_key(self): + """ + 子类重写.获得queryset的缓存key + """ + raise NotImplementedError() + + def get_queryset_data(self): + """ + 子类重写.获取queryset的数据 + """ + raise NotImplementedError() + + def get_queryset_from_cache(self, cache_key): + # raise NotImplementedError() + value = cache.get(cache_key) + if value: + logger.info('get view cache.key:{key}'.format(key=cache_key)) + return value + else: + article_list = self.get_queryset_data() + cache.set(cache_key, article_list) + logger.info('set view cache.key:{key}'.format(key=cache_key)) + return article_list -class IndexView(ArticleListView): def get_queryset(self): + key = self.get_queryset_cache_key() + value = self.get_queryset_from_cache(key) + return value + + +class IndexView(ArticleListView): + def get_queryset_data(self): article_list = Article.objects.filter(type='a', status='p') return article_list + def get_queryset_cache_key(self): + cache_key = 'index_{page}'.format(page=self.page_number) + return cache_key + + ''' + def get_queryset(self): + # return self.get_queryset_data() + cache_key = 'index_{page}'.format(page=self.page_number) + return self.get_queryset_from_cache(cache_key=cache_key) + """ + value = cache.get(cache_key) + if value: + logger.info('get view cache.key:{key}'.format(key=cache_key)) + return value + else: + article_list = Article.objects.filter(type='a', status='p') + cache.set(cache_key, article_list) + logger.info('set view cache.key:{key}'.format(key=cache_key)) + return article_list + """ + ''' + class ArticleDetailView(DetailView): template_name = 'blog/article_detail.html' @@ -107,7 +164,7 @@ class ArticleDetailView(DetailView): comment_form.fields["email"].initial = user.email comment_form.fields["name"].initial = user.username - article_comments = self.object.comment_set.all() + article_comments = self.object.comment_list() kwargs['form'] = comment_form kwargs['article_comments'] = article_comments @@ -153,6 +210,23 @@ class PageDetailView(ArticleDetailView): class CategoryDetailView(ArticleListView): page_type = "分类目录归档" + def get_queryset_data(self): + slug = self.kwargs['category_name'] + category = get_object_or_404(Category, slug=slug) + categoryname = category.name + self.categoryname = categoryname + article_list = Article.objects.filter(category__name=categoryname, status='p') + return article_list + + def get_queryset_cache_key(self): + slug = self.kwargs['category_name'] + category = get_object_or_404(Category, slug=slug) + categoryname = category.name + self.categoryname = categoryname + cache_key = 'category_list_{categoryname}_{page}'.format(categoryname=categoryname, page=self.page_number) + return cache_key + + ''' def get_queryset(self): slug = self.kwargs['category_name'] # category = Category.objects.get(slug=slug) @@ -163,8 +237,19 @@ class CategoryDetailView(ArticleListView): categoryname = categoryname.split('/')[-1] except: pass - article_list = Article.objects.filter(category__name=categoryname, status='p') - return article_list + + cache_key = 'category_list_{categoryname}_{page}'.format(categoryname=categoryname, page=self.page_number) + + value = cache.get(cache_key) + if value: + logger.info('get view cache.key:{key}'.format(key=cache_key)) + return value + else: + article_list = Article.objects.filter(category__name=categoryname, status='p') + cache.set(cache_key, article_list) + logger.info('set view cache.key:{key}'.format(key=cache_key)) + return article_list + ''' def get_context_data(self, **kwargs): # slug = self.kwargs['category_name'] @@ -183,7 +268,12 @@ class CategoryDetailView(ArticleListView): class AuthorDetailView(ArticleListView): page_type = '作者文章归档' - def get_queryset(self): + def get_queryset_cache_key(self): + author_name = self.kwargs['author_name'] + cache_key = 'author_{author_name}_{page}'.format(author_name=author_name, page=self.page_number) + return cache_key + + def get_queryset_data(self): author_name = self.kwargs['author_name'] article_list = Article.objects.filter(author__username=author_name) return article_list @@ -209,15 +299,22 @@ class TagListView(ListView): class TagDetailView(ArticleListView): page_type = '分类标签归档' - def get_queryset(self): + def get_queryset_data(self): slug = self.kwargs['tag_name'] - # tag = Tag.objects.get(slug=slug) tag = get_object_or_404(Tag, slug=slug) tag_name = tag.name self.name = tag_name article_list = Article.objects.filter(tags__name=tag_name) return article_list + def get_queryset_cache_key(self): + slug = self.kwargs['tag_name'] + tag = get_object_or_404(Tag, slug=slug) + tag_name = tag.name + self.name = tag_name + cache_key = 'tag_{tag_name}_{page}'.format(tag_name=tag_name, page=self.page_number) + return cache_key + def get_context_data(self, **kwargs): # tag_name = self.kwargs['tag_name'] tag_name = self.name diff --git a/comments/views.py b/comments/views.py index ce33037..4c5ce9c 100644 --- a/comments/views.py +++ b/comments/views.py @@ -76,8 +76,12 @@ class CommentPostView(FormView): from django.contrib.sites.models import Site path = article.get_absolute_url() site = Site.objects.get_current().domain - + if site.find(':') > 0: + site = site[0:site.find(':')] expire_view_cache(path, servername=site, serverport=self.request.get_port(), key_prefix='blogdetail') if cache.get('seo_processor'): cache.delete('seo_processor') + comment_cache_key = 'article_comments_{id}'.format(id=article_id) + cache.delete(comment_cache_key) + return HttpResponseRedirect("%s#div-comment-%d" % (article.get_absolute_url(), comment.pk)) diff --git a/oauth/oauthmanager.py b/oauth/oauthmanager.py index 5d76151..fce22d4 100644 --- a/oauth/oauthmanager.py +++ b/oauth/oauthmanager.py @@ -26,6 +26,8 @@ class BaseManager(metaclass=ABCMeta): TOKEN_URL = None """获取用户信息""" API_URL = None + '''icon图标名''' + ICON_NAME = None def __init__(self, client_id, client_secret, callback_url, access_token=None, openid=None): self.client_id = client_id @@ -121,6 +123,7 @@ class GoogleOauthManager(BaseManager): AUTH_URL = 'https://accounts.google.com/o/oauth2/v2/auth' TOKEN_URL = 'https://www.googleapis.com/oauth2/v4/token' API_URL = 'https://www.googleapis.com/oauth2/v3/userinfo' + ICON_NAME = 'google' def __init__(self, client_id, client_secret, callback_url, access_token=None, openid=None): super(GoogleOauthManager, self).__init__(client_id=client_id, client_secret=client_secret, diff --git a/templates/blog/article_index.html b/templates/blog/article_index.html index 544dcd3..2abebb9 100644 --- a/templates/blog/article_index.html +++ b/templates/blog/article_index.html @@ -1,6 +1,6 @@ {% extends 'share_layout/base.html' %} {% load blog_tags %} - +{% load cache %} {% block header %} {% if tag_name %} {{ page_type }}:{{ tag_name }} | {{ SITE_DESCRIPTION }} @@ -55,7 +55,9 @@ {% block sidebar %} - {% load_sidebar request.user %} + {% cache 36000 sidebar request.user.username %} + {% load_sidebar request.user %} + {% endcache %} {% endblock %}