# Path: archive_chan/models.py
# class Board(models.Model):
#     name = models.CharField(max_length=255, primary_key = True)
#     active = models.BooleanField(
#         default=True,
#         help_text='Should this board be updated with new posts?'
#     )
#     store_threads_for = models.IntegerField(
#         default=48,
#         help_text='[hours] After that much time passes from the last reply in a NOT SAVED thread it will be deleted. Set to 0 to preserve threads forever.'
#     )
#     replies_threshold = models.IntegerField(
#         default=20,
#         help_text='Store threads after they reach that many replies.'
#     )
# 
#     class Meta:
#         ordering = ['name']
# 
#     def __str__(self):
#         return format("/%s/" % self.name)
# 
#     def get_absolute_url(self):
#         return reverse('archive_chan:board', args=[self.name])
# 
# 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])
# 
# class Post(models.Model):
#     thread = models.ForeignKey('Thread')
# 
#     number = models.IntegerField()
#     time = models.DateTimeField()
# 
#     name = models.CharField(max_length=255, blank=True)
#     trip = models.CharField(max_length=255, blank=True)
#     email = models.CharField(max_length=255, blank=True)
#     country = models.CharField(max_length=2, blank=True)
# 
#     subject = models.TextField(blank=True)
#     comment = models.TextField(blank=True)
# 
#     save_time = models.DateTimeField(auto_now_add = True)
# 
#     def is_main(self):
#         return (self.number == self.thread.number)
# 
#     class Meta:
#         ordering = ['number']
# 
#     def __str__(self):
#         return format("#%s" % (self.number))
# 
#     def get_absolute_url(self):
#         return '%s#post-%s' % (self.thread.get_absolute_url(), self.number)


from django.shortcuts import get_object_or_404
from django.db.models import Max, Min, Count, Q, F
from django.views.generic import ListView, TemplateView
from archive_chan.models import Board, Thread, Post
        from archive_chan.lib.stats import get_posts_chart_data
import archive_chan.lib.modifiers as modifiers
        )

        self.modifiers['tagged'] = modifiers.SimpleFilter(
            self.available_parameters['tagged'],
            self.request.GET.get('tagged', None)
        )

        self.modifiers['last_reply'] = modifiers.TimeFilter(
            self.available_parameters['last_reply'],
            self.request.GET.get('last_reply', None)
        )

        self.modifiers['tag'] = modifiers.TagFilter(
            self.request.GET.get('tag', None)
        )

        parameters['sort'], parameters['sort_reverse'] = self.modifiers['sort'].get()
        parameters['sort_with_operator'] = self.modifiers['sort'].get_full()
        parameters['saved'] = self.modifiers['saved'].get()
        parameters['tagged'] = self.modifiers['tagged'].get()
        parameters['last_reply'] = self.modifiers['last_reply'].get()
        parameters['tag'] = self.modifiers['tag'].get()

        return parameters

    def get_queryset(self):
        self.parameters = self.get_parameters()

        # I don't know how to select all data I need using the ORM without executing
        # TWO damn additional queries for each thread (first post + tags).

# Path: archive_chan/models.py
# class Board(models.Model):
#     name = models.CharField(max_length=255, primary_key = True)
#     active = models.BooleanField(
#         default=True,
#         help_text='Should this board be updated with new posts?'
#     )
#     store_threads_for = models.IntegerField(
#         default=48,
#         help_text='[hours] After that much time passes from the last reply in a NOT SAVED thread it will be deleted. Set to 0 to preserve threads forever.'
#     )
#     replies_threshold = models.IntegerField(
#         default=20,
#         help_text='Store threads after they reach that many replies.'
#     )
# 
#     class Meta:
#         ordering = ['name']
# 
#     def __str__(self):
#         return format("/%s/" % self.name)
# 
#     def get_absolute_url(self):
#         return reverse('archive_chan:board', args=[self.name])
# 
# 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])
# 
# class Post(models.Model):
#     thread = models.ForeignKey('Thread')
# 
#     number = models.IntegerField()
#     time = models.DateTimeField()
# 
#     name = models.CharField(max_length=255, blank=True)
#     trip = models.CharField(max_length=255, blank=True)
#     email = models.CharField(max_length=255, blank=True)
#     country = models.CharField(max_length=2, blank=True)
# 
#     subject = models.TextField(blank=True)
#     comment = models.TextField(blank=True)
# 
#     save_time = models.DateTimeField(auto_now_add = True)
# 
#     def is_main(self):
#         return (self.number == self.thread.number)
# 
#     class Meta:
#         ordering = ['number']
# 
#     def __str__(self):
#         return format("#%s" % (self.number))
# 
#     def get_absolute_url(self):
#         return '%s#post-%s' % (self.thread.get_absolute_url(), self.number)


# Path: archive_chan/views/core.py
from django.shortcuts import get_object_or_404
from django.db.models import Max, Min, Count, Q, F
from django.views.generic import ListView, TemplateView
from archive_chan.models import Board, Thread, Post
        from archive_chan.lib.stats import get_posts_chart_data
import archive_chan.lib.modifiers as modifiers

        )

        self.modifiers['tagged'] = modifiers.SimpleFilter(
            self.available_parameters['tagged'],
            self.request.GET.get('tagged', None)
        )

        self.modifiers['last_reply'] = modifiers.TimeFilter(
            self.available_parameters['last_reply'],
            self.request.GET.get('last_reply', None)
        )

        self.modifiers['tag'] = modifiers.TagFilter(
            self.request.GET.get('tag', None)
        )

        parameters['sort'], parameters['sort_reverse'] = self.modifiers['sort'].get()
        parameters['sort_with_operator'] = self.modifiers['sort'].get_full()
        parameters['saved'] = self.modifiers['saved'].get()
        parameters['tagged'] = self.modifiers['tagged'].get()
        parameters['last_reply'] = self.modifiers['last_reply'].get()
        parameters['tag'] = self.modifiers['tag'].get()

        return parameters

    def get_queryset(self):
        self.parameters = self.get_parameters()

        # I don't know how to select all data I need using the ORM without executing
        # TWO damn additional queries for each thread (first post + tags).

        queryset = Thread.objects.filter(board=self.kwargs['board'], replies__gte=1).select_related('board')