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)
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
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/>.
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 (
40 from postorius
.models
import Domain
, Mailman404Error
43 log
= logging
.getLogger(__name__
)
48 def domain_index(request
):
49 existing_domains
= Domain
.objects
.all()
50 for domain
in existing_domains
:
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
61 'postorius/domain/index.html',
63 'domains': existing_domains
,
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
)
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
,
83 except HTTPError
as e
:
84 form
.add_error('mail_host', e
.reason
)
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'],
92 sender
=Domain
, mail_host
=form
.cleaned_data
['mail_host']
94 return redirect('domain_index')
96 form
= DomainForm(initial
=form_initial
)
97 return render(request
, 'postorius/domain/new.html', {'form': form
})
102 def domain_edit(request
, domain
):
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
)
110 domain_obj
.description
= form
.cleaned_data
['description']
111 domain_obj
.alias_domain
= form
.cleaned_data
['alias_domain']
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
119 web_host
.site
= form
.cleaned_data
['site']
123 except HTTPError
as e
:
124 messages
.error(request
, e
)
125 return redirect('domain_edit', domain
=domain
)
127 messages
.success(request
, _('Domain %s updated') % domain
)
128 return redirect('domain_index')
130 messages
.error(request
, _('Please check the errors below'))
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
)
140 request
, 'postorius/domain/edit.html', {'domain': domain
, 'form': form
}
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':
152 MailDomain
.objects
.filter(mail_domain
=domain
).delete()
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
:
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)
166 'postorius/domain/confirm_delete.html',
167 {'domain': domain_obj
, 'lists': domain_lists_page
},
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
)
178 email
= form
.cleaned_data
['email']
179 domain_obj
.add_owner(email
)
182 _('Added {} as an owner for {}').format(
183 email
, domain_obj
.mail_host
186 return redirect('domain_index')
188 form
= DomainOwnerForm()
191 'postorius/domain/owners.html',
192 {'domain': domain_obj
, 'form': form
},
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()
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
)
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
)
227 _('Removed {} as an owner for {}').format(
228 remove_email
, domain_obj
.mail_host
231 return redirect('domain_index')