# Path: account/models.py
# class Account(models.Model):
#     user = models.ForeignKey(to='auth.User', related_name='accounts')
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     @property
#     def user_avatar(self):
#         fs = FileSystemStorage()
#         if fs.exists(self.user.username + '.jpg'):
#             return '/static/avatar/' + self.user.username + '.jpg'
#         else:
#             return '/static/img/profile/default.jpg'
# 
#     def is_following(self, follow):
#         return self.following.filter(follow=follow).exists()
# 
#     def unfollow(self, follow):
#         relationship = self.following.filter(follow=follow).first()
#         relationship.delete()
# 
#     @staticmethod
#     def get_by_user(user):
#         return Account.objects.filter(user=user).first()
# 
#     @staticmethod
#     def get_by_username(username):
#         return Account.objects.filter(user__username=username).first()
# 
# Path: link/models.py
# class Post(models.Model):
#     owner = models.ForeignKey(to='account.Account', related_name='own_posts')
#     publisher = models.ForeignKey(to='account.Account', related_name='posts')
#     origin = models.ForeignKey(to='link.Post', related_name='adds', null=True)
#     url = models.URLField()
#     type = models.CharField(max_length=200)
#     title = models.CharField(max_length=200)
#     summary = models.CharField(null=True, max_length=250)
#     text = models.TextField(null=True)
#     image_url = models.CharField(null=True, max_length=500)
#     tags = models.CharField(null=True, max_length=500)
#     publish_date = models.DateTimeField(null=True)
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     def set_metadata(self):
#         page_info = utils.get_page_info(self.url)
#         self.type = page_info['type']
#         self.title = page_info['title']
#         self.summary = page_info['summary']
#         self.text = page_info['text']
#         self.image_url = page_info['image']
#         self.tags = ','.join(page_info['tags'])
#         self.publish_date = page_info['publish_date']
# 
#     def get_tags_as_list(self):
#         return self.tags.split(',')
# 
#     def add_link(self, user):
#         new_post = copy.copy(self)
#         new_post.owner = user
#         new_post.origin = self
#         new_post.id = None
#         new_post.save()
# 
#     @staticmethod
#     def feeds(username):
#         follower_ids = Relationship.objects.filter(
#             owner__user__username=username).values_list('follow_id', flat=True).distinct()
#         qs_reacted = Reaction.objects.filter(post=models.OuterRef('pk'),
#                                              owner__user__username=username)
#         qs_added = Post.objects.filter(origin=models.OuterRef('pk'),
#                                        owner__user__username=username)
# 
#         return Post.objects.filter(
#             models.Q(owner_id__in=follower_ids) |
#             models.Q(owner__user__username=username)).annotate(
#             is_reacted=models.Exists(queryset=qs_reacted)).annotate(
#             is_added=models.Exists(queryset=qs_added)).order_by('-created_at')
# 
#     @staticmethod
#     def links_by_user(username, user=None):
#         qs_reactions = Reaction.objects.filter(post=models.OuterRef('pk'), owner=user)
#         qs_added = Post.objects.filter(origin=models.OuterRef('pk'), owner=user)
# 
#         return Post.objects.filter(owner__user__username=username).annotate(
#                     is_reacted=models.Exists(queryset=qs_reactions)).annotate(
#                     is_added=models.Exists(queryset=qs_added)).order_by('-created_at')
# 
#     def __str__(self):
#         return self.title


from django.db.models import Q, Value, CharField
from account.models import Account
from link.models import Post
        q_filters = Q()
        for word in self.words:
            q_filters.add(Q(tags__icontains=word), Q.OR)
        return q_filters

    def query_level(self, q_filter_link=None, q_filter_user=None):
        levels = [
            {'link': Q(owner=self.owner), 'user': Q(pk=(self.owner.pk if self.owner else None))},
            {'link': Q(owner__followers__owner=self.owner), 'user': Q(followers__owner=self.owner)},
            {'link': Q(owner__followers__owner__followers__owner=self.owner),
             'user': Q(followers__owner__followers__owner=self.owner)},
        ]
        last_level = {'link': ~Q(levels[0]['link'] | levels[1]['link'] | levels[2]['link']),
                      'user': ~Q(levels[0]['user'] | levels[1]['user'] | levels[2]['user'])}

        if self.owner:
            for level in levels:
                self.results += self.process_query(q_filter_link, q_filter_user, level)
            self.results += self.process_query(q_filter_link, q_filter_user, last_level)
        else:
            self.results = self.process_query(q_filter_link, q_filter_user)

    @staticmethod
    def process_query(q_filter_link=None, q_filter_user=None, level=None):
        level_results = []

        if level is None:
            level = {'link': Q(), 'user': Q()}

        if q_filter_link:

