# Path: hyperkitty/models.py
# class Email(models.Model):
#     """
#     An archived email, from a mailing-list. It is identified by both the list
#     name and the message id.
#     """
#     mailinglist = models.ForeignKey("MailingList", related_name="emails")
#     message_id = models.CharField(max_length=255, db_index=True)
#     message_id_hash = models.CharField(max_length=255, db_index=True)
#     sender = models.ForeignKey("Sender", related_name="emails")
#     subject = models.CharField(max_length="512", db_index=True)
#     content = models.TextField()
#     date = models.DateTimeField(db_index=True)
#     timezone = models.SmallIntegerField()
#     in_reply_to = models.CharField(
#         max_length=255, null=True, blank=True, db_index=True)
#     parent = models.ForeignKey("self",
#         blank=True, null=True, on_delete=models.SET_NULL,
#         related_name="children")
#     thread = models.ForeignKey("Thread", related_name="emails")
#     archived_date = models.DateTimeField(auto_now_add=True, db_index=True)
#     thread_depth = models.IntegerField(default=0)
#     thread_order = models.IntegerField(default=0, db_index=True)
# 
#     class Meta:
#         unique_together = ("mailinglist", "message_id")
# 
# 
#     def get_votes(self):
#         return get_votes(self)
# 
#     def vote(self, value, user):
#         # Checks if the user has already voted for this message.
#         existing = self.votes.filter(user=user).first()
#         if existing is not None and existing.value == value:
#             return # Vote already recorded (should I raise an exception?)
#         if value not in (0, 1, -1):
#             raise ValueError("A vote can only be +1 or -1 (or 0 to cancel)")
#         if existing is not None:
#             # vote changed or cancelled
#             if value == 0:
#                 existing.delete()
#             else:
#                 existing.value = value
#                 existing.save()
#         else:
#             # new vote
#             vote = Vote(email=self, user=user, value=value)
#             vote.save()
# 
#     def set_parent(self, parent):
#         if self.id == parent.id:
#             raise ValueError("An email can't be its own parent")
#         # Compute the subthread
#         subthread = [self]
#         def _collect_children(current_email):
#             children = list(current_email.children.all())
#             if not children:
#                 return
#             subthread.extend(children)
#             for child in children:
#                 _collect_children(child)
#         _collect_children(self)
#         # now set my new parent value
#         old_parent_id = self.parent_id
#         self.parent = parent
#         self.save(update_fields=["parent_id"])
#         # If my future parent is in my current subthread, I need to set its
#         # parent to my current parent
#         if parent in subthread:
#             parent.parent_id = old_parent_id
#             parent.save(update_fields=["parent_id"])
#             # do it after setting the new parent_id to avoid having two
#             # parent_ids set to None at the same time (IntegrityError)
#         if self.thread_id != parent.thread_id:
#             # we changed the thread, reattach the subthread
#             former_thread = self.thread
#             for child in subthread:
#                 child.thread = parent.thread
#                 child.save(update_fields=["thread_id"])
#                 if child.date > parent.thread.date_active:
#                     parent.thread.date_active = child.date
#             parent.thread.save()
#             # if we were the starting email, or former thread may be empty
#             if former_thread.emails.count() == 0:
#                 former_thread.delete()
#         compute_thread_order_and_depth(parent.thread)


from haystack import indexes
from haystack.query import SearchQuerySet
from haystack.management.commands.update_index import \
    Command as UpdateIndexCommand
from hyperkitty.models import Email
# HyperKitty is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# HyperKitty is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# HyperKitty.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: Aurelien Bompard <abompard@fedoraproject.org>
#

from __future__ import absolute_import, unicode_literals, print_function



class EmailIndex(indexes.SearchIndex, indexes.Indexable):

    text = indexes.CharField(document=True, use_template=True)
    mailinglist = indexes.CharField(model_attr='mailinglist__name')
    subject = indexes.CharField(model_attr='subject', boost=1.25)
    date = indexes.DateTimeField(model_attr='date')
    sender = indexes.CharField(model_attr='sender__name', boost=1.125)
    archived_date = indexes.DateTimeField(model_attr='archived_date')

    def get_model(self):

