From b171cbf641b8f29a487626b7ff812b72d1d9abbb Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 14 Oct 2021 11:59:59 +0200 Subject: [PATCH] Use django.contrib.messages to show alerts --- musician/templates/musician/base.html | 10 ++++++++++ musician/views.py | 10 ++++++++++ userpanel/settings.py | 11 +++++++++++ 3 files changed, 31 insertions(+) diff --git a/musician/templates/musician/base.html b/musician/templates/musician/base.html index 27a1eb8..7b56ab9 100644 --- a/musician/templates/musician/base.html +++ b/musician/templates/musician/base.html @@ -82,6 +82,16 @@ {% endblock sidebar %}
+ {% block messages %} + {% for message in messages %} + + {% endfor %} + {% endblock messages %} {% block content %} {% endblock content %}
diff --git a/musician/views.py b/musician/views.py index 7347327..bf5319c 100644 --- a/musician/views.py +++ b/musician/views.py @@ -1,7 +1,9 @@ import logging +from os import stat import smtplib from django.conf import settings +from django.contrib import messages from django.core.exceptions import ImproperlyConfigured from django.core.mail import mail_managers from django.http import HttpResponse, HttpResponseRedirect @@ -457,6 +459,14 @@ class MailboxChangePasswordView(CustomContextMixin, UserTokenRequiredMixin, Form 'password': form.cleaned_data['password2'] } status, response = self.orchestra.set_password_mailbox(self.kwargs['pk'], data) + + if status < 400: + messages.add_message(self.request, messages.SUCCESS, _('Password updated!')) + else: + messages.add_message(self.request, messages.ERROR, _( + 'Cannot process your request, please try again later.')) + logger.error("{}: {}".format(status, str(response)[:100])) + return super().form_valid(form) diff --git a/userpanel/settings.py b/userpanel/settings.py index 937642e..08121c2 100644 --- a/userpanel/settings.py +++ b/userpanel/settings.py @@ -13,6 +13,7 @@ https://docs.djangoproject.com/en/2.2/ref/settings/ import os from decouple import config, Csv +from django.contrib.messages import constants as messages from django.utils.translation import gettext_lazy as _ from dj_database_url import parse as db_url @@ -178,3 +179,13 @@ 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 = [] + + +# redefine MESSAGE_TAGS for a better integration with bootstrap +MESSAGE_TAGS = { + messages.DEBUG: 'debug', + messages.INFO: 'info', + messages.SUCCESS: 'success', + messages.WARNING: 'warning', + messages.ERROR: 'danger', +}