Newer
Older
"""
Django settings for opentech project.
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
import sys
import dj_database_url
import raven
from raven.exceptions import InvalidGitRepository
env = os.environ.copy()
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
BASE_DIR = os.path.dirname(PROJECT_DIR)
APP_NAME = env.get('APP_NAME', 'opentech')
# Application definition
INSTALLED_APPS = [
'opentech.images',
'opentech.apply.categories',
'opentech.apply.dashboard',
'opentech.public.funds',
'opentech.public.navigation',
'opentech.public.news',
'opentech.public.people',
'opentech.public.search',
'opentech.public.standardpages',
'opentech.public.utils',
'wagtail.contrib.modeladmin',
'wagtail.contrib.postgres_search',
'wagtail.contrib.settings',
'wagtail.contrib.search_promotions',
'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.search',
'wagtail.admin',
'wagtail.core',
'django_bleach',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.postgres',
'django.contrib.staticfiles',
'django.contrib.sitemaps',
'django.forms',
MIDDLEWARE = [
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'opentech.apply.users.middleware.SocialAuthExceptionMiddleware',
'wagtail.core.middleware.SiteMiddleware',
'wagtail.contrib.redirects.middleware.RedirectMiddleware',
'opentech.apply.middleware.apply_url_conf_middleware',
]
ROOT_URLCONF = 'opentech.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(PROJECT_DIR, 'templates'),
os.path.join(PROJECT_DIR, 'apply', 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'wagtail.contrib.settings.context_processors.settings',
'opentech.public.utils.context_processors.global_vars',
'social_django.context_processors.backends',
'social_django.context_processors.login_redirect',
FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'
WSGI_APPLICATION = 'opentech.wsgi.application'
# Database
# https://docs.djangoproject.com/en/stable/ref/settings/#databases
DATABASES = {
'default': dj_database_url.config(
conn_max_age=600,
default=f"postgres:///{APP_NAME}"
)
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
}
# Cache
# Use database cache as the cache backend
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'database_cache',
}
}
# Search
WAGTAILSEARCH_BACKENDS = {
'default': {
'BACKEND': 'wagtail.contrib.postgres_search.backend',
},
}
# Password validation
# https://docs.djangoproject.com/en/stable/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/stable/topics/i18n/
LANGUAGE_CODE = 'en-gb'
TIME_ZONE = 'Europe/London'
USE_I18N = True
DATE_FORMAT = 'Y-m-d'
DATETIME_FORMAT = 'Y-m-d\TH:i:s'
SHORT_DATE_FORMAT = 'Y-m-d'
SHORT_DATETIME_FORMAT = 'Y-m-d\TH:i:s'
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/stable/howto/static-files/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
os.path.join(PROJECT_DIR, 'static_compiled'),
STATIC_ROOT = env.get('STATIC_DIR', os.path.join(BASE_DIR, 'static'))
STATIC_URL = env.get('STATIC_URL', '/static/')
MEDIA_ROOT = env.get('MEDIA_DIR', os.path.join(BASE_DIR, 'media'))
MEDIA_URL = env.get('MEDIA_URL', '/media/')
WAGTAIL_USER_EDIT_FORM = 'opentech.apply.users.forms.CustomUserEditForm'
WAGTAIL_USER_CREATION_FORM = 'opentech.apply.users.forms.CustomUserCreationForm'
WAGTAIL_USER_CUSTOM_FIELDS = ['full_name']
LOGIN_URL = 'users_public:login'
LOGIN_REDIRECT_URL = 'dashboard:dashboard'
AUTHENTICATION_BACKENDS = (
'social_core.backends.google.GoogleOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
# Logging
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
# Send logs with at least INFO level to the console.
'class': 'logging.StreamHandler',
'formatter': 'verbose',
},
# Send logs with level of at least ERROR to Sentry.
'sentry': {
'level': 'ERROR',
'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
},
'format': '[%(asctime)s][%(process)d][%(levelname)s][%(name)s] %(message)s'
'level': 'INFO',
'propagate': False,
},
'wagtail': {
'level': 'INFO',
'propagate': False,
},
'django.request': {
'handlers': ['console', 'sentry'],
'level': 'WARNING',
'handlers': ['console', 'sentry'],
'level': 'WARNING',
'propagate': False,
},
},
}
# Wagtail settings
WAGTAIL_SITE_NAME = "opentech"
WAGTAILIMAGES_IMAGE_MODEL = "images.CustomImage"
WAGTAILIMAGES_FEATURE_DETECTION_ENABLED = False
WAGTAILADMIN_RICH_TEXT_EDITORS = {
'default': {
'WIDGET': 'wagtail.admin.rich_text.DraftailRichTextArea',
# fixed in wagtail 2.0.1: https://github.com/wagtail/wagtail/commit/09f8a4f38a95f2760f38ab2f142443df93b5d8c6
# 'OPTIONS': {
# 'features': [
# 'bold', 'italic',
# 'h3', 'h4', 'h5',
# 'ol', 'ul',
# 'link'
# ]
# }
},
}
PASSWORD_REQUIRED_TEMPLATE = 'password_required.html'
DEFAULT_PER_PAGE = 20
ESI_ENABLED = False
# Custom settings
ENABLE_STYLEGUIDE = False
# Social Auth
SOCIAL_AUTH_URL_NAMESPACE = 'social'
# Set the Google OAuth2 credentials in ENV variables or local.py
# To create a new set of credentials, go to https://console.developers.google.com/apis/credentials
# Make sure the Google+ API is enabled for your API project
STAFF_EMAIL_DOMAINS = ['opentech.fund']
SOCIAL_AUTH_GOOGLE_OAUTH2_WHITELISTED_DOMAINS = STAFF_EMAIL_DOMAINS
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ''
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ''
SOCIAL_AUTH_LOGIN_ERROR_URL = 'users_public:login'
SOCIAL_AUTH_NEW_ASSOCIATION_REDIRECT_URL = 'users:account'
# For pipelines, see http://python-social-auth.readthedocs.io/en/latest/pipeline.html?highlight=pipelines#authentication-pipeline
# Create / associate accounts (including by email)
SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.auth_allowed',
'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username',
'social_core.pipeline.social_auth.associate_by_email',
'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details',
'opentech.apply.users.pipeline.make_otf_staff',
# Bleach Settings
BLEACH_ALLOWED_TAGS = ['h2', 'h3', 'p', 'b', 'i', 'em', 'strong', 'a', 'ul', 'ol', 'li', 'br']
BLEACH_ALLOWED_ATTRIBUTES = ['href', 'title', 'style']
BLEACH_ALLOWED_STYLES = ['font-family', 'font-weight', 'text-decoration', 'font-variant']
BLEACH_STRIP_TAGS = True
BLEACH_STRIP_COMMENTS = True
# Hijack Settings
HIJACK_LOGIN_REDIRECT_URL = '/dashboard/'
HIJACK_LOGOUT_REDIRECT_URL = '/account/'
HIJACK_DECORATOR = 'opentech.apply.users.decorators.superuser_decorator'
SEND_MESSAGES = env.get('SEND_MESSAGES', 'false').lower() == 'true'
SLACK_DESTINATION_URL = env.get('SLACK_DESTINATION_URL', None)
SLACK_DESTINATION_ROOM = env.get('SLACK_DESTINATION_ROOM', None)
# Celery config
if 'REDIS_URL' in env:
CELERY_BROKER_URL = env.get('REDIS_URL')
else:
CELERY_TASK_ALWAYS_EAGER = True
# S3 configuration
if 'AWS_STORAGE_BUCKET_NAME' in env:
Fredrik Jonsson
committed
DEFAULT_FILE_STORAGE = 'opentech.storage_backends.PublicMediaStorage'
PRIVATE_FILE_STORAGE = 'opentech.storage_backends.PrivateMediaStorage'
AWS_STORAGE_BUCKET_NAME = env['AWS_STORAGE_BUCKET_NAME']
Fredrik Jonsson
committed
if 'AWS_PUBLIC_BUCKET_NAME' in env:
AWS_PUBLIC_BUCKET_NAME = env['AWS_PUBLIC_BUCKET_NAME']
else:
AWS_PUBLIC_BUCKET_NAME = env['AWS_STORAGE_BUCKET_NAME']
if 'AWS_PRIVATE_BUCKET_NAME' in env:
AWS_PRIVATE_BUCKET_NAME = env['AWS_PRIVATE_BUCKET_NAME']
else:
AWS_PRIVATE_BUCKET_NAME = env['AWS_STORAGE_BUCKET_NAME']
if 'AWS_S3_CUSTOM_DOMAIN' in env:
AWS_S3_CUSTOM_DOMAIN = env['AWS_S3_CUSTOM_DOMAIN']
Fredrik Jonsson
committed
if 'AWS_QUERYSTRING_EXPIRE' in env:
AWS_QUERYSTRING_EXPIRE = env['AWS_QUERYSTRING_EXPIRE']
if 'AWS_PUBLIC_CUSTOM_DOMAIN' in env:
AWS_PUBLIC_CUSTOM_DOMAIN = env['AWS_PUBLIC_CUSTOM_DOMAIN']
MAILCHIMP_API_KEY = env.get('MAILCHIMP_API_KEY')
MAILCHIMP_LIST_ID = env.get('MAILCHIMP_LIST_ID')
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# Raven (sentry) configuration.
if 'SENTRY_DSN' in env:
INSTALLED_APPS += (
'raven.contrib.django.raven_compat',
)
RAVEN_CONFIG = {
'dsn': env['SENTRY_DSN'],
'tags': {},
}
# Specifying the programming language as a tag can be useful when
# e.g. javascript error logging is enabled within the same project,
# so that errors can be filtered by the programming language too.
# The 'lang' tag is just an arbitrarily chosen one; any other tags can be used as well.
# It has to overriden in javascript: Raven.setTagsContext({lang: 'javascript'});
RAVEN_CONFIG['tags']['lang'] = 'python'
# Prevent logging errors from the django shell.
# Errors from other managenent commands will be still logged.
if len(sys.argv) > 1 and sys.argv[1] in ['shell', 'shell_plus']:
RAVEN_CONFIG['ignore_exceptions'] = ['*']
# There's a chooser to toggle between environments at the top right corner on sentry.io
# Values are typically 'staging' or 'production' but can be set to anything else if needed.
# heroku config:set SENTRY_ENVIRONMENT=production
if 'SENTRY_ENVIRONMENT' in env:
RAVEN_CONFIG['environment'] = env['SENTRY_ENVIRONMENT']
try:
RAVEN_CONFIG['release'] = raven.fetch_git_sha(BASE_DIR)
except InvalidGitRepository:
try:
RAVEN_CONFIG['release'] = env['GIT_REV']
except KeyError:
pass