# Path: archive_chan/models.py
# class Thread(models.Model):
#     board = models.ForeignKey('Board')
#     number = models.IntegerField()
#     saved = models.BooleanField(default=False) # Threads which are not saved will get deleted after some time.
#     auto_saved = models.BooleanField(default=False) # Was this thread saved automatically by a trigger?
#     tags = models.ManyToManyField('Tag', through='TagToThread')
# 
#     replies = models.IntegerField(default=0)
#     images = models.IntegerField(default=0)
#     first_reply = models.DateTimeField(null=True, default=None)
#     last_reply = models.DateTimeField(null=True, default=None)
# 
#     # Used by scraper.
#     def last_reply_time(self):
#         last = self.post_set.last()
#         if last is None:
#             return None
#         else:
#             return last.time
# 
#     # Used by scraper.
#     def count_replies(self):
#         return self.post_set.count() - 1
# 
#     # Used by board template. Can't figure out a query which wouldd fetch everything in one go.
#     def first_post(self):
#         return self.post_set.select_related('image').first()
# 
#     class Meta:
#         unique_together = ('board', 'number')
# 
#     def __str__(self):
#         return format("#%s" % (self.number))
# 
#     def get_absolute_url(self):
#         return reverse('archive_chan:thread', args=[self.board.name, self.number])


from django.db.models import Max, Min, Count
from django.core.management.base import BaseCommand
from archive_chan.models import Thread


class Command(BaseCommand):
    args = ''
    help = 'Recount the data in the thread model.'

    def handle(self, *args, **options):

# Path: archive_chan/models.py
# class Thread(models.Model):
#     board = models.ForeignKey('Board')
#     number = models.IntegerField()
#     saved = models.BooleanField(default=False) # Threads which are not saved will get deleted after some time.
#     auto_saved = models.BooleanField(default=False) # Was this thread saved automatically by a trigger?
#     tags = models.ManyToManyField('Tag', through='TagToThread')
# 
#     replies = models.IntegerField(default=0)
#     images = models.IntegerField(default=0)
#     first_reply = models.DateTimeField(null=True, default=None)
#     last_reply = models.DateTimeField(null=True, default=None)
# 
#     # Used by scraper.
#     def last_reply_time(self):
#         last = self.post_set.last()
#         if last is None:
#             return None
#         else:
#             return last.time
# 
#     # Used by scraper.
#     def count_replies(self):
#         return self.post_set.count() - 1
# 
#     # Used by board template. Can't figure out a query which wouldd fetch everything in one go.
#     def first_post(self):
#         return self.post_set.select_related('image').first()
# 
#     class Meta:
#         unique_together = ('board', 'number')
# 
#     def __str__(self):
#         return format("#%s" % (self.number))
# 
#     def get_absolute_url(self):
#         return reverse('archive_chan:thread', args=[self.board.name, self.number])


# Path: archive_chan/management/commands/archive_chan_recount_denormalized.py
from django.db.models import Max, Min, Count
from django.core.management.base import BaseCommand
from archive_chan.models import Thread



class Command(BaseCommand):
    args = ''
    help = 'Recount the data in the thread model.'

    def handle(self, *args, **options):

        threads = Thread.objects.annotate(