<commit_before>from django.db import connection, models, transaction

# from lithium.forum.models import Thread, Post

class ForumManager(models.Manager):
    def get_query_set(self):
        qs = super(ForumManager, self).get_query_set()
        
        thread_count_query = 'SELECT COUNT(*) FROM %(thread_table)s WHERE %(thread_table)s.%(thread_forum)s = %(forum_table)s.%(forum_id)s' % {
            # 'thread_table': Thread._meta.db_table,
            # 'thread_forum': Thread._meta.get_field('forum').column,
            'thread_table': 'forum_thread',
            'thread_forum': 'forum_id',
            'forum_table': self.model._meta.db_table,
            'forum_id': self.model._meta.get_field('id').column
        }
        
        return qs.extra(select={
            'thread_count': thread_count_query,
        })
    
    def update_position(self, position, addition=1, increment=True):
        if isinstance(position, (list, tuple)):
            if position[0] == position[-1]:
                where = '%s == %d' % (self.model._meta.get_field('position').column, position[0])
            else:
                where = '%(position)s >= %(start)d AND %(position)s <= %(end)d' % {
                    'position': self.model._meta.get_field('position').column,
                    'start': position[0],
                    'end': position[-1]
                }
        else:
            where = '%s >= %d' %  (self.model._meta.get_field('position').column, position)
        
        if increment == True:
            increment = '+'
        else:
            increment = '-'
        
        query = 'UPDATE %(table)s SET %(position)s = %(position)s %(increment)s %(addition)d WHERE %(where)s' % {
            'table': self.model._meta.db_table,
            'position': self.model._meta.get_field('position').column,
            'increment': increment,
            'addition': addition,
            'where': where
        }
        cursor = connection.cursor()
        cursor.execute(query)
        transaction.commit_unless_managed()

class ThreadManager(models.Manager):
    def get_query_set(self):
        qs = super(ThreadManager, self).get_query_set()
        
        post_count_query = 'SELECT COUNT(*) FROM %(post_table)s WHERE %(post_table)s.%(post_thread)s = %(thread_table)s.%(thread_id)s' % {
            # 'post_table': Post._meta.db_table,
            # 'post_thread': Post._meta.get_field('thread').column,
            'post_table': 'forum_post',
            'post_thread': 'thread_id',
            'thread_table': self.model._meta.db_table,
            'thread_id': self.model._meta.get_field('id').column
        }
        
        last_post_time_query = 'SELECT %(post_table)s.%(post_pub_date)s FROM %(post_table)s WHERE %(post_table)s.%(post_thread)s = %(thread_table)s.%(thread_id)s ORDER BY %(post_table)s.%(post_pub_date)s DESC LIMIT 1' % {
            'post_table': 'forum_post',
            'post_thread': 'thread_id',
            'post_pub_date': 'pub_date',
            'thread_table': self.model._meta.db_table,
            'thread_id': self.model._meta.get_field('id').column
        }
        
        return qs.extra(select={
            'post_count': post_count_query,
            'last_post_date': last_post_time_query,
        }, order_by=['-is_sticky', '-last_post_date'])<commit_msg>forum: Use annotate instead of use raw SQL<commit_after>from django.db import connection, models, transaction
from django.db.models import Count, Max

# from lithium.forum.models import Thread, Post

class ForumManager(models.Manager):
    def get_query_set(self):
        return super(ForumManager, self).get_query_set().annotate(
                thread_count=Count('thread'))
    
    def update_position(self, position, addition=1, increment=True):
        if isinstance(position, (list, tuple)):
            if position[0] == position[-1]:
                where = '%s == %d' % (self.model._meta.get_field('position').column, position[0])
            else:
                where = '%(position)s >= %(start)d AND %(position)s <= %(end)d' % {
                    'position': self.model._meta.get_field('position').column,
                    'start': position[0],
                    'end': position[-1]
                }
        else:
            where = '%s >= %d' %  (self.model._meta.get_field('position').column, position)
        
        if increment == True:
            increment = '+'
        else:
            increment = '-'
        
        query = 'UPDATE %(table)s SET %(position)s = %(position)s %(increment)s %(addition)d WHERE %(where)s' % {
            'table': self.model._meta.db_table,
            'position': self.model._meta.get_field('position').column,
            'increment': increment,
            'addition': addition,
            'where': where
        }
        cursor = connection.cursor()
        cursor.execute(query)
        transaction.commit_unless_managed()

class ThreadManager(models.Manager):
    def get_query_set(self):
        return super(ThreadManager, self).get_query_set().annotate(
            post_count=Count('post'),
            last_post_date=Max('post__pub_date')
        ).order_by('-is_sticky', '-last_post_date')
{'fraction_non_alphanumeric': 0.14357101672112244, 'fraction_numerical': 0.0024985585239285027, 'mean_word_length': 2.2853535353535355, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5799319', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1556, 'n_words': 274}