topics paginator

This commit is contained in:
nitely 2015-01-10 21:47:52 -03:00
parent 9418d2af2c
commit c48b3e0818
24 changed files with 294 additions and 50 deletions

View File

@ -74,3 +74,4 @@ class BasicConfigForm(ConfigForm):
widget=forms.Textarea(attrs={'rows': 2, }),
help_text=_("This gets rendered just before the footer in your template."))
comments_per_page = forms.IntegerField(initial=20, label=_("comments per page"), min_value=1, max_value=100)
topics_per_page = forms.IntegerField(initial=20, label=_("topics per page"), min_value=1, max_value=100)

View File

@ -1,6 +1,6 @@
{% extends "spirit/_base.html" %}
{% load i18n %}
{% load spirit_tags i18n %}
{% block title %}{% trans "Closed topics" %}{% endblock %}
@ -12,6 +12,8 @@
{% include "spirit/admin/topic/_tabs.html" with active_tab="closed" %}
{% include "spirit/topic/_render_page_list.html" %}
{% include "spirit/topic/_render_list.html" %}
{% endblock %}
{% render_paginator page=topics %}
{% endblock %}

View File

@ -1,6 +1,6 @@
{% extends "spirit/_base.html" %}
{% load i18n %}
{% load spirit_tags i18n %}
{% block title %}{% trans "Deleted Topics" %}{% endblock %}
@ -12,6 +12,8 @@
{% include "spirit/admin/topic/_tabs.html" with active_tab="deleted" %}
{% include "spirit/topic/_render_page_list.html" %}
{% include "spirit/topic/_render_list.html" %}
{% endblock %}
{% render_paginator page=topics %}
{% endblock %}

View File

@ -1,6 +1,6 @@
{% extends "spirit/_base.html" %}
{% load i18n %}
{% load spirit_tags i18n %}
{% block title %}{% trans "Pinned topics" %}{% endblock %}
@ -12,6 +12,8 @@
{% include "spirit/admin/topic/_tabs.html" with active_tab="pinned" %}
{% include "spirit/topic/_render_page_list.html" %}
{% include "spirit/topic/_render_list.html" %}
{% endblock %}
{% render_paginator page=topics %}
{% endblock %}

View File

@ -1,12 +1,10 @@
{% load spirit_tags i18n %}
{% yt_paginator_autopaginate users as page %}
{% include "spirit/admin/user/_tabs.html" with active_tab=active_tab %}
<div class="rows">
{% for u in page %}
{% for u in users %}
<div class="row">
<a href="{{ u.get_absolute_url }}">{{ u.username }}</a> <a class="row-edit" href="{% url "spirit:admin-user-edit" user_id=u.pk %}"><i class="fa fa-cog"></i> {% trans "edit" %}</a>
</div>
@ -14,4 +12,4 @@
</div>
{% render_paginator page %}
{% render_paginator page=users %}

View File

@ -1,6 +1,6 @@
{% extends "spirit/_base.html" %}
{% load i18n %}
{% load spirit_tags i18n %}
{% block title %}{{ category.title }}{% endblock %}
@ -8,6 +8,8 @@
{% include "spirit/topic/_top_bar.html" with category=category categories=subcategories %}
{% include "spirit/topic/_render_page_list.html" with show_subcategory=True %}
{% include "spirit/topic/_render_list.html" with show_subcategory=True %}
{% endblock %}
{% render_paginator page=topics %}
{% endblock %}

View File

@ -18,7 +18,6 @@
{% else %}
<h1 class="headline">{% trans "Results" %}</h1>
{% yt_paginator_autopaginate page as page %}
{% get_topics_from_search_result results=page as topics_page %}
{% if topics_page %}

View File

@ -1,6 +1,6 @@
{% extends "spirit/_base.html" %}
{% load i18n %}
{% load spirit_tags i18n %}
{% block title %}{% trans "Latest active topics" %}{% endblock %}
@ -8,6 +8,8 @@
{% include "spirit/topic/_top_bar.html" with category=None categories=categories %}
{% include "spirit/topic/_render_page_list.html" %}
{% include "spirit/topic/_render_list.html" %}
{% endblock %}
{% render_paginator page=topics %}
{% endblock %}

