# 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/lib/compat.py
# def get_list_by_name(list_name, domain):
#     matching = list(MailingList.objects.filter(name__startswith=list_name+"@"))
#     if len(matching) == 0: # no candidate found
#         raise Http404("No archived mailinglist by that name")
#     if len(matching) == 1: # only one candidate
#         return matching[0]
# 
#     # more than one result, try using the hostname
#     domain = PORT_IN_URL.sub('', domain)
#     list_fqdn = "%s@%s" % (list_name, domain)
#     try:
#         return MailingList.objects.get(name=list_fqdn)
#     except MailingList.DoesNotExist:
#         # return the first match, arbitrarily
#         return matching[0]
# 
# def month_name_to_num(month_name):
#     """map month names to months numbers"""
#     today = datetime.date.today()
#     months = dict( (today.replace(month=num).strftime('%B'), num)
#                    for num in range(1, 12) )
#     return months[month_name]


import os
import mailbox
import datetime
import tempfile
import gzip
from cStringIO import StringIO
from django.core.urlresolvers import reverse
from django.http import HttpResponse, Http404
from django.shortcuts import redirect
from hyperkitty.models import Email
from hyperkitty.lib.compat import get_list_by_name, month_name_to_num
# -*- coding: utf-8 -*-
# Copyright (C) 1998-2012 by the Free Software Foundation, Inc.
#
# This file is part of HyperKitty.
#
# 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 with_statement, absolute_import, unicode_literals, print_function





def summary(request, list_name=None):
    if list_name is None:
        return redirect(reverse('hk_root'))

# 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/lib/compat.py
# def get_list_by_name(list_name, domain):
#     matching = list(MailingList.objects.filter(name__startswith=list_name+"@"))
#     if len(matching) == 0: # no candidate found
#         raise Http404("No archived mailinglist by that name")
#     if len(matching) == 1: # only one candidate
#         return matching[0]
# 
#     # more than one result, try using the hostname
#     domain = PORT_IN_URL.sub('', domain)
#     list_fqdn = "%s@%s" % (list_name, domain)
#     try:
#         return MailingList.objects.get(name=list_fqdn)
#     except MailingList.DoesNotExist:
#         # return the first match, arbitrarily
#         return matching[0]
# 
# def month_name_to_num(month_name):
#     """map month names to months numbers"""
#     today = datetime.date.today()
#     months = dict( (today.replace(month=num).strftime('%B'), num)
#                    for num in range(1, 12) )
#     return months[month_name]


# Path: hyperkitty/views/compat.py
import os
import mailbox
import datetime
import tempfile
import gzip
from cStringIO import StringIO
from django.core.urlresolvers import reverse
from django.http import HttpResponse, Http404
from django.shortcuts import redirect
from hyperkitty.models import Email
from hyperkitty.lib.compat import get_list_by_name, month_name_to_num

# -*- coding: utf-8 -*-
# Copyright (C) 1998-2012 by the Free Software Foundation, Inc.
#
# This file is part of HyperKitty.
#
# 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 with_statement, absolute_import, unicode_literals, print_function





def summary(request, list_name=None):
    if list_name is None:
        return redirect(reverse('hk_root'))

    mlist = get_list_by_name(list_name, request.get_host())