production.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import os
  2. import random
  3. import string
  4. import dj_database_url
  5. import django_cache_url
  6. from .base import * # noqa: F403
  7. DEBUG = os.getenv("DJANGO_DEBUG", "off") == "on"
  8. # DJANGO_SECRET_KEY *should* be specified in the environment. If it's not, generate an ephemeral key.
  9. if "DJANGO_SECRET_KEY" in os.environ:
  10. SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
  11. else:
  12. # Use if/else rather than a default value to avoid calculating this if we don't need it
  13. print( # noqa: T201
  14. "WARNING: DJANGO_SECRET_KEY not found in os.environ. Generating ephemeral SECRET_KEY."
  15. )
  16. SECRET_KEY = "".join(
  17. [random.SystemRandom().choice(string.printable) for i in range(50)]
  18. )
  19. # Make sure Django can detect a secure connection properly on Heroku:
  20. SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
  21. # Redirect all requests to HTTPS
  22. SECURE_SSL_REDIRECT = os.getenv("DJANGO_SECURE_SSL_REDIRECT", "off") == "on"
  23. # Accept all hostnames, since we don't know in advance which hostname will be used for any given Heroku instance.
  24. # IMPORTANT: Set this to a real hostname when using this in production!
  25. # See https://docs.djangoproject.com/en/3.2/ref/settings/#allowed-hosts
  26. ALLOWED_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS", "*").split(";")
  27. EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
  28. # WAGTAILADMIN_BASE_URL required for notification emails
  29. WAGTAILADMIN_BASE_URL = "http://localhost:8000"
  30. db_from_env = dj_database_url.config(conn_max_age=500)
  31. DATABASES["default"].update(db_from_env)
  32. # AWS creds may be used for S3 and/or Elasticsearch
  33. AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID", "")
  34. AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY", "")
  35. AWS_REGION = os.getenv("AWS_REGION", "")
  36. # configure CACHES from CACHE_URL environment variable (defaults to locmem if no CACHE_URL is set)
  37. CACHES = {"default": django_cache_url.config()}
  38. # Configure Elasticsearch, if present in os.environ
  39. ELASTICSEARCH_ENDPOINT = os.getenv("ELASTICSEARCH_ENDPOINT", "")
  40. if ELASTICSEARCH_ENDPOINT:
  41. from elasticsearch import RequestsHttpConnection
  42. WAGTAILSEARCH_BACKENDS = {
  43. "default": {
  44. "BACKEND": "wagtail.search.backends.elasticsearch5",
  45. "HOSTS": [
  46. {
  47. "host": ELASTICSEARCH_ENDPOINT,
  48. "port": int(os.getenv("ELASTICSEARCH_PORT", "9200")),
  49. "use_ssl": os.getenv("ELASTICSEARCH_USE_SSL", "off") == "on",
  50. "verify_certs": os.getenv("ELASTICSEARCH_VERIFY_CERTS", "off")
  51. == "on",
  52. }
  53. ],
  54. "OPTIONS": {
  55. "connection_class": RequestsHttpConnection,
  56. },
  57. }
  58. }
  59. if AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY:
  60. from aws_requests_auth.aws_auth import AWSRequestsAuth
  61. WAGTAILSEARCH_BACKENDS["default"]["HOSTS"][0]["http_auth"] = AWSRequestsAuth(
  62. aws_access_key=AWS_ACCESS_KEY_ID,
  63. aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
  64. aws_token=os.getenv("AWS_SESSION_TOKEN", ""),
  65. aws_host=ELASTICSEARCH_ENDPOINT,
  66. aws_region=AWS_REGION,
  67. aws_service="es",
  68. )
  69. elif AWS_REGION:
  70. # No API keys in the environ, so attempt to discover them with Boto instead, per:
  71. # https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html#configuring-credentials
  72. # This may be useful if your credentials are obtained via EC2 instance meta data.
  73. from aws_requests_auth.boto_utils import BotoAWSRequestsAuth
  74. WAGTAILSEARCH_BACKENDS["default"]["HOSTS"][0][
  75. "http_auth"
  76. ] = BotoAWSRequestsAuth(
  77. aws_host=ELASTICSEARCH_ENDPOINT,
  78. aws_region=AWS_REGION,
  79. aws_service="es",
  80. )
  81. # Simplified static file serving.
  82. # https://warehouse.python.org/project/whitenoise/
  83. MIDDLEWARE.append("whitenoise.middleware.WhiteNoiseMiddleware")
  84. STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
  85. if "AWS_STORAGE_BUCKET_NAME" in os.environ:
  86. AWS_STORAGE_BUCKET_NAME = os.getenv("AWS_STORAGE_BUCKET_NAME")
  87. AWS_S3_CUSTOM_DOMAIN = "%s.s3.amazonaws.com" % AWS_STORAGE_BUCKET_NAME
  88. AWS_AUTO_CREATE_BUCKET = True
  89. INSTALLED_APPS.append("storages")
  90. MEDIA_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
  91. DEFAULT_FILE_STORAGE = "storages.backends.s3boto3.S3Boto3Storage"
  92. if "GS_BUCKET_NAME" in os.environ:
  93. GS_BUCKET_NAME = os.getenv("GS_BUCKET_NAME")
  94. GS_PROJECT_ID = os.getenv("GS_PROJECT_ID")
  95. GS_DEFAULT_ACL = "publicRead"
  96. GS_AUTO_CREATE_BUCKET = True
  97. INSTALLED_APPS.append("storages")
  98. DEFAULT_FILE_STORAGE = "storages.backends.gcloud.GoogleCloudStorage"
  99. LOGGING = {
  100. "version": 1,
  101. "disable_existing_loggers": False,
  102. "handlers": {
  103. "console": {
  104. "class": "logging.StreamHandler",
  105. },
  106. },
  107. "loggers": {
  108. "django": {
  109. "handlers": ["console"],
  110. "level": os.getenv("DJANGO_LOG_LEVEL", "INFO"),
  111. },
  112. },
  113. }