Exemple #1
0
    def get(self, request, *args, **kwargs):
        if 'HTTP_X_FORWARDED_FOR' in request.META:
            ip = request.META['HTTP_X_FORWARDED_FOR']
        else:
            ip = request.META['REMOTE_ADDR']
        self.cur_user_ip = ip

        alias = self.kwargs.get('slug')
        visited_ips = cache.get(alias, [])

        if ip not in visited_ips:
            try:
                post = self.queryset.get(alias=alias)
            except Post.DoesNotExist:
                logger.error(u'访问不存在的文章:[%s]' % alias)
                context = super(PostDetailView, self).get_context_data(**kwargs)
                return render(request, '404.html', context)
            else:
                post.view_times += 1
                post.save()
                visited_ips.append(ip)

                self.set_lru_read(ip, post)

            cache.set(alias, visited_ips, FIF_MIN)
            cache.set("postCategory_"+alias, post.category.alias, FIF_MIN)
            self.curCategory = post.category.alias
        else:
            self.curCategory = cache.get("postCategory_"+alias)

        return super(PostDetailView, self).get(request, *args, **kwargs)
Exemple #2
0
    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

        return context
Exemple #3
0
    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)

        if post not in lru_views.values():
            lru_views[ip] = post

        cache.set('lru_views', lru_views, FIF_MIN)
Exemple #4
0
    def process_view(self, request, view_func, view_args, view_kwargs):
        """
                            处理当前在线人数
        """
        http_user_agent = request.META.get("HTTP_USER_AGENT")
        if "Spider" in http_user_agent or "spider" in http_user_agent:
            return

        if "HTTP_X_FORWARDED_FOR" in request.META:
            ip = request.META["HTTP_X_FORWARDED_FOR"]
        else:
            ip = request.META["REMOTE_ADDR"]

        online_ips = cache.get("online_ips", [])

        if online_ips:
            online_ips = cache.get_many(online_ips).keys()

        cache.set(ip, 0, 5 * 60)

        if ip not in online_ips:
            online_ips.append(ip)

        cache.set("online_ips", online_ips)
Exemple #5
0
    def get_context_data(self, *args, **kwargs):
        context = super(BaseMixin, self).get_context_data(**kwargs)
        try:
            context['categories'] = [[True, "/",{"name":"首页","alias":"","is_nav":True}]]
                    
            #logger.info(context['categories'])
            context['widgets'] = Widget.available_list()
            context['recently_posts'] = Post.get_recently_posts(RECENTLY_NUM)
            context['hot_posts'] = Post.get_hots_posts(HOT_NUM)
            context['pages'] = Page.objects.filter(status=0)
            #print "online ips",cache.get('online_ips')
            context['online_num'] = len(cache.get('online_ips'))
            
            for i in Category.available_list():

                if i.alias == self.curCategory:
                    context['categories'][0][0] = False
                    context['categories'].append([True, "/category/%s/"%(i.alias), i])
                else:
                    context['categories'].append([False, "/category/%s/"%(i.alias), i])
        except Exception as e:
            logger.exception(u'加载基本信息出错[%s]!', e)

        return context