From 874812298c1e29ff57a9929a91368914b2a0cf44 Mon Sep 17 00:00:00 2001
From: J08nY
Date: Fri, 11 Aug 2017 16:08:23 +0200
Subject: Setup some abstractions above PGP enabled mailing lists.
---
src/django_pgpmailman/models.py | 64 +++++++++++++++++++++++++++++++++++++
src/django_pgpmailman/plugin.py | 49 ++++++++++++++++++++++++++++
src/django_pgpmailman/views/list.py | 4 ++-
3 files changed, 116 insertions(+), 1 deletion(-)
create mode 100644 src/django_pgpmailman/models.py
create mode 100644 src/django_pgpmailman/plugin.py
(limited to 'src')
diff --git a/src/django_pgpmailman/models.py b/src/django_pgpmailman/models.py
new file mode 100644
index 0000000..f396e73
--- /dev/null
+++ b/src/django_pgpmailman/models.py
@@ -0,0 +1,64 @@
+# -*- 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 .
+from itertools import chain
+
+from mailmanclient._client import RESTObject, MailingList
+from pgpy import PGPKey
+from pgpy.errors import PGPError
+from six.moves.urllib_error import HTTPError
+
+
+class PGPMailingList(RESTObject):
+ _writable_properties = (
+ 'unsigned_msg_action', 'inline_pgp_action', 'expired_sig_action',
+ 'revoked_sig_action', 'invalid_sig_action', 'duplicate_sig_action',
+ 'strip_original_sig', 'sign_outgoing', 'nonencrypted_msg_action',
+ 'encrypt_outgoing', 'key_change_workflow', 'key_signing_allowed')
+ _read_only_properties = ('self_link', 'list_id')
+ _properties = chain(_writable_properties, _read_only_properties)
+
+ @property
+ def mlist(self):
+ return MailingList(self._connection, 'lists/{}'.format(self.list_id))
+
+ @property
+ def key(self):
+ try:
+ response, content = self._connection.call(self._url + '/key')
+ key, _ = PGPKey.from_blob(response['key'])
+ return key
+ except PGPError:
+ return None
+
+ @key.setter
+ def key(self, value):
+ str_key = str(value) if value else ''
+ try:
+ self._connection.call(self._url + '/key', data=dict(key=str_key),
+ method='PUT')
+ except HTTPError:
+ pass
+
+ @property
+ def pubkey(self):
+ try:
+ response, content = self._connection.call(self._url + '/pubkey')
+ key, _ = PGPKey.from_blob(response['key'])
+ return key
+ except PGPError:
+ return None
diff --git a/src/django_pgpmailman/plugin.py b/src/django_pgpmailman/plugin.py
new file mode 100644
index 0000000..77ed5c8
--- /dev/null
+++ b/src/django_pgpmailman/plugin.py
@@ -0,0 +1,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 .
+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))
diff --git a/src/django_pgpmailman/views/list.py b/src/django_pgpmailman/views/list.py
index 4bdfdff..0c0f367 100644
--- a/src/django_pgpmailman/views/list.py
+++ b/src/django_pgpmailman/views/list.py
@@ -20,7 +20,9 @@ from __future__ import absolute_import, unicode_literals
from django.shortcuts import render
+from django_pgpmailman.plugin import get_pgp_plugin
+
def pgp_list_index(request):
return render(request, 'django_pgpmailman/index.html',
- {'lists': []})
+ {'lists': get_pgp_plugin().lists})
--
cgit v1.2.3-70-g09d2