View File

@ -6,13 +6,11 @@
{% block content %}
{% yt_paginator_autopaginate topics as page %}
{% include "spirit/topic_private/_top_bar.html" with active="left" %}
<div class="rows">
{% for t in page %}
{% for t in topics %}
<div class="row">
<a href="{% url "spirit:private-join" topic_id=t.pk %}">{{ t.title }}</a> <a class="row-edit js-post" href="{% url "spirit:private-join" topic_id=t.pk %}"><i class="fa fa-users"></i> {% trans "join" %}</a>
</div>
@ -22,6 +20,6 @@
</div>
{% render_paginator page %}
{% render_paginator page=topics %}
{% endblock %}

View File

@ -1,6 +1,6 @@
{% extends "spirit/_base.html" %}
{% load i18n %}
{% load spirit_tags i18n %}
{% block title %}{% trans "Private topics" %}{% endblock %}
@ -8,6 +8,8 @@
{% include "spirit/topic_private/_top_bar.html" with active="participating" %}
{% include "spirit/topic/_render_page_list.html" %}
{% include "spirit/topic/_render_list.html" %}
{% endblock %}
{% render_paginator page=topics %}
{% endblock %}

View File

@ -4,6 +4,9 @@ from __future__ import unicode_literals
from django.shortcuts import render
from djconfig import config
from ...utils.paginator import yt_paginate
from spirit.utils.decorators import administrator_required
from spirit.models.topic import Topic
@ -11,15 +14,22 @@ from spirit.models.topic import Topic
@administrator_required
def topic_deleted(request):
# Private topics cant be deleted, closed or pinned so we are ok
# TODO: paginate
topics = Topic.objects.filter(is_removed=True)
topics = yt_paginate(
Topic.objects.filter(is_removed=True),
per_page=config.topics_per_page,
page_number=request.GET.get('page', 1)
)
context = {'topics': topics, }
return render(request, 'spirit/admin/topic/topic_deleted.html', context)
@administrator_required
def topic_closed(request):
topics = Topic.objects.filter(is_closed=True)
topics = yt_paginate(
Topic.objects.filter(is_closed=True),
per_page=config.topics_per_page,
page_number=request.GET.get('page', 1)
)
context = {'topics': topics, }
return render(request, 'spirit/admin/topic/topic_closed.html', context)
@ -27,5 +37,10 @@ def topic_closed(request):
@administrator_required
def topic_pinned(request):
topics = Topic.objects.filter(is_pinned=True) | Topic.objects.filter(is_globally_pinned=True)
topics = yt_paginate(
topics,
per_page=config.topics_per_page,
page_number=request.GET.get('page', 1)
)
context = {'topics': topics, }
return render(request, 'spirit/admin/topic/topic_pinned.html', context)

View File

@ -7,6 +7,9 @@ from django.contrib.auth import get_user_model
from django.contrib import messages
from django.utils.translation import ugettext as _
from djconfig import config
from ...utils.paginator import yt_paginate
from spirit.utils.decorators import administrator_required
from spirit.forms.admin import UserEditForm
@ -36,28 +39,43 @@ def user_edit(request, user_id):
@administrator_required
def user_list(request):
# TODO: paginate
users = User.objects.all()
users = yt_paginate(
User.objects.all(),
per_page=config.topics_per_page,
page_number=request.GET.get('page', 1)
)
context = {'users': users, }
return render(request, 'spirit/admin/user/user_list.html', context)
@administrator_required
def user_admins(request):
users = User.objects.filter(is_administrator=True)
users = yt_paginate(
User.objects.filter(is_administrator=True),
per_page=config.topics_per_page,
page_number=request.GET.get('page', 1)
)
context = {'users': users, }
return render(request, 'spirit/admin/user/user_admins.html', context)
@administrator_required
def user_mods(request):
users = User.objects.filter(is_moderator=True, is_administrator=False)
users = yt_paginate(
User.objects.filter(is_moderator=True, is_administrator=False),
per_page=config.topics_per_page,
page_number=request.GET.get('page', 1)
)
context = {'users': users, }
return render(request, 'spirit/admin/user/user_mods.html', context)
@administrator_required
def user_unactive(request):
users = User.objects.filter(is_active=False)
users = yt_paginate(
User.objects.filter(is_active=False),
per_page=config.topics_per_page,
page_number=request.GET.get('page', 1)
)
context = {'users': users, }
return render(request, 'spirit/admin/user/user_unactive.html', context)

