/
base.py
284 lines (204 loc) · 7.27 KB
/
base.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
import os
from distutils.util import strtobool
from django.utils.translation import gettext_lazy as _
from dotenv import load_dotenv, find_dotenv
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(
os.path.dirname(
os.path.dirname(
os.path.abspath(__file__)
)
)
)
# Environment variables
# Check for and load environment variables from a .env file.
load_dotenv(find_dotenv())
# Required settings
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '*').split(',')
SECRET_KEY = os.environ.get('SECRET_KEY') or None
DEBUG = os.environ.get('DEBUG') or False
# Applications
# https://docs.djangoproject.com/en/3.0/ref/applications/
INSTALLED_APPS = [
'api',
'babybuddy',
'core',
'dashboard',
'reports',
'axes',
'django_filters',
'rest_framework',
'rest_framework.authtoken',
'widget_tweaks',
'easy_thumbnails',
'storages',
'import_export',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
]
# Middleware
# https://docs.djangoproject.com/en/3.0/ref/middleware/
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'babybuddy.middleware.RollingSessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'babybuddy.middleware.UserTimezoneMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'axes.middleware.AxesMiddleware',
]
# URL dispatcher
# https://docs.djangoproject.com/en/3.0/topics/http/urls/
ROOT_URLCONF = 'babybuddy.urls'
# Templates
# https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-TEMPLATES
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'data/db.sqlite3'),
}
}
# Cache
# https://docs.djangoproject.com/en/3.0/topics/cache/
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'cache_default',
}
}
# WGSI
# https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/
WSGI_APPLICATION = 'babybuddy.wsgi.application'
# Authentication
# https://docs.djangoproject.com/en/3.0/topics/auth/default/
AUTHENTICATION_BACKENDS = [
'axes.backends.AxesBackend',
'django.contrib.auth.backends.ModelBackend',
]
LOGIN_REDIRECT_URL = '/'
LOGIN_URL = '/login/'
LOGOUT_REDIRECT_URL = '/login/'
# Timezone
# https://docs.djangoproject.com/en/3.0/topics/i18n/timezones/
USE_TZ = True
TIME_ZONE = os.environ.get('TIME_ZONE') or 'UTC'
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
USE_I18N = True
LANGUAGE_CODE = 'en'
LOCALE_PATHS = [
os.path.join(BASE_DIR, "locale"),
]
LANGUAGES = [
('en', _('English')),
('nl', _('Dutch')),
('fr', _('French')),
('fi', _('Finnish')),
('de', _('German')),
('it', _('Italian')),
('es', _('Spanish')),
('sv', _('Swedish')),
('tr', _('Turkish')),
]
# Format localization
# https://docs.djangoproject.com/en/3.0/topics/i18n/formatting/
USE_L10N = True
# Custom setting that can be used to override the locale-based time set by
# USE_L10N _for specific locales_ to use 24-hour format. In order for this to
# work with a given locale it must be set at the FORMAT_MODULE_PATH with
# conditionals on this setting. See babybuddy/forms/en/formats.py for an example
# implementation for the English locale.
USE_24_HOUR_TIME_FORMAT = strtobool(os.environ.get('USE_24_HOUR_TIME_FORMAT') or 'False')
FORMAT_MODULE_PATH = ['babybuddy.formats']
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
# http://whitenoise.evans.io/en/stable/django.html
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
WHITENOISE_ROOT = os.path.join(BASE_DIR, 'static', 'babybuddy', 'root')
# Media files (User uploaded content)
# https://docs.djangoproject.com/en/3.0/topics/files/
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME') or None
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID') or None
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY') or None
if AWS_STORAGE_BUCKET_NAME:
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
THUMBNAIL_DEFAULT_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
# Django Rest Framework
# https://www.django-rest-framework.org/
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
'DEFAULT_FILTER_BACKENDS': [
'django_filters.rest_framework.DjangoFilterBackend',
],
'DEFAULT_METADATA_CLASS': 'api.metadata.APIMetadata',
'DEFAULT_PAGINATION_CLASS':
'rest_framework.pagination.LimitOffsetPagination',
'DEFAULT_PERMISSION_CLASSES': [
'api.permissions.BabyBuddyDjangoModelPermissions'
],
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
],
'PAGE_SIZE': 100
}
# Import/Export configuration
# See https://django-import-export.readthedocs.io/
IMPORT_EXPORT_IMPORT_PERMISSION_CODE = 'add'
IMPORT_EXPORT_EXPORT_PERMISSION_CODE = 'change'
IMPORT_EXPORT_USE_TRANSACTIONS = True
# Axes configuration
# See https://django-axes.readthedocs.io/en/latest/4_configuration.html
AXES_COOLOFF_TIME = 1
AXES_FAILURE_LIMIT = 5
# Session configuration
# Used by RollingSessionMiddleware to determine how often to reset the session.
# See https://docs.djangoproject.com/en/3.0/topics/http/sessions/
ROLLING_SESSION_REFRESH = 86400
# Set default auto field for models.
# See https://docs.djangoproject.com/en/3.2/releases/3.2/#customizing-type-of-auto-created-primary-keys
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
# Baby Buddy configuration
# See README.md#configuration for details about these settings.
BABY_BUDDY = {
'NAP_START_MIN': os.environ.get('NAP_START_MIN') or '06:00',
'NAP_START_MAX': os.environ.get('NAP_START_MAX') or '18:00',
'ALLOW_UPLOADS': os.environ.get('ALLOW_UPLOADS') or True
}