Fix loading of conext-dependant Django middleware
[mygpo.git] / mygpo / settings.py
blob2cc7e2ca497fc2c52c48c48c5ead44baeef40808
1 import re
2 import sys
3 import os.path
4 import dj_database_url
7 BASE_DIR = os.path.dirname(os.path.abspath(__file__))
10 def get_bool(name, default):
11 return os.getenv(name, str(default)).lower() == 'true'
14 def get_intOrNone(name, default):
15 """ Parses the env variable, accepts ints and literal None"""
16 value = os.getenv(name, str(default))
17 if value.lower() == 'none':
18 return None
19 return int(value)
22 DEBUG = get_bool('DEBUG', False)
24 ADMINS = re.findall(r'\s*([^<]+) <([^>]+)>\s*', os.getenv('ADMINS', ''))
26 MANAGERS = ADMINS
28 DATABASES = {
29 'default': dj_database_url.config(
30 default='postgres://mygpo:mygpo@localhost/mygpo'),
34 _cache_used = bool(os.getenv('CACHE_BACKEND', False))
36 if _cache_used:
37 CACHES = {}
38 CACHES['default'] = {
39 'BACKEND': os.getenv(
40 'CACHE_BACKEND',
41 'django.core.cache.backends.memcached.MemcachedCache'),
42 'LOCATION': os.getenv('CACHE_LOCATION'),
46 # Local time zone for this installation. Choices can be found here:
47 # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
48 # although not all choices may be available on all operating systems.
49 # If running in a Windows environment this must be set to the same as your
50 # system time zone.
51 TIME_ZONE = 'UTC'
53 # Language code for this installation. All choices can be found here:
54 # http://www.i18nguy.com/unicode/language-identifiers.html
55 LANGUAGE_CODE = 'en-us'
57 SITE_ID = 1
59 # If you set this to False, Django will make some optimizations so as not
60 # to load the internationalization machinery.
61 USE_I18N = True
63 STATIC_ROOT = 'staticfiles'
64 STATIC_URL = '/media/'
66 STATICFILES_DIRS = (
67 os.path.abspath(os.path.join(BASE_DIR, '..', 'htdocs', 'media')),
71 TEMPLATES = [{
72 'BACKEND': 'django.template.backends.django.DjangoTemplates',
73 'DIRS': [],
74 'OPTIONS': {
75 'debug': DEBUG,
76 'context_processors': [
77 'django.contrib.auth.context_processors.auth',
78 'django.template.context_processors.debug',
79 'django.template.context_processors.i18n',
80 'django.template.context_processors.media',
81 'django.template.context_processors.static',
82 'django.template.context_processors.tz',
83 'django.contrib.messages.context_processors.messages',
84 'mygpo.web.google.analytics',
85 'mygpo.web.google.adsense',
86 # make the debug variable available in templates
87 # https://docs.djangoproject.com/en/dev/ref/templates/api/#django-core-context-processors-debug
88 'django.template.context_processors.debug',
90 # required so that the request obj can be accessed from
91 # templates. this is used to direct users to previous
92 # page after login
93 'django.template.context_processors.request',
95 'libraries': {
96 'staticfiles' : 'django.templatetags.static',
98 'loaders': [
99 ('django.template.loaders.cached.Loader', [
100 'django.template.loaders.app_directories.Loader',
107 MIDDLEWARE = [
108 'django.middleware.common.CommonMiddleware',
109 'django.middleware.csrf.CsrfViewMiddleware',
110 'django.contrib.sessions.middleware.SessionMiddleware',
111 'django.contrib.auth.middleware.AuthenticationMiddleware',
112 'django.middleware.locale.LocaleMiddleware',
113 'django.contrib.messages.middleware.MessageMiddleware',
116 ROOT_URLCONF = 'mygpo.urls'
118 INSTALLED_APPS = [
119 'django.contrib.contenttypes',
120 'django.contrib.messages',
121 'django.contrib.admin',
122 'django.contrib.humanize',
123 'django.contrib.auth',
124 'django.contrib.sessions',
125 'django.contrib.staticfiles',
126 'django.contrib.sites',
127 'djcelery',
128 'mygpo.core',
129 'mygpo.podcasts',
130 'mygpo.chapters',
131 'mygpo.search',
132 'mygpo.users',
133 'mygpo.api',
134 'mygpo.web',
135 'mygpo.publisher',
136 'mygpo.subscriptions',
137 'mygpo.history',
138 'mygpo.favorites',
139 'mygpo.usersettings',
140 'mygpo.data',
141 'mygpo.userfeeds',
142 'mygpo.suggestions',
143 'mygpo.directory',
144 'mygpo.categories',
145 'mygpo.episodestates',
146 'mygpo.maintenance',
147 'mygpo.share',
148 'mygpo.administration',
149 'mygpo.pubsub',
150 'mygpo.podcastlists',
151 'mygpo.votes',
152 'django_nose',
155 try:
156 if DEBUG:
157 import debug_toolbar
158 INSTALLED_APPS += ['debug_toolbar']
159 MIDDLEWARE += ['debug_toolbar.middleware.DebugToolbarMiddleware']
161 except ImportError:
162 pass
165 try:
166 import opbeat
168 if not DEBUG:
169 INSTALLED_APPS += ['opbeat.contrib.django']
171 except ImportError:
172 pass
175 ACCOUNT_ACTIVATION_DAYS = int(os.getenv('ACCOUNT_ACTIVATION_DAYS', 7))
177 AUTHENTICATION_BACKENDS = (
178 'mygpo.users.backend.CaseInsensitiveModelBackend',
179 'mygpo.web.auth.EmailAuthenticationBackend',
182 SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"
184 # TODO: use (default) JSON serializer for security
185 # this would currently fail as we're (de)serializing datetime objects
186 # https://docs.djangoproject.com/en/1.5/topics/http/sessions/#session-serialization
187 SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'
190 MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'
192 USER_CLASS = 'mygpo.users.models.User'
194 LOGIN_URL = '/login/'
196 CSRF_FAILURE_VIEW = 'mygpo.web.views.csrf_failure'
199 DEFAULT_FROM_EMAIL = os.getenv('DEFAULT_FROM_EMAIL', '')
201 SECRET_KEY = os.getenv('SECRET_KEY', '')
203 if 'test' in sys.argv:
204 SECRET_KEY = 'test'
206 GOOGLE_ANALYTICS_PROPERTY_ID = os.getenv('GOOGLE_ANALYTICS_PROPERTY_ID', '')
208 DIRECTORY_EXCLUDED_TAGS = os.getenv('DIRECTORY_EXCLUDED_TAGS', '').split()
210 FLICKR_API_KEY = os.getenv('FLICKR_API_KEY', '')
212 SOUNDCLOUD_CONSUMER_KEY = os.getenv('SOUNDCLOUD_CONSUMER_KEY', '')
214 MAINTENANCE = get_bool('MAINTENANCE', False)
217 ALLOWED_HOSTS = ['*']
220 LOGGING = {
221 'version': 1,
222 'disable_existing_loggers': False,
223 'formatters': {
224 'verbose': {
225 'format': '%(asctime)s %(name)s %(levelname)s %(message)s',
228 'filters': {
229 'require_debug_false': {
230 '()': 'django.utils.log.RequireDebugFalse'
233 'handlers': {
234 'console': {
235 'level': os.getenv('LOGGING_CONSOLE_LEVEL', 'DEBUG'),
236 'class': 'logging.StreamHandler',
237 'formatter': 'verbose',
239 'mail_admins': {
240 'level': 'ERROR',
241 'filters': ['require_debug_false'],
242 'class': 'django.utils.log.AdminEmailHandler',
245 'loggers': {
246 'django': {
247 'handlers': os.getenv('LOGGING_DJANGO_HANDLERS',
248 'console').split(),
249 'propagate': True,
250 'level': os.getenv('LOGGING_DJANGO_LEVEL', 'WARN'),
252 'mygpo': {
253 'handlers': os.getenv('LOGGING_MYGPO_HANDLERS', 'console').split(),
254 'level': os.getenv('LOGGING_MYGPO_LEVEL', 'INFO'),
256 'celery': {
257 'handlers': os.getenv('LOGGING_CELERY_HANDLERS',
258 'console').split(),
259 'level': os.getenv('LOGGING_CELERY_LEVEL', 'DEBUG'),
264 _use_log_file = bool(os.getenv('LOGGING_FILENAME', False))
266 if _use_log_file:
267 LOGGING['handlers']['file'] = {
268 'level': 'INFO',
269 'class': 'logging.handlers.RotatingFileHandler',
270 'filename': os.getenv('LOGGING_FILENAME'),
271 'maxBytes': 10000000,
272 'backupCount': 10,
273 'formatter': 'verbose',
277 # minimum number of subscribers a podcast must have to be assigned a slug
278 PODCAST_SLUG_SUBSCRIBER_LIMIT = int(os.getenv(
279 'PODCAST_SLUG_SUBSCRIBER_LIMIT', 10))
281 # minimum number of subscribers that a podcast needs to "push" one of its
282 # categories to the top
283 MIN_SUBSCRIBERS_CATEGORY = int(os.getenv('MIN_SUBSCRIBERS_CATEGORY', 10))
285 # maximum number of episode actions that the API processes immediatelly before
286 # returning the response. Larger requests will be handled in background.
287 # Handler can be set to None to disable
288 API_ACTIONS_MAX_NONBG = int(os.getenv('API_ACTIONS_MAX_NONBG', 100))
289 API_ACTIONS_BG_HANDLER = 'mygpo.api.tasks.episode_actions_celery_handler'
292 ADSENSE_CLIENT = os.getenv('ADSENSE_CLIENT', '')
294 ADSENSE_SLOT_BOTTOM = os.getenv('ADSENSE_SLOT_BOTTOM', '')
296 # we're running behind a proxy that sets the X-Forwarded-Proto header correctly
297 # see https://docs.djangoproject.com/en/dev/ref/settings/#secure-proxy-ssl-header
298 SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
301 # enabled access to staff-only areas with ?staff=<STAFF_TOKEN>
302 STAFF_TOKEN = os.getenv('STAFF_TOKEN', None)
304 # Flattr settings -- available after you register your app
305 FLATTR_KEY = os.getenv('FLATTR_KEY', '')
306 FLATTR_SECRET = os.getenv('FLATTR_SECRET', '')
308 # Flattr thing of the webservice. Will be flattr'd when a user sets
309 # the "Auto-Flattr gpodder.net" option
310 FLATTR_MYGPO_THING = os.getenv(
311 'FLATTR_MYGPO_THING',
312 'https://flattr.com/submit/auto?user_id=stefankoegl&url=http://gpodder.net'
315 # The User-Agent string used for outgoing HTTP requests
316 USER_AGENT = 'gpodder.net (+https://github.com/gpodder/mygpo)'
318 # Base URL of the website that is used if the actually used parameters is not
319 # available. Request handlers, for example, can access the requested domain.
320 # Code that runs in background can not do this, and therefore requires a
321 # default value. This should be set to something like 'http://example.com'
322 DEFAULT_BASE_URL = os.getenv('DEFAULT_BASE_URL', '')
325 ### Celery
327 BROKER_URL = os.getenv('BROKER_URL', 'redis://localhost')
328 CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend'
330 SERVER_EMAIL = os.getenv('SERVER_EMAIL', 'no-reply@example.com')
332 CELERY_TASK_RESULT_EXPIRES = 60 * 60 # 1h expiry time in seconds
334 CELERY_ACCEPT_CONTENT = ['pickle', 'json']
336 CELERY_SEND_TASK_ERROR_EMAILS = get_bool('CELERY_SEND_TASK_ERROR_EMAILS',
337 False)
339 BROKER_POOL_LIMIT = get_intOrNone('BROKER_POOL_LIMIT', 10)
341 ### Google API
343 GOOGLE_CLIENT_ID = os.getenv('GOOGLE_CLIENT_ID', '')
344 GOOGLE_CLIENT_SECRET = os.getenv('GOOGLE_CLIENT_SECRET', '')
346 # URL where users of the site can get support
347 SUPPORT_URL = os.getenv('SUPPORT_URL', '')
350 FEEDSERVICE_URL = os.getenv('FEEDSERVICE_URL', 'http://feeds.gpodder.net/')
352 # Elasticsearch settings
354 ELASTICSEARCH_SERVER = os.getenv('ELASTICSEARCH_SERVER', '127.0.0.1:9200')
355 ELASTICSEARCH_INDEX = os.getenv('ELASTICSEARCH_INDEX', 'mygpo')
356 ELASTICSEARCH_TIMEOUT = float(os.getenv('ELASTICSEARCH_TIMEOUT', '2'))
358 # time for how long an activation is valid; after that, an unactivated user
359 # will be deleted
360 ACTIVATION_VALID_DAYS = int(os.getenv('ACTIVATION_VALID_DAYS', 10))
363 OPBEAT = {
364 "ORGANIZATION_ID": os.getenv('OPBEAT_ORGANIZATION_ID', ''),
365 "APP_ID": os.getenv('OPBEAT_APP_ID', ''),
366 "SECRET_TOKEN": os.getenv('OPBEAT_SECRET_TOKEN', ''),
369 LOCALE_PATHS = [
370 os.path.abspath(os.path.join(BASE_DIR, 'locale')),
373 INTERNAL_IPS = os.getenv('INTERNAL_IPS', '').split()
375 EMAIL_BACKEND = os.getenv('EMAIL_BACKEND',
376 'django.core.mail.backends.smtp.EmailBackend')
378 PODCAST_AD_ID = os.getenv('PODCAST_AD_ID')
380 TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
382 NOSE_ARGS = [
383 '--with-doctest',
384 '--stop',
385 '--where=mygpo',