View File

@ -7,6 +7,9 @@ from django.shortcuts import render
from django.http import HttpResponsePermanentRedirect
from django.shortcuts import get_object_or_404
from djconfig import config
from ..utils.paginator import yt_paginate
from spirit.models.topic import Topic
from spirit.models.category import Category
@ -23,7 +26,6 @@ def category_detail(request, pk, slug):
.visible()\
.children(parent=category)
# TODO: paginate
topics = Topic.objects\
.unremoved()\
.with_bookmarks(user=request.user)\
@ -31,6 +33,12 @@ def category_detail(request, pk, slug):
.order_by('-is_globally_pinned', '-is_pinned', '-last_active')\
.select_related('category')
topics = yt_paginate(
topics,
per_page=config.topics_per_page,
page_number=request.GET.get('page', 1)
)
context = {
'category': category,
'subcategories': subcategories,

View File

@ -4,10 +4,19 @@ from __future__ import unicode_literals
from haystack.views import SearchView as BaseSearchView
from djconfig import config
from ..utils.paginator import yt_paginate
class SearchView(BaseSearchView):
def build_page(self):
# TODO: test
paginator = None
page = self.results
page = yt_paginate(
self.results,
per_page=config.topics_per_page,
page_number=self.request.GET.get('page', 1)
)
return paginator, page

View File

@ -8,7 +8,7 @@ from django.http import HttpResponsePermanentRedirect
from djconfig import config
from ..utils.paginator import paginate
from ..utils.paginator import paginate, yt_paginate
from ..utils.ratelimit.decorators import ratelimit
from ..models.category import Category
from ..models.comment import MOVED
@ -124,13 +124,18 @@ def topic_active_list(request):
.visible()\
.parents()
# TODO: paginate
topics = Topic.objects\
.visible()\
.with_bookmarks(user=request.user)\
.order_by('-is_globally_pinned', '-last_active')\
.select_related('category')
topics = yt_paginate(
topics,
per_page=config.topics_per_page,
page_number=request.GET.get('page', 1)
)
context = {
'categories': categories,
'topics': topics

View File

@ -14,7 +14,7 @@ from django.conf import settings
from djconfig import config
from .. import utils
from ..utils.paginator import paginate
from ..utils.paginator import paginate, yt_paginate
from ..utils.ratelimit.decorators import ratelimit
from ..forms.comment import CommentForm
from ..signals.comment import comment_posted
@ -162,11 +162,18 @@ def private_join(request, topic_id):
@login_required
def private_list(request):
# TODO: paginate
topics = Topic.objects\
.with_bookmarks(user=request.user)\
.filter(topics_private__user=request.user)
topics = yt_paginate(
topics,
per_page=config.topics_per_page,
page_number=request.GET.get('page', 1)
)
context = {'topics': topics, }
return render(request, 'spirit/topic_private/private_list.html', context)
@ -174,10 +181,17 @@ def private_list(request):
def private_created_list(request):
# Show created topics but exclude those the user is participating on
# TODO: show all, show join link in those the user is not participating
# TODO: move to manager
topics = Topic.objects\
.filter(user=request.user, category_id=settings.ST_TOPIC_PRIVATE_CATEGORY_PK)\
.exclude(topics_private__user=request.user)
topics = yt_paginate(
topics,
per_page=config.topics_per_page,
page_number=request.GET.get('page', 1)
)
context = {'topics': topics, }
return render(request, 'spirit/topic_private/private_created_list.html', context)

View File

@ -15,7 +15,6 @@ def topic_unread_list(request):
# TODO: add button to clean up read topics? or read all?
# redirect to first page if empty
# TODO: paginate
topics = Topic.objects\
.for_access(user=request.user)\
.for_unread(user=request.user)\

View File

@ -191,9 +191,9 @@ def profile_topics(request, pk, slug):
p_user = get_object_or_404(User, pk=pk)
if p_user.slug != slug:
return HttpResponsePermanentRedirect(reverse("spirit:profile-topics", kwargs={'pk': p_user.pk,
'slug': p_user.slug}))
# TODO: paginate
url = reverse("spirit:profile-topics", kwargs={'pk': p_user.pk, 'slug': p_user.slug})
return HttpResponsePermanentRedirect(url)
topics = Topic.objects\
.visible()\
.with_bookmarks(user=request.user)\
@ -201,6 +201,12 @@ def profile_topics(request, pk, slug):
.order_by('-date', '-pk')\
.select_related('user')
topics = yt_paginate(
topics,
per_page=config.topics_per_page,
page_number=request.GET.get('page', 1)
)
context = {
'p_user': p_user,
'topics': topics

View File

@ -80,6 +80,17 @@ class AdminViewTest(TestCase):
response = self.client.get(reverse('spirit:admin-user-list'))
self.assertQuerysetEqual(response.context['users'], map(repr, [self.user, ]))
@override_djconfig(topics_per_page=1)
def test_user_list_paginate(self):
"""
List of all users paginated
"""
user2 = utils.create_user()
utils.login(self)
response = self.client.get(reverse('spirit:admin-user-list'))
self.assertQuerysetEqual(response.context['users'], map(repr, [user2, ]))
def test_user_admins(self):
"""
List of admins
@ -88,15 +99,38 @@ class AdminViewTest(TestCase):
response = self.client.get(reverse('spirit:admin-user-admins'))
self.assertQuerysetEqual(response.context['users'], map(repr, [self.user, ]))
@override_djconfig(topics_per_page=1)
def test_user_admins_paginate(self):
"""
List of admins paginated
"""
user2 = utils.create_user(is_administrator=True)
utils.login(self)
response = self.client.get(reverse('spirit:admin-user-admins'))
self.assertQuerysetEqual(response.context['users'], map(repr, [user2, ]))
def test_user_mods(self):
"""
List of admins
List of mods
"""
mod = utils.create_user(is_moderator=True)
utils.login(self)
response = self.client.get(reverse('spirit:admin-user-mods'))
self.assertQuerysetEqual(response.context['users'], map(repr, [mod, ]))
@override_djconfig(topics_per_page=1)
def test_user_mods_paginate(self):
"""
List of mods paginated
"""
utils.create_user(is_moderator=True)
mod = utils.create_user(is_moderator=True)
utils.login(self)
response = self.client.get(reverse('spirit:admin-user-mods'))
self.assertQuerysetEqual(response.context['users'], map(repr, [mod, ]))
def test_user_unactive(self):
"""
List of unactive
@ -107,6 +141,20 @@ class AdminViewTest(TestCase):
response = self.client.get(reverse('spirit:admin-user-unactive'))
self.assertQuerysetEqual(response.context['users'], map(repr, [unactive, ]))
@override_djconfig(topics_per_page=1)
def test_user_unactive_paginate(self):
"""
List of unactive paginated
"""
unactive = utils.create_user()
User.objects.filter(pk=unactive.pk).update(is_active=False)
unactive2 = utils.create_user()
User.objects.filter(pk=unactive2.pk).update(is_active=False)
utils.login(self)
response = self.client.get(reverse('spirit:admin-user-unactive'))
self.assertQuerysetEqual(response.context['users'], map(repr, [unactive2, ]))
def test_index_dashboard(self):
utils.login(self)
response = self.client.get(reverse('spirit:admin-topic'))
@ -121,6 +169,18 @@ class AdminViewTest(TestCase):
response = self.client.get(reverse('spirit:admin-topic-deleted'))
self.assertQuerysetEqual(response.context['topics'], map(repr, [topic_, ]))
@override_djconfig(topics_per_page=1)
def test_topic_deleted_paginate(self):
"""
Deleted topics paginated
"""
utils.create_topic(self.category, is_removed=True)
topic_ = utils.create_topic(self.category, is_removed=True)
utils.login(self)
response = self.client.get(reverse('spirit:admin-topic-deleted'))
self.assertQuerysetEqual(response.context['topics'], map(repr, [topic_, ]))
def test_topic_closed(self):
"""
Closed topics
@ -130,6 +190,17 @@ class AdminViewTest(TestCase):
response = self.client.get(reverse('spirit:admin-topic-closed'))
self.assertQuerysetEqual(response.context['topics'], map(repr, [topic_, ]))
@override_djconfig(topics_per_page=1)
def test_topic_closed_paginate(self):
"""
Closed topics paginated
"""
utils.create_topic(self.category, is_closed=True)
topic_ = utils.create_topic(self.category, is_closed=True)
utils.login(self)
response = self.client.get(reverse('spirit:admin-topic-closed'))
self.assertQuerysetEqual(response.context['topics'], map(repr, [topic_, ]))
def test_topic_pinned(self):
"""
Pinned topics
@ -139,6 +210,17 @@ class AdminViewTest(TestCase):
response = self.client.get(reverse('spirit:admin-topic-pinned'))
self.assertQuerysetEqual(response.context['topics'], map(repr, [topic_, ]))
@override_djconfig(topics_per_page=1)
def test_topic_pinned_paginate(self):
"""
Pinned topics paginated
"""
utils.create_topic(self.category, is_pinned=True)
topic_ = utils.create_topic(self.category, is_pinned=True)
utils.login(self)
response = self.client.get(reverse('spirit:admin-topic-pinned'))
self.assertQuerysetEqual(response.context['topics'], map(repr, [topic_, ]))
def test_category_list(self):
"""
Categories, excludes Topic Private and subcats
@ -182,7 +264,7 @@ class AdminViewTest(TestCase):
Config
"""
utils.login(self)
form_data = {"site_name": "foo", "site_description": "bar", "comments_per_page": 10}
form_data = {"site_name": "foo", "site_description": "bar", "comments_per_page": 10, "topics_per_page": 10}
response = self.client.post(reverse('spirit:admin-config-basic'),
form_data)
expected_url = reverse('spirit:admin-config-basic')
@ -359,14 +441,16 @@ class AdminFormTest(TestCase):
form_data = {"site_name": "foo",
"site_description": "",
"template_footer": "",
"comments_per_page": 10}
"comments_per_page": 10,
"topics_per_page": 10}
form = BasicConfigForm(data=form_data)
self.assertEqual(form.is_valid(), True)
form_data = {"site_name": "foo",
"site_description": "bar",
"template_footer": "foobar",
"comments_per_page": 10}
"comments_per_page": 10,
"topics_per_page": 10}
form = BasicConfigForm(data=form_data)
self.assertEqual(form.is_valid(), True)

View File

@ -9,6 +9,8 @@ from django.core.urlresolvers import reverse
from django.core.cache import cache
from django.utils import timezone
from djconfig.utils import override_djconfig
from . import utils
from spirit.models.topic import Topic
@ -141,3 +143,15 @@ class CategoryViewTest(TestCase):
'slug': self.category_1.slug}))
self.assertQuerysetEqual(response.context['topics'], [repr(topic), ])
self.assertEqual(response.context['topics'][0].bookmark, bookmark)
@override_djconfig(topics_per_page=1)
def test_category_detail_view_paginate(self):
"""
List of topics paginated
"""
utils.create_topic(category=self.category_1)
topic = utils.create_topic(category=self.category_1)
response = self.client.get(reverse('spirit:category-detail', kwargs={'pk': self.category_1.pk,
'slug': self.category_1.slug}))
self.assertQuerysetEqual(response.context['topics'], [repr(topic), ])

