chore: Bump copyright year
[mailman-postorious.git] / src / postorius / signals.py
blob572e6a1679d383cdfff652b2f4b24b641c35944c
1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2020-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/>.
20 import logging
22 from django.dispatch import receiver
24 from django_mailman3.models import MailDomain
25 from django_mailman3.signals import domain_created
27 from postorius.models import Domain
30 log = logging.getLogger(__name__)
33 @receiver(domain_created, sender=Domain)
34 def maybe_update_site(sender, **kw):
35 """Update the Site's name and description match the domain iff it is
36 example.com.
38 On a new Django instance, the default Site is example.com and when a user
39 creates a new domain, they'll still see example.com in the Title. It is
40 safe to update the Site object to the domain's mail host and description.
42 Do not touch the Site object if it isn't example.com.
44 :param sender: The model class.
45 :param instance: The actual instance of the sender class.
46 :param created: IF the new record was created.
47 """
48 mail_host = kw.get('mail_host')
50 mail_domain = MailDomain.objects.get(mail_domain=mail_host)
51 domain = Domain.objects.get(mail_host=mail_host)
52 site = mail_domain.site
53 if not site.domain == 'example.com':
54 log.info('Not updating site domain')
55 return
56 log.info('Updating site domain.')
57 # Update the Site to match the domain with mail_host and description to
58 # description if not empty else mail_host.
59 site.domain = domain.mail_host
60 site.name = domain.description or domain.mail_host
61 site.save()
62 log.debug('Update Site %s to match domain %s', site, domain)