aboutsummaryrefslogtreecommitdiff
path: root/src/django_pgpmailman/plugin.py
blob: 77ed5c80e3e1616276cea10dfddf189de9d5ad0c (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
# -*- coding: utf-8 -*-
# Copyright (C) 2017 Jan Jancar
#
# This file is a part of the Django Mailman PGP plugin.
#
# This program 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.
#
# This program 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
# this program.  If not, see <http://www.gnu.org/licenses/>.
from operator import itemgetter

from django.conf import settings
from django_mailman3.lib.mailman import get_mailman_client
from mailmanclient._client import Plugin

from django_pgpmailman.models import PGPMailingList


class PGPPlugin(Plugin):
    def __init__(self, base_plugin):
        super(PGPPlugin, self).__init__(base_plugin.connection,
                                        base_plugin.name,
                                        base_plugin.rest_data)
        self._base_plugin = base_plugin

    @property
    def lists(self):
        response, content = self.call('lists')
        if 'entries' not in content:
            return []
        return [PGPMailingList(self._connection, entry['self_link'], entry) for
                entry in sorted(content['entries'], key=itemgetter('list_id'))]

    def get_list(self, list_identifier):
        response, content = self.call('lists/{}'.format(list_identifier))
        return PGPMailingList(self._connection, content['self_link', content])


def get_pgp_plugin():
    return PGPPlugin(
            get_mailman_client().get_plugin(settings.MAILMAN_PGP_PLUGIN_NAME))