# Path: hyperkitty/lib/mailman.py
# def get_mailman_client():
#     # easier to patch during unit tests
#     client = MailmanClient('%s/3.0' %
#                 settings.MAILMAN_REST_SERVER,
#                 settings.MAILMAN_API_USER,
#                 settings.MAILMAN_API_PASS)
#     return client


import datetime
import pytz
import dateutil.parser
import logging
from urllib2 import HTTPError
from django.conf import settings
from django.db import models, IntegrityError
from django.db.models.signals import (
    post_init, pre_save, post_save, pre_delete, post_delete)
from django.contrib import admin
from django.dispatch import receiver
from django.utils.timezone import now, utc
from django.core.cache.utils import make_template_fragment_key
from django.contrib.auth import get_user_model
from paintstore.fields import ColorPickerField
from enum import Enum
from mailmanclient import MailmanConnectionError
from hyperkitty.lib.mailman import get_mailman_client
from hyperkitty.lib.analysis import compute_thread_order_and_depth
from hyperkitty.lib.cache import cache
    from hyperkitty.lib.utils import get_message_id_hash # circular import
    def __unicode__(self):
        return u'%s' % (unicode(self.user))

    @property
    def emails(self):
        return Email.objects.filter(
            sender__address__in=self.addresses).order_by("date")

    @property
    def addresses(self):
        addresses = set([self.user.email,])
        mm_user = self.get_mailman_user()
        if mm_user:
            # TODO: caching?
            addresses.update(mm_user.addresses)
        return list(sorted(addresses))

    def get_votes_in_list(self, list_name):
        # TODO: Caching ?
        votes = self.user.votes.filter(email__mailinglist__name=list_name)
        likes = votes.filter(value=1).count()
        dislikes = votes.filter(value=-1).count()
        return likes, dislikes

    def get_mailman_user(self):
        # Only cache the user_id, not the whole user instance, because
        # mailmanclient is not pickle-safe
        cache_key = "User:%s:mailman_user_id" % self.id
        user_id = cache.get(cache_key)
        try:

# Path: hyperkitty/lib/mailman.py
# def get_mailman_client():
#     # easier to patch during unit tests
#     client = MailmanClient('%s/3.0' %
#                 settings.MAILMAN_REST_SERVER,
#                 settings.MAILMAN_API_USER,
#                 settings.MAILMAN_API_PASS)
#     return client


# Path: hyperkitty/models.py
import datetime
import pytz
import dateutil.parser
import logging
from urllib2 import HTTPError
from django.conf import settings
from django.db import models, IntegrityError
from django.db.models.signals import (
    post_init, pre_save, post_save, pre_delete, post_delete)
from django.contrib import admin
from django.dispatch import receiver
from django.utils.timezone import now, utc
from django.core.cache.utils import make_template_fragment_key
from django.contrib.auth import get_user_model
from paintstore.fields import ColorPickerField
from enum import Enum
from mailmanclient import MailmanConnectionError
from hyperkitty.lib.mailman import get_mailman_client
from hyperkitty.lib.analysis import compute_thread_order_and_depth
from hyperkitty.lib.cache import cache
    from hyperkitty.lib.utils import get_message_id_hash # circular import

    def __unicode__(self):
        return u'%s' % (unicode(self.user))

    @property
    def emails(self):
        return Email.objects.filter(
            sender__address__in=self.addresses).order_by("date")

    @property
    def addresses(self):
        addresses = set([self.user.email,])
        mm_user = self.get_mailman_user()
        if mm_user:
            # TODO: caching?
            addresses.update(mm_user.addresses)
        return list(sorted(addresses))

    def get_votes_in_list(self, list_name):
        # TODO: Caching ?
        votes = self.user.votes.filter(email__mailinglist__name=list_name)
        likes = votes.filter(value=1).count()
        dislikes = votes.filter(value=-1).count()
        return likes, dislikes

    def get_mailman_user(self):
        # Only cache the user_id, not the whole user instance, because
        # mailmanclient is not pickle-safe
        cache_key = "User:%s:mailman_user_id" % self.id
        user_id = cache.get(cache_key)
        try:

            mm_client = get_mailman_client()