| 1 |
import copyreg |
| 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 .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 ( |
| 48 |
engine not in SUPPORTED_DATABASE_ENGINES |
| 49 |
and engine |
| 50 |
not in cachalot_settings.CACHALOT_ADDITIONAL_SUPPORTED_DATABASES |
| 51 |
and not cachalot_settings.CACHALOT_USE_UNSUPPORTED_DATABASE |
| 52 |
): |
| 53 |
errors.append(Warning( |
| 54 |
'Database engine %r is not supported ' |
| 55 |
'by django-cachalot.' % engine, |
| 56 |
hint='Switch to a supported database engine, ' |
| 57 |
'add an entry in `CACHALOT_ADDITIONAL_SUPPORTED_DATABASES`' |
| 58 |
', or set True to `CACHALOT_USE_UNSUPPORTED_DATABASE`.', |
| 59 |
id='cachalot.W003' |
| 60 |
)) |
| 61 |
else: |
| 62 |
errors.append(Error( |
| 63 |
'Database alias %r from `CACHALOT_DATABASES` ' |
| 64 |
'is not defined in `DATABASES`.' % db_alias, |
| 65 |
hint='Change `CACHALOT_DATABASES` to be compliant with' |
| 66 |
'`CACHALOT_DATABASES`', |
| 67 |
id='cachalot.E001', |
| 68 |
)) |
| 69 |
|
| 70 |
if not enabled_databases: |
| 71 |
errors.append(Warning( |
| 72 |
'Django-cachalot is useless because no database ' |
| 73 |
'is configured in `CACHALOT_DATABASES`.', |
| 74 |
hint='Reconfigure django-cachalot or remove it.', |
| 75 |
id='cachalot.W004' |
| 76 |
)) |
| 77 |
else: |
| 78 |
errors.append(Error( |
| 79 |
"`CACHALOT_DATABASES` must be either %r or a list, tuple, " |
| 80 |
"frozenset or set of database aliases." % SUPPORTED_ONLY, |
| 81 |
hint='Remove `CACHALOT_DATABASES` or change it.', |
| 82 |
id='cachalot.E002', |
| 83 |
)) |
| 84 |
return errors |
| 85 |
|
| 86 |
|
| 87 |
class CachalotConfig(AppConfig): |
| 88 |
name = 'cachalot' |
| 89 |
|
| 90 |
def ready(self): |
| 91 |
# Cast memoryview objects to bytes to be able to pickle them. |
| 92 |
# https://docs.python.org/3/library/copyreg.html#copyreg.pickle |
| 93 |
copyreg.pickle(memoryview, lambda val: (memoryview, (bytes(val),))) |
| 94 |
cachalot_settings.load() |