Completed
Push — master ( b812f7...ce9ec0 )
by Bertrand
01:05
created

check_cache_compatibility()   A

Complexity

Conditions 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
1
from django.apps import AppConfig
2
from django.conf import settings
3
from django.core.checks import register, Tags, Warning, Error
4
from cachalot.utils import ITERABLES
5
6
from .monkey_patch import patch
7
from .settings import (
8
    cachalot_settings, SUPPORTED_CACHE_BACKENDS, SUPPORTED_DATABASE_ENGINES,
9
    SUPPORTED_ONLY)
10
11
12
@register(Tags.caches, Tags.compatibility)
13
def check_cache_compatibility(app_configs, **kwargs):
14
    cache = settings.CACHES[cachalot_settings.CACHALOT_CACHE]
15
    cache_backend = cache['BACKEND']
16
    if cache_backend not in SUPPORTED_CACHE_BACKENDS:
17
        return [Warning(
18
            'Cache backend %r is not supported by django-cachalot.'
19
            % cache_backend,
20
            hint='Switch to a supported cache backend '
21
                 'like Redis or Memcached.',
22
            id='cachalot.W001')]
23
    return []
24
25
26
@register(Tags.database, Tags.compatibility)
27
def check_databases_compatibility(app_configs, **kwargs):
28
    errors = []
29
    databases = settings.DATABASES
30
    original_enabled_databases = getattr(settings, 'CACHALOT_DATABASES',
31
                                         SUPPORTED_ONLY)
32
    enabled_databases = cachalot_settings.CACHALOT_DATABASES
33
    if original_enabled_databases == SUPPORTED_ONLY:
34
        if not cachalot_settings.CACHALOT_DATABASES:
35
            errors.append(Warning(
36
                'None of the configured databases are supported '
37
                'by django-cachalot.',
38
                hint='Use a supported database, or remove django-cachalot, or '
39
                     'put at least one database alias in `CACHALOT_DATABASES` '
40
                     'to force django-cachalot to use it.',
41
                id='cachalot.W002'
42
            ))
43
    elif enabled_databases.__class__ in ITERABLES:
44
        for db_alias in enabled_databases:
45
            if db_alias in databases:
46
                engine = databases[db_alias]['ENGINE']
47
                if engine not in SUPPORTED_DATABASE_ENGINES:
48
                    errors.append(Warning(
49
                        'Database engine %r is not supported '
50
                        'by django-cachalot.' % engine,
51
                        hint='Switch to a supported database engine.',
52
                        id='cachalot.W003'
53
                    ))
54
            else:
55
                errors.append(Error(
56
                    'Database alias %r from `CACHALOT_DATABASES` '
57
                    'is not defined in `DATABASES`.' % db_alias,
58
                    hint='Change `CACHALOT_DATABASES` to be compliant with'
59
                         '`CACHALOT_DATABASES`',
60
                    id='cachalot.E001',
61
                ))
62
63
        if not enabled_databases:
64
            errors.append(Warning(
65
                'Django-cachalot is useless because no database '
66
                'is configured in `CACHALOT_DATABASES`.',
67
                hint='Reconfigure django-cachalot or remove it.',
68
                id='cachalot.W004'
69
            ))
70
    else:
71
        errors.append(Error(
72
            "`CACHALOT_DATABASES` must be either %r or a list, tuple, "
73
            "frozenset or set of database aliases." % SUPPORTED_ONLY,
74
            hint='Remove `CACHALOT_DATABASES` or change it.',
75
            id='cachalot.E002',
76
        ))
77
    return errors
78
79
80
class CachalotConfig(AppConfig):
81
    name = 'cachalot'
82
    patched = False
83
84
    def ready(self):
85
        cachalot_settings.load()
86
87
        if not self.patched:
88
            patch()
89
            self.patched = True
90