]> git.openstreetmap.org Git - osqa.git/commitdiff
Fixes OSQA-88 (Improve "hottest" functionality on front page), and makes some improve...
authorhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Fri, 9 Jul 2010 12:55:22 +0000 (12:55 +0000)
committerhernani <hernani@0cfe37f9-358a-4d5e-be75-b63607b5c754>
Fri, 9 Jul 2010 12:55:22 +0000 (12:55 +0000)
git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@513 0cfe37f9-358a-4d5e-be75-b63607b5c754

forum/middleware/request_utils.py
forum/models/base.py
forum/skins/default/templates/question_list/count.html
forum/skins/default/templates/questions.html
forum/templatetags/question_list_tags.py
forum/utils/pagination.py
forum/views/readers.py

index 40736bf7ff2ce1b88f5239741c0ec62e3943da88..bbccbf7462017ff8e44699c1fdbb776b18fa7a40 100644 (file)
@@ -3,28 +3,6 @@ from forum.settings import MAINTAINANCE_MODE, APP_LOGO, APP_TITLE
 from forum.http_responses import HttpResponseServiceUnavailable\r
 \r
 class RequestUtils(object):\r
-    def __init__(self):\r
-        self.request = None\r
-\r
-    def set_sort_method(self, sort):\r
-        self.request.session['questions_sort_method'] = sort\r
-\r
-    def sort_method(self, default):\r
-        sort = self.request.REQUEST.get('sort', None)\r
-        if sort is None:\r
-            return self.request.session.get('questions_sort_method', default)\r
-        else:\r
-            self.set_sort_method(sort)\r
-            return sort\r
-\r
-    def page_size(self, default):\r
-        pagesize = self.request.REQUEST.get('pagesize', None)\r
-        if pagesize is None:\r
-            return int(self.request.session.get('questions_pagesize', default))\r
-        else:\r
-            self.request.session['questions_pagesize'] = pagesize\r
-            return int(pagesize)\r
-\r
     def process_request(self, request):\r
         if MAINTAINANCE_MODE.value is not None and isinstance(MAINTAINANCE_MODE.value.get('allow_ips', None), list):\r
             ip = request.META['REMOTE_ADDR']\r
@@ -37,5 +15,4 @@ class RequestUtils(object):
             request.META['REQUEST_METHOD'] = "POST"\r
 \r
         self.request = request\r
-        request.utils = self\r
         return None
\ No newline at end of file
index 3d54d75e95d8025efee2a6af8c97ebf3b47e60a8..2f934a916f349be0eca77c9c56d0347d7b3944fa 100644 (file)
@@ -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
index b160b6e08e15bca152ea0ad11d9905aab521e73a..cf347ed16a09929f9af342f9c8af016f4852084f 100644 (file)
@@ -16,7 +16,7 @@
 \r
     <div>\r
         <p class="nomargin">\r
-            {{ sort_description|safe }}\r
+            {{ questions.paginator.sort_description }}\r
         </p>\r
     </div>\r
 </div>
\ No newline at end of file
index 34c81ba42d4854ba074d55a946a1f6eb8e8b0597..d3166703642fd2df8a4109b4b8f3f339c48039e4 100644 (file)
@@ -14,7 +14,7 @@
 {% block content %}\r
 \r
 <div class="tabBar">\r
-    {% question_list_title %}\r
+    {% include "question_list/title.html" %}\r
     <a class="feed-icon" style="background-image:url('{% media "media/images/feed-icon-small.png" %}');" href="{{ feed_url }}" title="{% trans "subscribe to question RSS feed" %}"></a>\r
     {{ questions.paginator.sort_tabs }}\r
 </div>\r
@@ -29,7 +29,7 @@
 {% endblock %}\r
 \r
 {% block sidebar %}\r
-    {% question_list_count %}\r
+    {% include "question_list/count.html" %}\r
     {% sidebar_upper %}\r
     {% tag_selector %}\r
     {% sidebar_lower %}\r
index dc735f3c508c47633f952bdc7b57ae829946954d..60cbd7f25649c575c789028f2514cb6103840a58 100644 (file)
@@ -56,18 +56,3 @@ def tag_selector(context):
         }\r
     else:\r
         return {'user_authenticated': False}\r
-\r
-@register.inclusion_tag('question_list/count.html', takes_context=True)\r
-def question_list_count(context):\r
-    context['sort_description'] = mark_safe({\r
-        'latest': _('<strong>Newest</strong> questions are shown first. '),\r
-        'active': _('Questions are sorted by the <strong>time of last update</strong>.'),\r
-        'hottest': _('Questions sorted by <strong>number of responses</strong>.'),\r
-        'mostvoted': _('Questions are sorted by the <strong>number of votes</strong>.')\r
-    }.get(context['request'].utils.sort_method('latest'), ''))\r
-\r
-    return context\r
-\r
-@register.inclusion_tag('question_list/title.html', takes_context=True)\r
-def question_list_title(context):\r
-    return context
\ No newline at end of file
index f955e6052ae89454f882001f66a32e3deed7fb7f..a573954e46df720761738d397e2eb280306f8078 100644 (file)
@@ -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
index 6a490876506c59acaf03e944444d7a6acc3a7511..40bd16cfd7ddb272bf7b0da07d6a456b32b1349f 100644 (file)
@@ -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 <strong>recently updated</strong> questions"))),
+            (_('newest'), pagination.SimpleSort(_('newest'), '-added_at', _("most <strong>recently asked</strong> questions"))),
+            (_('hottest'), HottestQuestionsSort(_('hottest'), _("most <strong>active</strong> questions in the last 24 hours</strong>"))),
+            (_('mostvoted'), pagination.SimpleSort(_('most voted'), '-score', _("most <strong>voted</strong> 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 <span class="tag">%(tag)s</span>') % {'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,