View File

@ -10,6 +10,7 @@ from django.conf import settings
from django.core.management import call_command
from haystack.query import SearchQuerySet
from djconfig.utils import override_djconfig
from . import utils
@ -65,7 +66,7 @@ class SearchViewTest(TestCase):
cache.clear()
self.user = utils.create_user()
self.category = utils.create_category()
self.topic = utils.create_topic(category=self.category, user=self.user, title="spirit search test")
self.topic = utils.create_topic(category=self.category, user=self.user, title="spirit search test foo")
self.topic2 = utils.create_topic(category=self.category, user=self.user, title="foo")
call_command("rebuild_index", interactive=False)
@ -92,6 +93,18 @@ class SearchViewTest(TestCase):
self.assertEqual(response.status_code, 200)
self.assertQuerysetEqual([s.object for s in response.context['page']], map(repr, [self.topic, ]))
@override_djconfig(topics_per_page=1)
def test_advanced_search_topics_paginate(self):
"""
advanced search by topic paginated
"""
utils.login(self)
data = {'q': 'foo', }
response = self.client.get(reverse('spirit:search'),
data)
self.assertEqual(response.status_code, 200)
self.assertQuerysetEqual([s.object for s in response.context['page']], map(repr, [self.topic2, ]))
def test_advanced_search_in_category(self):
"""
search by topic in category

View File

@ -344,6 +344,18 @@ class TopicViewTest(TestCase):
self.assertQuerysetEqual(response.context['topics'], map(repr, [topic, topic2]))
self.assertEqual(response.context['topics'][0].bookmark, bookmark)
@override_djconfig(topics_per_page=1)
def test_topic_active_view_paginate(self):
"""
topics ordered by activity paginated
"""
category = utils.create_category()
topic_a = utils.create_topic(category=category)
topic_b = utils.create_topic(category=category, user=self.user, view_count=10)
response = self.client.get(reverse('spirit:topic-active'))
self.assertQuerysetEqual(response.context['topics'], map(repr, [topic_b, ]))
class TopicFormTest(TestCase):

View File

@ -260,6 +260,18 @@ class TopicPrivateViewTest(TestCase):
self.assertQuerysetEqual(response.context['topics'], [repr(private.topic), ])
self.assertEqual(response.context['topics'][0].bookmark, bookmark)
@override_djconfig(topics_per_page=1)
def test_private_list(self):
"""
private topic list paginated
"""
utils.create_private_topic(user=self.user)
private = utils.create_private_topic(user=self.user)
utils.login(self)
response = self.client.get(reverse('spirit:private-list'))
self.assertQuerysetEqual(response.context['topics'], [repr(private.topic), ])
def test_private_join(self):
"""
private topic join
@ -362,6 +374,20 @@ class TopicPrivateViewTest(TestCase):
self.assertQuerysetEqual(response.context['topics'], map(repr, [private_b.topic, private_c.topic,
private_a.topic]))
@override_djconfig(topics_per_page=1)
def test_private_created_list_paginate(self):
"""
private topic created list paginated
"""
private = utils.create_private_topic(user=self.user)
private.delete()
private2 = utils.create_private_topic(user=self.user)
private2.delete()
utils.login(self)
response = self.client.get(reverse('spirit:private-created-list'))
self.assertQuerysetEqual(response.context['topics'], map(repr, [private2.topic, ]))
class TopicPrivateFormTest(TestCase):

View File

@ -148,6 +148,19 @@ class UserViewTest(TestCase):
self.assertQuerysetEqual(response.context['topics'], [repr(self.topic), ])
self.assertEqual(response.context['topics'][0].bookmark, bookmark)
@override_djconfig(topics_per_page=1)
def test_profile_topics_paginate(self):
"""
profile user's topics paginated
"""
topic = utils.create_topic(self.category, user=self.user2)
utils.login(self)
response = self.client.get(reverse("spirit:profile-topics", kwargs={'pk': self.user2.pk,
'slug': self.user2.slug}))
self.assertEqual(response.status_code, 200)
self.assertQuerysetEqual(response.context['topics'], [repr(topic), ])
def test_profile_topics_dont_show_removed_or_private(self):
"""
dont show private topics or removed