diff --git a/src/postorius/forms.py b/src/postorius/forms.py index 39ecfb7..889d2e1 100644 --- a/src/postorius/forms.py +++ b/src/postorius/forms.py @@ -57,15 +57,22 @@ return result -def _get_site_choices(): - for site in Site.objects.order_by("name"): - yield (site.pk, "{} ({})".format(site.name, site.domain)) +class SiteModelChoiceField(forms.ModelChoiceField): + + def label_from_instance(self, obj): + return "%s (%s)" % (obj.name, obj.domain) -class DomainNew(forms.Form): +def _get_web_host_help(): + # Using a function is necessary, otherwise reverse() will be called before + # URLConfs are loaded. + return (_('Edit the list of available web hosts.') + % reverse("admin:sites_site_changelist")) + +class DomainForm(forms.Form): """ - Form field to add a new domain + Add or edit a domain. """ mail_host = forms.CharField( label=_('Mail Host'), @@ -77,14 +84,14 @@ description = forms.CharField( label=_('Description'), required=False) - web_host = forms.ChoiceField( + site = SiteModelChoiceField( label=_('Web Host'), error_messages={'required': _('Please enter a domain name'), 'invalid': _('Please enter a valid domain name.')}, required=True, - choices=_get_site_choices, - help_text=lambda: _('Edit the list of available web hosts.') - % reverse("admin:sites_site_changelist"), + queryset=Site.objects.order_by("name").all(), + initial=Site.objects.get_current(), + help_text=_get_web_host_help, ) def clean_mail_host(self): @@ -95,19 +102,6 @@ raise forms.ValidationError(_("Please enter a valid domain name")) return mail_host - class Meta: - - """ - Class to handle the automatic insertion of fieldsets and divs. - - To use it: add a list for each wished fieldset. The first item in - the list should be the wished name of the fieldset, the following - the fields that should be included in the fieldset. - """ - layout = [["Please enter Details", - "mail_host", - "description"]] - class MemberForm(forms.Form): """Assing a role to the member""" diff --git a/src/postorius/templates/postorius/domain/edit.html b/src/postorius/templates/postorius/domain/edit.html new file mode 100644 index 0000000..ce1e69e --- /dev/null +++ b/src/postorius/templates/postorius/domain/edit.html @@ -0,0 +1,20 @@ +{% extends "postorius/base.html" %} +{% load i18n %} +{% load bootstrap_tags %} + +{% block head_title %} +{% trans 'Edit domain' %} {{ domain }} - {{ block.super }} +{% endblock %} + +{% block content %} + + + +
+ {% bootstrap_form_horizontal form 2 8 'Update' %} +
+ +{% endblock content %} + diff --git a/src/postorius/templates/postorius/domain/index.html b/src/postorius/templates/postorius/domain/index.html index 298fa9e..c31166d 100644 --- a/src/postorius/templates/postorius/domain/index.html +++ b/src/postorius/templates/postorius/domain/index.html @@ -13,6 +13,7 @@

{% trans 'Add Domain' %}

+ {% if domains %}
@@ -29,11 +30,17 @@ - + {% endfor %}
{{ domain.mail_host }} {{ domain.description }} {{ domain.site.name }} ({{ domain.site.domain }}){% trans 'Delete' %} + {% trans 'Edit' %} + {% trans 'Delete' %} +
+ {% else %} +

No domains yet.

