# Path: django_simple_forum/models.py
# class ForumCategory(models.Model):
#     created_by = models.ForeignKey(User)
#     title = models.CharField(max_length=1000)
#     is_active = models.BooleanField(default=False)
#     color = models.CharField(max_length=20, default="#999999")
#     is_votable = models.BooleanField(default=False)
#     created_on = models.DateTimeField(auto_now_add=True)
#     slug = models.SlugField(max_length=1000)
#     description = models.TextField()
#     parent = models.ForeignKey('self', blank=True, null=True)
# 
#     def get_topics(self):
#         topics = Topic.objects.filter(category=self, status='Published')
#         return topics
# 
#     def __str__(self):
#         return self.title
# 
# class Tags(models.Model):
#     title = models.CharField(max_length=50, unique=True)
#     slug = models.CharField(max_length=50, unique=True)
# 
#     def get_topics(self):
#         topics = Topic.objects.filter(tags__in=[self], status='Published')
#         return topics
# 
# class Badge(models.Model):
#     title = models.CharField(max_length=50, unique=True)
#     slug = models.CharField(max_length=50, unique=True)
# 
#     def get_users(self):
#         user_profile = UserProfile.objects.filter(badges__in=[self])
#         return user_profile
# 
# class UserTopics(models.Model):
#     user = models.ForeignKey(User)
#     topic = models.ForeignKey(Topic)
#     is_followed = models.BooleanField(default=False)
#     followed_on = models.DateField(null=True, blank=True)
#     no_of_votes = models.IntegerField(default='0')
#     no_of_down_votes = models.IntegerField(default='0')
#     is_like = models.BooleanField(default=False)
# 
# class UserProfile(models.Model):
#     file_prepend = "forum_user/profilepics/"
#     
#     user = models.ForeignKey(User)
#     used_votes = models.IntegerField(default='0')
#     user_roles = models.CharField(choices=USER_ROLES, max_length=10)
#     badges = models.ManyToManyField(Badge)
#     profile_pic = models.FileField(
#         max_length=500, null=True, blank=True, upload_to=img_url)
#     send_mailnotifications = models.BooleanField(default=False)
# 
#     # need to add social details for a user if we implement socail login
# 
#     def get_no_of_up_votes(self):
#         user_topics = UserTopics.objects.filter(user=self.user)
#         votes = 0
#         for topic in user_topics:
#             votes += topic.no_of_votes
#         return votes
# 
#     def get_no_of_down_votes(self):
#         user_topics = UserTopics.objects.filter(user=self.user)
#         votes = 0
#         for topic in user_topics:
#             votes += topic.no_of_down_votes
#         return votes
# 
#     def get_topics(self):
#         topics = Topic.objects.filter(created_by=self.user)
#         return topics
# 
#     def get_followed_topics(self):
#         topics = UserTopics.objects.filter(user=self.user, is_followed=True)
#         topics = Topic.objects.filter(id__in=topics.values_list('topic', flat=True))
#         return topics
# 
#     def get_liked_topics(self):
#         topics = UserTopics.objects.filter(user=self.user, is_like=True)
#         topics = Topic.objects.filter(id__in=topics.values_list('topic', flat=True))
#         return topics
# 
#     def get_timeline(self):
#         timeline = Timeline.objects.filter(user=self.user).order_by('-created_on')
#         return timeline
# 
#     def get_user_topic_tags(self):
#         tags = Tags.objects.filter(id__in=self.get_topics().values_list('tags', flat=True))
#         return tags
# 
#     def get_user_topic_categories(self):
#         categories = ForumCategory.objects.filter(id__in=self.get_topics().values_list('category', flat=True))
#         return categories
#         # return []
# 
#     def get_user_suggested_topics(self):
#         categories = ForumCategory.objects.filter(id__in=self.get_topics().values_list('category', flat=True))
#         topics = Topic.objects.filter(category__id__in=categories.values_list('id', flat=True))
#         return topics
#         # return []


from django import template
from django_simple_forum.models import ForumCategory, Tags, Badge, UserTopics, UserProfile
from django.db.models import Count
    from django.contrib.auth import get_user_model
    from django.contrib.auth.models import User
def get_users():
    all_users = User.objects.annotate(num_topics=Count('topic')).order_by('-num_topics')[:10]
    return all_users


@register.assignment_tag()
def get_badges():
    all_badges = Badge.objects.filter()[:10]
    return all_badges


@register.filter
def is_topic_like(topic_id, user_id):
    user_topic = UserTopics.objects.filter(topic_id=topic_id, user_id=user_id).first()
    if user_topic:
        if user_topic.is_like:
            return True
    return False


@register.filter
def user_profile_pic(user_id):
    user = UserProfile.objects.filter(user_id=user_id).first()
    if user:
        return user.profile_pic
    return ''


@register.filter
def is_topic_followed(topic_id, user_id):

