production.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. # Compress static files offline and minify CSS
  10. # http://django-compressor.readthedocs.org/en/latest/settings/#django.conf.settings.COMPRESS_OFFLINE
  11. COMPRESS_OFFLINE = True
  12. COMPRESS_CSS_FILTERS = [
  13. 'compressor.filters.css_default.CssAbsoluteFilter',
  14. 'compressor.filters.cssmin.CSSMinFilter',
  15. ]
  16. COMPRESS_CSS_HASHING_METHOD = 'content'
  17. # Configuration from environment variables
  18. # Alternatively, you can set these in a local.py file on the server
  19. env = os.environ.copy()
  20. # On Torchbox servers, many environment variables are prefixed with "CFG_"
  21. for key, value in os.environ.items():
  22. if key.startswith('CFG_'):
  23. env[key[4:]] = value
  24. # Basic configuration
  25. APP_NAME = env.get('APP_NAME', 'bakerydemo')
  26. if 'SECRET_KEY' in env:
  27. SECRET_KEY = env['SECRET_KEY']
  28. if 'ALLOWED_HOSTS' in env:
  29. ALLOWED_HOSTS = env['ALLOWED_HOSTS'].split(',')
  30. if 'PRIMARY_HOST' in env:
  31. BASE_URL = 'http://%s/' % env['PRIMARY_HOST']
  32. if 'SERVER_EMAIL' in env:
  33. SERVER_EMAIL = env['SERVER_EMAIL']
  34. if 'CACHE_PURGE_URL' in env:
  35. INSTALLED_APPS += ( 'wagtail.contrib.wagtailfrontendcache', )
  36. WAGTAILFRONTENDCACHE = {
  37. 'default': {
  38. 'BACKEND': 'wagtail.contrib.wagtailfrontendcache.backends.HTTPBackend',
  39. 'LOCATION': env['CACHE_PURGE_URL'],
  40. },
  41. }
  42. if 'STATIC_URL' in env:
  43. STATIC_URL = env['STATIC_URL']
  44. if 'STATIC_DIR' in env:
  45. STATIC_ROOT = env['STATIC_DIR']
  46. if 'MEDIA_URL' in env:
  47. MEDIA_URL = env['MEDIA_URL']
  48. if 'MEDIA_DIR' in env:
  49. MEDIA_ROOT = env['MEDIA_DIR']
  50. # Database
  51. if 'DATABASE_URL' in os.environ:
  52. DATABASES = {'default': dj_database_url.config()}
  53. else:
  54. DATABASES = {
  55. 'default': {
  56. 'ENGINE': 'django.db.backends.postgresql_psycopg2',
  57. 'NAME': env.get('PGDATABASE', APP_NAME),
  58. 'CONN_MAX_AGE': 600, # number of seconds database connections should persist for
  59. # User, host and port can be configured by the PGUSER, PGHOST and
  60. # PGPORT environment variables (these get picked up by libpq).
  61. }
  62. }
  63. # Redis
  64. # Redis location can either be passed through with REDIS_HOST or REDIS_SOCKET
  65. if 'REDIS_URL' in env:
  66. REDIS_LOCATION = env['REDIS_URL']
  67. BROKER_URL = env['REDIS_URL']
  68. elif 'REDIS_HOST' in env:
  69. REDIS_LOCATION = env['REDIS_HOST']
  70. BROKER_URL = 'redis://%s' % env['REDIS_HOST']
  71. elif 'REDIS_SOCKET' in env:
  72. REDIS_LOCATION = 'unix://%s' % env['REDIS_SOCKET']
  73. BROKER_URL = 'redis+socket://%s' % env['REDIS_SOCKET']
  74. else:
  75. REDIS_LOCATION = None
  76. if REDIS_LOCATION is not None:
  77. CACHES = {
  78. 'default': {
  79. 'BACKEND': 'django_redis.cache.RedisCache',
  80. 'LOCATION': REDIS_LOCATION,
  81. 'KEY_PREFIX': APP_NAME,
  82. 'OPTIONS': {
  83. 'CLIENT_CLASS': 'django_redis.client.DefaultClient',
  84. }
  85. }
  86. }
  87. # Elasticsearch
  88. if 'ELASTICSEARCH_URL' in env:
  89. WAGTAILSEARCH_BACKENDS = {
  90. 'default': {
  91. 'BACKEND': 'wagtail.wagtailsearch.backends.elasticsearch.ElasticSearch',
  92. 'URLS': [env['ELASTICSEARCH_URL']],
  93. 'INDEX': APP_NAME,
  94. 'ATOMIC_REBUILD': True,
  95. },
  96. }
  97. # Logging
  98. LOGGING = {
  99. 'version': 1,
  100. 'disable_existing_loggers': False,
  101. 'handlers': {
  102. 'mail_admins': {
  103. 'level': 'ERROR',
  104. 'class': 'django.utils.log.AdminEmailHandler',
  105. },
  106. },
  107. 'formatters': {
  108. 'default': {
  109. 'verbose': '[%(asctime)s] (%(process)d/%(thread)d) %(name)s %(levelname)s: %(message)s'
  110. }
  111. },
  112. 'loggers': {
  113. 'bakerydemo': {
  114. 'handlers': [],
  115. 'level': 'INFO',
  116. 'propagate': False,
  117. 'formatter': 'verbose',
  118. },
  119. 'wagtail': {
  120. 'handlers': [],
  121. 'level': 'INFO',
  122. 'propagate': False,
  123. 'formatter': 'verbose',
  124. },
  125. 'django.request': {
  126. 'handlers': ['mail_admins'],
  127. 'level': 'ERROR',
  128. 'propagate': False,
  129. 'formatter': 'verbose',
  130. },
  131. 'django.security': {
  132. 'handlers': ['mail_admins'],
  133. 'level': 'ERROR',
  134. 'propagate': False,
  135. 'formatter': 'verbose',
  136. },
  137. },
  138. }
  139. if 'LOG_DIR' in env:
  140. # bakerydemo log
  141. LOGGING['handlers']['bakerydemo_file'] = {
  142. 'level': 'INFO',
  143. 'class': 'cloghandler.ConcurrentRotatingFileHandler',
  144. 'filename': os.path.join(env['LOG_DIR'], 'bakerydemo.log'),
  145. 'maxBytes': 5242880, # 5MB
  146. 'backupCount': 5
  147. }
  148. LOGGING['loggers']['wagtail']['handlers'].append('bakerydemo_file')
  149. # Wagtail log
  150. LOGGING['handlers']['wagtail_file'] = {
  151. 'level': 'INFO',
  152. 'class': 'cloghandler.ConcurrentRotatingFileHandler',
  153. 'filename': os.path.join(env['LOG_DIR'], 'wagtail.log'),
  154. 'maxBytes': 5242880, # 5MB
  155. 'backupCount': 5
  156. }
  157. LOGGING['loggers']['wagtail']['handlers'].append('wagtail_file')
  158. # Error log
  159. LOGGING['handlers']['errors_file'] = {
  160. 'level': 'ERROR',
  161. 'class': 'cloghandler.ConcurrentRotatingFileHandler',
  162. 'filename': os.path.join(env['LOG_DIR'], 'error.log'),
  163. 'maxBytes': 5242880, # 5MB
  164. 'backupCount': 5
  165. }
  166. LOGGING['loggers']['django.request']['handlers'].append('errors_file')
  167. LOGGING['loggers']['django.security']['handlers'].append('errors_file')
  168. try:
  169. from .local import *
  170. except ImportError:
  171. pass