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
request.META['REQUEST_METHOD'] = "POST"\r
\r
self.request = request\r
- request.utils = self\r
return None
\ No newline at end of file
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):
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
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,
'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
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
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):
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)
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)
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,