Projet

Général

Profil

« Précédent | Suivant » 

Révision 3d53199c

Ajouté par Jérôme Schneider il y a presque 10 ans

wcsinst: rewrite settings to support an ini file

Refs #4956

Voir les différences:

wcsinst/settings.py
1 1
# Django settings for wcsinst project.
2 2

  
3 3
import os
4
from ConfigParser import SafeConfigParser
5

  
6
# get configuration files from :
7
# 1. default-settings.ini from source code
8
# 2. os.environ.get('SETTINGS_INI') if it exists
9
#    else /etc/wcsinstd/settings.ini
10
#         and then /etc/wcsinstd/local-settings.ini
11
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
12
SETTINGS_INI = (os.path.join(BASE_DIR, 'default-settings.ini'),)
13
if os.environ.get('SETTINGS_INI'):
14
    SETTINGS_INI += (os.environ.get('SETTINGS_INI'),)
15
else:
16
    ETC_DIR = os.path.join('/', 'etc', 'univnautes-idp')
17
    SETTINGS_INI += (
18
        os.path.join(ETC_DIR, 'settings.ini'),
19
        os.path.join(ETC_DIR, 'local-settings.ini')
20
    )
4 21

  
5
DEBUG = 'DEBUG' in os.environ
6
TEMPLATE_DEBUG = DEBUG
7

  
8
PROJECT_PATH = os.path.dirname(os.path.dirname(__file__))
9

  
10
ADMINS = (
11
    # ('Your Name', 'your_email@example.com'),
12
)
22
config = SafeConfigParser()
23
config.read(SETTINGS_INI)
13 24

  
14
if 'ADMINS' in os.environ:
15
    ADMINS = filter(None, os.environ.get('ADMINS').split(':'))
16
    ADMINS = [ admin.split(';') for admin in ADMINS ]
17
    for admin in ADMINS:
18
        assert len(admin) == 2, 'ADMINS setting must be a colon separated list of name and emails separated by a semi-colon'
19
        assert '@' in admin[1], 'ADMINS setting pairs second value must be emails'
20 25

  
21
MANAGERS = ADMINS
26
DEBUG = config.getboolean('debug', 'general')
27
INTERNAL_IPS = tuple(config.get('debug', 'internal_ips').split())
28
TEMPLATE_DEBUG = config.getboolean('debug', 'template')
29
ADMINS = tuple(config.items('admins'))
30
MANAGERS = tuple(config.items('managers'))
31
SENTRY_DSN = config.get('debug', 'sentry_dsn')
32
DEBUG_TOOLBAR = config.getboolean('debug', 'toolbar')
22 33

  
23 34
DATABASES = {
24 35
    'default': {
25
        'ENGINE': 'django.db.backends.sqlite3',
26
        'NAME': os.path.join(PROJECT_PATH, 'wcsinst.sqlite3'),
36
        'ENGINE': config.get('database', 'engine'),
37
        'NAME': config.get('database','name'),
38
        'USER': config.get('database','user'),
39
        'PASSWORD': config.get('database','password'),
40
        'HOST': config.get('database','host'),
41
        'PORT': config.get('database','port'),
27 42
    }
28 43
}
29 44

  
30 45
# Hosts/domain names that are valid for this site; required if DEBUG is False
31 46
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
32
ALLOWED_HOSTS = []
47
ALLOWED_HOSTS = ['*']
48
USE_X_FORWARDED_HOST = True
33 49

  
34 50
# Local time zone for this installation. Choices can be found here:
35 51
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
36 52
# although not all choices may be available on all operating systems.
37 53
# In a Windows environment this must be set to your system time zone.
38
TIME_ZONE = 'Europe/Brussels'
54
TIME_ZONE = 'Europe/Paris'
39 55

  
40 56
# Language code for this installation. All choices can be found here:
41 57
# http://www.i18nguy.com/unicode/language-identifiers.html
42 58
LANGUAGE_CODE = 'fr-fr'
59
gettext_noop = lambda s: s
60
LANGUAGES = (
61
    ('en', gettext_noop('English')),
62
    ('fr', gettext_noop('French')),
63
)
43 64

  
44 65
SITE_ID = 1
45 66

  
......
56 77

  
57 78
# Absolute filesystem path to the directory that will hold user-uploaded files.
58 79
# Example: "/var/www/example.com/media/"
59
MEDIA_ROOT = os.path.join(PROJECT_PATH, 'media')
80
MEDIA_ROOT = config.get('dirs','media_root')
60 81

  
61 82
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
62 83
# trailing slash.
63 84
# Examples: "http://example.com/media/", "http://media.example.com/"
64
MEDIA_URL = '/media/'
85
MEDIA_URL = ''
65 86

  
66 87
# Absolute path to the directory static files should be collected to.
67 88
# Don't put anything in this directory yourself; store your static files
68 89
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
69 90
# Example: "/var/www/example.com/static/"
70
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
91
STATIC_ROOT = config.get('dirs','static_root')
71 92

  
72 93
# URL prefix for static files.
73 94
# Example: "http://example.com/static/", "http://static.example.com/"
74 95
STATIC_URL = '/static/'
75 96

  
76 97
# Additional locations of static files
77
STATICFILES_DIRS = (
78
)
98
STATICFILES_DIRS = tuple(config.get('dirs','static_dirs').split())
79 99

  
80 100
# List of finder classes that know how to find static files in
81 101
# various locations.
......
86 106
)
87 107

  
88 108
# Make this unique, and don't share it with anybody.
89
SECRET_KEY = 'sw-v(^psaet3)44flti-zr!=u64mzfeaodkey(m=&^nz(=43!o'
109
SECRET_KEY = config.get('secrets', 'secret_key')
90 110

  
91 111
# List of callables that know how to import templates from various sources.
92 112
TEMPLATE_LOADERS = (
......
105 125
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
106 126
)
107 127

  
108
ROOT_URLCONF = 'wcsinst.urls'
109

  
110 128
# Python dotted path to the WSGI application used by Django's runserver.
111 129
WSGI_APPLICATION = 'wcsinst.wsgi.application'
112 130

  
113
TEMPLATE_DIRS = (
114
    os.path.join(PROJECT_PATH, 'wcsinst', 'templates'),
115
)
131
TEMPLATE_DIRS = tuple(config.get('dirs', 'template_dirs').split())
116 132

  
117 133
INSTALLED_APPS = (
118 134
    'south',
......
125 141
    'django.contrib.admin',
126 142
)
127 143

  
144
ROOT_URLCONF = 'wcsinst.urls'
145

  
146
if config.getboolean('cache', 'memcached'):
147
    CACHES = {
148
        'default': {
149
            'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
150
            'LOCATION': '127.0.0.1:11211',
151
        },
152
    }