# Path: account/models.py
# class Account(models.Model):
#     user = models.ForeignKey(to='auth.User', related_name='accounts')
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     @property
#     def user_avatar(self):
#         fs = FileSystemStorage()
#         if fs.exists(self.user.username + '.jpg'):
#             return '/static/avatar/' + self.user.username + '.jpg'
#         else:
#             return '/static/img/profile/default.jpg'
# 
#     def is_following(self, follow):
#         return self.following.filter(follow=follow).exists()
# 
#     def unfollow(self, follow):
#         relationship = self.following.filter(follow=follow).first()
#         relationship.delete()
# 
#     @staticmethod
#     def get_by_user(user):
#         return Account.objects.filter(user=user).first()
# 
#     @staticmethod
#     def get_by_username(username):
#         return Account.objects.filter(user__username=username).first()
# 
# Path: link/models.py
# class Post(models.Model):
#     owner = models.ForeignKey(to='account.Account', related_name='own_posts')
#     publisher = models.ForeignKey(to='account.Account', related_name='posts')
#     origin = models.ForeignKey(to='link.Post', related_name='adds', null=True)
#     url = models.URLField()
#     type = models.CharField(max_length=200)
#     title = models.CharField(max_length=200)
#     summary = models.CharField(null=True, max_length=250)
#     text = models.TextField(null=True)
#     image_url = models.CharField(null=True, max_length=500)
#     tags = models.CharField(null=True, max_length=500)
#     publish_date = models.DateTimeField(null=True)
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     def set_metadata(self):
#         page_info = utils.get_page_info(self.url)
#         self.type = page_info['type']
#         self.title = page_info['title']
#         self.summary = page_info['summary']
#         self.text = page_info['text']
#         self.image_url = page_info['image']
#         self.tags = ','.join(page_info['tags'])
#         self.publish_date = page_info['publish_date']
# 
#     def get_tags_as_list(self):
#         return self.tags.split(',')
# 
#     def add_link(self, user):
#         new_post = copy.copy(self)
#         new_post.owner = user
#         new_post.origin = self
#         new_post.id = None
#         new_post.save()
# 
#     @staticmethod
#     def feeds(username):
#         follower_ids = Relationship.objects.filter(
#             owner__user__username=username).values_list('follow_id', flat=True).distinct()
#         qs_reacted = Reaction.objects.filter(post=models.OuterRef('pk'),
#                                              owner__user__username=username)
#         qs_added = Post.objects.filter(origin=models.OuterRef('pk'),
#                                        owner__user__username=username)
# 
#         return Post.objects.filter(
#             models.Q(owner_id__in=follower_ids) |
#             models.Q(owner__user__username=username)).annotate(
#             is_reacted=models.Exists(queryset=qs_reacted)).annotate(
#             is_added=models.Exists(queryset=qs_added)).order_by('-created_at')
# 
#     @staticmethod
#     def links_by_user(username, user=None):
#         qs_reactions = Reaction.objects.filter(post=models.OuterRef('pk'), owner=user)
#         qs_added = Post.objects.filter(origin=models.OuterRef('pk'), owner=user)
# 
#         return Post.objects.filter(owner__user__username=username).annotate(
#                     is_reacted=models.Exists(queryset=qs_reactions)).annotate(
#                     is_added=models.Exists(queryset=qs_added)).order_by('-created_at')
# 
#     def __str__(self):
#         return self.title


# Path: search/search.py
from django.db.models import Q, Value, CharField
from account.models import Account
from link.models import Post

        q_filters = Q()
        for word in self.words:
            q_filters.add(Q(tags__icontains=word), Q.OR)
        return q_filters

    def query_level(self, q_filter_link=None, q_filter_user=None):
        levels = [
            {'link': Q(owner=self.owner), 'user': Q(pk=(self.owner.pk if self.owner else None))},
            {'link': Q(owner__followers__owner=self.owner), 'user': Q(followers__owner=self.owner)},
            {'link': Q(owner__followers__owner__followers__owner=self.owner),
             'user': Q(followers__owner__followers__owner=self.owner)},
        ]
        last_level = {'link': ~Q(levels[0]['link'] | levels[1]['link'] | levels[2]['link']),
                      'user': ~Q(levels[0]['user'] | levels[1]['user'] | levels[2]['user'])}

        if self.owner:
            for level in levels:
                self.results += self.process_query(q_filter_link, q_filter_user, level)
            self.results += self.process_query(q_filter_link, q_filter_user, last_level)
        else:
            self.results = self.process_query(q_filter_link, q_filter_user)

    @staticmethod
    def process_query(q_filter_link=None, q_filter_user=None, level=None):
        level_results = []

        if level is None:
            level = {'link': Q(), 'user': Q()}

        if q_filter_link:

            level_results += Post.objects.filter(q_filter_link, level['link']).annotate(