Notify managers via email on mailbox deletion

This commit is contained in:
Santiago L 2021-10-08 13:33:09 +02:00
parent 2aab4a666f
commit 6c773893f7
2 changed files with 27 additions and 10 deletions

View file

@ -1,7 +1,9 @@
import logging import logging
import smtplib
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.core.mail import mail_managers
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render from django.shortcuts import render
from django.urls import reverse_lazy from django.urls import reverse_lazy
@ -18,15 +20,14 @@ from requests.exceptions import HTTPError
from . import api, get_version from . import api, get_version
from .auth import login as auth_login from .auth import login as auth_login
from .auth import logout as auth_logout from .auth import logout as auth_logout
from .forms import LoginForm, MailForm, MailboxCreateForm, MailboxUpdateForm from .forms import LoginForm, MailboxCreateForm, MailboxUpdateForm, MailForm
from .mixins import (CustomContextMixin, ExtendedPaginationMixin, from .mixins import (CustomContextMixin, ExtendedPaginationMixin,
UserTokenRequiredMixin) UserTokenRequiredMixin)
from .models import (Address, Bill, DatabaseService, Mailbox, MailinglistService, from .models import (Address, Bill, DatabaseService, Mailbox,
PaymentSource, SaasService, UserAccount) MailinglistService, PaymentSource, SaasService)
from .settings import ALLOWED_RESOURCES from .settings import ALLOWED_RESOURCES
from .utils import get_bootstraped_percent from .utils import get_bootstraped_percent
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -416,8 +417,23 @@ class MailboxDeleteView(CustomContextMixin, UserTokenRequiredMixin, DeleteView):
# TODO(@slamora): show error message to user # TODO(@slamora): show error message to user
logger.error(e) logger.error(e)
self.notify_managers(self.object)
return HttpResponseRedirect(self.success_url) return HttpResponseRedirect(self.success_url)
def notify_managers(self, mailbox):
user = self.get_context_data()['profile']
subject = 'Mailbox {} ({}) deleted | Musician'.format(mailbox.id, mailbox.name)
content = (
"User {} ({}) has deleted its mailbox {} ({}) via musician.\n"
"The mailbox has been marked as inactive but has not been removed."
).format(user.username, user.full_name, mailbox.id, mailbox.name)
try:
mail_managers(subject, content, fail_silently=False)
except (smtplib.SMTPException, ConnectionRefusedError):
logger.error("Error sending email to managers", exc_info=True)
class DatabasesView(ServiceListView): class DatabasesView(ServiceListView):
template_name = "musician/databases.html" template_name = "musician/databases.html"

View file

@ -41,6 +41,8 @@ EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD', default='')
EMAIL_PORT = config('EMAIL_PORT', default=25, cast=int) EMAIL_PORT = config('EMAIL_PORT', default=25, cast=int)
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default=[], cast=Csv()) ALLOWED_HOSTS = config('ALLOWED_HOSTS', default=[], cast=Csv())
@ -149,12 +151,6 @@ USE_L10N = True
USE_TZ = True USE_TZ = True
LANGUAGES = (
('ca', _('Catalan')),
('es', _('Spanish')),
('en', _('English')),
)
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/ # https://docs.djangoproject.com/en/2.2/howto/static-files/
@ -177,3 +173,8 @@ URL_SAAS_GITLAB = config('URL_SAAS_GITLAB', None)
URL_SAAS_OWNCLOUD = config('URL_SAAS_OWNCLOUD', None) URL_SAAS_OWNCLOUD = config('URL_SAAS_OWNCLOUD', None)
URL_SAAS_WORDPRESS = config('URL_SAAS_WORDPRESS', None) URL_SAAS_WORDPRESS = config('URL_SAAS_WORDPRESS', None)
# Managers: who should get notifications about services changes that
# may require human actions (e.g. deleted mailboxes)
MANAGERS = []