# Path: django_simple_forum/models.py
# class ForumCategory(models.Model):
#     created_by = models.ForeignKey(User)
#     title = models.CharField(max_length=1000)
#     is_active = models.BooleanField(default=False)
#     color = models.CharField(max_length=20, default="#999999")
#     is_votable = models.BooleanField(default=False)
#     created_on = models.DateTimeField(auto_now_add=True)
#     slug = models.SlugField(max_length=1000)
#     description = models.TextField()
#     parent = models.ForeignKey('self', blank=True, null=True)
# 
#     def get_topics(self):
#         topics = Topic.objects.filter(category=self, status='Published')
#         return topics
# 
#     def __str__(self):
#         return self.title
# 
# class Tags(models.Model):
#     title = models.CharField(max_length=50, unique=True)
#     slug = models.CharField(max_length=50, unique=True)
# 
#     def get_topics(self):
#         topics = Topic.objects.filter(tags__in=[self], status='Published')
#         return topics
# 
# class Badge(models.Model):
#     title = models.CharField(max_length=50, unique=True)
#     slug = models.CharField(max_length=50, unique=True)
# 
#     def get_users(self):
#         user_profile = UserProfile.objects.filter(badges__in=[self])
#         return user_profile
# 
# class UserTopics(models.Model):
#     user = models.ForeignKey(User)
#     topic = models.ForeignKey(Topic)
#     is_followed = models.BooleanField(default=False)
#     followed_on = models.DateField(null=True, blank=True)
#     no_of_votes = models.IntegerField(default='0')
#     no_of_down_votes = models.IntegerField(default='0')
#     is_like = models.BooleanField(default=False)
# 
# class UserProfile(models.Model):
#     file_prepend = "forum_user/profilepics/"
#     
#     user = models.ForeignKey(User)
#     used_votes = models.IntegerField(default='0')
#     user_roles = models.CharField(choices=USER_ROLES, max_length=10)
#     badges = models.ManyToManyField(Badge)
#     profile_pic = models.FileField(
#         max_length=500, null=True, blank=True, upload_to=img_url)
#     send_mailnotifications = models.BooleanField(default=False)
# 
#     # need to add social details for a user if we implement socail login
# 
#     def get_no_of_up_votes(self):
#         user_topics = UserTopics.objects.filter(user=self.user)
#         votes = 0
#         for topic in user_topics:
#             votes += topic.no_of_votes
#         return votes
# 
#     def get_no_of_down_votes(self):
#         user_topics = UserTopics.objects.filter(user=self.user)
#         votes = 0
#         for topic in user_topics:
#             votes += topic.no_of_down_votes
#         return votes
# 
#     def get_topics(self):
#         topics = Topic.objects.filter(created_by=self.user)
#         return topics
# 
#     def get_followed_topics(self):
#         topics = UserTopics.objects.filter(user=self.user, is_followed=True)
#         topics = Topic.objects.filter(id__in=topics.values_list('topic', flat=True))
#         return topics
# 
#     def get_liked_topics(self):
#         topics = UserTopics.objects.filter(user=self.user, is_like=True)
#         topics = Topic.objects.filter(id__in=topics.values_list('topic', flat=True))
#         return topics
# 
#     def get_timeline(self):
#         timeline = Timeline.objects.filter(user=self.user).order_by('-created_on')
#         return timeline
# 
#     def get_user_topic_tags(self):
#         tags = Tags.objects.filter(id__in=self.get_topics().values_list('tags', flat=True))
#         return tags
# 
#     def get_user_topic_categories(self):
#         categories = ForumCategory.objects.filter(id__in=self.get_topics().values_list('category', flat=True))
#         return categories
#         # return []
# 
#     def get_user_suggested_topics(self):
#         categories = ForumCategory.objects.filter(id__in=self.get_topics().values_list('category', flat=True))
#         topics = Topic.objects.filter(category__id__in=categories.values_list('id', flat=True))
#         return topics
#         # return []


# Path: django_simple_forum/templatetags/forum_tags.py
from django import template
from django_simple_forum.models import ForumCategory, Tags, Badge, UserTopics, UserProfile
from django.db.models import Count
    from django.contrib.auth import get_user_model
    from django.contrib.auth.models import User

def get_users():
    all_users = User.objects.annotate(num_topics=Count('topic')).order_by('-num_topics')[:10]
    return all_users


@register.assignment_tag()
def get_badges():
    all_badges = Badge.objects.filter()[:10]
    return all_badges


@register.filter
def is_topic_like(topic_id, user_id):
    user_topic = UserTopics.objects.filter(topic_id=topic_id, user_id=user_id).first()
    if user_topic:
        if user_topic.is_like:
            return True
    return False


@register.filter
def user_profile_pic(user_id):
    user = UserProfile.objects.filter(user_id=user_id).first()
    if user:
        return user.profile_pic
    return ''


@register.filter
def is_topic_followed(topic_id, user_id):

    user_topic = UserTopics.objects.filter(topic_id=topic_id, user_id=user_id).first()