File: checks.py

package info (click to toggle)
django-dbbackup 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 512 kB
  • sloc: python: 3,767; makefile: 7
file content (110 lines) | stat: -rw-r--r-- 3,204 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import re
from datetime import datetime

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

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",
)
W006 = Warning(
    "FAILURE_RECIPIENTS has been deprecated",
    hint="settings.DBBACKUP_FAILURE_RECIPIENTS is replaced by "
    "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)
def check_settings(app_configs, **kwargs):
    errors = []
    if not settings.HOSTNAME:
        errors.append(W001)

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

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

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

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

    if getattr(settings, "FAILURE_RECIPIENTS", None) is not None:
        errors.append(W006)

    errors += check_filename_templates()

    return errors