From 6c773893f7887e8d6a2d59ddc20a61c38365cd64 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Fri, 8 Oct 2021 13:33:09 +0200 Subject: [PATCH] Notify managers via email on mailbox deletion --- musician/views.py | 24 ++++++++++++++++++++---- userpanel/settings.py | 13 +++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/musician/views.py b/musician/views.py index 56bc28f..878a60b 100644 --- a/musician/views.py +++ b/musician/views.py @@ -1,7 +1,9 @@ import logging +import smtplib from django.conf import settings from django.core.exceptions import ImproperlyConfigured +from django.core.mail import mail_managers from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render from django.urls import reverse_lazy @@ -18,15 +20,14 @@ from requests.exceptions import HTTPError from . import api, get_version from .auth import login as auth_login 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, UserTokenRequiredMixin) -from .models import (Address, Bill, DatabaseService, Mailbox, MailinglistService, - PaymentSource, SaasService, UserAccount) +from .models import (Address, Bill, DatabaseService, Mailbox, + MailinglistService, PaymentSource, SaasService) from .settings import ALLOWED_RESOURCES from .utils import get_bootstraped_percent - logger = logging.getLogger(__name__) @@ -416,8 +417,23 @@ class MailboxDeleteView(CustomContextMixin, UserTokenRequiredMixin, DeleteView): # TODO(@slamora): show error message to user logger.error(e) + self.notify_managers(self.object) + 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): template_name = "musician/databases.html" diff --git a/userpanel/settings.py b/userpanel/settings.py index f288ba6..937642e 100644 --- a/userpanel/settings.py +++ b/userpanel/settings.py @@ -41,6 +41,8 @@ EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD', default='') 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()) @@ -149,12 +151,6 @@ USE_L10N = True USE_TZ = True -LANGUAGES = ( - ('ca', _('Catalan')), - ('es', _('Spanish')), - ('en', _('English')), -) - # Static files (CSS, JavaScript, Images) # 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_WORDPRESS = config('URL_SAAS_WORDPRESS', None) + + +# Managers: who should get notifications about services changes that +# may require human actions (e.g. deleted mailboxes) +MANAGERS = []