| 1 |
import re |
| 2 |
from datetime import datetime |
| 3 |
|
| 4 |
from django.core.checks import Tags, register |
| 5 |
from django.core.checks import Warning as DjangoWarning |
| 6 |
|
| 7 |
from dbbackup import settings |
| 8 |
|
| 9 |
W001 = DjangoWarning( |
| 10 |
"Invalid HOSTNAME parameter", |
| 11 |
hint="Set a non empty string to this settings.DBBACKUP_HOSTNAME", |
| 12 |
id="dbbackup.W001", |
| 13 |
) |
| 14 |
W002 = DjangoWarning( |
| 15 |
"Invalid STORAGE parameter", |
| 16 |
hint="Set a valid path to a storage in settings.DBBACKUP_STORAGE", |
| 17 |
id="dbbackup.W002", |
| 18 |
) |
| 19 |
W003 = DjangoWarning( |
| 20 |
"Invalid FILENAME_TEMPLATE parameter", |
| 21 |
hint="Include {datetime} to settings.DBBACKUP_FILENAME_TEMPLATE", |
| 22 |
id="dbbackup.W003", |
| 23 |
) |
| 24 |
W004 = DjangoWarning( |
| 25 |
"Invalid MEDIA_FILENAME_TEMPLATE parameter", |
| 26 |
hint="Include {datetime} to settings.DBBACKUP_MEDIA_FILENAME_TEMPLATE", |
| 27 |
id="dbbackup.W004", |
| 28 |
) |
| 29 |
W005 = DjangoWarning( |
| 30 |
"Invalid DATE_FORMAT parameter", |
| 31 |
hint="settings.DBBACKUP_DATE_FORMAT can contain only [A-Za-z0-9%_-]", |
| 32 |
id="dbbackup.W005", |
| 33 |
) |
| 34 |
# W006: Historical - "FAILURE_RECIPIENTS has been deprecated" |
| 35 |
|
| 36 |
W007 = DjangoWarning( |
| 37 |
"Invalid FILENAME_TEMPLATE parameter", |
| 38 |
hint="settings.DBBACKUP_FILENAME_TEMPLATE must not contain slashes ('/'). " |
| 39 |
"Did you mean to change the value for 'location'?", |
| 40 |
id="dbbackup.W007", |
| 41 |
) |
| 42 |
W008 = DjangoWarning( |
| 43 |
"Invalid MEDIA_FILENAME_TEMPLATE parameter", |
| 44 |
hint="settings.DBBACKUP_MEDIA_FILENAME_TEMPLATE must not contain slashes ('/')" |
| 45 |
"Did you mean to change the value for 'location'?", |
| 46 |
id="dbbackup.W008", |
| 47 |
) |
| 48 |
|
| 49 |
# W009: Historical - "Using removed DBBACKUP_STORAGE parameter" |
| 50 |
# W010: Historical - "Using removed DBBACKUP_STORAGE_OPTIONS parameter" |
| 51 |
|
| 52 |
|
| 53 |
def check_filename_templates(): |
| 54 |
return _check_filename_template( |
| 55 |
settings.FILENAME_TEMPLATE, |
| 56 |
W007, |
| 57 |
"db", |
| 58 |
) + _check_filename_template( |
| 59 |
settings.MEDIA_FILENAME_TEMPLATE, |
| 60 |
W008, |
| 61 |
"media", |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
def _check_filename_template(filename_template, check_code, content_type) -> list: |
| 66 |
if callable(filename_template): |
| 67 |
params = { |
| 68 |
"servername": "localhost", |
| 69 |
"datetime": datetime.now().strftime(settings.DATE_FORMAT), |
| 70 |
"databasename": "default", |
| 71 |
"extension": "dump", |
| 72 |
"content_type": content_type, |
| 73 |
} |
| 74 |
filename_template = filename_template(params) |
| 75 |
|
| 76 |
if isinstance(filename_template, str) and "/" in filename_template: |
| 77 |
return [check_code] |
| 78 |
return [] |
| 79 |
|
| 80 |
|
| 81 |
@register(Tags.compatibility) |
| 82 |
def check_settings(app_configs, **kwargs): |
| 83 |
errors = [] |
| 84 |
if not settings.HOSTNAME: |
| 85 |
errors.append(W001) |
| 86 |
|
| 87 |
if not settings.STORAGE or not isinstance(settings.STORAGE, str): |
| 88 |
errors.append(W002) |
| 89 |
|
| 90 |
if not callable(settings.FILENAME_TEMPLATE) and "{datetime}" not in settings.FILENAME_TEMPLATE: |
| 91 |
errors.append(W003) |
| 92 |
|
| 93 |
if not callable(settings.MEDIA_FILENAME_TEMPLATE) and "{datetime}" not in settings.MEDIA_FILENAME_TEMPLATE: |
| 94 |
errors.append(W004) |
| 95 |
|
| 96 |
if re.search(r"[^A-Za-z0-9%_-]", settings.DATE_FORMAT): |
| 97 |
errors.append(W005) |
| 98 |
|
| 99 |
errors += check_filename_templates() |
| 100 |
|
| 101 |
return errors |