- 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')