]> git.openstreetmap.org Git - osqa.git/blobdiff - forum/views/readers.py
Remove footer link to squatted domain
[osqa.git] / forum / views / readers.py
index 8dad7801a1720afc8b0a7c551a5fc6676f5a650b..fc819bf164b77f5ed5a2ff36c78d57f8f3956223 100644 (file)
@@ -8,6 +8,7 @@ from django.core.paginator import Paginator, EmptyPage, InvalidPage
 from django.template import RequestContext
 from django import template
 from django.utils.html import *
+from django.utils.http import urlquote
 from django.db.models import Q, Count
 from django.utils.translation import ugettext as _
 from django.core.urlresolvers import reverse
@@ -29,9 +30,28 @@ 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')
-
+        return questions.extra(select={'new_child_count': '''
+            SELECT COUNT(1)
+                FROM forum_node fn1
+                WHERE fn1.abs_parent_id = forum_node.id
+                    AND fn1.id != forum_node.id
+                    AND NOT(fn1.state_string LIKE '%%(deleted)%%')
+                    AND added_at > %s'''
+            },
+            select_params=[ (datetime.datetime.now() - datetime.timedelta(days=2))
+                .strftime('%Y-%m-%d')]
+        ).order_by('-new_child_count', 'last_activity_at')
+
+class UnansweredQuestionsSort(pagination.SortBase):
+    def apply(self, questions):
+        return questions.extra(select={'answer_count': '''
+            SELECT COUNT(1)
+                FROM forum_node fn1
+                WHERE fn1.abs_parent_id = forum_node.id
+                    AND fn1.id != forum_node.id
+                    AND fn1.node_type='answer'
+                    AND NOT(fn1.state_string LIKE '%%(deleted)%%')'''
+            }).order_by('answer_count', 'last_activity_at')
 
 class QuestionListPaginatorContext(pagination.PaginatorContext):
     def __init__(self, id='QUESTIONS_LIST', prefix='', pagesizes=(15, 30, 50), default_pagesize=30):
@@ -40,6 +60,7 @@ class QuestionListPaginatorContext(pagination.PaginatorContext):
             (_('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"))),
+            (_('unanswered'), UnansweredQuestionsSort('unanswered', "questions with no answers")),
         ), pagesizes=pagesizes, default_pagesize=default_pagesize, prefix=prefix)
 
 class AnswerSort(pagination.SimpleSort):
@@ -52,6 +73,7 @@ class AnswerSort(pagination.SimpleSort):
 class AnswerPaginatorContext(pagination.PaginatorContext):
     def __init__(self, id='ANSWER_LIST', prefix='', default_pagesize=10):
         super (AnswerPaginatorContext, self).__init__(id, sort_methods=(
+            (_('active'), AnswerSort(_('active answers'), '-last_activity_at', _("most recently updated answers/comments will be shown first"))),
             (_('oldest'), AnswerSort(_('oldest answers'), 'added_at', _("oldest answers will be shown first"))),
             (_('newest'), AnswerSort(_('newest answers'), '-added_at', _("newest answers will be shown first"))),
             (_('votes'), AnswerSort(_('popular answers'), ('-score', 'added_at'), _("most voted answers will be shown first"))),
@@ -92,7 +114,9 @@ def unanswered(request):
 
 @decorators.render('questions.html', 'questions', _('questions'), weight=0)
 def questions(request):
-    return question_list(request, Question.objects.all(), _('questions'))
+    return question_list(request,
+                         Question.objects.all(),
+                         _('questions'))
 
 @decorators.render('questions.html')
 def tag(request, tag):
@@ -119,12 +143,18 @@ def tag(request, tag):
     }
 
     # The context returned by the question_list function, contains info about the questions
-    question_context = dict(question_list(request,
+    question_context = question_list(request,
                          questions,
                          mark_safe(_(u'questions tagged <span class="tag">%(tag)s</span>') % {'tag': tag}),
                          None,
                          mark_safe(_(u'Questions Tagged With %(tag)s') % {'tag': tag}),
-                         False))
+                         False)
+
+    # If the return data type is not a dict just return it
+    if not isinstance(question_context, dict):
+        return question_context
+
+    question_context = dict(question_context)
 
     # Create the combined context
     context = dict(question_context.items() + tag_context.items())
@@ -165,8 +195,13 @@ def question_list(request, initial,
                   allowIgnoreTags=True,
                   feed_url=None,
                   paginator_context=None,
+                  show_summary=None,
                   feed_sort=('-added_at',),
-                  feed_req_params_exclude=(_('page'), _('pagesize'), _('sort'))):
+                  feed_req_params_exclude=(_('page'), _('pagesize'), _('sort')),
+                  extra_context={}):
+
+    if show_summary is None:
+        show_summary = bool(settings.SHOW_SUMMARY_ON_QUESTIONS_LIST)
 
     questions = initial.filter_state(deleted=False)
 
@@ -205,7 +240,9 @@ def question_list(request, initial,
         'page_title' : page_title,
         'tab' : 'questions',
         'feed_url': feed_url,
+        'show_summary' : show_summary,
     }
+    context.update(extra_context)
 
     return pagination.paginated(request,
                                ('questions', paginator_context or QuestionListPaginatorContext()), context)
@@ -316,8 +353,8 @@ def answer_redirect(request, answer):
     if page == 0:
         page = 1
 
-    return HttpResponsePermanentRedirect("%s?%s=%s#%s" % (
-        answer.question.get_absolute_url(), _('page'), page, answer.id))
+    return HttpResponseRedirect("%s?%s=%s&focusedAnswerId=%s#%s" % (
+        answer.question.get_absolute_url(), _('page'), page, answer.id, answer.id))
 
 @decorators.render("question.html", 'questions')
 def question(request, id, slug='', answer=None):
@@ -367,6 +404,10 @@ def question(request, id, slug='', answer=None):
             subscription = False
     else:
         subscription = False
+    try:
+        focused_answer_id = int(request.GET.get("focusedAnswerId", None))
+    except TypeError, ValueError:
+        focused_answer_id = None
 
     return pagination.paginated(request, ('answers', AnswerPaginatorContext()), {
     "question" : question,
@@ -375,6 +416,7 @@ def question(request, id, slug='', answer=None):
     "similar_questions" : question.get_related_questions(),
     "subscription": subscription,
     "embed_youtube_videos" : settings.EMBED_YOUTUBE_VIDEOS,
+    "focused_answer_id" : focused_answer_id
     })