Пример #1
0
def posts_list(request, topic_pk):
    topic = get_object_or_404(Topic, pk=topic_pk)
    if topic.is_deleted:
        return HttpResponseGone()
    posts = Post.objects.filter(topic=topic, is_deleted=False)\
            .select_related('author').order_by('created')
    paginator = Paginator(posts, settings.FORUM_POSTS_PER_PAGE)
    try:
        page = paginator.page(request.GET.get('page', 1))
    except PageNotAnInteger:
        page = paginator.page(1)
    except EmptyPage:
        page = paginator.page(paginator.num_pages)

    # this is not always called (condition cache decorator), but that's good
    # thing. It means that the same user is not bumping the counter all the
    # time
    # XXX remove if from here, because the view is cached
    counter = backend.default()
    counter.increment('topic:view:{}'.format(topic.pk))

    ctx = {
        'topic': topic,
        'posts': page,
    }
    return render(request, 'forum/posts_list.html', ctx)
Пример #2
0
def topic_view_count(request, widgets):
    key_tmpl = "topic:view:{}"
    counter = backend.default()
    keys = [key_tmpl.format(w['params']['tid']) for w in widgets]
    counters = counter.get(*keys)
    res = {}
    for widget in widgets:
        value = counters.get(key_tmpl.format(widget['params']['tid']), 0)
        ctx = {'value': value}
        html = render_to_string('forum/widgets/topic_view_count.html', ctx)
        res[widget['wid']] = {'html': html, 'counter': value}
    return res