Commit cd14fc86 authored by Aurélien Bompard's avatar Aurélien Bompard

Add a way to choose a web host when creating a domain

parent 4aabc8d2
......@@ -19,10 +19,12 @@
from __future__ import absolute_import, unicode_literals
from django import forms
from django.core.urlresolvers import reverse
from django.core.validators import validate_email
from django.utils.encoding import smart_text
from django.utils.translation import ugettext_lazy as _
from django.utils.version import get_complete_version
from django.contrib.sites.models import Site
ACTION_CHOICES = (
......@@ -55,6 +57,11 @@ class ListOfStringsField(forms.Field):
return result
def _get_site_choices():
for site in Site.objects.order_by("name"):
yield (site.pk, "{} ({})".format(site.name, site.domain))
class DomainNew(forms.Form):
"""
......@@ -70,6 +77,15 @@ class DomainNew(forms.Form):
description = forms.CharField(
label=_('Description'),
required=False)
web_host = forms.ChoiceField(
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: _('<a href="%s">Edit</a> the list of available web hosts.')
% reverse("admin:sites_site_changelist"),
)
def clean_mail_host(self):
mail_host = self.cleaned_data['mail_host']
......
......@@ -19,6 +19,7 @@
<tr>
<th>{% trans 'Mail Host' %}</th>
<th>{% trans 'Description' %}</th>
<th>{% trans 'Web Host' %}</th>
<th>&nbsp;</th>
</tr>
</thead>
......@@ -27,6 +28,7 @@
<tr>
<td>{{ domain.mail_host }}</td>
<td>{{ domain.description }}</td>
<td>{{ domain.site.name }} ({{ domain.site.domain }})</td>
<td><a href="{% url 'domain_delete' domain.mail_host %}" class="btn btn-xs btn-danger">{% trans 'Delete' %}</a></td>
</tr>
{% endfor %}
......
......@@ -83,6 +83,19 @@ interactions:
content-type: [application/json; charset=UTF-8]
location: ['http://localhost:9001/3.0/members/66']
status: {code: 201, message: Created}
- request:
body: null
headers:
accept-encoding: ['gzip, deflate']
method: !!python/unicode GET
uri: http://localhost:9001/3.0/domains/example.com
response:
body: {string: !!python/unicode '{"description": null, "http_etag": "\"d150abd34fabbcef42a2c654bfac81aa04ea4d6f\"",
"mail_host": "example.com", "self_link": "http://localhost:9001/3.0/domains/example.com"}'}
headers:
content-length: ['172']
content-type: [application/json; charset=UTF-8]
status: {code: 200, message: OK}
- request:
body: null
headers:
......
......@@ -20,6 +20,8 @@ from __future__ import absolute_import, print_function, unicode_literals
from allauth.account.models import EmailAddress
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django_mailman3.models import MailDomain
from postorius.tests.utils import ViewTestCase
......@@ -45,6 +47,8 @@ class DomainDeleteTest(ViewTestCase):
user=user, email=user.email, verified=True)
self.foo_list.add_owner('owner@example.com')
self.foo_list.add_moderator('moderator@example.com')
MailDomain.objects.create(
site=Site.objects.get_current(), mail_domain='example.com')
self.url = reverse('domain_delete', args=['example.com'])
def test_access_anonymous(self):
......@@ -85,3 +89,5 @@ class DomainDeleteTest(ViewTestCase):
self.assertEqual(len(self.mm_client.domains), 0)
self.assertEqual(len(self.mm_client.lists), 0)
self.assertHasSuccessMessage(response)
self.assertFalse(
MailDomain.objects.filter(mail_domain='example.com').exists())
......@@ -20,6 +20,7 @@ from __future__ import absolute_import, print_function, unicode_literals
from allauth.account.models import EmailAddress
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
from django_mailman3.models import MailDomain
try:
from urllib2 import HTTPError
except ImportError:
......@@ -72,10 +73,12 @@ class DomainIndexPageTest(ViewTestCase):
response = self.client.get(reverse('domain_index'))
self.assertEqual(response.status_code, 403)
def test_domain_index_contains_the_domains(self):
def test_contains_domains_and_site(self):
# The list index page should contain the lists
self.client.login(username='testsu', password='testpass')
response = self.client.get(reverse('domain_index'))
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context['domains']), 1)
self.assertContains(response, 'example.com')
self.assertTrue(
MailDomain.objects.filter(mail_domain='example.com').exists())
......@@ -20,6 +20,7 @@ from __future__ import absolute_import, print_function, unicode_literals
from allauth.account.models import EmailAddress
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django_mailman3.models import MailDomain
from postorius.tests.utils import ViewTestCase
......@@ -44,7 +45,9 @@ class DomainCreationTest(ViewTestCase):
def test_new_domain_created_with_owner(self):
self.client.login(username='su', password='pwd')
post_data = {'mail_host': 'example.com',
'description': 'A new Domain.'}
'description': 'A new Domain.',
'web_host': '1',
}
response = self.client.post(reverse('domain_new'), post_data)
self.assertHasSuccessMessage(response)
......@@ -54,12 +57,16 @@ class DomainCreationTest(ViewTestCase):
self.assertEqual(a_new_domain.mail_host, u'example.com')
self.assertEqual(a_new_domain.owners[0]['user_id'],
self.mm_client.get_user('su@example.com').user_id)
self.assertTrue(
MailDomain.objects.filter(mail_domain='example.com').exists())
a_new_domain.delete()
def test_validation_of_mail_host(self):
self.client.login(username='su', password='pwd')
post_data = {'mail_host': 'example com',
'description': 'A new Domain'}
'description': 'A new Domain',
'web_host': '1',
}
response = self.client.post(reverse('domain_new'), post_data)
self.assertContains(response, 'Please check the errors below')
self.assertContains(response, 'Please enter a valid domain name')
......
......@@ -21,9 +21,11 @@ from __future__ import absolute_import, unicode_literals
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.sites.models import Site
from django.shortcuts import render, redirect
from django.utils.translation import gettext as _
from django_mailman3.lib.mailman import get_mailman_client
from django_mailman3.models import MailDomain
try:
from urllib2 import HTTPError
except ImportError:
......@@ -41,8 +43,17 @@ def domain_index(request):
existing_domains = Domain.objects.all()
except MailmanApiError:
return utils.render_api_error(request)
return render(request, 'postorius/domain/index.html',
{'domains': existing_domains})
for domain in existing_domains:
try:
web_host = MailDomain.objects.get(mail_domain=domain.mail_host)
except MailDomain.DoesNotExist:
site = Site.objects.get_current(request)
web_host = MailDomain.objects.create(
site=site, mail_domain=domain.mail_host)
domain.site = web_host.site
return render(request, 'postorius/domain/index.html', {
'domains': existing_domains,
})
@login_required
......@@ -62,6 +73,9 @@ def domain_new(request):
messages.error(request, e)
else:
messages.success(request, _("New Domain registered"))
MailDomain.objects.get_or_create(
site=Site.objects.get(pk=int(form.cleaned_data['web_host'])),
mail_domain=form.cleaned_data['mail_host'])
return redirect("domain_index")
else:
messages.error(request, _('Please check the errors below'))
......@@ -77,8 +91,9 @@ def domain_delete(request, domain):
"""
if request.method == 'POST':
try:
client = get_mailman_client()
client.delete_domain(domain)
domain_obj = Domain.objects.get(mail_host=domain)
domain_obj.delete()
MailDomain.objects.filter(mail_domain=domain).delete()
messages.success(request,
_('The domain %s has been deleted.' % domain))
return redirect("domain_index")
......
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