Skip to content

Commit

Permalink
Some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ZuluPro committed Nov 16, 2016
1 parent 92c9b4d commit 2599cb7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
8 changes: 8 additions & 0 deletions dbbackup/log.py
@@ -1,4 +1,5 @@
import logging
import django
from django.utils.log import AdminEmailHandler

DEFAULT_LOGGING = {
Expand Down Expand Up @@ -39,6 +40,13 @@


class DbbackupAdminEmailHandler(AdminEmailHandler):
def emit(self, record):
# Monkey patch for old Django versions without send_mail method
if django.VERSION < (1, 8):
from . import utils
django.core.mail.mail_admins = utils.mail_admins
super(DbbackupAdminEmailHandler, self).emit(record)

def send_mail(self, subject, message, *args, **kwargs):
from . import utils
utils.mail_admins(subject, message, *args, connection=self.connection(), **kwargs)
Expand Down
43 changes: 16 additions & 27 deletions dbbackup/tests/test_log.py
Expand Up @@ -49,18 +49,18 @@ def test_django(self, captures):
)

@log_capture()
def test_dbbackup_command(self, captures):
logger = logging.getLogger('dbbackup.command')
def test_dbbackup(self, captures):
logger = logging.getLogger('dbbackup')
logger.debug('a noise')
logger.info('a message')
logger.warning('a warning')
logger.error('an error')
logger.critical('a critical error')
captures.check(
('dbbackup.command', 'INFO', 'a message'),
('dbbackup.command', 'WARNING', 'a warning'),
('dbbackup.command', 'ERROR', 'an error'),
('dbbackup.command', 'CRITICAL', 'a critical error'),
('dbbackup', 'INFO', 'a message'),
('dbbackup', 'WARNING', 'a warning'),
('dbbackup', 'ERROR', 'an error'),
('dbbackup', 'CRITICAL', 'a critical error'),
)

@log_capture()
Expand Down Expand Up @@ -95,30 +95,19 @@ def test_other_module(self, captures):
)


class DBbackupAdminEmailHandlerTest(TestCase):
logger = logging.getLogger('dbbackup')

def get_admin_email_handler(self, logger):
admin_email_handler = [
h for h in logger.handlers
if h.__class__.__name__ == "DbbackupAdminEmailHandler"
][0]
return admin_email_handler
class DbbackupAdminEmailHandlerTest(TestCase):
def setUp(self):
self.logger = logging.getLogger('dbbackup')

@patch('dbbackup.settings.SEND_EMAIL', True)
def test_send_mail(self):
# Test mail error
msg = "Super msg"
admin_email_handler = self.get_admin_email_handler(self.logger)
orig_filters = admin_email_handler.filters # Backup filters
try:
admin_email_handler.filters = []
# Test mail error
self.logger.error(msg)
self.assertEqual(mail.outbox[0].subject, '[dbbackup] ERROR: Super msg')
# Test don't mail below
self.logger.warning(msg)
self.assertEqual(len(mail.outbox), 1)
finally:
admin_email_handler.filters = orig_filters
self.logger.error(msg)
self.assertEqual(mail.outbox[0].subject, '[dbbackup] ERROR: Super msg')
# Test don't mail below
self.logger.warning(msg)
self.assertEqual(len(mail.outbox), 1)

@patch('dbbackup.settings.SEND_EMAIL', False)
def test_send_mail_is_false(self):
Expand Down
6 changes: 5 additions & 1 deletion dbbackup/tests/test_utils.py
Expand Up @@ -4,6 +4,7 @@
from mock import patch
from datetime import datetime

import django
from django.test import TestCase
from django.core import mail
from django.utils.six import StringIO
Expand Down Expand Up @@ -86,7 +87,10 @@ def func():
utils.email_uncaught_exception(func)()
self.assertEqual(len(mail.outbox), 1)
error_mail = mail.outbox[0]
self.assertIn("Exception('Foo')", error_mail.body)
self.assertEqual(['foo@bar'], error_mail.to)
self.assertIn("Exception('Foo')", error_mail.subject)
if django.VERSION >= (1, 7):
self.assertIn("Exception('Foo')", error_mail.body)


class Encrypt_FileTest(TestCase):
Expand Down
5 changes: 5 additions & 0 deletions docs/configuration.rst
Expand Up @@ -124,6 +124,10 @@ when making a backup with the ``--encrypt`` or ``--decrypt`` option.
Email configuration
-------------------

.. note::

Django 1.6 won't send the full traceback

DBBACKUP_SEND_EMAIL
~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -151,6 +155,7 @@ email address).
Default: ``django.conf.settings.ADMINS``

.. warning::

``DBBACKUP_FAILURE_RECIPIENTS`` was used before and is deprecated

DBBACKUP_EMAIL_SUBJECT_PREFIX
Expand Down

0 comments on commit 2599cb7

Please sign in to comment.