From 216256f0af673de1d81b10110adf7fa7a5d88f40 Mon Sep 17 00:00:00 2001 From: J08nY Date: Tue, 22 Aug 2017 00:58:11 +0200 Subject: Allow creating PGP enabled lists. --- src/django_pgpmailman/forms.py | 56 +++++++++++++++++++++ .../templates/django_pgpmailman/index.html | 4 ++ .../templates/django_pgpmailman/list/create.html | 20 ++++++++ src/django_pgpmailman/urls.py | 5 +- src/django_pgpmailman/views/list.py | 58 ++++++++++++++++++++-- 5 files changed, 139 insertions(+), 4 deletions(-) create mode 100644 src/django_pgpmailman/templates/django_pgpmailman/list/create.html (limited to 'src') diff --git a/src/django_pgpmailman/forms.py b/src/django_pgpmailman/forms.py index 493d3d8..3083274 100644 --- a/src/django_pgpmailman/forms.py +++ b/src/django_pgpmailman/forms.py @@ -17,6 +17,7 @@ # this program. If not, see . from django import forms from django.core.exceptions import ValidationError +from django.core.validators import validate_email from django.utils.translation import ugettext_lazy as _ from pgpy import PGPKey @@ -196,3 +197,58 @@ class ListKeyManagementForm(forms.Form): 'has the same key material and contains the UID that ' 'was signed.') ) + + +class ListCreateForm(forms.Form): + listname = forms.CharField( + label=_('List Name'), + required=True, + error_messages={ + 'required': _('Please enter a name for your list.'), + 'invalid': _('Please enter a valid list name.')}) + mail_host = forms.ChoiceField() + list_owner = forms.EmailField( + label=_('Inital list owner address'), + error_messages={ + 'required': _( + 'Please enter the list owner\'s email address.')}, + required=True) + advertised = forms.ChoiceField( + widget=forms.RadioSelect(), + label=_('Advertise this list?'), + error_messages={ + 'required': _('Please choose a list type.')}, + required=True, + choices=( + (True, _('Advertise this list in list index')), + (False, _('Hide this list in list index')))) + description = forms.CharField( + label=_('Description'), + required=False) + list_style = forms.ChoiceField() + + def __init__(self, domain_choices=None, style_choices=None, *args, **kwargs): + super(ListCreateForm, self).__init__(*args, **kwargs) + self.fields['mail_host'] = forms.ChoiceField( + widget=forms.Select(), + label=_('Mail Host'), + required=True, + choices=domain_choices, + error_messages={'required': _('Choose an existing Domain.'), + 'invalid': _('Invalid mail host')}) + self.fields['list_style'] = forms.ChoiceField( + widget=forms.Select(), + label=_('List style'), + required=True, + choices=style_choices + ) + if len(domain_choices) < 2: + self.fields['mail_host'].help_text = _( + 'Site admin has not created any domains') + + def clean_listname(self): + try: + validate_email(self.cleaned_data['listname'] + '@example.net') + except: + raise forms.ValidationError(_('Please enter a valid listname')) + return self.cleaned_data['listname'] diff --git a/src/django_pgpmailman/templates/django_pgpmailman/index.html b/src/django_pgpmailman/templates/django_pgpmailman/index.html index cbe309f..1c7eae0 100644 --- a/src/django_pgpmailman/templates/django_pgpmailman/index.html +++ b/src/django_pgpmailman/templates/django_pgpmailman/index.html @@ -54,5 +54,9 @@ {% else %}

{% trans 'There are currently no PGP enabled mailing lists.' %}