+ {% endif %} {% endblock content %} diff --git a/src/postorius/tests/mailman_api_tests/test_domain_new.py b/src/postorius/tests/mailman_api_tests/test_domain_new.py index ab13204..20a6f45 100644 --- a/src/postorius/tests/mailman_api_tests/test_domain_new.py +++ b/src/postorius/tests/mailman_api_tests/test_domain_new.py @@ -46,7 +46,7 @@ self.client.login(username='su', password='pwd') post_data = {'mail_host': 'example.com', 'description': 'A new Domain.', - 'web_host': '1', + 'site': '1', } response = self.client.post(reverse('domain_new'), post_data) @@ -65,7 +65,7 @@ self.client.login(username='su', password='pwd') post_data = {'mail_host': 'example com', 'description': 'A new Domain', - 'web_host': '1', + 'site': '1', } response = self.client.post(reverse('domain_new'), post_data) self.assertContains(response, 'Please check the errors below') diff --git a/src/postorius/urls.py b/src/postorius/urls.py index 06f4347..957428a 100644 --- a/src/postorius/urls.py +++ b/src/postorius/urls.py @@ -86,6 +86,8 @@ # /domains/ url(r'^domains/$', domain_views.domain_index, name='domain_index'), url(r'^domains/new/$', domain_views.domain_new, name='domain_new'), + url(r'^domains/(?P[^/]+)/$', domain_views.domain_edit, + name='domain_edit'), url(r'^domains/(?P[^/]+)/delete$', domain_views.domain_delete, name='domain_delete'), # /lists/ diff --git a/src/postorius/views/domain.py b/src/postorius/views/domain.py index abf7a2c..1013a13 100644 --- a/src/postorius/views/domain.py +++ b/src/postorius/views/domain.py @@ -22,6 +22,8 @@ from django.contrib import messages from django.contrib.auth.decorators import login_required from django.contrib.sites.models import Site +from django.forms.widgets import HiddenInput +from django.http import Http404 from django.shortcuts import render, redirect from django.utils.translation import gettext as _ from django_mailman3.lib.mailman import get_mailman_client @@ -32,8 +34,8 @@ from urllib.error import HTTPError from postorius import utils from postorius.auth.decorators import superuser_required -from postorius.models import Domain, MailmanApiError -from postorius.forms import DomainNew +from postorius.models import Domain, MailmanApiError, Mailman404Error +from postorius.forms import DomainForm @login_required @@ -59,8 +61,9 @@ @login_required @superuser_required def domain_new(request): + form_initial = {'site': Site.objects.get_current(request)} if request.method == 'POST': - form = DomainNew(request.POST) + form = DomainForm(request.POST, initial=form_initial) if form.is_valid(): domain = Domain(mail_host=form.cleaned_data['mail_host'], description=form.cleaned_data['description'], @@ -74,18 +77,62 @@ else: messages.success(request, _("New Domain registered")) MailDomain.objects.get_or_create( - site=Site.objects.get(pk=int(form.cleaned_data['web_host'])), + site=form.cleaned_data['site'], mail_domain=form.cleaned_data['mail_host']) return redirect("domain_index") else: messages.error(request, _('Please check the errors below')) else: - form = DomainNew() + form = DomainForm(initial=form_initial) return render(request, 'postorius/domain/new.html', {'form': form}) @login_required @superuser_required +def domain_edit(request, domain): + try: + domain_obj = Domain.objects.get(mail_host=domain) + except Mailman404Error: + raise Http404('Domain does not exist') + form_args = [] + if request.method == 'POST': + form_args.append(request.POST) + form_initial = { + 'mail_host': domain, + 'description': domain_obj.description, + 'site': MailDomain.objects.get(mail_domain=domain).site, + } + form = DomainForm(*form_args, initial=form_initial) + form.fields["mail_host"].widget = HiddenInput() + + if request.method == 'POST': + if form.is_valid(): + domain_obj.description = form.cleaned_data['description'] + try: + web_host = MailDomain.objects.get(mail_domain=domain) + except MailDomain.DoesNotExist: + web_host = MailDomain.objects.create( + site=form.cleaned_data['site'], mail_domain=domain) + else: + web_host.site = form.cleaned_data['site'] + web_host.save() + try: + domain_obj.save() + except MailmanApiError: + return utils.render_api_error(request) + except HTTPError as e: + messages.error(request, e) + else: + messages.success(request, _("Domain %s updated") % domain) + return redirect("domain_edit", domain=domain) + else: + messages.error(request, _('Please check the errors below')) + return render(request, 'postorius/domain/edit.html', { + 'domain': domain, 'form': form}) + + +@login_required +@superuser_required def domain_delete(request, domain): """Deletes a domain but asks for confirmation first. """