-
Notifications
You must be signed in to change notification settings - Fork 46
/
common.py
446 lines (384 loc) · 16 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
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
# -*- coding: utf-8 -*-
"""Django settings for {{cookiecutter.project_name}} project.
see: https://docs.djangoproject.com/en/dev/ref/settings/
"""
# Third Party Stuff
{%- if cookiecutter.use_sentry_for_error_reporting == 'y' %}
import os
{%- endif %}
import environ
from django.utils.translation import ugettext_lazy as _
ROOT_DIR = environ.Path(__file__) - 2 # (/a/b/myfile.py - 2 = /a/)
APPS_DIR = ROOT_DIR.path('{{ cookiecutter.main_module }}')
env = environ.Env()
# INSTALLED APPS
# ==========================================================================
# List of strings representing installed apps.
# See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django_sites', # http://niwinz.github.io/django-sites/latest/
'django.contrib.messages',
'django.contrib.staticfiles',
'flat_responsive',
'django.contrib.admin',
# 'django.contrib.humanize', # Useful template tags
'{{ cookiecutter.main_module }}.base',
'{{ cookiecutter.main_module }}.users',
'rest_framework', # http://www.django-rest-framework.org/
'rest_framework_swagger',
'versatileimagefield', # https://github.com/WGBH/django-versatileimagefield/
{%- if cookiecutter.add_django_cors_headers.lower() == 'y' %}
'corsheaders', # https://github.com/ottoyiu/django-cors-headers/
{%- endif %}
{%- if cookiecutter.add_sass_with_django_compressor.lower() == 'y' %}
'compressor',
{%- endif %}
{%- if cookiecutter.use_sentry_for_error_reporting == 'y' %}
'raven.contrib.django.raven_compat',
{%- endif %}
)
# INSTALLED APPS CONFIGURATION
# ==========================================================================
# django.contrib.auth
# ------------------------------------------------------------------------------
AUTH_USER_MODEL = 'users.User'
AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',)
PASSWORD_HASHERS = [
'django.contrib.auth.hashers.Argon2PasswordHasher',
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
'django.contrib.auth.hashers.BCryptPasswordHasher',
]
AUTH_PASSWORD_VALIDATORS = [
{'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', },
{'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 'OPTIONS': {'min_length': 6, }},
{'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', },
{'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', },
]
# For Exposing browsable api urls. By default urls won't be exposed.
API_DEBUG = env.bool('API_DEBUG', default=False)
# rest_framework
# ------------------------------------------------------------------------------
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': '{{ cookiecutter.main_module }}.base.api.pagination.PageNumberPagination',
'PAGE_SIZE': 30,
# Default renderer classes for Rest framework
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
'rest_framework.renderers.BrowsableAPIRenderer',
],
# 'Accept' header based versioning
# http://www.django-rest-framework.org/api-guide/versioning/
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.AcceptHeaderVersioning',
'DEFAULT_VERSION': '1.0',
'ALLOWED_VERSIONS': ['1.0', ],
'VERSION_PARAMETER': 'version',
# Use Django's standard `django.contrib.auth` permissions,
# or allow read-only access for unauthenticated users.
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
'DEFAULT_THROTTLE_CLASSES': (
'rest_framework.throttling.AnonRateThrottle',
),
'DEFAULT_THROTTLE_RATES': {
'anon': '10000/day',
},
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.BasicAuthentication',
# Mainly used for api debug.
'rest_framework.authentication.SessionAuthentication',
),
'EXCEPTION_HANDLER': '{{ cookiecutter.main_module }}.base.exceptions.exception_handler',
}
# DJANGO_SITES
# ------------------------------------------------------------------------------
# see: http://django-sites.readthedocs.org
SITES = {
'local': {'domain': 'localhost:8000', 'scheme': 'http', 'name': 'localhost'},
}
SITE_ID = 'local'
# MIDDLEWARE CONFIGURATION
# ------------------------------------------------------------------------------
# List of middleware classes to use. Order is important; in the request phase,
# this middleware classes will be applied in the order given, and in the
# response phase the middleware will be applied in reverse order.
MIDDLEWARE = [
{%- if cookiecutter.add_django_cors_headers.lower() == 'y' %}
'corsheaders.middleware.CorsMiddleware',
{%- endif %}
'log_request_id.middleware.RequestIDMiddleware', # For generating/adding Request id for all the logs
'django.middleware.security.SecurityMiddleware',
'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',
]
# DJANGO CORE
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug
# Defaults to false, which is safe, enable them only in development.
DEBUG = env.bool('DJANGO_DEBUG', False)
# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = '{{ cookiecutter.timezone }}'
# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = True
# Language code for this installation. All choices can be found here:
# http://www.i18nguy.com/unicode/language-identifiers.html
LANGUAGE_CODE = 'en-us'
# Languages we provide translations for
LANGUAGES = (
('en', _('English')),
)
# A tuple of directories where Django looks for translation files.
LOCALE_PATHS = (
str(APPS_DIR.path('locale')),
)
# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True
# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True
# The list of directories to search for fixtures
# See: https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-FIXTURE_DIRS
FIXTURE_DIRS = (
str(APPS_DIR.path('fixtures')),
)
# The Python dotted path to the WSGI application that Django's internal servers
# (runserver, runfcgi) will use. If `None`, the return value of
# 'django.core.wsgi.get_wsgi_application' is used, thus preserving the same
# behavior as previous versions of Django. Otherwise this should point to an
# actual WSGI application object.
# See: https://docs.djangoproject.com/en/dev/ref/settings/#wsgi-application
WSGI_APPLICATION = 'wsgi.application'
# URL CONFIGURATION
# ------------------------------------------------------------------------------
ROOT_URLCONF = '{{ cookiecutter.main_module }}.urls'
# Use this to change base url path django admin
DJANGO_ADMIN_URL = env.str('DJANGO_ADMIN_URL', default='admin')
# EMAIL CONFIGURATION
# ------------------------------------------------------------------------------
EMAIL_BACKEND = env('DJANGO_EMAIL_BACKEND',
default='django.core.mail.backends.smtp.EmailBackend')
# DATABASE CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': env.db('DATABASE_URL', default='postgres://localhost/{{ cookiecutter.main_module }}'),
}
DATABASES['default']['ATOMIC_REQUESTS'] = True
DATABASES['default']['CONN_MAX_AGE'] = 10
{% if cookiecutter.postgis.lower() == 'y' %}DATABASES['default']['ENGINE'] = 'django.contrib.gis.db.backends.postgis'{% endif %}
# TEMPLATE CONFIGURATION
# -----------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
str(APPS_DIR.path('templates')),
],
'OPTIONS': {
'debug': DEBUG,
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
# See: https://docs.djangoproject.com/en/dev/ref/settings/#template-context-processors
'context_processors': [
'{{cookiecutter.main_module}}.base.context_processors.site_settings',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.request',
# Your stuff: custom template context processors go here
],
},
},
]
CSRF_FAILURE_VIEW = '{{ cookiecutter.main_module }}.base.views.csrf_failure'
# STATIC FILE CONFIGURATION
# -----------------------------------------------------------------------------
# Absolute path to the directory static files should be collected to.
# Example: '/var/www/example.com/static/'
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = str(ROOT_DIR.path('.staticfiles'))
# URL that handles the static files served from STATIC_ROOT.
# Example: 'http://example.com/static/', 'http://static.example.com/'
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-url
STATIC_URL = '/static/'
# A list of locations of additional static files
STATICFILES_DIRS = (
str(APPS_DIR.path('static')),
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
{%- if cookiecutter.add_sass_with_django_compressor.lower() == 'y' %}
'compressor.finders.CompressorFinder',
{%- endif %}
)
{%- if cookiecutter.add_sass_with_django_compressor.lower() == 'y' %}
# Django Compressor Configuration
COMPRESS_CSS_FILTERS = [
'django_compressor_autoprefixer.AutoprefixerFilter',
'compressor.filters.cssmin.CSSMinFilter',
]
COMPRESS_PRECOMPILERS = (
('text/x-scss', 'django_libsass.SassCompiler'),
)
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = env.bool('COMPRESS_OFFLINE', default=False)
{%- endif %}
# MEDIA CONFIGURATION
# ------------------------------------------------------------------------------
# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example: '/var/www/example.com/media/'
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-root
MEDIA_ROOT = str(ROOT_DIR.path('.media'))
# URL that handles the media served from MEDIA_ROOT.
# Examples: 'http://example.com/media/', 'http://media.example.com/'
# See: https://docs.djangoproject.com/en/dev/ref/settings/#media-url
MEDIA_URL = '/media/'
# SECURITY
# -----------------------------------------------------------------------------
CSRF_COOKIE_HTTPONLY = False # Allow javascripts to read CSRF token from cookies
SESSION_COOKIE_HTTPONLY = True # Do not allow Session cookies to be read by javascript
SECURE_CONTENT_TYPE_NOSNIFF = True
SECURE_BROWSER_XSS_FILTER = True
X_FRAME_OPTIONS = 'DENY'
# django-log-request-id - Sending request id in response
REQUEST_ID_RESPONSE_HEADER = 'REQUEST_ID'
{%- if cookiecutter.add_celery.lower() == 'y' %}
# DJANGO CELERY CONFIGURATION
# -----------------------------------------------------------------------------
# see: http://celery.readthedocs.org/en/latest/userguide/tasks.html#task-states
CELERY_BROKER_URL = env('REDIS_URL', default="redis://localhost:6379/0")
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = env('CELERY_TIMEZONE', default=TIME_ZONE) # Use django's timezone by default
{%- endif %}
# LOGGING CONFIGURATION
# ------------------------------------------------------------------------------
# See: https://docs.djangoproject.com/en/dev/ref/settings/#logging
# Default logging for Django. This sends an email to the site admins on every
# HTTP 500 error. Depending on DEBUG, all other log records are either sent to
# the console (DEBUG=True) or discarded by mean of the NullHandler (DEBUG=False).
# See http://docs.djangoproject.com/en/dev/topics/logging
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse'
},
'request_id': {
'()': 'log_request_id.filters.RequestIDFilter'
}
},
'formatters': {
'complete': {
# NOTE: make sure to include 'request_id' in filters when using this
# formatter in any handlers.
'format': '%(asctime)s:[%(levelname)s]:logger=%(name)s:request_id=%(request_id)s message="%(message)s"'
},
'simple': {
'format': '%(levelname)s:%(asctime)s: %(message)s'
},
'null': {
'format': '%(message)s',
},
},
'handlers': {
'null': {
'level': 'DEBUG',
'class': 'logging.NullHandler',
},
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'complete',
'filters': ['request_id'],
},
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django': {
'handlers': ['null'],
'propagate': False,
'level': 'INFO',
},
'django.request': {
'handlers': ['mail_admins', 'console'],
'level': 'ERROR',
'propagate': False,
},
'django.server': {
'handlers': ['console'],
'level': 'INFO',
'propagate': False,
},
'{{cookiecutter.main_module}}': {
'handlers': ['console'],
'level': 'INFO',
'propagate': False,
},
# Catch All Logger -- Captures any other logging
'': {
'handlers': ['console'],
'level': 'ERROR',
'propagate': True,
},
}
}
def get_release():
import {{cookiecutter.main_module}}
{%- if cookiecutter.use_sentry_for_error_reporting == 'y' %}
import raven
from raven import exceptions as raven_exceptions
{%- endif %}
release = {{cookiecutter.main_module}}.__version__
{%- if cookiecutter.use_sentry_for_error_reporting == 'y' %}
try:
git_hash = raven.fetch_git_sha(os.path.dirname(os.pardir))[:7]
release = '{}-{}'.format(release, git_hash)
except raven_exceptions.InvalidGitRepository:
pass
{%- endif %}
return release
RELEASE_VERSION = get_release()
{%- if cookiecutter.use_sentry_for_error_reporting == 'y' %}
RAVEN_CONFIG = {
'dsn': env('SENTRY_DSN', default=''),
'environment': env('SENTRY_ENVIRONMENT', default='production'),
'release': RELEASE_VERSION,
}
{%- endif %}
SITE_INFO = {
'RELEASE_VERSION': RELEASE_VERSION,
{%- if cookiecutter.use_sentry_for_error_reporting == 'y' %}
'IS_RAVEN_INSTALLED': RAVEN_CONFIG['dsn'] is not ''
{%- endif %}
}