# Path: hyperkitty/models.py
# class MailingList(models.Model):
#     """
#     An archived mailing-list.
#     """
#     name = models.CharField(max_length=254, primary_key=True)
#     display_name = models.CharField(max_length=255)
#     description = models.TextField()
#     subject_prefix = models.CharField(max_length=255)
#     archive_policy = models.IntegerField(
#         choices=[ (p.value, p.name) for p in ArchivePolicy ],
#         default=ArchivePolicy.public.value)
#     created_at = models.DateTimeField(default=now)
# 
#     MAILMAN_ATTRIBUTES = (
#         "display_name", "description", "subject_prefix",
#         "archive_policy", "created_at",
#     )
# 
#     @property
#     def is_private(self):
#         return self.archive_policy == ArchivePolicy.private.value
# 
#     @property
#     def is_new(self):
#         return self.created_at and \
#                 now() - self.created_at <= datetime.timedelta(days=30)
# 
#     def get_recent_dates(self):
#         today = now()
#         #today -= datetime.timedelta(days=400) #debug
#         # the upper boundary is excluded in the search, add one day
#         end_date = today + datetime.timedelta(days=1)
#         begin_date = end_date - datetime.timedelta(days=32)
#         return begin_date, end_date
# 
#     def get_participants_count_between(self, begin_date, end_date):
#         # We filter on emails dates instead of threads dates because that would
#         # also include last month's participants when threads carry from one
#         # month to the next
#         # TODO: caching?
#         return self.emails.filter(
#                 date__gte=begin_date, date__lt=end_date
#             ).values("sender_id").distinct().count()
# 
#     def get_threads_between(self, begin_date, end_date):
#         return self.threads.filter(
#                 starting_email__date__lt=end_date,
#                 date_active__gte=begin_date
#             ).order_by("-date_active")
# 
#     @property
#     def recent_participants_count(self):
#         begin_date, end_date = self.get_recent_dates()
#         return cache.get_or_set(
#             "MailingList:%s:recent_participants_count" % self.name,
#             lambda: self.get_participants_count_between(begin_date, end_date),
#             3600 * 6) # 6 hours
# 
#     @property
#     def recent_threads(self):
#         begin_date, end_date = self.get_recent_dates()
#         return cache.get_or_set(
#             "MailingList:%s:recent_threads" % self.name,
#             lambda: self.get_threads_between(begin_date, end_date),
#             3600 * 6) # 6 hours
# 
#     def get_participants_count_for_month(self, year, month):
#         def _get_value():
#             begin_date = datetime.datetime(year, month, 1, tzinfo=utc)
#             end_date = begin_date + datetime.timedelta(days=32)
#             end_date = end_date.replace(day=1)
#             return self.get_participants_count_between(begin_date, end_date)
#         return cache.get_or_set(
#             "MailingList:%s:p_count_for:%s:%s" % (self.name, year, month),
#             _get_value, None)
# 
#     @property
#     def top_posters(self):
#         begin_date, end_date = self.get_recent_dates()
#         query = Sender.objects.filter(
#             emails__mailinglist=self,
#             emails__date__gte=begin_date,
#             emails__date__lt=end_date,
#             ).annotate(count=models.Count("emails")
#             ).order_by("-count")
#         # Because of South, ResultSets are not pickleizable directly, they must
#         # be converted to lists (there's an extra field without the _deferred
#         # attribute that causes tracebacks)
#         return cache.get_or_set(
#             "MailingList:%s:top_posters" % self.name,
#             lambda: list(query[:5]),
#             3600 * 6) # 6 hours
#         # It's not actually necessary to convert back to instances since it's
#         # only used in templates where access to instance attributes or
#         # dictionnary keys is identical
#         #return [ Sender.objects.get(address=data["address"]) for data in sender_ids ]
#         #return sender_ids
# 
#     def update_from_mailman(self):
#         try:
#             client = get_mailman_client()
#             mm_list = client.get_list(self.name)
#         except MailmanConnectionError:
#             return
#         except HTTPError:
#             return # can't update at this time
#         if not mm_list:
#             return
#         converters = {
#             "created_at": dateutil.parser.parse,
#             "archive_policy": lambda p: ArchivePolicy[p].value,
#         }
#         for propname in self.MAILMAN_ATTRIBUTES:
#             try:
#                 value = getattr(mm_list, propname)
#             except AttributeError:
#                 value = mm_list.settings[propname]
#             if propname in converters:
#                 value = converters[propname](value)
#             setattr(self, propname, value)
#         self.save()


import re
import datetime
from django.http import Http404
from hyperkitty.models import MailingList
#-*- 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 absolute_import, unicode_literals, print_function



PORT_IN_URL = re.compile(r':\d+$')


def get_list_by_name(list_name, domain):

