Completed
Push — master ( 74e000...50ff4a )
by Bertrand
40s
created

check_django_version()   A

Complexity

Conditions 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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