Use django.contrib.messages to show alerts

This commit is contained in:
Santiago L 2021-10-14 11:59:59 +02:00
parent 33e68b5d07
commit b171cbf641
3 changed files with 31 additions and 0 deletions

View File

@ -82,6 +82,16 @@
{% endblock sidebar %}
</nav><!-- ./sidebar -->
<div id="content" class="container-fluid pt-4">
{% block messages %}
{% for message in messages %}
<div class="alert alert-{{ message.tags|default:'info' }} alert-dismissible fade show" role="alert">
{{ message }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
{% endfor %}
{% endblock messages %}
{% block content %}
{% endblock content %}
</div><!-- ./content -->

View File

@ -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)

View File

@ -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',
}