From: hernani Date: Fri, 9 Jul 2010 12:55:22 +0000 (+0000) Subject: Fixes OSQA-88 (Improve "hottest" functionality on front page), and makes some improve... X-Git-Tag: live~621 X-Git-Url: https://git.openstreetmap.org./osqa.git/commitdiff_plain/b885b2a795c839540f299a7e5f33ed4b670101ab Fixes OSQA-88 (Improve "hottest" functionality on front page), and makes some improvements in question lists display. git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@513 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- diff --git a/forum/middleware/request_utils.py b/forum/middleware/request_utils.py index 40736bf..bbccbf7 100644 --- a/forum/middleware/request_utils.py +++ b/forum/middleware/request_utils.py @@ -3,28 +3,6 @@ from forum.settings import MAINTAINANCE_MODE, APP_LOGO, APP_TITLE from forum.http_responses import HttpResponseServiceUnavailable class RequestUtils(object): - def __init__(self): - self.request = None - - def set_sort_method(self, sort): - self.request.session['questions_sort_method'] = sort - - def sort_method(self, default): - sort = self.request.REQUEST.get('sort', None) - if sort is None: - return self.request.session.get('questions_sort_method', default) - else: - self.set_sort_method(sort) - return sort - - def page_size(self, default): - pagesize = self.request.REQUEST.get('pagesize', None) - if pagesize is None: - return int(self.request.session.get('questions_pagesize', default)) - else: - self.request.session['questions_pagesize'] = pagesize - return int(pagesize) - def process_request(self, request): if MAINTAINANCE_MODE.value is not None and isinstance(MAINTAINANCE_MODE.value.get('allow_ips', None), list): ip = request.META['REMOTE_ADDR'] @@ -37,5 +15,4 @@ class RequestUtils(object): request.META['REQUEST_METHOD'] = "POST" self.request = request - request.utils = self return None \ No newline at end of file diff --git a/forum/models/base.py b/forum/models/base.py index 3d54d75..2f934a9 100644 --- a/forum/models/base.py +++ b/forum/models/base.py @@ -35,7 +35,7 @@ class LazyQueryList(object): class CachedQuerySet(models.query.QuerySet): def lazy(self): - if len(self.query.extra) == 0: + if (not len(self.query.extra)) and (not len(self.query.aggregates)): return LazyQueryList(self.model, list(self.values_list('id', flat=True))) else: return self diff --git a/forum/skins/default/templates/question_list/count.html b/forum/skins/default/templates/question_list/count.html index b160b6e..cf347ed 100644 --- a/forum/skins/default/templates/question_list/count.html +++ b/forum/skins/default/templates/question_list/count.html @@ -16,7 +16,7 @@

- {{ sort_description|safe }} + {{ questions.paginator.sort_description }}

