summaryrefslogtreecommitdiffstats
path: root/kittystore/caching/mlist.py
blob: 16e993d194fed1bbc26e4197d396f9890680d2ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
"""
Cached values concerning mailing-lists
"""

import datetime
from urllib2 import HTTPError

import mailmanclient
from dateutil.parser import parse as date_parse
from mailman.interfaces.archiver import ArchivePolicy

from kittystore.caching import CachedValue


class CompatibleMList(object):
    """
    Convert the List object returned by mailmanclient to an
    IMailingList-compatible object
    """
    converters = {
        "created_at": date_parse,
        "archive_policy": lambda p: getattr(ArchivePolicy, p),
    }
    def __init__(self, mlist, props):
        for prop in props:
            try:
                value = getattr(mlist, prop)
            except AttributeError:
                value = mlist.settings[prop]
            if prop in self.converters:
                value = self.converters[prop](value)
            setattr(self, prop, value)


class ListProperties(CachedValue):

    def on_new_message(self, store, mlist, message):
        l = store.get_list(mlist.fqdn_listname)
        if isinstance(mlist, mailmanclient._client._List):
            mlist = CompatibleMList(mlist, l.mailman_props)
        for propname in l.mailman_props:
            setattr(l, propname, getattr(mlist, propname))

    def daily(self, store):
        return self.refresh(store)

    def refresh(self, store):
        try:
            mm_client = self._get_mailman_client(store.settings)
        except HTTPError:
            return # Can't refresh at this time
        for list_name in store.get_list_names():
            try:
                mm_mlist = mm_client.get_list(list_name)
            except (HTTPError, mailmanclient.MailmanConnectionError):
                continue
            if mm_mlist:
                self.on_new_message(store, mm_mlist, None)


class RecentListActivity(CachedValue):
    """
    Refresh the recent_participants_count and recent_threads_count properties.
    """

    def on_new_message(self, store, mlist, message):
        l = store.get_list(mlist.fqdn_listname)
        begin_date = l.get_recent_dates()[0]
        if message.date >= begin_date:
            l.refresh_cache()

    def daily(self, store):
        return self.refresh(store)

    def refresh(self, store):
        for mlist in store.get_lists():
            mlist.refresh_cache()


class MonthlyListActivity(CachedValue):
    """
    Refresh the monthly participants_count and threads_count values.
    """

    def on_new_message(self, store, mlist, message):
        l = store.get_list(mlist.fqdn_listname)
        activity = l.get_month_activity(message.date.year, message.date.month)
        activity.refresh()

    def refresh(self, store):
        for mlist in store.get_lists():
            mlist.clear_month_activity()