# Path: hyperkitty/models.py
# class MailingList(models.Model):
#     """
#     An archived mailing-list.
#     """
#     name = models.CharField(max_length=254, primary_key=True)
#     display_name = models.CharField(max_length=255)
#     description = models.TextField()
#     subject_prefix = models.CharField(max_length=255)
#     archive_policy = models.IntegerField(
#         choices=[ (p.value, p.name) for p in ArchivePolicy ],
#         default=ArchivePolicy.public.value)
#     created_at = models.DateTimeField(default=now)
# 
#     MAILMAN_ATTRIBUTES = (
#         "display_name", "description", "subject_prefix",
#         "archive_policy", "created_at",
#     )
# 
#     @property
#     def is_private(self):
#         return self.archive_policy == ArchivePolicy.private.value
# 
#     @property
#     def is_new(self):
#         return self.created_at and \
#                 now() - self.created_at <= datetime.timedelta(days=30)
# 
#     def get_recent_dates(self):
#         today = now()
#         #today -= datetime.timedelta(days=400) #debug
#         # the upper boundary is excluded in the search, add one day
#         end_date = today + datetime.timedelta(days=1)
#         begin_date = end_date - datetime.timedelta(days=32)
#         return begin_date, end_date
# 
#     def get_participants_count_between(self, begin_date, end_date):
#         # We filter on emails dates instead of threads dates because that would
#         # also include last month's participants when threads carry from one
#         # month to the next
#         # TODO: caching?
#         return self.emails.filter(
#                 date__gte=begin_date, date__lt=end_date
#             ).values("sender_id").distinct().count()
# 
#     def get_threads_between(self, begin_date, end_date):
#         return self.threads.filter(
#                 starting_email__date__lt=end_date,
#                 date_active__gte=begin_date
#             ).order_by("-date_active")
# 
#     @property
#     def recent_participants_count(self):
#         begin_date, end_date = self.get_recent_dates()
#         return cache.get_or_set(
#             "MailingList:%s:recent_participants_count" % self.name,
#             lambda: self.get_participants_count_between(begin_date, end_date),
#             3600 * 6) # 6 hours
# 
#     @property
#     def recent_threads(self):
#         begin_date, end_date = self.get_recent_dates()
#         return cache.get_or_set(
#             "MailingList:%s:recent_threads" % self.name,
#             lambda: self.get_threads_between(begin_date, end_date),
#             3600 * 6) # 6 hours
# 
#     def get_participants_count_for_month(self, year, month):
#         def _get_value():
#             begin_date = datetime.datetime(year, month, 1, tzinfo=utc)
#             end_date = begin_date + datetime.timedelta(days=32)
#             end_date = end_date.replace(day=1)
#             return self.get_participants_count_between(begin_date, end_date)
#         return cache.get_or_set(
#             "MailingList:%s:p_count_for:%s:%s" % (self.name, year, month),
#             _get_value, None)
# 
#     @property
#     def top_posters(self):
#         begin_date, end_date = self.get_recent_dates()
#         query = Sender.objects.filter(
#             emails__mailinglist=self,
#             emails__date__gte=begin_date,
#             emails__date__lt=end_date,
#             ).annotate(count=models.Count("emails")
#             ).order_by("-count")
#         # Because of South, ResultSets are not pickleizable directly, they must
#         # be converted to lists (there's an extra field without the _deferred
#         # attribute that causes tracebacks)
#         return cache.get_or_set(
#             "MailingList:%s:top_posters" % self.name,
#             lambda: list(query[:5]),
#             3600 * 6) # 6 hours
#         # It's not actually necessary to convert back to instances since it's
#         # only used in templates where access to instance attributes or
#         # dictionnary keys is identical
#         #return [ Sender.objects.get(address=data["address"]) for data in sender_ids ]
#         #return sender_ids
# 
#     def update_from_mailman(self):
#         try:
#             client = get_mailman_client()
#             mm_list = client.get_list(self.name)
#         except MailmanConnectionError:
#             return
#         except HTTPError:
#             return # can't update at this time
#         if not mm_list:
#             return
#         converters = {
#             "created_at": dateutil.parser.parse,
#             "archive_policy": lambda p: ArchivePolicy[p].value,
#         }
#         for propname in self.MAILMAN_ATTRIBUTES:
#             try:
#                 value = getattr(mm_list, propname)
#             except AttributeError:
#                 value = mm_list.settings[propname]
#             if propname in converters:
#                 value = converters[propname](value)
#             setattr(self, propname, value)
#         self.save()


# Path: hyperkitty/lib/compat.py
import re
import datetime
from django.http import Http404
from hyperkitty.models import MailingList

#-*- 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 absolute_import, unicode_literals, print_function



PORT_IN_URL = re.compile(r':\d+$')


def get_list_by_name(list_name, domain):

    matching = list(MailingList.objects.filter(name__startswith=list_name+"@"))