Commit 216256f0 authored by J08nY's avatar J08nY

Allow creating PGP enabled lists.

parent bfc7f14b
Pipeline #11054676 failed with stage
in 3 minutes and 32 seconds
......@@ -17,6 +17,7 @@
# this program. If not, see <http://www.gnu.org/licenses/>.
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']
......@@ -54,5 +54,9 @@
{% else %}
<p>{% trans 'There are currently no PGP enabled mailing lists.' %}</p>
{% endif %}
{% if user.is_superuser %}
<a class="btn btn-success" href="{% url 'pgp_list_create' %}">Create
new list</a>
{% endif %}
{% endblock content %}
{% extends "django_pgpmailman/base.html" %}
{% load i18n %}
{% load bootstrap_tags %}
{% block head_title %}
{% trans 'PGP List' %} - {{ block.super }}
{% endblock %}
{% block content %}
<div class="page-header">
<h1>{% trans 'Create a new list' %}</h1>
</div>
<form action="{% url 'pgp_list_create' %}" method="post"
class="form-horizontal">
{% bootstrap_form_horizontal form 2 8 'Create list' %}
</form>
{% endblock content %}
......@@ -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<list_id>[^/]+)/', include(list_patterns)),
url(r'^accounts/', include(user_patterns))
]
......@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment