Skip to content

Commit 3cab6e4

Browse files
author
liangliangyy
committed
Merge branch 'dev'
2 parents 6093274 + 36c45f7 commit 3cab6e4

File tree

8 files changed

+111
-62
lines changed

8 files changed

+111
-62
lines changed

DjangoBlog/feeds.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from blog.models import Article
1818
from django.conf import settings
1919
from django.utils.feedgenerator import Rss201rev2Feed
20-
from DjangoBlog.common_markdown import common_markdown
20+
from DjangoBlog.utils import common_markdown
2121
from django.contrib.auth import get_user_model
2222
from django.contrib.auth.models import User
2323
from django.contrib.sites.models import Site

DjangoBlog/common_markdown.py renamed to DjangoBlog/utils.py

+26-4
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,42 @@
55
"""
66
@version: ??
77
@author: liangliangyy
8-
@license: MIT Licence
8+
@license: MIT Licence
99
@contact: liangliangyy@gmail.com
1010
@site: https://www.lylinux.org/
1111
@software: PyCharm
12-
@file: common_markdown.py
13-
@time: 2017/1/14 上午2:30
12+
@file: utils.py
13+
@time: 2017/1/19 上午2:30
1414
"""
15-
15+
from django.core.cache import cache
16+
from hashlib import md5
1617
import mistune
1718
from pygments import highlight
1819
from pygments.lexers import get_lexer_by_name
1920
from pygments.formatters import html
2021

2122

23+
def cache_decorator(expiration=3 * 60):
24+
def wrapper(func):
25+
def news(*args, **kwargs):
26+
unique_str = repr((func, args, kwargs))
27+
m = md5(unique_str.encode('utf-8'))
28+
key = m.hexdigest()
29+
value = cache.get(key)
30+
if value:
31+
print('get key: ' + key)
32+
return value
33+
else:
34+
print('set key:' + key)
35+
value = func(*args, **kwargs)
36+
cache.set(key, value, expiration)
37+
return value
38+
39+
return news
40+
41+
return wrapper
42+
43+
2244
def block_code(text, lang, inlinestyles=False, linenos=False):
2345
if not lang:
2446
text = text.strip()

blog/context_processors.py

