0001-misc-fix-override-of-trace-emails-recipients-for-Dja.patch
passerelle/base/models.py | ||
---|---|---|
971 | 971 | |
972 | 972 |
admins = settings.ADMINS |
973 | 973 |
logging_parameters = self.connector.logging_parameters |
974 |
if logging_parameters.trace_emails: |
|
975 |
admins = [('', x) for x in logging_parameters.trace_emails.splitlines()] |
|
976 |
with override_settings(ADMINS=admins): |
|
977 |
getattr(self._logger, levelname.lower())(message, *args, **kwargs) |
|
974 |
kwargs.setdefault('extra', {})['admin_emails'] = logging_parameters.trace_emails.splitlines() |
|
975 | ||
976 |
getattr(self._logger, levelname.lower())(message, *args, **kwargs) |
|
978 | 977 | |
979 | 978 |
def exception(self, message, *args, **kwargs): |
980 | 979 |
kwargs['exc_info'] = 1 |
passerelle/log.py | ||
---|---|---|
1 |
# Copyright (C) 2021 Entr'ouvert |
|
2 |
# |
|
3 |
# This program is free software: you can redistribute it and/or modify it |
|
4 |
# under the terms of the GNU Affero General Public License as published |
|
5 |
# by the Free Software Foundation, either version 3 of the License, or |
|
6 |
# (at your option) any later version. |
|
7 |
# |
|
8 |
# This program is distributed in the hope that it will be useful, |
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 |
# GNU Affero General Public License for more details. |
|
12 |
# |
|
13 |
# You should have received a copy of the GNU Affero General Public License |
|
14 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | ||
16 |
import django.utils.log |
|
17 |
from django.conf import settings |
|
18 |
from django.core.mail.message import EmailMultiAlternatives |
|
19 | ||
20 | ||
21 |
class AdminEmailHandler(django.utils.log.AdminEmailHandler): |
|
22 |
def emit(self, record): |
|
23 |
self.emails = [] |
|
24 |
if getattr(record, 'admin_emails', None): |
|
25 |
self.emails = record.admin_emails |
|
26 |
elif settings.ADMINS: |
|
27 |
self.emails = [a[1] for a in settings.ADMINS] |
|
28 |
return super().emit(record) |
|
29 | ||
30 |
def send_mail(self, subject, message, *args, **kwargs): |
|
31 |
if not self.emails: |
|
32 |
return |
|
33 | ||
34 |
mail = EmailMultiAlternatives( |
|
35 |
'%s%s' % (settings.EMAIL_SUBJECT_PREFIX, subject), |
|
36 |
message, |
|
37 |
settings.SERVER_EMAIL, |
|
38 |
self.emails, |
|
39 |
connection=self.connection(), |
|
40 |
) |
|
41 |
if 'html_message' in kwargs: |
|
42 |
mail.attach_alternative(kwargs['html_message'], 'text/html') |
|
43 | ||
44 |
mail.send(fail_silently=kwargs.get('fail_silently', False)) |
passerelle/settings.py | ||
---|---|---|
244 | 244 |
'level': 'DEBUG', |
245 | 245 |
'class': 'logging.StreamHandler', |
246 | 246 |
}, |
247 |
'mail_admins': { |
|
248 |
'level': 'ERROR', |
|
249 |
'class': 'passerelle.log.AdminEmailHandler', |
|
250 |
'include_html': True, |
|
251 |
}, |
|
247 | 252 |
}, |
248 | 253 |
'loggers': { |
249 | 254 |
'django.request': { |
tests/test_misc.py | ||
---|---|---|
88 | 88 |
def email_handler(): |
89 | 89 |
import logging |
90 | 90 | |
91 |
from django.utils.log import AdminEmailHandler
|
|
91 |
from passerelle.log import AdminEmailHandler
|
|
92 | 92 | |
93 | 93 |
root = logging.getLogger() |
94 | 94 |
handler = AdminEmailHandler(include_html=True) |
95 |
- |