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

check_databases_compatibility()   D

Complexity

Conditions 9

Size

Total Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
c 1
b 0
f 0
dl 0
loc 53
rs 4.9852

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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