Skip to content

Commit

Permalink
Merge pull request #183 from ZuluPro/check
Browse files Browse the repository at this point in the history
Added apps and check frameworks
  • Loading branch information
ZuluPro committed Jul 31, 2016
2 parents c2e150e + c770f5e commit 6c05901
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 44 deletions.
1 change: 1 addition & 0 deletions dbbackup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
__author__ = 'Michael Shepanski'
__email__ = 'mjs7231@gmail.com'
__url__ = 'https://github.com/django-dbbackup/django-dbbackup'
default_app_config = 'dbbackup.apps.DbbackupConfig'
15 changes: 15 additions & 0 deletions dbbackup/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""Apps for DBBackup"""
from django.apps import AppConfig
from django.utils.translation import ugettext_lazy as _


class DbbackupConfig(AppConfig):
"""
Config for DBBackup application.
"""
name = 'dbbackup'
label = 'dbbackup'
verbose_name = _('Backup and restore')

def ready(self):
from dbbackup import checks
43 changes: 43 additions & 0 deletions dbbackup/checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import re
from django.core.checks import Warning, register, Tags
from django.utils.six import string_types
from dbbackup import settings

W001 = Warning('Invalid HOSTNAME parameter',
hint='Set a non empty string to this settings.DBBACKUP_HOSTNAME',
id='dbbackup.W001')
W002 = Warning('Invalid STORAGE parameter',
hint='Set a valid path to a storage in settings.DBBACKUP_STORAGE',
id='dbbackup.W002')
W003 = Warning('Invalid FILENAME_TEMPLATE parameter',
hint='Include {datetime} to settings.DBBACKUP_FILENAME_TEMPLATE',
id='dbbackup.W003')
W004 = Warning('Invalid MEDIA_FILENAME_TEMPLATE parameter',
hint='Include {datetime} to settings.DBBACKUP_MEDIA_FILENAME_TEMPLATE',
id='dbbackup.W004')
W005 = Warning('Invalid DATE_FORMAT parameter',
hint='settings.DBBACKUP_DATE_FORMAT can contain only [A-Za-z0-9%_-]',
id='dbbackup.W005')


@register(Tags.compatibility)
def check_settings(app_configs, **kwargs):
errors = []
if not settings.HOSTNAME:
errors.append(W001)

if not settings.STORAGE or not isinstance(settings.STORAGE, string_types):
errors.append(W002)

if not callable(settings.FILENAME_TEMPLATE):
if '{datetime}' not in settings.FILENAME_TEMPLATE:
errors.append(W003)

if not callable(settings.MEDIA_FILENAME_TEMPLATE):
if '{datetime}' not in settings.MEDIA_FILENAME_TEMPLATE:
errors.append(W004)

if re.search(r'[^A-Za-z0-9%_-]', settings.DATE_FORMAT):
errors.append(W005)

return errors
17 changes: 0 additions & 17 deletions dbbackup/settings.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# DO NOT IMPORT THIS BEFORE django.configure() has been run!

import os
import re
import tempfile
import socket
import warnings
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

DATABASES = getattr(settings, 'DBBACKUP_DATABASES', list(settings.DATABASES.keys()))

Expand Down Expand Up @@ -87,18 +85,3 @@
msg = "DBBACKUP_S3_%s is now useless" % old_suffix
warnings.warn(msg, DeprecationWarning)
del old_suffix, new_key

# TODO: Make a module ?
# Checks
for sett in [sett for sett in locals().copy() if sett.endswith('FILENAME_TEMPLATE')]:
if callable(locals()[sett]):
continue
for param in ('datetime',):
if '{%s}' % param not in locals()[sett]:
msg = "You must provide '{%s}' in DBBACKUP_%s" % (param, 'FILENAME_TEMPLATE')
raise ImproperlyConfigured(msg)
del sett

if re.search(r'[^A-Za-z0-9%_-]', DATE_FORMAT): # pragma: no cover
msg = "Bad DBBACKUP_DATE_FORMAT: %s, it must match with [A-Za-z0-9%_-]" % DATE_FORMAT
raise ImproperlyConfigured(msg)
66 changes: 66 additions & 0 deletions dbbackup/tests/test_checks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from mock import patch
from django.test import TestCase
try:
from dbbackup import checks
from dbbackup.apps import DbbackupConfig
except ImportError:
checks = None


def test_func(*args, **kwargs):
return 'foo'


class ChecksTest(TestCase):
def setUp(self):
if checks is None:
self.skipTest("Test framework has been released in Django 1.7")

def test_check(self):
self.assertFalse(checks.check_settings(DbbackupConfig))

@patch('dbbackup.checks.settings.HOSTNAME', '')
def test_hostname_invalid(self):
expected_errors = [checks.W001]
errors = checks.check_settings(DbbackupConfig)
self.assertEqual(expected_errors, errors)

@patch('dbbackup.checks.settings.STORAGE', '')
def test_hostname_storage(self):
expected_errors = [checks.W002]
errors = checks.check_settings(DbbackupConfig)
self.assertEqual(expected_errors, errors)

@patch('dbbackup.checks.settings.FILENAME_TEMPLATE', test_func)
def test_filename_template_is_callable(self):
self.assertFalse(checks.check_settings(DbbackupConfig))

@patch('dbbackup.checks.settings.FILENAME_TEMPLATE', '{datetime}.bak')
def test_filename_template_is_string(self):
self.assertFalse(checks.check_settings(DbbackupConfig))

@patch('dbbackup.checks.settings.FILENAME_TEMPLATE', 'foo.bak')
def test_filename_template_no_date(self):
expected_errors = [checks.W003]
errors = checks.check_settings(DbbackupConfig)
self.assertEqual(expected_errors, errors)

@patch('dbbackup.checks.settings.MEDIA_FILENAME_TEMPLATE', test_func)
def test_media_filename_template_is_callable(self):
self.assertFalse(checks.check_settings(DbbackupConfig))

@patch('dbbackup.checks.settings.MEDIA_FILENAME_TEMPLATE', '{datetime}.bak')
def test_media_filename_template_is_string(self):
self.assertFalse(checks.check_settings(DbbackupConfig))

@patch('dbbackup.checks.settings.MEDIA_FILENAME_TEMPLATE', 'foo.bak')
def test_media_filename_template_no_date(self):
expected_errors = [checks.W004]
errors = checks.check_settings(DbbackupConfig)
self.assertEqual(expected_errors, errors)

@patch('dbbackup.checks.settings.DATE_FORMAT', 'foo@net.pt')
def test_date_format_warning(self):
expected_errors = [checks.W005]
errors = checks.check_settings(DbbackupConfig)
self.assertEqual(expected_errors, errors)
27 changes: 0 additions & 27 deletions dbbackup/tests/test_settings.py

This file was deleted.

0 comments on commit 6c05901

Please sign in to comment.