From bfc7f14b1df86834abf395fdbf11ab54f076dca6 Mon Sep 17 00:00:00 2001 From: J08nY Date: Sun, 20 Aug 2017 22:48:09 +0200 Subject: Add basic user address index. --- src/django_pgpmailman/decorators.py | 20 +++++++++-- src/django_pgpmailman/models.py | 15 ++++++-- src/django_pgpmailman/plugin.py | 28 +++++++++++---- .../templates/django_pgpmailman/user/summary.html | 34 +++++++++++++++++- src/django_pgpmailman/urls.py | 5 +-- src/django_pgpmailman/views/list.py | 4 +-- src/django_pgpmailman/views/user.py | 40 +++++++++++++++++++--- 7 files changed, 126 insertions(+), 20 deletions(-) diff --git a/src/django_pgpmailman/decorators.py b/src/django_pgpmailman/decorators.py index 5f994b6..7e700a0 100644 --- a/src/django_pgpmailman/decorators.py +++ b/src/django_pgpmailman/decorators.py @@ -21,14 +21,14 @@ from django.http import Http404 from six import wraps from six.moves.urllib_error import HTTPError -from django_pgpmailman.plugin import get_pgp_plugin +from django_pgpmailman.plugin import get_plugin, get_client def list_view(fn): @wraps(fn) def wrapper(request, *args, **kwargs): try: - pgp_list = get_pgp_plugin().get_list(kwargs.pop('list_id')) + pgp_list = get_plugin().get_list(kwargs.pop('list_id')) except HTTPError: raise Http404 return fn(request, pgp_list, *args, **kwargs) @@ -39,7 +39,7 @@ def list_view(fn): def list_class_view(fn): @wraps(fn) def wrapper(self, request, *args, **kwargs): - self.pgp_list = get_pgp_plugin().get_list(kwargs.pop('list_id')) + self.pgp_list = get_plugin().get_list(kwargs.pop('list_id')) return fn(self, request, *args, **kwargs) return wrapper @@ -68,3 +68,17 @@ def member_role_required(*roles): return wrapped return wrapper + + +def user_class_view(fn): + @wraps(fn) + def wrapper(self, request, *args, **kwargs): + client = get_client() + user = request.user + try: + self.mm_user = client.get_user(address=user.email) + except HTTPError: + self.mm_user = client.create_user(user.email, user.get_full_name()) + return fn(self, request, *args, **kwargs) + + return wrapper diff --git a/src/django_pgpmailman/models.py b/src/django_pgpmailman/models.py index 0a49475..572e51b 100644 --- a/src/django_pgpmailman/models.py +++ b/src/django_pgpmailman/models.py @@ -20,7 +20,8 @@ from __future__ import absolute_import, unicode_literals from itertools import chain -from mailmanclient._client import MailingList, RESTObject +from mailmanclient import Address, MailingList +from mailmanclient.restbase.base import RESTObject from pgpy import PGPKey from pgpy.errors import PGPError @@ -32,7 +33,7 @@ class PGPMailingList(RESTObject): 'strip_original_sig', 'sign_outgoing', 'nonencrypted_msg_action', 'encrypt_outgoing', 'key_change_workflow', 'key_signing_allowed') _read_only_properties = ('self_link', 'list_id') - _properties = list(chain(_writable_properties, _read_only_properties)) + _properties = tuple(chain(_writable_properties, _read_only_properties)) @property def mlist(self): @@ -68,3 +69,13 @@ class PGPMailingList(RESTObject): self._connection.call(self._url + '/pubkey', data=dict(public_key=str_key), method='PUT') + + +class PGPAddress(RESTObject): + _read_only_properties = ('self_link', 'email', 'key_fingerprint', + 'key_confirmed') + _properties = _read_only_properties + + @property + def address(self): + return Address(self._connection, 'addresses/{}'.format(self.email)) diff --git a/src/django_pgpmailman/plugin.py b/src/django_pgpmailman/plugin.py index de6cbcc..5a5d62a 100644 --- a/src/django_pgpmailman/plugin.py +++ b/src/django_pgpmailman/plugin.py @@ -20,7 +20,7 @@ from operator import itemgetter from django.conf import settings from mailmanclient._client import Plugin, Client -from django_pgpmailman.models import PGPMailingList +from django_pgpmailman.models import PGPMailingList, PGPAddress class PGPPlugin(Plugin): @@ -42,16 +42,32 @@ class PGPPlugin(Plugin): response, content = self.call('lists/%s' % list_identifier) return PGPMailingList(self._connection, content['self_link'], content) + @property + def addresses(self): + response, content = self.call('addresses') + if 'entries' not in content: + return [] + return [PGPAddress(self._connection, entry['self_link'], entry) for + entry in sorted(content['entries'], key=itemgetter('email'))] + + def get_address(self, email): + response, content = self.call('addresses/%s' % email) + return PGPAddress(self._connection, content['self_link'], content) + + +def get_client(): + return Client('%s/3.1' % + settings.MAILMAN_REST_API_URL, + settings.MAILMAN_REST_API_USER, + settings.MAILMAN_REST_API_PASS) + plugin = None -def get_pgp_plugin(): +def get_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) + client = get_client() plugin = PGPPlugin(client.get_plugin(settings.MAILMAN_PGP_PLUGIN_NAME)) return plugin diff --git a/src/django_pgpmailman/templates/django_pgpmailman/user/summary.html b/src/django_pgpmailman/templates/django_pgpmailman/user/summary.html index aab98eb..4889e1b 100644 --- a/src/django_pgpmailman/templates/django_pgpmailman/user/summary.html +++ b/src/django_pgpmailman/templates/django_pgpmailman/user/summary.html @@ -1 +1,33 @@ -{% extends "django_pgpmailman/base.html" %} \ No newline at end of file +{% extends "django_pgpmailman/base.html" %} +{% load i18n %} + +{% block head_title %} + {% trans 'PGP User settings' %} - {{ block.super }} +{% endblock %} + +{% block content %} + {% if addresses|length > 0 %} +
+ + + + + + + + + + {% for address in addresses %} + + + + + + {% endfor %} + +
{% trans 'Email' %}{% trans 'Key fingerprint' %}{% trans 'Key confirmed' %}
{{ address.email }}{{ address.key_fingerprint }}{{ address.key_confirmed }}
+
+ {% else %} +

