Skip to content

Commit

Permalink
Merge pull request #181 from liangliangyy/dev
Browse files Browse the repository at this point in the history
友链功能优化 close #176
  • Loading branch information
liangliangyy committed Nov 3, 2018
2 parents e0b2e21 + 4db213a commit 15dcbd4
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 35 deletions.
12 changes: 7 additions & 5 deletions DjangoBlog/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ def news(*args, **kwargs):
key = m.hexdigest()
value = cache.get(key)
if value:
logger.info('cache_decorator get cache:%s key:%s' % (func.__name__, key))
# logger.info('cache_decorator get cache:%s key:%s' % (func.__name__, key))
return value
else:
logger.info('cache_decorator set cache:%s key:%s' % (func.__name__, key))
# logger.info('cache_decorator set cache:%s key:%s' % (func.__name__, key))
value = func(*args, **kwargs)
cache.set(key, value, expiration)
return value
Expand Down Expand Up @@ -241,6 +241,8 @@ def save_user_avatar(url):

def delete_view_cache(username):
from django.core.cache.utils import make_template_fragment_key
key = make_template_fragment_key('sidebar', [username])
logger.info('delete sidebar key:' + key)
cache.delete(key)
from blog.models import LINK_SHOW_TYPE
keys = (make_template_fragment_key('sidebar', [username + x[0]]) for x in LINK_SHOW_TYPE)
for k in keys:
logger.info('delete sidebar key:' + k)
cache.delete(k)
10 changes: 10 additions & 0 deletions blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@

logger = logging.getLogger(__name__)

LINK_SHOW_TYPE = (
('i', '首页'),
('l', '列表页'),
('p', '文章页面'),
('a', '全站'),
)


class BaseModel(models.Model):
id = models.AutoField(primary_key=True)
Expand Down Expand Up @@ -207,9 +214,12 @@ class Meta:

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

name = models.CharField('链接名称', max_length=30, unique=True)
link = models.URLField('链接地址')
sequence = models.IntegerField('排序', unique=True)
is_enable = models.BooleanField('是否显示', default=True, blank=False, null=False)
show_type = models.CharField('显示类型', max_length=1, choices=LINK_SHOW_TYPE, default='i')
created_time = models.DateTimeField('创建时间', default=now)
last_mod_time = models.DateTimeField('修改时间', default=now)

Expand Down
9 changes: 5 additions & 4 deletions blog/templatetags/blog_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""

from django import template
from django.db.models import Q
from django.conf import settings
from django.template.defaultfilters import stringfilter
from django.utils.safestring import mark_safe
Expand Down Expand Up @@ -126,7 +127,7 @@ def load_articletags(article):


