# Path: django_simple_forum/models.py
# class Topic(models.Model):
#     title = models.CharField(max_length=2000)
#     description = models.TextField()
#     created_by = models.ForeignKey(User)
#     status = models.CharField(choices=STATUS, max_length=10)
#     category = models.ForeignKey(ForumCategory)
#     created_on = models.DateTimeField(auto_now=True)
#     updated_on = models.DateTimeField(auto_now=True)
#     no_of_views = models.IntegerField(default='0')
#     slug = models.SlugField(max_length=1000)
#     tags = models.ManyToManyField(Tags)
#     no_of_likes = models.IntegerField(default='0')
#     votes = models.ManyToManyField(Vote)
# 
#     def get_comments(self):
#         comments = Comment.objects.filter(topic=self, parent=None)
#         return comments
# 
#     def get_all_comments(self):
#         comments = Comment.objects.filter(topic=self)
#         return comments
# 
#     def get_last_comment(self):
#         comments = Comment.objects.filter(topic=self).order_by('-updated_on').first()
#         return comments
# 
#     def get_topic_users(self):
#         comment_user_ids = Comment.objects.filter(topic=self).values_list('commented_by', flat=True)
#         liked_users_ids = UserTopics.objects.filter(topic=self, is_like=True).values_list('user', flat=True)
#         followed_users = UserTopics.objects.filter(topic=self, is_followed=True).values_list('user', flat=True)
#         all_users = list(comment_user_ids) + list(liked_users_ids) + list(followed_users) + [self.created_by.id]
#         users = UserProfile.objects.filter(user_id__in=set(all_users))
#         return users
# 
#     # def get_total_of_votes(self):
#     #     no_of_votes = self.no_of_votes + self.no_of_down_votes
#     #     return no_of_votes
# 
#     def up_votes_count(self):
#         return self.votes.filter(type="U").count()
# 
#     def down_votes_count(self):
#         return self.votes.filter(type="D").count()  
# 
#     def __str__(self):
#         return self.title      


from django.shortcuts import redirect, get_object_or_404
from django.core.urlresolvers import reverse
from django.contrib.auth import logout
from django_simple_forum.models import Topic


class AdminMixin(object):

    def dispatch(self, request, *args, **kwargs):
        user = self.request.user
        if not user.is_superuser:
            return redirect(reverse('django_simple_forum:topic_list'))
        return super(AdminMixin, self).dispatch(request, *args, **kwargs)


class CanUpdateTopicMixin(object):

    def dispatch(self, request, *args, **kwargs):
        user = self.request.user
        if user.is_anonymous():
            return redirect(reverse("django_simple_forum:topic_list"))
        pk = kwargs.get("slug")

# Path: django_simple_forum/models.py
# class Topic(models.Model):
#     title = models.CharField(max_length=2000)
#     description = models.TextField()
#     created_by = models.ForeignKey(User)
#     status = models.CharField(choices=STATUS, max_length=10)
#     category = models.ForeignKey(ForumCategory)
#     created_on = models.DateTimeField(auto_now=True)
#     updated_on = models.DateTimeField(auto_now=True)
#     no_of_views = models.IntegerField(default='0')
#     slug = models.SlugField(max_length=1000)
#     tags = models.ManyToManyField(Tags)
#     no_of_likes = models.IntegerField(default='0')
#     votes = models.ManyToManyField(Vote)
# 
#     def get_comments(self):
#         comments = Comment.objects.filter(topic=self, parent=None)
#         return comments
# 
#     def get_all_comments(self):
#         comments = Comment.objects.filter(topic=self)
#         return comments
# 
#     def get_last_comment(self):
#         comments = Comment.objects.filter(topic=self).order_by('-updated_on').first()
#         return comments
# 
#     def get_topic_users(self):
#         comment_user_ids = Comment.objects.filter(topic=self).values_list('commented_by', flat=True)
#         liked_users_ids = UserTopics.objects.filter(topic=self, is_like=True).values_list('user', flat=True)
#         followed_users = UserTopics.objects.filter(topic=self, is_followed=True).values_list('user', flat=True)
#         all_users = list(comment_user_ids) + list(liked_users_ids) + list(followed_users) + [self.created_by.id]
#         users = UserProfile.objects.filter(user_id__in=set(all_users))
#         return users
# 
#     # def get_total_of_votes(self):
#     #     no_of_votes = self.no_of_votes + self.no_of_down_votes
#     #     return no_of_votes
# 
#     def up_votes_count(self):
#         return self.votes.filter(type="U").count()
# 
#     def down_votes_count(self):
#         return self.votes.filter(type="D").count()  
# 
#     def __str__(self):
#         return self.title      


# Path: django_simple_forum/mixins.py
from django.shortcuts import redirect, get_object_or_404
from django.core.urlresolvers import reverse
from django.contrib.auth import logout
from django_simple_forum.models import Topic



class AdminMixin(object):

    def dispatch(self, request, *args, **kwargs):
        user = self.request.user
        if not user.is_superuser:
            return redirect(reverse('django_simple_forum:topic_list'))
        return super(AdminMixin, self).dispatch(request, *args, **kwargs)


class CanUpdateTopicMixin(object):

    def dispatch(self, request, *args, **kwargs):
        user = self.request.user
        if user.is_anonymous():
            return redirect(reverse("django_simple_forum:topic_list"))
        pk = kwargs.get("slug")

        self.object = get_object_or_404(Topic, slug=pk)