# Path: hyperkitty/models.py
# class Email(models.Model):
#     """
#     An archived email, from a mailing-list. It is identified by both the list
#     name and the message id.
#     """
#     mailinglist = models.ForeignKey("MailingList", related_name="emails")
#     message_id = models.CharField(max_length=255, db_index=True)
#     message_id_hash = models.CharField(max_length=255, db_index=True)
#     sender = models.ForeignKey("Sender", related_name="emails")
#     subject = models.CharField(max_length="512", db_index=True)
#     content = models.TextField()
#     date = models.DateTimeField(db_index=True)
#     timezone = models.SmallIntegerField()
#     in_reply_to = models.CharField(
#         max_length=255, null=True, blank=True, db_index=True)
#     parent = models.ForeignKey("self",
#         blank=True, null=True, on_delete=models.SET_NULL,
#         related_name="children")
#     thread = models.ForeignKey("Thread", related_name="emails")
#     archived_date = models.DateTimeField(auto_now_add=True, db_index=True)
#     thread_depth = models.IntegerField(default=0)
#     thread_order = models.IntegerField(default=0, db_index=True)
# 
#     class Meta:
#         unique_together = ("mailinglist", "message_id")
# 
# 
#     def get_votes(self):
#         return get_votes(self)
# 
#     def vote(self, value, user):
#         # Checks if the user has already voted for this message.
#         existing = self.votes.filter(user=user).first()
#         if existing is not None and existing.value == value:
#             return # Vote already recorded (should I raise an exception?)
#         if value not in (0, 1, -1):
#             raise ValueError("A vote can only be +1 or -1 (or 0 to cancel)")
#         if existing is not None:
#             # vote changed or cancelled
#             if value == 0:
#                 existing.delete()
#             else:
#                 existing.value = value
#                 existing.save()
#         else:
#             # new vote
#             vote = Vote(email=self, user=user, value=value)
#             vote.save()
# 
#     def set_parent(self, parent):
#         if self.id == parent.id:
#             raise ValueError("An email can't be its own parent")
#         # Compute the subthread
#         subthread = [self]
#         def _collect_children(current_email):
#             children = list(current_email.children.all())
#             if not children:
#                 return
#             subthread.extend(children)
#             for child in children:
#                 _collect_children(child)
#         _collect_children(self)
#         # now set my new parent value
#         old_parent_id = self.parent_id
#         self.parent = parent
#         self.save(update_fields=["parent_id"])
#         # If my future parent is in my current subthread, I need to set its
#         # parent to my current parent
#         if parent in subthread:
#             parent.parent_id = old_parent_id
#             parent.save(update_fields=["parent_id"])
#             # do it after setting the new parent_id to avoid having two
#             # parent_ids set to None at the same time (IntegrityError)
#         if self.thread_id != parent.thread_id:
#             # we changed the thread, reattach the subthread
#             former_thread = self.thread
#             for child in subthread:
#                 child.thread = parent.thread
#                 child.save(update_fields=["thread_id"])
#                 if child.date > parent.thread.date_active:
#                     parent.thread.date_active = child.date
#             parent.thread.save()
#             # if we were the starting email, or former thread may be empty
#             if former_thread.emails.count() == 0:
#                 former_thread.delete()
#         compute_thread_order_and_depth(parent.thread)


# Path: hyperkitty/search_indexes.py
from haystack import indexes
from haystack.query import SearchQuerySet
from haystack.management.commands.update_index import \
    Command as UpdateIndexCommand
from hyperkitty.models import Email

# HyperKitty is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# HyperKitty is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# HyperKitty.  If not, see <http://www.gnu.org/licenses/>.
#
# Author: Aurelien Bompard <abompard@fedoraproject.org>
#

from __future__ import absolute_import, unicode_literals, print_function



class EmailIndex(indexes.SearchIndex, indexes.Indexable):

    text = indexes.CharField(document=True, use_template=True)
    mailinglist = indexes.CharField(model_attr='mailinglist__name')
    subject = indexes.CharField(model_attr='subject', boost=1.25)
    date = indexes.DateTimeField(model_attr='date')
    sender = indexes.CharField(model_attr='sender__name', boost=1.125)
    archived_date = indexes.DateTimeField(model_attr='archived_date')

    def get_model(self):

        return Email