@register.inclusion_tag('blog/tags/sidebar.html')
def load_sidebar(user):
def load_sidebar(user, linktype):
"""
加载侧边栏
:return:
Expand All @@ -139,7 +140,7 @@ def load_sidebar(user):
extra_sidebars = SideBar.objects.filter(is_enable=True).order_by('sequence')
most_read_articles = Article.objects.filter(status='p').order_by('-views')[:blogsetting.sidebar_article_count]
dates = Article.objects.datetimes('created_time', 'month', order='DESC')
links = Links.objects.all()
links = Links.objects.filter(is_enable=True).filter(Q(show_type=str(linktype)) | Q(show_type='a'))
commment_list = Comment.objects.filter(is_enable=True).order_by('-id')[:blogsetting.sidebar_comment_count]
# show_adsense = settings.SHOW_GOOGLE_ADSENSE
# 标签云 计算字体大小
Expand All @@ -150,17 +151,17 @@ def load_sidebar(user):
if tags and len(tags) > 0:
s = list(map(lambda t: (t, t.get_article_count()), tags))
count = sum(map(lambda t: t[1], s))
dd = 1 if count == 0 else count / len(tags)
dd = 1 if count == 0 and not len(tags) else count / len(tags)
sidebar_tags = list(map(lambda x: (x[0], x[1], (x[1] / dd) * increment + 10), s))

return {
'recent_articles': recent_articles,
'sidebar_categorys': sidebar_categorys,
'most_read_articles': most_read_articles,
'article_dates': dates,
'sidabar_links': links,
'sidebar_comments': commment_list,
'user': user,
'sidabar_links': links,
'show_google_adsense': blogsetting.show_google_adsense,
'google_adsense_codes': blogsetting.google_adsense_codes,
'open_site_comment': blogsetting.open_site_comment,
Expand Down
7 changes: 7 additions & 0 deletions blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ArticleListView(ListView):
page_type = ''
paginate_by = settings.PAGINATE_BY
page_kwarg = 'page'
link_type = 'l'

def get_view_cache_key(self):
return self.request.get['pages']
Expand Down Expand Up @@ -70,8 +71,14 @@ def get_queryset(self):
value = self.get_queryset_from_cache(key)
return value

def get_context_data(self, **kwargs):
kwargs['linktype'] = self.link_type
return super(ArticleListView, self).get_context_data(**kwargs)


class IndexView(ArticleListView):
link_type = 'i'

def get_queryset_data(self):
article_list = Article.objects.filter(type='a', status='p')
return article_list
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cffi==1.11.5
chardet==3.0.4
coverage==4.5.1
cryptography==2.3.1
Django==2.1.2
Django==2.1.3
django-appconf==1.0.2
django-autoslug==1.9.3
django-compressor==2.2
Expand All @@ -31,18 +31,18 @@ pycparser==2.19
Pygments==2.2.0
pylint==2.1.1
PyMySQL==0.9.2
pyparsing==2.2.2
pyparsing==2.3.0
python-memcached==1.59
python-slugify==1.2.6
pytz==2018.5
pytz==2018.7
raven==6.9.0
rcssmin==1.0.6
requests==2.20.0
rjsmin==1.0.12
six==1.11.0
sqlparse==0.2.4
Unidecode==1.0.22
urllib3==1.24
urllib3==1.24.1
webencodings==0.5.1
WeRoBot==1.6.0
Whoosh==2.7.4
Expand Down
2 changes: 1 addition & 1 deletion templates/blog/article_archives.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@


{% block sidebar %}
{% load_sidebar user %}
{% load_sidebar user 'i' %}
{% endblock %}


10 changes: 6 additions & 4 deletions templates/blog/article_detail.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% extends 'share_layout/base.html' %}
{% load blog_tags %}
{% load cache %}
{% block header %}
<title>{{ article.title }} | {{ SITE_DESCRIPTION }}</title>
<meta property="og:type" content="article"/>
Expand Down Expand Up @@ -70,9 +71,10 @@ <h3 class="comment-meta">您还没有登录,请您<a

{% endblock %}


{% block sidebar %}
{% load_sidebar user %}
{% with request.user.username|add:'p' as cachekey %}
{% cache 36000 sidebar cachekey %}
{% load_sidebar user 'p' %}
{% endcache %}
{% endwith %}
{% endblock %}


12 changes: 6 additions & 6 deletions templates/blog/article_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
</div><!-- #primary -->

{% endblock %}


{% block sidebar %}
{% load_sidebar user %}
{% endblock %}


{% with request.user.username|add:linktype as cachekey %}
{% cache 36000 sidebar cachekey %}
{% load_sidebar user linktype %}
{% endcache %}
{% endwith %}
{% endblock %}
2 changes: 1 addition & 1 deletion templates/blog/error_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h1 class="archive-title">{{ message }}</h1>


{% block sidebar %}
{% load_sidebar user %}
{% load_sidebar user 'i' %}
{% endblock %}


2 changes: 1 addition & 1 deletion templates/search/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ <h1 class="archive-title">哎呀,关键字:<span>{{ query }}</span>没有找


{% block sidebar %}
{% load_sidebar request.user %}
{% load_sidebar request.user 'i' %}
{% endblock %}


8 changes: 4 additions & 4 deletions templates/share_layout/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ <h2 class="site-description">{{ SITE_DESCRIPTION }}</h2>
{% block content %}
{% endblock %}

{% cache 36000 sidebar request.user.username %}
{% block sidebar %}
{% endblock %}
{% endcache %}

{% block sidebar %}
{% endblock %}


</div><!-- #main .wrapper -->
{% include 'share_layout/footer.html' %}
Expand Down
10 changes: 5 additions & 5 deletions travis_test/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cffi==1.11.5
chardet==3.0.4
coverage==4.5.1
cryptography==2.3.1
Django==2.1.2
Django==2.1.3
django-appconf==1.0.2
django-autoslug==1.9.3
django-compressor==2.2
Expand All @@ -31,19 +31,19 @@ pycparser==2.19
Pygments==2.2.0
pylint==2.1.1
PyMySQL==0.9.2
pyparsing==2.2.2
pyparsing==2.3.0
python-slugify==1.2.6
pytz==2018.5
pytz==2018.7
raven==6.9.0
rcssmin==1.0.6
requests==2.20.0
rjsmin==1.0.12
six==1.11.0
sqlparse==0.2.4
Unidecode==1.0.22
urllib3==1.24
urllib3==1.24.1
webencodings==0.5.1
WeRoBot==1.6.0
Whoosh==2.7.4
wrapt==1.10.11
xmltodict==0.11.0
xmltodict==0.11.0

0 comments on commit 15dcbd4

Please sign in to comment.