production.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import os
  2. import dj_database_url
  3. import random
  4. import string
  5. from .base import *
  6. DEBUG = os.getenv('DJANGO_DEBUG', 'off') == 'on'
  7. # DJANGO_SECRET_KEY *should* be specified in the environment. If it's not, generate an ephemeral key.
  8. if 'DJANGO_SECRET_KEY' in os.environ:
  9. SECRET_KEY = os.environ['DJANGO_SECRET_KEY']
  10. else:
  11. # Use if/else rather than a default value to avoid calculating this if we don't need it
  12. print("WARNING: DJANGO_SECRET_KEY not found in os.environ. Generating ephemeral SECRET_KEY.")
  13. SECRET_KEY = ''.join([random.SystemRandom().choice(string.printable) for i in range(50)])
  14. # Make sure Django can detect a secure connection properly on Heroku:
  15. SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
  16. # Redirect all requests to HTTPS
  17. SECURE_SSL_REDIRECT = os.getenv('DJANGO_SECURE_SSL_REDIRECT', 'off') == 'on'
  18. # Accept all hostnames, since we don't know in advance which hostname will be used for any given Heroku instance.
  19. # IMPORTANT: Set this to a real hostname when using this in production!
  20. # See https://docs.djangoproject.com/en/1.10/ref/settings/#allowed-hosts
  21. ALLOWED_HOSTS = os.getenv('DJANGO_ALLOWED_HOSTS', '*').split(';')
  22. EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
  23. # BASE_URL required for notification emails
  24. BASE_URL = 'http://localhost:8000'
  25. db_from_env = dj_database_url.config(conn_max_age=500)
  26. DATABASES['default'].update(db_from_env)
  27. # AWS creds may be used for S3 and/or Elasticsearch
  28. AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID', '')
  29. AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY', '')
  30. AWS_REGION = os.getenv('AWS_REGION', '')
  31. # Configure Elasticsearch, if present in os.environ
  32. if 'ELASTICSEARCH_ENDPOINT' in os.environ:
  33. from elasticsearch import RequestsHttpConnection
  34. WAGTAILSEARCH_BACKENDS = {
  35. 'default': {
  36. 'BACKEND': 'wagtail.wagtailsearch.backends.elasticsearch2',
  37. 'HOSTS': [{
  38. 'host': os.getenv('ELASTICSEARCH_ENDPOINT', ''),
  39. 'port': os.getenv('ELASTICSEARCH_PORT', '9200'),
  40. }],
  41. 'connection_class': RequestsHttpConnection,
  42. }
  43. }
  44. if AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY:
  45. from requests_aws4auth import AWS4Auth
  46. WAGTAILSEARCH_BACKENDS['default']['http_auth'] = AWS4Auth(
  47. AWS_ACCESS_KEY_ID,
  48. AWS_SECRET_ACCESS_KEY,
  49. AWS_REGION,
  50. 'es'
  51. )
  52. elif AWS_REGION:
  53. # No API keys in the environ, so attempt to discover them with Boto instead, per:
  54. # http://boto3.readthedocs.io/en/latest/guide/configuration.html#configuring-credentials
  55. # This may be useful if your credentials are obtained via EC2 instance meta data.
  56. from botocore.session import Session
  57. from requests_aws4auth import AWS4Auth
  58. aws_creds = Session().get_credentials()
  59. if aws_creds:
  60. WAGTAILSEARCH_BACKENDS['default']['http_auth'] = AWS4Auth(
  61. aws_creds.access_key,
  62. aws_creds.secret_key,
  63. AWS_REGION,
  64. 'es',
  65. aws_creds.token,
  66. )
  67. # Simplified static file serving.
  68. # https://warehouse.python.org/project/whitenoise/
  69. MIDDLEWARE.append('whitenoise.middleware.WhiteNoiseMiddleware')
  70. STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
  71. if 'AWS_STORAGE_BUCKET_NAME' in os.environ:
  72. AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')
  73. AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
  74. AWS_AUTO_CREATE_BUCKET = True
  75. INSTALLED_APPS.append('storages')
  76. MEDIA_URL = "https://%s/" % AWS_S3_CUSTOM_DOMAIN
  77. DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
  78. LOGGING = {
  79. 'version': 1,
  80. 'disable_existing_loggers': False,
  81. 'handlers': {
  82. 'console': {
  83. 'class': 'logging.StreamHandler',
  84. },
  85. },
  86. 'loggers': {
  87. 'django': {
  88. 'handlers': ['console'],
  89. 'level': os.getenv('DJANGO_LOG_LEVEL', 'INFO'),
  90. },
  91. },
  92. }