From 162b4fd442c169a61d2f5b2b3bd613546c8472da Mon Sep 17 00:00:00 2001 From: hernani Date: Wed, 7 Jul 2010 12:47:35 +0000 Subject: [PATCH] Makes tags and users lists use the new paginators, completelly remove the old cnprog stuff. Fixes OSQA 306, Tags are badly sorted. git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@497 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- forum/skins/default/media/style/style.css | 1 + forum/skins/default/templates/book.html | 152 ------------------ forum/skins/default/templates/question.html | 10 +- forum/skins/default/templates/questions.html | 10 +- forum/skins/default/templates/tags.html | 13 +- .../skins/default/templates/users/users.html | 42 ++--- forum/templatetags/extra_tags.py | 64 -------- forum/utils/pagination.py | 17 +- forum/views/decorators.py | 4 +- forum/views/readers.py | 57 ++----- forum/views/users.py | 58 +++---- 11 files changed, 70 insertions(+), 358 deletions(-) delete mode 100644 forum/skins/default/templates/book.html diff --git a/forum/skins/default/media/style/style.css b/forum/skins/default/media/style/style.css index 99edef0..e74bba6 100644 --- a/forum/skins/default/media/style/style.css +++ b/forum/skins/default/media/style/style.css @@ -1103,6 +1103,7 @@ ins { line-height: 140%; padding: 5px; width: 170px; + display: inline-block; } .user ul { diff --git a/forum/skins/default/templates/book.html b/forum/skins/default/templates/book.html deleted file mode 100644 index 8574fa7..0000000 --- a/forum/skins/default/templates/book.html +++ /dev/null @@ -1,152 +0,0 @@ -{% extends "base_content.html" %} - -{% load i18n %} -{% load extra_tags %} -{% load extra_filters %} -{% load humanize %} -{% block title %}{% spaceless %}{{ book.title }}-{% trans "reading channel" %}{% endspaceless %}{% endblock %} -{% block forejs %} - -{% endblock %} -{% block content %} -
《{{ book.title }}》
-
-
- -
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {% if author_info.blog_url %} - - - - - - {% endif %} - - - - - - - - -
{% trans "[author]" %}{{ book.author }}
{% trans "[publisher]" %}{{ book.publication }}
{% trans "[publication date]" %}{{ book.published_at|date:"Y-m" }}
{% trans "[price]" %}{{ book.price }} {% trans "currency unit" %}
{% trans "[pages]" %}{{ book.pages }} {% trans "pages abbreviation" %}
{% trans "[tags]" %}{{ book.tagnames }}
 
{% trans "author blog" %} »
{% trans "book directory" %} »
{% trans "buy online" %} »
-
-
- -
-
- -
- - - - -
-{% endblock %} -{% block tail %} -
- {% cnprog_paginator context %} - -
-
- -
- -{% endblock %} - diff --git a/forum/skins/default/templates/question.html b/forum/skins/default/templates/question.html index 00bbd51..305f62c 100644 --- a/forum/skins/default/templates/question.html +++ b/forum/skins/default/templates/question.html @@ -111,17 +111,17 @@
- {% blocktrans count answers.count as counter %} + {% blocktrans count answers.paginator.count as counter %} One Answer: {% plural %} {{counter}} Answers: {% endblocktrans %}
- {{ answers.sort_tabs }} + {{ answers.paginator.sort_tabs }}
- {{ answers.page_numbers }} + {{ answers.paginator.page_numbers }} - {% for answer in answers.page %} + {% for answer in answers.paginator.page %}
@@ -152,7 +152,7 @@ {% endfor %}
- {{ answers.page_numbers }} + {{ answers.paginator.page_numbers }}
{% endif %} diff --git a/forum/skins/default/templates/questions.html b/forum/skins/default/templates/questions.html index c0476d8..34c81ba 100644 --- a/forum/skins/default/templates/questions.html +++ b/forum/skins/default/templates/questions.html @@ -16,16 +16,16 @@
{% question_list_title %} - {{ questions.sort_tabs }} + {{ questions.paginator.sort_tabs }}
-
{% for question in questions.page %} +
{% for question in questions.paginator.page %} {% question_list_item question %} {% endfor %}
{% endblock %} {% block tail %} -
{{ questions.page_numbers }}
-
{{ questions.page_sizes }}
+
{{ questions.paginator.page_numbers }}
+
{{ questions.paginator.page_sizes }}
{% endblock %} {% block sidebar %} @@ -33,7 +33,7 @@ {% sidebar_upper %} {% tag_selector %} {% sidebar_lower %} - {% question_list_related_tags questions.page %} + {% question_list_related_tags questions.paginator.page %} {% endblock %} diff --git a/forum/skins/default/templates/tags.html b/forum/skins/default/templates/tags.html index e58fad5..ec5d888 100644 --- a/forum/skins/default/templates/tags.html +++ b/forum/skins/default/templates/tags.html @@ -27,23 +27,20 @@
{% trans "Tag list" %}
- + {{ tags.paginator.sort_tabs }}

{% if stag %} {% trans "All tags matching query" %} '{{ stag }}': {% endif %} -{% if not tags.object_list %} +{% if not tags.paginator.count %} {% trans "Nothing found" %} {% endif %}

-{% if tags.object_list %} +{% if tags.paginator.count %}
- - - - -
- {% for user in users.object_list %} - -
- -
- - {% if forloop.counter|divisibleby:"7" %} -
- {% endif %} - - {% endfor %} -
+ {% for user in users.paginator.page %} +
+ +
+ {% endfor %}
{% endblock %} {% block tail %}
- {% cnprog_paginator context %} + {{ users.paginator.page_numbers }}
{% endblock %} diff --git a/forum/templatetags/extra_tags.py b/forum/templatetags/extra_tags.py index 892f38b..302412f 100644 --- a/forum/templatetags/extra_tags.py +++ b/forum/templatetags/extra_tags.py @@ -40,70 +40,6 @@ def gravatar(user, size): }) -LEADING_PAGE_RANGE_DISPLAYED = TRAILING_PAGE_RANGE_DISPLAYED = 5 -LEADING_PAGE_RANGE = TRAILING_PAGE_RANGE = 4 -NUM_PAGES_OUTSIDE_RANGE = 1 -ADJACENT_PAGES = 2 -@register.inclusion_tag("paginator.html") -def cnprog_paginator(context): - """ - custom paginator tag - Inspired from http://blog.localkinegrinds.com/2007/09/06/digg-style-pagination-in-django/ - """ - if (context["is_paginated"]): - " Initialize variables " - in_leading_range = in_trailing_range = False - pages_outside_leading_range = pages_outside_trailing_range = range(0) - - if (context["pages"] <= LEADING_PAGE_RANGE_DISPLAYED): - in_leading_range = in_trailing_range = True - page_numbers = [n for n in range(1, context["pages"] + 1) if n > 0 and n <= context["pages"]] - elif (context["page"] <= LEADING_PAGE_RANGE): - in_leading_range = True - page_numbers = [n for n in range(1, LEADING_PAGE_RANGE_DISPLAYED + 1) if n > 0 and n <= context["pages"]] - pages_outside_leading_range = [n + context["pages"] for n in range(0, -NUM_PAGES_OUTSIDE_RANGE, -1)] - elif (context["page"] > context["pages"] - TRAILING_PAGE_RANGE): - in_trailing_range = True - page_numbers = [n for n in range(context["pages"] - TRAILING_PAGE_RANGE_DISPLAYED + 1, context["pages"] + 1) - if n > 0 and n <= context["pages"]] - pages_outside_trailing_range = [n + 1 for n in range(0, NUM_PAGES_OUTSIDE_RANGE)] - else: - page_numbers = [n for n in range(context["page"] - ADJACENT_PAGES, context["page"] + ADJACENT_PAGES + 1) if - n > 0 and n <= context["pages"]] - pages_outside_leading_range = [n + context["pages"] for n in range(0, -NUM_PAGES_OUTSIDE_RANGE, -1)] - pages_outside_trailing_range = [n + 1 for n in range(0, NUM_PAGES_OUTSIDE_RANGE)] - - extend_url = context.get('extend_url', '') - return { - "base_url": context["base_url"], - "is_paginated": context["is_paginated"], - "previous": context["previous"], - "has_previous": context["has_previous"], - "next": context["next"], - "has_next": context["has_next"], - "page": context["page"], - "pages": context["pages"], - "page_numbers": page_numbers, - "in_leading_range" : in_leading_range, - "in_trailing_range" : in_trailing_range, - "pages_outside_leading_range": pages_outside_leading_range, - "pages_outside_trailing_range": pages_outside_trailing_range, - "extend_url" : extend_url - } - -@register.inclusion_tag("pagesize.html") -def cnprog_pagesize(context): - """ - display the pagesize selection boxes for paginator - """ - if (context["is_paginated"]): - return { - "base_url": context["base_url"], - "pagesize" : context["pagesize"], - "is_paginated": context["is_paginated"] - } - - @register.simple_tag def get_score_badge(user): if user.is_suspended(): diff --git a/forum/utils/pagination.py b/forum/utils/pagination.py index afadb28..3f873dd 100644 --- a/forum/utils/pagination.py +++ b/forum/utils/pagination.py @@ -171,8 +171,8 @@ def paginated(request, list_name, context, tpl_context): if hasattr(object_list, 'lazy'): return object_list.lazy() - return page_obj.object_list - objects.page = get_page + return object_list + paginator.page = get_page() total_pages = paginator.num_pages @@ -225,9 +225,9 @@ def paginated(request, list_name, context, tpl_context): 'current': page, 'page_numbers': page_numbers })) - objects.page_numbers = page_nums + paginator.page_numbers = page_nums else: - objects.page_numbers = '' + paginator.page_numbers = '' if pagesize: def page_sizes(): @@ -243,9 +243,9 @@ def paginated(request, list_name, context, tpl_context): 'sizes': sizes })) - objects.page_sizes = page_sizes + paginator.page_sizes = page_sizes else: - objects.page_sizes = '' + paginator.page_sizes = '' if sort: def sort_tabs(): @@ -257,10 +257,11 @@ def paginated(request, list_name, context, tpl_context): 'sorts': sorts, 'sticky': session_prefs.get('sticky_sort', False) })) - objects.sort_tabs = sort_tabs() + paginator.sort_tabs = sort_tabs() else: - objects.sort_tabs = '' + paginator.sort_tabs = '' request.session['paginator_%s' % context.id] = session_prefs + objects.paginator = paginator tpl_context[list_name] = objects return tpl_context \ No newline at end of file diff --git a/forum/views/decorators.py b/forum/views/decorators.py index d86a62e..2219f98 100644 --- a/forum/views/decorators.py +++ b/forum/views/decorators.py @@ -10,7 +10,7 @@ import logging def render(template=None, tab=None, tab_title='', weight=500, tabbed=True): def decorator(func): - def decorated(request, *args, **kwargs): + def decorated(func, request, *args, **kwargs): context = func(request, *args, **kwargs) if isinstance(context, HttpResponse): @@ -26,7 +26,7 @@ def render(template=None, tab=None, tab_title='', weight=500, tabbed=True): ui.register(ui.PAGE_TOP_TABS, ui.PageTab(tab, tab_title, lambda: reverse(func.__name__), weight=weight)) - return decorated + return decorate.withfn(decorated)(func) return decorator diff --git a/forum/views/readers.py b/forum/views/readers.py index 9420ce0..999d320 100644 --- a/forum/views/readers.py +++ b/forum/views/readers.py @@ -31,18 +31,6 @@ from forum.http_responses import HttpResponseUnauthorized from forum.feed import RssQuestionFeed import decorators -# used in index page -#refactor - move these numbers somewhere? -INDEX_PAGE_SIZE = 30 -INDEX_AWARD_SIZE = 15 -INDEX_TAGS_SIZE = 25 -# used in tags list -DEFAULT_PAGE_SIZE = 60 -# used in questions -QUESTIONS_PAGE_SIZE = 30 -# used in answers -ANSWERS_PAGE_SIZE = 10 - class QuestionListPaginatorContext(pagination.PaginatorContext): def __init__(self): super (QuestionListPaginatorContext, self).__init__('QUESTIONS_LIST', sort_methods=( @@ -66,6 +54,13 @@ class AnswerPaginatorContext(pagination.PaginatorContext): (_('newest'), AnswerSort(_('newest answers'), '-added_at', _("newest answers will be shown first"))), (_('votes'), AnswerSort(_('popular answers'), '-score', _("most voted answers will be shown first"))), ), default_sort=_('votes'), sticky_sort = True, pagesizes=(5, 10, 20)) + +class TagPaginatorContext(pagination.PaginatorContext): + def __init__(self): + super (TagPaginatorContext, self).__init__('TAG_LIST', sort_methods=( + (_('name'), pagination.SimpleSort(_('by name'), 'name', _("sorted alphabetically"))), + (_('used'), pagination.SimpleSort(_('by popularity'), '-used_count', _("sorted by frequency of tag use"))), + ), default_sort=_('used'), pagesizes=(30, 60, 120)) def feed(request): @@ -221,44 +216,18 @@ def question_search(request, keywords): @decorators.render('tags.html', 'tags', _('tags'), weight=100) def tags(request): stag = "" - is_paginated = True - sortby = request.GET.get('sort', 'used') - try: - page = int(request.GET.get('page', '1')) - except ValueError: - page = 1 + tags = Tag.active.all() if request.method == "GET": stag = request.GET.get("q", "").strip() - if stag != '': - objects_list = Paginator(Tag.active.filter(name__contains=stag), DEFAULT_PAGE_SIZE) - else: - if sortby == "name": - objects_list = Paginator(Tag.active.order_by("name"), DEFAULT_PAGE_SIZE) - else: - objects_list = Paginator(Tag.active.order_by("-used_count"), DEFAULT_PAGE_SIZE) - - try: - tags = objects_list.page(page) - except (EmptyPage, InvalidPage): - tags = objects_list.page(objects_list.num_pages) + if stag: + tags = tags.filter(name__contains=stag) - return { + return pagination.paginated(request, 'tags', TagPaginatorContext(), { "tags" : tags, "stag" : stag, - "tab_id" : sortby, - "keywords" : stag, - "context" : { - 'is_paginated' : is_paginated, - 'pages': objects_list.num_pages, - 'page': page, - 'has_previous': tags.has_previous(), - 'has_next': tags.has_next(), - 'previous': tags.previous_page_number(), - 'next': tags.next_page_number(), - 'base_url' : reverse('tags') + '?sort=%s&' % sortby - } - } + "keywords" : stag + }) def update_question_view_times(request, question): if not 'last_seen_in_question' in request.session: diff --git a/forum/views/users.py b/forum/views/users.py index 2bda263..9cac193 100644 --- a/forum/views/users.py +++ b/forum/views/users.py @@ -21,60 +21,38 @@ from datetime import datetime, date import decorators from forum.actions import EditProfileAction, FavoriteAction, BonusRepAction, SuspendAction from forum.modules import ui +from forum.utils import pagination import time import decorators +class UserReputationSort(pagination.SimpleSort): + def apply(self, objects): + return objects.order_by('-is_active', self.order_by) + +class UserListPaginatorContext(pagination.PaginatorContext): + def __init__(self): + super (UserListPaginatorContext, self).__init__('USERS_LIST', sort_methods=( + (_('reputation'), UserReputationSort(_('reputation'), '-reputation', _("sorted by reputation"))), + (_('newest'), pagination.SimpleSort(_('recent'), '-date_joined', _("newest members"))), + (_('last'), pagination.SimpleSort(_('oldest'), 'date_joined', _("oldest members"))), + (_('name'), pagination.SimpleSort(_('by username'), 'username', _("sorted by username"))), + ), pagesizes=(20, 35, 60)) + USERS_PAGE_SIZE = 35# refactor - move to some constants file @decorators.render('users/users.html', 'users', _('users'), weight=200) def users(request): - is_paginated = True - sortby = request.GET.get('sort', 'reputation') suser = request.REQUEST.get('q', "") - try: - page = int(request.GET.get('page', '1')) - except ValueError: - page = 1 + users = User.objects.all() if suser == "": - if sortby == "newest": - objects_list = Paginator(User.objects.all().order_by('-date_joined'), USERS_PAGE_SIZE) - elif sortby == "last": - objects_list = Paginator(User.objects.all().order_by('date_joined'), USERS_PAGE_SIZE) - elif sortby == "user": - objects_list = Paginator(User.objects.all().order_by('username'), USERS_PAGE_SIZE) - # default - else: - objects_list = Paginator(User.objects.all().order_by('-is_active', '-reputation'), USERS_PAGE_SIZE) - base_url = reverse('users') + '?sort=%s&' % sortby - else: - sortby = "reputation" - objects_list = Paginator(User.objects.filter(username__icontains=suser).order_by('-reputation'), USERS_PAGE_SIZE - ) - base_url = reverse('users') + '?name=%s&sort=%s&' % (suser, sortby) + users = users.filter(username__icontains=suser) - try: - users = objects_list.page(page) - except (EmptyPage, InvalidPage): - users = objects_list.page(objects_list.num_pages) - - return { + return pagination.paginated(request, 'users', UserListPaginatorContext(), { "users" : users, "suser" : suser, - "keywords" : suser, - "tab_id" : sortby, - "context" : { - 'is_paginated' : is_paginated, - 'pages': objects_list.num_pages, - 'page': page, - 'has_previous': users.has_previous(), - 'has_next': users.has_next(), - 'previous': users.previous_page_number(), - 'next': users.next_page_number(), - 'base_url' : base_url - } - } + }) @login_required -- 2.39.5