-
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #183 from ZuluPro/check
Added apps and check frameworks
- Loading branch information
Showing
6 changed files
with
125 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.