{% endif %} + {% if user.is_superuser %} + Create + new list + {% endif %} {% endblock content %} diff --git a/src/django_pgpmailman/templates/django_pgpmailman/list/create.html b/src/django_pgpmailman/templates/django_pgpmailman/list/create.html new file mode 100644 index 0000000..71030f8 --- /dev/null +++ b/src/django_pgpmailman/templates/django_pgpmailman/list/create.html @@ -0,0 +1,20 @@ +{% extends "django_pgpmailman/base.html" %} +{% load i18n %} +{% load bootstrap_tags %} + +{% block head_title %} + {% trans 'PGP List' %} - {{ block.super }} +{% endblock %} + +{% block content %} + + + +
+ {% bootstrap_form_horizontal form 2 8 'Create list' %} +
+ +{% endblock content %} diff --git a/src/django_pgpmailman/urls.py b/src/django_pgpmailman/urls.py index a451e9c..519d5dc 100644 --- a/src/django_pgpmailman/urls.py +++ b/src/django_pgpmailman/urls.py @@ -22,7 +22,8 @@ from django.conf.urls import url, include from django_pgpmailman.views.list import ( pgp_list_index, pgp_list_summary, ListSignatureSettingsView, ListEncryptionSettingsView, - ListMiscSettingsView, ListKeyManagementView, ListPubkey, ListPrivkey) + ListMiscSettingsView, ListKeyManagementView, ListPubkey, ListPrivkey, + ListCreate) from django_pgpmailman.views.user import UserSummaryView list_patterns = [ @@ -50,6 +51,8 @@ user_patterns = [ urlpatterns = [ url(r'^$', pgp_list_index, name='pgp_list_index'), + url(r'^lists/create/$', ListCreate.as_view(success_url='pgp_list_summary'), + name='pgp_list_create'), url(r'^lists/(?P[^/]+)/', include(list_patterns)), url(r'^accounts/', include(user_patterns)) ] diff --git a/src/django_pgpmailman/views/list.py b/src/django_pgpmailman/views/list.py index f062ff5..f2289b2 100644 --- a/src/django_pgpmailman/views/list.py +++ b/src/django_pgpmailman/views/list.py @@ -19,7 +19,7 @@ from __future__ import absolute_import, unicode_literals from django.contrib import messages -from django.contrib.auth.decorators import login_required +from django.contrib.auth.decorators import login_required, user_passes_test from django.core.files.base import ContentFile from django.http import HttpResponse, Http404 from django.shortcuts import render @@ -35,8 +35,8 @@ from django_pgpmailman.decorators import (list_view, list_class_view, from django_pgpmailman.forms import (ListSignatureSettingsForm, ListEncryptionSettingsForm, ListMiscSettingsForm, - ListKeyManagementForm) -from django_pgpmailman.plugin import get_plugin + ListKeyManagementForm, ListCreateForm) +from django_pgpmailman.plugin import get_plugin, get_client def pgp_list_index(request): @@ -50,6 +50,58 @@ def pgp_list_summary(request, pgp_list): {'pgp_list': pgp_list}) +class ListCreate(FormView): + template_name = 'django_pgpmailman/list/create.html' + form_class = ListCreateForm + initial = {'advertised': True} + + def get_initial(self): + self.initial.update({'list_owner': self.request.user.email}) + return super(ListCreate, self).get_initial() + + def get_form_kwargs(self): + kwargs = super(ListCreate, self).get_form_kwargs() + domains = [] + for domain in get_client().domains: + domains.append((domain.mail_host, domain.mail_host)) + kwargs['domain_choices'] = domains + styles = [('pgp-default', 'PGP discussion'), + ('pgp-announce', 'PGP announce')] + kwargs['style_choices'] = styles + return kwargs + + @user_passes_test(lambda u: u.is_superuser) + def dispatch(self, request, *args, **kwargs): + return super(ListCreate, self).dispatch(request, *args, **kwargs) + + def form_valid(self, form): + try: + client = get_client() + domain = client.get_domain( + mail_host=form.cleaned_data['mail_host']) + + mlist = domain.create_list(form.cleaned_data['listname'], + style_name=form.cleaned_data[ + 'list_style']) + mlist.add_owner(form.cleaned_data['list_owner']) + list_settings = mlist.settings + if form.cleaned_data['description']: + list_settings['description'] = form.cleaned_data['description'] + list_settings['advertised'] = form.cleaned_data['advertised'] + list_settings.save() + + self.mlist = mlist + messages.success(self.request, _('List created')) + except HTTPError as e: + messages.error(self.request, e.msg) + + return super(ListCreate, self).form_valid(form) + + def get_success_url(self): + return reverse(self.success_url, + kwargs=dict(list_id=self.mlist.list_id)) + + class ListKey(View): which_key = None -- cgit v1.2.3-70-g09d2