aboutsummaryrefslogtreecommitdiff
path: root/src/django_pgpmailman/plugin.py
blob: de6cbcc0d8d80c609139b54701ea3324ab8b24cd (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
# -*- 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 mailmanclient._client import Plugin, Client

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/%s' % list_identifier)
        return PGPMailingList(self._connection, content['self_link'], content)


plugin = None


def get_pgp_plugin():
    global plugin
    if not plugin:
        client = Client('%s/3.1' %
                        settings.MAILMAN_REST_API_URL,
                        settings.MAILMAN_REST_API_USER,
                        settings.MAILMAN_REST_API_PASS)
        plugin = PGPPlugin(client.get_plugin(settings.MAILMAN_PGP_PLUGIN_NAME))
    return plugin