apps.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. from __future__ import unicode_literals
  2. from django.apps import AppConfig
  3. from django.core.exceptions import ImproperlyConfigured
  4. from django.conf import settings
  5. class ExtrasConfig(AppConfig):
  6. name = "extras"
  7. def ready(self):
  8. # Check that we can connect to the configured Redis database if webhooks are enabled.
  9. if settings.WEBHOOKS_ENABLED:
  10. try:
  11. import redis
  12. except ImportError:
  13. raise ImproperlyConfigured(
  14. "WEBHOOKS_ENABLED is True but the redis Python package is not installed. (Try 'pip install "
  15. "redis'.)"
  16. )
  17. try:
  18. rs = redis.Redis(
  19. host=settings.REDIS_HOST,
  20. port=settings.REDIS_PORT,
  21. db=settings.REDIS_DATABASE,
  22. password=settings.REDIS_PASSWORD or None,
  23. )
  24. rs.ping()
  25. except redis.exceptions.ConnectionError:
  26. raise ImproperlyConfigured(
  27. "Unable to connect to the Redis database. Check that the Redis configuration has been defined in "
  28. "configuration.py."
  29. )