Merge branch 'weblate-gnu-mailman-postorius' into 'master'
[mailman-postorious.git] / src / postorius / views / domain.py
blob8fa2f8278fa2d514d0e39450bacdf1967fffdba2
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 1998-2023 by the Free Software Foundation, Inc.
4 # This file is part of Postorius.
6 # Postorius is free software: you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option)
9 # any later version.
11 # Postorius is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 # more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Postorius. If not, see <http://www.gnu.org/licenses/>.
19 import logging
20 from urllib.error import HTTPError
22 from django.contrib import messages
23 from django.contrib.auth.decorators import login_required
24 from django.contrib.sites.models import Site
25 from django.http import Http404
26 from django.shortcuts import redirect, render
27 from django.utils.translation import gettext_lazy as _
28 from django.views.decorators.http import require_POST
30 from django_mailman3.lib.mailman import get_mailman_client
31 from django_mailman3.models import MailDomain
32 from django_mailman3.signals import domain_created, domain_deleted
34 from postorius.auth.decorators import superuser_required
35 from postorius.forms.domain_forms import (
36 DomainEditForm,
37 DomainForm,
38 DomainOwnerForm,
40 from postorius.models import Domain, Mailman404Error
43 log = logging.getLogger(__name__)
46 @login_required
47 @superuser_required
48 def domain_index(request):
49 existing_domains = Domain.objects.all()
50 for domain in existing_domains:
51 try:
52 web_host = MailDomain.objects.get(mail_domain=domain.mail_host)
53 except MailDomain.DoesNotExist:
54 site = Site.objects.get_current(request)
55 web_host = MailDomain.objects.create(
56 site=site, mail_domain=domain.mail_host
58 domain.site = web_host.site
59 return render(
60 request,
61 'postorius/domain/index.html',
63 'domains': existing_domains,
68 @login_required
69 @superuser_required
70 def domain_new(request):
71 form_initial = {'site': Site.objects.get_current(request)}
72 if request.method == 'POST':
73 form = DomainForm(request.POST, initial=form_initial)
74 if form.is_valid():
75 domain = Domain(
76 mail_host=form.cleaned_data['mail_host'],
77 description=form.cleaned_data['description'],
78 alias_domain=form.cleaned_data['alias_domain'],
79 owner=request.user.email,
81 try:
82 domain.save()
83 except HTTPError as e:
84 form.add_error('mail_host', e.reason)
85 else:
86 messages.success(request, _('New Domain registered'))
87 MailDomain.objects.get_or_create(
88 site=form.cleaned_data['site'],
89 mail_domain=form.cleaned_data['mail_host'],
91 domain_created.send(
92 sender=Domain, mail_host=form.cleaned_data['mail_host']
94 return redirect('domain_index')
95 else:
96 form = DomainForm(initial=form_initial)
97 return render(request, 'postorius/domain/new.html', {'form': form})
100 @login_required
101 @superuser_required
102 def domain_edit(request, domain):
103 try:
104 domain_obj = Domain.objects.get(mail_host=domain)
105 except Mailman404Error:
106 raise Http404('Domain does not exist')
107 if request.method == 'POST':
108 form = DomainEditForm(request.POST)
109 if form.is_valid():
110 domain_obj.description = form.cleaned_data['description']
111 domain_obj.alias_domain = form.cleaned_data['alias_domain']
112 try:
113 web_host = MailDomain.objects.get(mail_domain=domain)
114 except MailDomain.DoesNotExist:
115 web_host = MailDomain.objects.create(
116 site=form.cleaned_data['site'], mail_domain=domain
118 else:
119 web_host.site = form.cleaned_data['site']
120 web_host.save()
121 try:
122 domain_obj.save()
123 except HTTPError as e:
124 messages.error(request, e)
125 return redirect('domain_edit', domain=domain)
126 else:
127 messages.success(request, _('Domain %s updated') % domain)
128 return redirect('domain_index')
129 else:
130 messages.error(request, _('Please check the errors below'))
131 else:
132 form_initial = {
133 'description': domain_obj.description,
134 'alias_domain': domain_obj.alias_domain,
135 'site': MailDomain.objects.get(mail_domain=domain).site,
137 form = DomainEditForm(initial=form_initial)
139 return render(
140 request, 'postorius/domain/edit.html', {'domain': domain, 'form': form}
144 @login_required
145 @superuser_required
146 def domain_delete(request, domain):
147 """Deletes a domain but asks for confirmation first."""
148 domain_obj = Domain.objects.get(mail_host=domain)
149 if request.method == 'POST':
150 try:
151 domain_obj.delete()
152 MailDomain.objects.filter(mail_domain=domain).delete()
153 messages.success(
154 request, _('The domain %s has been deleted.') % domain
156 domain_deleted.send(sender=Domain, mail_host=domain)
157 return redirect('domain_index')
158 except HTTPError as e:
159 messages.error(
160 request, _('The domain could not be deleted: %s') % e.msg
162 return redirect('domain_index')
163 domain_lists_page = domain_obj.get_list_page(count=10)
164 return render(
165 request,
166 'postorius/domain/confirm_delete.html',
167 {'domain': domain_obj, 'lists': domain_lists_page},
171 @login_required
172 @superuser_required
173 def domain_owners(request, domain):
174 domain_obj = Domain.objects.get(mail_host=domain)
175 if request.method == 'POST':
176 form = DomainOwnerForm(request.POST)
177 if form.is_valid():
178 email = form.cleaned_data['email']
179 domain_obj.add_owner(email)
180 messages.success(
181 request,
182 _('Added {} as an owner for {}').format(
183 email, domain_obj.mail_host
186 return redirect('domain_index')
187 else:
188 form = DomainOwnerForm()
189 return render(
190 request,
191 'postorius/domain/owners.html',
192 {'domain': domain_obj, 'form': form},
196 @require_POST
197 @login_required
198 @superuser_required
199 def remove_owners(request, domain, user_id):
200 domain_obj = Domain.objects.get(mail_host=domain)
201 # Since there is no way to remove one single owner, we do the only possible
202 # thing, remove all owners and add the rest back.
203 client = get_mailman_client()
204 try:
205 remove_email = client.get_user(user_id).addresses[0].email
206 all_owners_emails = [
207 owner.addresses[0].email for owner in domain_obj.owners
209 except (KeyError, ValueError) as e:
210 # We get KeyError if the user has no address due to [0].
211 log.error('Unable to delete owner: %s', str(e))
212 raise Http404(str(e))
213 if remove_email in all_owners_emails:
214 all_owners_emails.remove(remove_email)
215 else:
216 messages.error(
217 _('{} is not an owner for {}').format(
218 remove_email, domain_obj.mail_host
221 return redirect('domain_index')
222 domain_obj.remove_all_owners()
223 for owner in all_owners_emails:
224 domain_obj.add_owner(owner)
225 messages.success(
226 request,
227 _('Removed {} as an owner for {}').format(
228 remove_email, domain_obj.mail_host
231 return redirect('domain_index')