+19-11
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,25 @@
1414
"""
1515
from .models import Category, Article, Tag
1616
from django.conf import settings
17+
from django.core.cache import cache
1718

1819

1920
def seo_processor(requests):
20-
return {
21-
'SITE_NAME': settings.SITE_NAME,
22-
'SHOW_GOOGLE_ADSENSE': settings.SHOW_GOOGLE_ADSENSE,
23-
'SITE_SEO_DESCRIPTION': settings.SITE_SEO_DESCRIPTION,
24-
'SITE_DESCRIPTION': settings.SITE_DESCRIPTION,
25-
'SITE_KEYWORDS': settings.SITE_SEO_KEYWORDS,
26-
'SITE_BASE_URL': 'http://' + requests.get_host() + '/',
27-
'ARTICLE_SUB_LENGTH': settings.ARTICLE_SUB_LENGTH,
28-
'nav_category_list': Category.objects.all(),
29-
'nav_pages': Article.objects.filter(type='p', status='p')
30-
}
21+
key = 'seo_processor'
22+
value = cache.get(key)
23+
if value:
24+
return value
25+
else:
26+
value = {
27+
'SITE_NAME': settings.SITE_NAME,
28+
'SHOW_GOOGLE_ADSENSE': settings.SHOW_GOOGLE_ADSENSE,
29+
'SITE_SEO_DESCRIPTION': settings.SITE_SEO_DESCRIPTION,
30+
'SITE_DESCRIPTION': settings.SITE_DESCRIPTION,
31+
'SITE_KEYWORDS': settings.SITE_SEO_KEYWORDS,
32+
'SITE_BASE_URL': 'https://' + requests.get_host() + '/',
33+
'ARTICLE_SUB_LENGTH': settings.ARTICLE_SUB_LENGTH,
34+
'nav_category_list': Category.objects.all(),
35+
'nav_pages': Article.objects.filter(type='p', status='p')
36+
}
37+
cache.set(key, value, 60 * 60 * 10)
38+
return value

blog/models.py

+40-23
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,32 @@
55
from uuslug import slugify
66
from DjangoBlog.spider_notify import sipder_notify
77
from django.contrib.sites.models import Site
8+
from DjangoBlog.utils import cache_decorator
9+
from django.utils.functional import cached_property
10+
11+
class BaseModel(models.Model):
12+
def save(self, *args, **kwargs):
13+
super().save(*args, **kwargs)
14+
15+
if 'update_fields' in kwargs and len(kwargs['update_fields']) == 1 and kwargs['update_fields'][0] == 'views':
16+
return
17+
try:
18+
notify = sipder_notify()
19+
notify_url = self.get_full_url()
20+
notify.baidu_notify(notify_url)
21+
except Exception as ex:
22+
print(ex)
23+
24+
def get_full_url(self):
25+
site = Site.objects.get_current().domain
26+
url = "https://{site}{path}".format(site=site, path=self.get_absolute_url())
27+
return url
28+
29+
class Meta:
30+
abstract = True
831

932

10-
class Article(models.Model):
33+
class Article(BaseModel):
1134
"""文章"""
1235
STATUS_CHOICES = (
1336
('d', '草稿'),
@@ -57,6 +80,7 @@ def get_absolute_url(self):
5780
'slug': self.slug
5881
})
5982

83+
@cache_decorator(60 * 60 * 10)
6084
def get_category_tree(self):
6185
names = []
6286

@@ -73,17 +97,20 @@ def save(self, *args, **kwargs):
7397
if not self.slug or self.slug == 'no-slug' or not self.id:
7498
# Only set the slug when the object is created.
7599
self.slug = slugify(self.title)
100+
"""
76101
try:
77102
notify = sipder_notify()
78103
notify.notify(self.get_full_url())
79104
except Exception as e:
80105
print(e)
106+
"""
81107
super().save(*args, **kwargs)
82108

83109
def viewed(self):
84110
self.views += 1
85111
self.save(update_fields=['views'])
86112

113+
@cache_decorator(60 * 60 * 10)
87114
def comment_list(self):
88115
comments = self.comment_set.all()
89116
parent_comments = comments.filter(parent_comment=None)
@@ -92,10 +119,15 @@ def get_admin_url(self):
92119
info = (self._meta.app_label, self._meta.model_name)
93120
return reverse('admin:%s_%s_change' % info, args=(self.pk,))
94121

95-
def get_full_url(self):
96-
site = Site.objects.get_current().domain
97-
article_url = "https://{site}{path}".format(site=site, path=self.get_absolute_url())
98-
return article_url
122+
@cached_property
123+
def next_article(self):
124+
# 下一篇
125+
return Article.objects.filter(id__gt=self.id, status=0).order_by('id').first()
126+
127+
@cached_property
128+
def prev_article(self):
129+
# 前一篇
130+
return Article.objects.filter(id__lt=self.id, status=0).first()
99131

100132

101133
'''
@@ -162,7 +194,7 @@ def get_category_tree(self):
162194
'''
163195

164196

165-
class Category(models.Model):
197+
class Category(BaseModel):
166198
"""文章分类"""
167199
name = models.CharField('分类名', max_length=30)
168200
created_time = models.DateTimeField('创建时间', auto_now_add=True)
@@ -180,16 +212,8 @@ def get_absolute_url(self):
180212
def __str__(self):
181213
return self.name
182214

183-
def save(self, *args, **kwargs):
184-
try:
185-
notify = sipder_notify()
186-
notify.notify(self.get_absolute_url())
187-
except Exception as e:
188-
print(e)
189-
super().save(*args, **kwargs)
190-
191215

192-
class Tag(models.Model):
216+
class Tag(BaseModel):
193217
"""文章标签"""
194218
name = models.CharField('标签名', max_length=30)
195219
created_time = models.DateTimeField('创建时间', auto_now_add=True)
@@ -201,6 +225,7 @@ def __str__(self):
201225
def get_absolute_url(self):
202226
return reverse('blog:tag_detail', kwargs={'tag_name': self.name})
203227

228+
@cache_decorator(60 * 60 * 10)
204229
def get_article_count(self):
205230
return Article.objects.filter(tags__name=self.name).distinct().count()
206231

@@ -209,14 +234,6 @@ class Meta:
209234
verbose_name = "标签"
210235
verbose_name_plural = verbose_name
211236

212-
def save(self, *args, **kwargs):
213-
try:
214-
notify = sipder_notify()
215-
notify.notify(self.get_absolute_url())
216-
except Exception as e:
217-
print(e)
218-
super().save(*args, **kwargs)
219-
220237

221238
class Links(models.Model):
222239
"""友情链接"""

blog/templatetags/blog_tags.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def datetimeformat(data):
4949
@register.filter(is_safe=True)
5050
@stringfilter
5151
def custom_markdown(content):
52-
from DjangoBlog.common_markdown import common_markdown
52+
from DjangoBlog.utils import common_markdown
5353
return mark_safe(common_markdown.get_markdown(content))
5454

5555

blog/views.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,12 @@ def get_object(self):
7474
obj = super(ArticleDetailView, self).get_object()
7575
obj.viewed()
7676
# obj.body = markdown2.markdown(obj.body)
77+
self.object = obj
7778
return obj
7879

7980
def get_context_data(self, **kwargs):
8081
articleid = int(self.kwargs[self.pk_url_kwarg])
8182

82-
def get_article(id):
83-
try:
84-
return Article.objects.get(pk=id)
85-
except ObjectDoesNotExist:
86-
return None
87-
8883
comment_form = CommentForm()
8984
u = self.request.user
9085

@@ -102,10 +97,9 @@ def get_article(id):
10297
kwargs['form'] = comment_form
10398
kwargs['article_comments'] = article_comments
10499
kwargs['comment_count'] = len(article_comments) if article_comments else 0;
105-
next_article = get_article(articleid + 1)
106-
prev_article = get_article(articleid - 1)
107-
kwargs['next_article'] = next_article
108-
kwargs['prev_article'] = prev_article
100+
101+
kwargs['next_article'] = self.object.next_article
102+
kwargs['prev_article'] = self.object.prev_article
109103

110104
return super(ArticleDetailView, self).get_context_data(**kwargs)
111105

templates/blog/tags/article_meta_info.html

+12-9
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@
2323
标签。
2424
{% endif %}
2525
{% endif %}
26-
<span class="by-author">作者是<span class="author vcard"><a class="url fn n"
27-
href="{{ article.author.get_absolute_url }}"
28-
title="查看所有由{{ article.author.username }}发布的文章"
29-
rel="author">
30-
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
26+
<span class="by-author">作者是
27+
<span class="author vcard">
28+
<a class="url fn n" href="{{ article.author.get_absolute_url }}"
29+
title="查看所有由{{ article.author.username }}发布的文章"
30+
rel="author">
31+
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
3132

32-
<span itemprop="name">
33+
<span itemprop="name" itemprop="publisher">
3334

34-
{{ article.author.username }}
35-
</span></span>
36-
</a></span>
35+
{{ article.author.username }}
36+
</span>
37+
</span>
38+
</a>
39+
</span>
3740
{% if user.is_authenticated %}
3841
<a href="{{ article.get_admin_url }}">编辑</a>
3942
{% endif %}

templates/share_layout/base.html

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% load static %}
2+
{% load cache %}
23
<!DOCTYPE html>
34
<!--[if IE 7]>
45
<html class="ie ie7" lang="zh-CN" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# article: http://ogp.me/ns/article#">
@@ -53,16 +54,20 @@ <h1 class="site-title"><a href="{{ SITE_BASE_URL }}" title="{{ SITE_NAME }}" rel
5354
<h2 class="site-description">{{ SITE_DESCRIPTION }}</h2>
5455
</hgroup>
5556

57+
{% cache 36000 sidebar %}
58+
{% include 'share_layout/nav.html' %}
59+
{% endcache %}
5660

57-
{% include 'share_layout/nav.html' %}
5861
</header><!-- #masthead -->
5962
<div id="main" class="wrapper">
6063
{% block content %}
6164
{% endblock %}
6265

63-
{% block sidebar %}
64-
{% endblock %}
66+
{% cache 36000 nav %}
67+
{% block sidebar %}
6568

69+
{% endblock %}
70+
{% endcache %}
6671
</div><!-- #main .wrapper -->
6772
{% include 'share_layout/footer.html' %}
6873
</div><!-- #page -->

0 commit comments

Comments
 (0)