153

  
128 154
# A sample logging configuration. The only tangible logging
129 155
# performed by this configuration is to send an email to
130 156
# the site admins on every HTTP 500 error when DEBUG=False.
......
138 164
            '()': 'django.utils.log.RequireDebugFalse'
139 165
        }
140 166
    },
167
    'formatters': {
168
        'verbose': {
169
            'format': '[%(asctime)s] %(levelname)s %(name)s: %(message)s',
170
            'datefmt': '%Y-%m-%d %a %H:%M:%S'
171
            },
172
        'syslog': {
173
            'format': '%(levelname)s %(name)s: %(message)s',
174
            },
175
        },
141 176
    'handlers': {
177
        'console': {
178
            'level': 'DEBUG',
179
            'class':'logging.StreamHandler',
180
            'formatter': 'verbose',
181
        },
142 182
        'mail_admins': {
143 183
            'level': 'ERROR',
144 184
            'filters': ['require_debug_false'],
145 185
            'class': 'django.utils.log.AdminEmailHandler'
146
        }
186
            },
187
        'syslog': {
188
            'level': 'DEBUG',
189
            'address': '/dev/log',
190
            'class': 'logging.handlers.SysLogHandler',
191
            'formatter': 'syslog',
192
            },
147 193
    },
148 194
    'loggers': {
149 195
        'django.request': {
......
151 197
            'level': 'ERROR',
152 198
            'propagate': True,
153 199
        },
200
        '': {
201
            'handlers': ['mail_admins', 'syslog', 'console'],
202
            'level': 'INFO',
203
        }
154 204
    }
155 205
}
156 206

  
207
# debug settings
208
if DEBUG:
209
    LOGGING['loggers']['']['level'] = 'DEBUG'
210

  
211
# email settings
212
EMAIL_HOST = config.get('email', 'host')
213
EMAIL_PORT = config.getint('email', 'port')
214
EMAIL_HOST_USER = config.get('email', 'user')
215
EMAIL_HOST_PASSWORD = config.get('email', 'password')
216
EMAIL_SUBJECT_PREFIX = config.get('email', 'subject_prefix')
217
EMAIL_USE_TLS = config.getboolean('email', 'use_tls')
218
SERVER_EMAIL = config.get('email', 'server_email')
219
DEFAULT_FROM_EMAIL = config.get('email', 'default_from_email')
220

  
221
# wcsinst / wcsintd settings
157 222
WCSINSTD_URL = os.environ.get('WCSINSTD_URL')
158

  
159 223
if WCSINSTD_URL:
160 224
    INSTALLED_APPS += ('wcsinst.wcsinst',)
161 225
else:
162 226
    INSTALLED_APPS += ('wcsinst.wcsinstd',)
163

  
164
WCSINST_WCSCTL_SCRIPT = os.environ.get('WCSINST_WCSCTL_SCRIPT', 'wcsctl')
165

  
166
if 'SENTRY_DSN' in os.environ:
227
WCSINST_URL_TEMPLATE = config.get('wcsinstd', 'url_template')
228
WCSINST_WCSCTL_SCRIPT = config.get('wcsinstd', 'wcsctl_script')
229
WCSINST_WCS_APP_DIR = config.get('wcsinstd', 'wcs_app_dir')
230

  
231
# debug toolbar needs more
232
if DEBUG_TOOLBAR:
233
    DEBUG_TOOLBAR_CONFIG = {'INTERCEPT_REDIRECTS': False}
234
    INSTALLED_APPS += ('debug_toolbar',)
235
    MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',)
236

  
237
# sentry needs more
238
if SENTRY_DSN:
239
    RAVEN_CONFIG = {'dsn': SENTRY_DSN}
167 240
    INSTALLED_APPS += ('raven.contrib.django.raven_compat',)
168
    RAVEN_CONFIG = os.environ.get('SENTRY_DSN')
241
    LOGGING['handlers']['sentry'] = {
242
            'level': 'ERROR',
243
            'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
244
            }
245
    LOGGING['loggers']['']['handlers'].append('sentry')
169 246

  
170 247
try:
171 248
    from local_settings import *
172 249
except ImportError:
173 250
    pass
251

  

Formats disponibles : Unified diff