Skip to content

Commit

Permalink
优化上一篇、下一篇的策略
Browse files Browse the repository at this point in the history
  • Loading branch information
the5fire committed Jan 12, 2015
1 parent b23128d commit d709f6f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
13 changes: 12 additions & 1 deletion selfblog/blog/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#coding:utf-8
# coding:utf-8
from datetime import datetime

from django.db import models
from django.contrib.auth.models import User
from django.utils.functional import cached_property

from selfblog import settings
from utils.cache import cache_decorator
Expand Down Expand Up @@ -77,6 +78,16 @@ def tags_list(self):
def get_absolute_url(self):
return '%s/%s.html' % (settings.DOMAIN, self.alias)

@cached_property
def next_post(self):
# 下一篇
return Post.objects.filter(id__gt=self.id, status=0).order_by('id').first()

@cached_property
def prev_post(self):
# 前一篇
return Post.objects.filter(id__lt=self.id, status=0).first()

@classmethod
@cache_decorator(1*60)
def get_recently_posts(cls, num):
Expand Down
8 changes: 4 additions & 4 deletions selfblog/blog/templates/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ <h2 class="under_line">

<div class="box">
【上一篇】
{% if prev_post %}
<a href="{{ prev_post.get_absolute_url }}">{{ prev_post.title }}</a>
{% if post.prev_post %}
<a href="{{ post.prev_post.get_absolute_url }}">{{ post.prev_post.title }}</a>
{% else %}
没有了
{% endif %}
<br/>
【下一篇】
{% if next_post %}
<a href="{{ next_post.get_absolute_url }}">{{ next_post.title }}</a>
{% if post.next_post %}
<a href="{{ post.next_post.get_absolute_url }}">{{ post.next_post.title }}</a>
{% else %}
没有了
{% endif %}
Expand Down
26 changes: 5 additions & 21 deletions selfblog/blog/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#coding:utf-8
# coding:utf-8
import logging

from django.db.models import Q
from django.db.models import F
from django.core.paginator import Paginator
from django.views.generic import ListView, DetailView
from django.shortcuts import render
Expand Down Expand Up @@ -129,8 +130,7 @@ def get(self, request, *args, **kwargs):
visited_ips = cache.get(self.object.id, [])

if ip not in visited_ips:
self.object.view_times += 1
self.object.save()
Post.objects.filter(id=self.object.id).update(view_times=F('view_times')+1)

visited_ips.append(ip)

Expand All @@ -143,7 +143,7 @@ def get(self, request, *args, **kwargs):
return self.render_to_response(context)

def set_lru_read(self, ip, post):
#保存别人正在读
# 保存别人正在读
lru_views = cache.get('lru_views')
if not lru_views:
lru_views = LRUCacheDict(max_size=10, expiration=FIF_MIN)
Expand All @@ -155,27 +155,11 @@ def set_lru_read(self, ip, post):

def get_context_data(self, **kwargs):
context = super(PostDetailView, self).get_context_data(**kwargs)
post = self.get_object()
next_id = post.id + 1
prev_id = post.id - 1

try:
next_post = self.queryset.get(id=next_id)
except Post.DoesNotExist:
next_post = None

try:
prev_post = self.queryset.get(id=prev_id)
except Post.DoesNotExist:
prev_post = None

context['next_post'] = next_post
context['prev_post'] = prev_post

context['lru_views'] = cache.get('lru_views', {}).items()
context['cur_user_ip'] = self.cur_user_ip

context['related_posts'] = post.related_posts
context['related_posts'] = self.object.related_posts

return context

Expand Down

0 comments on commit d709f6f

Please sign in to comment.