Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check for filename template values #484

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions dbbackup/checks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from datetime import datetime

from django.core.checks import Tags, Warning, register

Expand Down Expand Up @@ -35,6 +36,46 @@
"settings.DBBACKUP_ADMINS",
id="dbbackup.W006",
)
W007 = Warning(
"Invalid FILENAME_TEMPLATE parameter",
hint="settings.DBBACKUP_FILENAME_TEMPLATE must not contain slashes ('/'). "
"Did you mean to change the value for 'location'?",
id="dbbackup.W007",
)
W008 = Warning(
"Invalid MEDIA_FILENAME_TEMPLATE parameter",
hint="settings.DBBACKUP_MEDIA_FILENAME_TEMPLATE must not contain slashes ('/')"
"Did you mean to change the value for 'location'?",
id="dbbackup.W007",
)


def check_filename_templates():
return _check_filename_template(
settings.FILENAME_TEMPLATE,
W007,
"db",
) + _check_filename_template(
settings.MEDIA_FILENAME_TEMPLATE,
W008,
"media",
)


def _check_filename_template(filename_template, check_code, content_type) -> list:
if callable(filename_template):
params = {
"servername": "localhost",
"datetime": datetime.now().strftime(settings.DATE_FORMAT),
"databasename": "default",
"extension": "dump",
"content_type": content_type,
}
filename_template = filename_template(params)

if "/" in filename_template:
return [check_code]
return []


@register(Tags.compatibility)
Expand Down Expand Up @@ -64,4 +105,6 @@ def check_settings(app_configs, **kwargs):
if getattr(settings, "FAILURE_RECIPIENTS", None) is not None:
errors.append(W006)

errors += check_filename_templates()

return errors
24 changes: 24 additions & 0 deletions dbbackup/tests/test_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,27 @@ def test_Failure_recipients_warning(self):
expected_errors = [checks.W006]
errors = checks.check_settings(DbbackupConfig)
self.assertEqual(expected_errors, errors)

@patch("dbbackup.checks.settings.FILENAME_TEMPLATE", "foo/bar-{datetime}.ext")
def test_db_filename_template_with_slash(self):
expected_errors = [checks.W007]
errors = checks.check_settings(DbbackupConfig)
self.assertEqual(expected_errors, errors)

@patch("dbbackup.checks.settings.FILENAME_TEMPLATE", lambda _: "foo/bar")
def test_db_filename_template_callable_with_slash(self):
expected_errors = [checks.W007]
errors = checks.check_settings(DbbackupConfig)
self.assertEqual(expected_errors, errors)

@patch("dbbackup.checks.settings.MEDIA_FILENAME_TEMPLATE", "foo/bar-{datetime}.ext")
def test_media_filename_template_with_slash(self):
expected_errors = [checks.W008]
errors = checks.check_settings(DbbackupConfig)
self.assertEqual(expected_errors, errors)

@patch("dbbackup.checks.settings.MEDIA_FILENAME_TEMPLATE", lambda _: "foo/bar")
def test_media_filename_template_callable_with_slash(self):
expected_errors = [checks.W008]
errors = checks.check_settings(DbbackupConfig)
self.assertEqual(expected_errors, errors)
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Unreleased
* Default HOST to localhost for postgres databases. https://github.com/jazzband/django-dbbackup/issues/520
* Add PostgreSQL Schema support by @angryfoxx in https://github.com/jazzband/django-dbbackup/pull/507
* Fix restore of database from S3 storage by reintroducing inputfile.seek(0) to utils.uncompress_file
* Add warning for filenames with slashes in them
* Fix bug where dbbackup management command would not respect settings.py:DBBACKUP_DATABASES
* Remove usage of deprecated 'get_storage_class' function in newer Django versions
* Add support for new STORAGES (Django 4.2+) setting under the 'dbbackup' alias
Expand Down
Loading