{% trans "You currently don't have any PGP enabled addresses. Subscribe to a PGP enabled list to setup one." %}

+ {% endif %} +{% endblock %} \ No newline at end of file diff --git a/src/django_pgpmailman/urls.py b/src/django_pgpmailman/urls.py index 6276b82..a451e9c 100644 --- a/src/django_pgpmailman/urls.py +++ b/src/django_pgpmailman/urls.py @@ -23,7 +23,7 @@ from django_pgpmailman.views.list import ( pgp_list_index, pgp_list_summary, ListSignatureSettingsView, ListEncryptionSettingsView, ListMiscSettingsView, ListKeyManagementView, ListPubkey, ListPrivkey) -from django_pgpmailman.views.user import pgp_user_profile +from django_pgpmailman.views.user import UserSummaryView list_patterns = [ url(r'^$', pgp_list_summary, name='pgp_list_summary'), @@ -44,7 +44,8 @@ list_patterns = [ ] user_patterns = [ - url(r'^$', pgp_user_profile, name='pgp_user_profile') + url(r'^$', UserSummaryView.as_view(), + name='pgp_user_profile') ] urlpatterns = [ diff --git a/src/django_pgpmailman/views/list.py b/src/django_pgpmailman/views/list.py index 5e64b85..f062ff5 100644 --- a/src/django_pgpmailman/views/list.py +++ b/src/django_pgpmailman/views/list.py @@ -36,12 +36,12 @@ from django_pgpmailman.forms import (ListSignatureSettingsForm, ListEncryptionSettingsForm, ListMiscSettingsForm, ListKeyManagementForm) -from django_pgpmailman.plugin import get_pgp_plugin +from django_pgpmailman.plugin import get_plugin def pgp_list_index(request): return render(request, 'django_pgpmailman/index.html', - {'lists': get_pgp_plugin().lists}) + {'lists': get_plugin().lists}) @list_view diff --git a/src/django_pgpmailman/views/user.py b/src/django_pgpmailman/views/user.py index ecc1b9e..cc1dbc3 100644 --- a/src/django_pgpmailman/views/user.py +++ b/src/django_pgpmailman/views/user.py @@ -16,9 +16,41 @@ # You should have received a copy of the GNU General Public License along with # this program. If not, see . from django.contrib.auth.decorators import login_required -from django.shortcuts import render +from django.utils.decorators import method_decorator +from django.views.generic import TemplateView +from six.moves.urllib_error import HTTPError +from django_pgpmailman.decorators import user_class_view +from django_pgpmailman.plugin import get_plugin -@login_required -def pgp_user_profile(request): - return render(request, 'django_pgpmailman/user/summary.html') + +class UserSummaryView(TemplateView): + template_name = 'django_pgpmailman/user/summary.html' + + @method_decorator(login_required) + @user_class_view + def dispatch(self, request, *args, **kwargs): + return super(UserSummaryView, self).dispatch(request, *args, **kwargs) + + def get_context_data(self, **kwargs): + data = super(UserSummaryView, self).get_context_data(**kwargs) + addresses = [] + for address in self.mm_user.addresses: + try: + addresses.append(get_plugin().get_address(address.email)) + except HTTPError: + pass + data['addresses'] = addresses + return data + +# +# class UserSummaryView(FormView): +# template_name = 'django_pgpmailman/user/summary.html' +# +# @method_decorator(login_required) +# @user_class_view +# def dispatch(self, request, *args, **kwargs): +# return super(UserSummaryView, self).dispatch(request, *args, **kwargs) +# +# def get_form_class(self): +# return formset_factory(AddressForm, ) -- cgit v1.2.3-70-g09d2