# Path: archive_chan/models.py
# class Update(models.Model):
#     CURRENT = 0
#     FAILED = 1
#     COMPLETED = 2
# 
#     STATUS_CHOICES = (
#         (CURRENT, 'Started'),
#         (FAILED, 'Failed'),
#         (COMPLETED, 'Completed'),
#     )
# 
#     board = models.ForeignKey('Board')
#     status = models.SmallIntegerField(choices=STATUS_CHOICES, default=CURRENT)
#     start = models.DateTimeField()
#     end = models.DateTimeField(null=True)
# 
#     used_threads = models.IntegerField()
# 
#     total_time = models.FloatField(default=0)
#     wait_time = models.FloatField(default=0)
#     download_time = models.FloatField(default=0)
# 
#     processed_threads = models.IntegerField(default=0)
#     added_posts = models.IntegerField(default=0)
#     removed_posts = models.IntegerField(default=0)
# 
#     downloaded_images = models.IntegerField(default=0)
#     downloaded_thumbnails = models.IntegerField(default=0)
#     downloaded_threads = models.IntegerField(default=0)
# 
#     class Meta:
#         ordering = ['-start']
# 
# class Image(models.Model):
#     original_name = models.CharField(max_length=255)
#     post = models.OneToOneField('Post')
#     image = models.FileField(upload_to = "post_images", storage=fs) # It is impossible to use ImageField to store webm.
#     thumbnail = models.FileField(upload_to = "post_thumbnails", storage=fs)
# 
#     def get_extension(self):
#         name, extension = os.path.splitext(self.image.name)
#         return extension
# 
# 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 Tag(models.Model):
#     name = models.CharField(max_length=255, unique=True)
# 
#     def __str__(self):
#         return self.name
# 
# class TagToThread(models.Model):
#     thread = models.ForeignKey('Thread')
#     tag = models.ForeignKey('Tag')
#     automatically_added = models.BooleanField(default=False, editable=False)
#     save_time = models.DateTimeField(auto_now_add = True)
# 
#     def __str__(self):
#         return format('%s - %s' % (self.thread, self.tag))


import json
import archive_chan.lib.stats as stats
from django.db.models import Avg
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.views.generic.base import View
from archive_chan.models import Update, Image, Thread, Tag, TagToThread

                added = True
            else:
                added = False

            response = {
                'added': added
            }

        except:
            response = {
                'error': 'Error.'
            }
    else:
        response = {
            'error': 'Not authorized.'
        }

    return HttpResponse(json.dumps(response), content_type='application/json')

def ajax_remove_tag(request):
    """View used to remove a tag related to a thread."""
    response = {}

    if request.user.is_staff:
        try:
            thread_number = int(request.POST['thread'])
            board_name = request.POST['board']
            tag = request.POST['tag']


# Path: archive_chan/models.py
# class Update(models.Model):
#     CURRENT = 0
#     FAILED = 1
#     COMPLETED = 2
# 
#     STATUS_CHOICES = (
#         (CURRENT, 'Started'),
#         (FAILED, 'Failed'),
#         (COMPLETED, 'Completed'),
#     )
# 
#     board = models.ForeignKey('Board')
#     status = models.SmallIntegerField(choices=STATUS_CHOICES, default=CURRENT)
#     start = models.DateTimeField()
#     end = models.DateTimeField(null=True)
# 
#     used_threads = models.IntegerField()
# 
#     total_time = models.FloatField(default=0)
#     wait_time = models.FloatField(default=0)
#     download_time = models.FloatField(default=0)
# 
#     processed_threads = models.IntegerField(default=0)
#     added_posts = models.IntegerField(default=0)
#     removed_posts = models.IntegerField(default=0)
# 
#     downloaded_images = models.IntegerField(default=0)
#     downloaded_thumbnails = models.IntegerField(default=0)
#     downloaded_threads = models.IntegerField(default=0)
# 
#     class Meta:
#         ordering = ['-start']
# 
# class Image(models.Model):
#     original_name = models.CharField(max_length=255)
#     post = models.OneToOneField('Post')
#     image = models.FileField(upload_to = "post_images", storage=fs) # It is impossible to use ImageField to store webm.
#     thumbnail = models.FileField(upload_to = "post_thumbnails", storage=fs)
# 
#     def get_extension(self):
#         name, extension = os.path.splitext(self.image.name)
#         return extension
# 
# 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 Tag(models.Model):
#     name = models.CharField(max_length=255, unique=True)
# 
#     def __str__(self):
#         return self.name
# 
# class TagToThread(models.Model):
#     thread = models.ForeignKey('Thread')
#     tag = models.ForeignKey('Tag')
#     automatically_added = models.BooleanField(default=False, editable=False)
#     save_time = models.DateTimeField(auto_now_add = True)
# 
#     def __str__(self):
#         return format('%s - %s' % (self.thread, self.tag))


# Path: archive_chan/views/api.py
import json
import archive_chan.lib.stats as stats
from django.db.models import Avg
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from django.views.generic.base import View
from archive_chan.models import Update, Image, Thread, Tag, TagToThread


                added = True
            else:
                added = False

            response = {
                'added': added
            }

        except:
            response = {
                'error': 'Error.'
            }
    else:
        response = {
            'error': 'Not authorized.'
        }

    return HttpResponse(json.dumps(response), content_type='application/json')

def ajax_remove_tag(request):
    """View used to remove a tag related to a thread."""
    response = {}

    if request.user.is_staff:
        try:
            thread_number = int(request.POST['thread'])
            board_name = request.POST['board']
            tag = request.POST['tag']


            TagToThread.objects.get(