production.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. import os
  2. import dj_database_url
  3. from .base import *
  4. # Do not set SECRET_KEY, Postgres or LDAP password or any other sensitive data here.
  5. # Instead, use environment variables or create a local.py file on the server.
  6. # Disable debug mode
  7. DEBUG = False
  8. TEMPLATES[0]['OPTIONS']['debug'] = False
  9. # Configuration from environment variables
  10. # Alternatively, you can set these in a local.py file on the server
  11. env = os.environ.copy()
  12. # On Torchbox servers, many environment variables are prefixed with "CFG_"
  13. for key, value in os.environ.items():
  14. if key.startswith('CFG_'):
  15. env[key[4:]] = value
  16. # Basic configuration
  17. APP_NAME = env.get('APP_NAME', 'bakerydemo')
  18. if 'SECRET_KEY' in env:
  19. SECRET_KEY = env['SECRET_KEY']
  20. if 'ALLOWED_HOSTS' in env:
  21. ALLOWED_HOSTS = env['ALLOWED_HOSTS'].split(',')
  22. if 'PRIMARY_HOST' in env:
  23. BASE_URL = 'http://%s/' % env['PRIMARY_HOST']
  24. if 'SERVER_EMAIL' in env:
  25. SERVER_EMAIL = env['SERVER_EMAIL']
  26. if 'CACHE_PURGE_URL' in env:
  27. INSTALLED_APPS += ( 'wagtail.contrib.wagtailfrontendcache', )
  28. WAGTAILFRONTENDCACHE = {
  29. 'default': {
  30. 'BACKEND': 'wagtail.contrib.wagtailfrontendcache.backends.HTTPBackend',
  31. 'LOCATION': env['CACHE_PURGE_URL'],
  32. },
  33. }
  34. if 'STATIC_URL' in env:
  35. STATIC_URL = env['STATIC_URL']
  36. if 'STATIC_DIR' in env:
  37. STATIC_ROOT = env['STATIC_DIR']
  38. if 'MEDIA_URL' in env:
  39. MEDIA_URL = env['MEDIA_URL']
  40. if 'MEDIA_DIR' in env:
  41. MEDIA_ROOT = env['MEDIA_DIR']
  42. # Database
  43. if 'DATABASE_URL' in os.environ:
  44. DATABASES = {'default': dj_database_url.config()}
  45. else:
  46. DATABASES = {
  47. 'default': {
  48. 'ENGINE': 'django.db.backends.postgresql_psycopg2',
  49. 'NAME': env.get('PGDATABASE', APP_NAME),
  50. 'CONN_MAX_AGE': 600, # number of seconds database connections should persist for
  51. # User, host and port can be configured by the PGUSER, PGHOST and
  52. # PGPORT environment variables (these get picked up by libpq).
  53. }
  54. }
  55. # Elasticsearch
  56. if 'ELASTICSEARCH_URL' in env:
  57. WAGTAILSEARCH_BACKENDS = {
  58. 'default': {
  59. 'BACKEND': 'wagtail.wagtailsearch.backends.elasticsearch.ElasticSearch',
  60. 'URLS': [env['ELASTICSEARCH_URL']],
  61. 'INDEX': APP_NAME,
  62. 'ATOMIC_REBUILD': True,
  63. },
  64. }
  65. # Logging
  66. LOGGING = {
  67. 'version': 1,
  68. 'disable_existing_loggers': False,
  69. 'handlers': {
  70. 'mail_admins': {
  71. 'level': 'ERROR',
  72. 'class': 'django.utils.log.AdminEmailHandler',
  73. },
  74. },
  75. 'formatters': {
  76. 'default': {
  77. 'verbose': '[%(asctime)s] (%(process)d/%(thread)d) %(name)s %(levelname)s: %(message)s'
  78. }
  79. },
  80. 'loggers': {
  81. 'bakerydemo': {
  82. 'handlers': [],
  83. 'level': 'INFO',
  84. 'propagate': False,
  85. 'formatter': 'verbose',
  86. },
  87. 'wagtail': {
  88. 'handlers': [],
  89. 'level': 'INFO',
  90. 'propagate': False,
  91. 'formatter': 'verbose',
  92. },
  93. 'django.request': {
  94. 'handlers': ['mail_admins'],
  95. 'level': 'ERROR',
  96. 'propagate': False,
  97. 'formatter': 'verbose',
  98. },
  99. 'django.security': {
  100. 'handlers': ['mail_admins'],
  101. 'level': 'ERROR',
  102. 'propagate': False,
  103. 'formatter': 'verbose',
  104. },
  105. },
  106. }
  107. if 'LOG_DIR' in env:
  108. # bakerydemo log
  109. LOGGING['handlers']['bakerydemo_file'] = {
  110. 'level': 'INFO',
  111. 'class': 'cloghandler.ConcurrentRotatingFileHandler',
  112. 'filename': os.path.join(env['LOG_DIR'], 'bakerydemo.log'),
  113. 'maxBytes': 5242880, # 5MB
  114. 'backupCount': 5
  115. }
  116. LOGGING['loggers']['wagtail']['handlers'].append('bakerydemo_file')
  117. # Wagtail log
  118. LOGGING['handlers']['wagtail_file'] = {
  119. 'level': 'INFO',
  120. 'class': 'cloghandler.ConcurrentRotatingFileHandler',
  121. 'filename': os.path.join(env['LOG_DIR'], 'wagtail.log'),
  122. 'maxBytes': 5242880, # 5MB
  123. 'backupCount': 5
  124. }
  125. LOGGING['loggers']['wagtail']['handlers'].append('wagtail_file')
  126. # Error log
  127. LOGGING['handlers']['errors_file'] = {
  128. 'level': 'ERROR',
  129. 'class': 'cloghandler.ConcurrentRotatingFileHandler',
  130. 'filename': os.path.join(env['LOG_DIR'], 'error.log'),
  131. 'maxBytes': 5242880, # 5MB
  132. 'backupCount': 5
  133. }
  134. LOGGING['loggers']['django.request']['handlers'].append('errors_file')
  135. LOGGING['loggers']['django.security']['handlers'].append('errors_file')
  136. try:
  137. from .local import *
  138. except ImportError:
  139. pass