Completed
Push — master ( a279e3...49403b )
by Bertrand
15s queued 12s
created

check_databases_compatibility()   C

Complexity

Conditions 8

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
c 1
b 0
f 0
dl 0
loc 52
rs 5.5452

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