\ No newline at end of file diff --git a/forum/skins/default/templates/questions.html b/forum/skins/default/templates/questions.html index 34c81ba..d316670 100644 --- a/forum/skins/default/templates/questions.html +++ b/forum/skins/default/templates/questions.html @@ -14,7 +14,7 @@ {% block content %}
- {% question_list_title %} + {% include "question_list/title.html" %} {{ questions.paginator.sort_tabs }}
@@ -29,7 +29,7 @@ {% endblock %} {% block sidebar %} - {% question_list_count %} + {% include "question_list/count.html" %} {% sidebar_upper %} {% tag_selector %} {% sidebar_lower %} diff --git a/forum/templatetags/question_list_tags.py b/forum/templatetags/question_list_tags.py index dc735f3..60cbd7f 100644 --- a/forum/templatetags/question_list_tags.py +++ b/forum/templatetags/question_list_tags.py @@ -56,18 +56,3 @@ def tag_selector(context): } else: return {'user_authenticated': False} - -@register.inclusion_tag('question_list/count.html', takes_context=True) -def question_list_count(context): - context['sort_description'] = mark_safe({ - 'latest': _('Newest questions are shown first. '), - 'active': _('Questions are sorted by the time of last update.'), - 'hottest': _('Questions sorted by number of responses.'), - 'mostvoted': _('Questions are sorted by the number of votes.') - }.get(context['request'].utils.sort_method('latest'), '')) - - return context - -@register.inclusion_tag('question_list/title.html', takes_context=True) -def question_list_title(context): - return context \ No newline at end of file diff --git a/forum/utils/pagination.py b/forum/utils/pagination.py index f955e60..a573954 100644 --- a/forum/utils/pagination.py +++ b/forum/utils/pagination.py @@ -5,13 +5,18 @@ from django.core.paginator import Paginator, EmptyPage from django.utils.translation import ugettext as _ from django.http import Http404 from django.utils.safestring import mark_safe -from django.utils.http import urlquote +from django.utils.html import strip_tags + import logging -class SimpleSort(object): - def __init__(self, label, order_by, description=''): +class SortBase(object): + def __init__(self, label, description=''): self.label = label self.description = description + +class SimpleSort(SortBase): + def __init__(self, label, order_by, description=''): + super(SimpleSort, self) .__init__(label, description) self.order_by = order_by def apply(self, objects): @@ -20,15 +25,6 @@ class SimpleSort(object): else: return objects.order_by(self.order_by) -class DummySort(object): - def __init__(self, label, description=''): - self.label = label - self.description = description - - def apply(self, objects): - return objects - - class PaginatorContext(object): visible_page_range = 5 outside_page_range = 1 @@ -285,7 +281,7 @@ def _paginated(request, objects, context): if sort: def sort_tabs(): url_builder = lambda s: mark_safe("%s%s%s=%s" % (base_path, url_joiner, context.SORT, s)) - sorts = [(n, s.label, url_builder(n), s.description) for n, s in context.sort_methods.items()] + sorts = [(n, s.label, url_builder(n), strip_tags(s.description)) for n, s in context.sort_methods.items()] return sort_tabs_template.render(template.Context({ 'current': sort, @@ -293,8 +289,9 @@ def _paginated(request, objects, context): 'sticky': session_prefs.get('sticky_sort', False) })) paginator.sort_tabs = sort_tabs() + paginator.sort_description = mark_safe(context.sort_methods[sort].description) else: - paginator.sort_tabs = '' + paginator.sort_tabs = paginator.sort_description = '' context.set_preferences(request, session_prefs) objects.paginator = paginator diff --git a/forum/views/readers.py b/forum/views/readers.py index 6a49087..40bd16c 100644 --- a/forum/views/readers.py +++ b/forum/views/readers.py @@ -10,7 +10,7 @@ from django.template import RequestContext from django import template from django.utils.html import * from django.utils import simplejson -from django.db.models import Q +from django.db.models import Q, Count from django.utils.translation import ugettext as _ from django.template.defaultfilters import slugify from django.core.urlresolvers import reverse @@ -31,13 +31,19 @@ from forum.http_responses import HttpResponseUnauthorized from forum.feed import RssQuestionFeed, RssAnswerFeed import decorators +class HottestQuestionsSort(pagination.SortBase): + def apply(self, questions): + return questions.annotate(new_child_count=Count('all_children')).filter( + all_children__added_at__gt=datetime.datetime.now() - datetime.timedelta(days=1)).order_by('-new_child_count') + + class QuestionListPaginatorContext(pagination.PaginatorContext): def __init__(self, id='QUESTIONS_LIST', prefix='', default_pagesize=30): super (QuestionListPaginatorContext, self).__init__(id, sort_methods=( - (_('active'), pagination.SimpleSort(_('active'), '-last_activity_at', _("most recently updated questions"))), - (_('newest'), pagination.SimpleSort(_('newest'), '-added_at', _("most recently asked questions"))), - (_('hottest'), pagination.SimpleSort(_('hottest'), '-extra_count', _("hottest questions"))), - (_('mostvoted'), pagination.SimpleSort(_('most voted'), '-score', _("most voted questions"))), + (_('active'), pagination.SimpleSort(_('active'), '-last_activity_at', _("Most recently updated questions"))), + (_('newest'), pagination.SimpleSort(_('newest'), '-added_at', _("most recently asked questions"))), + (_('hottest'), HottestQuestionsSort(_('hottest'), _("most active questions in the last 24 hours"))), + (_('mostvoted'), pagination.SimpleSort(_('most voted'), '-score', _("most voted questions"))), ), pagesizes=(15, 30, 50), default_pagesize=default_pagesize, prefix=prefix) class AnswerPaginatorContext(pagination.PaginatorContext): @@ -70,7 +76,6 @@ def index(request): paginator_context.base_path = reverse('questions') return question_list(request, Question.objects.all(), - sort=request.utils.set_sort_method('active'), base_path=reverse('questions'), feed_url=reverse('latest_questions_feed'), paginator_context=paginator_context) @@ -80,20 +85,18 @@ def unanswered(request): return question_list(request, Question.objects.filter(extra_ref=None), _('open questions without an accepted answer'), - request.utils.set_sort_method('active'), None, _("Unanswered Questions")) @decorators.render('questions.html', 'questions', _('questions'), weight=0) def questions(request): - return question_list(request, Question.objects.all(), _('questions'), request.utils.set_sort_method('active')) + return question_list(request, Question.objects.all(), _('questions')) @decorators.render('questions.html') def tag(request, tag): return question_list(request, Question.objects.filter(tags__name=unquote(tag)), mark_safe(_('questions tagged %(tag)s') % {'tag': tag}), - request.utils.set_sort_method('active'), None, mark_safe(_('Questions Tagged With %(tag)s') % {'tag': tag}), False) @@ -123,12 +126,10 @@ def user_questions(request, mode, user, slug): return question_list(request, questions, mark_safe(description % hyperlink(user.get_profile_url(), user.username)), - request.utils.set_sort_method('active'), page_title=description % user.username) def question_list(request, initial, list_description=_('questions'), - sort=None, base_path=None, page_title=_("All Questions"), allowIgnoreTags=True,