mybuddy/babybuddy/settings/base.py

325 lines
8.6 KiB
Python
Raw Normal View History

2017-08-11 18:32:02 +00:00
import os
from distutils.util import strtobool
2017-08-11 18:32:02 +00:00
2019-04-14 03:09:55 +00:00
from django.utils.translation import gettext_lazy as _
from dotenv import load_dotenv, find_dotenv
2017-08-11 18:32:02 +00:00
# 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__)
)
)
)
2017-08-11 18:32:02 +00:00
# Environment variables
# Check for and load environment variables from a .env file.
load_dotenv(find_dotenv())
2017-08-11 18:32:02 +00:00
# Required settings
2017-10-23 13:49:09 +00:00
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '*').split(',')
SECRET_KEY = os.environ.get('SECRET_KEY') or None
DEBUG = os.environ.get('DEBUG') or False
2017-10-23 13:49:09 +00:00
# Applications
2020-01-19 16:49:45 +00:00
# https://docs.djangoproject.com/en/3.0/ref/applications/
2017-08-11 18:32:02 +00:00
INSTALLED_APPS = [
2017-08-13 14:48:16 +00:00
'api',
2017-10-22 18:00:42 +00:00
'babybuddy',
2017-08-13 14:48:16 +00:00
'core',
2017-08-18 13:02:31 +00:00
'dashboard',
2017-08-21 23:21:08 +00:00
'reports',
2017-08-13 14:48:16 +00:00
'axes',
'django_filters',
2017-08-13 14:48:16 +00:00
'rest_framework',
'rest_framework.authtoken',
2017-08-15 20:50:09 +00:00
'widget_tweaks',
'easy_thumbnails',
'storages',
'import_export',
2017-08-13 14:48:16 +00:00
2017-08-11 18:32:02 +00:00
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.humanize',
2017-08-11 18:32:02 +00:00
]
# Middleware
2020-01-19 16:49:45 +00:00
# https://docs.djangoproject.com/en/3.0/ref/middleware/
2017-08-11 18:32:02 +00:00
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
2017-08-11 18:32:02 +00:00
'django.contrib.sessions.middleware.SessionMiddleware',
'babybuddy.middleware.RollingSessionMiddleware',
2019-04-14 02:13:14 +00:00
'django.middleware.locale.LocaleMiddleware',
'babybuddy.middleware.UserTimezoneMiddleware',
2017-08-11 18:32:02 +00:00
'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',
2017-08-11 18:32:02 +00:00
]
# URL dispatcher
2020-01-19 16:49:45 +00:00
# https://docs.djangoproject.com/en/3.0/topics/http/urls/
2017-10-22 18:00:42 +00:00
ROOT_URLCONF = 'babybuddy.urls'
2017-08-11 18:32:02 +00:00
# Templates
2020-01-19 16:49:45 +00:00
# https://docs.djangoproject.com/en/3.0/ref/settings/#std:setting-TEMPLATES
2017-08-11 18:32:02 +00:00
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
config = {
'ENGINE': os.getenv('DB_ENGINE') or 'django.db.backends.sqlite3',
'NAME': os.getenv('DB_NAME') or os.path.join(BASE_DIR, 'data/db.sqlite3')
}
if os.getenv('DB_USER'):
config['USER'] = os.getenv('DB_USER')
if os.environ.get('DB_PASSWORD') or os.environ.get('POSTGRES_PASSWORD'):
config['PASSWORD'] = os.environ.get('DB_PASSWORD') or os.environ.get('POSTGRES_PASSWORD')
if os.getenv('DB_HOST'):
config['HOST'] = os.getenv('DB_HOST')
if os.getenv('DB_PORT'):
config['PORT'] = os.getenv('DB_PORT')
DATABASES = {'default': config}
2020-01-29 04:45:37 +00:00
# Cache
# https://docs.djangoproject.com/en/3.0/topics/cache/
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'cache_default',
}
}
# WGSI
2020-01-19 16:49:45 +00:00
# https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/
2017-08-11 18:32:02 +00:00
2017-10-22 18:00:42 +00:00
WSGI_APPLICATION = 'babybuddy.wsgi.application'
2017-08-11 18:32:02 +00:00
# Authentication
2020-01-19 16:49:45 +00:00
# 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/
2017-08-11 18:32:02 +00:00
USE_TZ = True
2017-08-11 18:32:02 +00:00
TIME_ZONE = os.environ.get('TIME_ZONE') or 'UTC'
2017-08-11 18:32:02 +00:00
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
2017-08-11 18:32:02 +00:00
USE_I18N = True
2017-08-11 18:32:02 +00:00
LANGUAGE_CODE = 'en'
2017-08-11 18:32:02 +00:00
2019-04-14 02:13:14 +00:00
LOCALE_PATHS = [
os.path.join(BASE_DIR, "locale"),
2019-04-14 03:09:55 +00:00
]
LANGUAGES = [
('en', _('English')),
2021-02-25 14:00:23 +00:00
('nl', _('Dutch')),
2019-04-14 03:09:55 +00:00
('fr', _('French')),
2020-07-29 13:40:18 +00:00
('fi', _('Finnish')),
('de', _('German')),
2021-03-04 14:03:29 +00:00
('it', _('Italian')),
2021-09-17 01:53:46 +00:00
('pt', _('Portuguese')),
2020-01-17 03:14:22 +00:00
('es', _('Spanish')),
2019-05-02 18:48:36 +00:00
('sv', _('Swedish')),
2021-09-17 01:53:46 +00:00
('tr', _('Turkish'))
2019-04-14 02:13:14 +00:00
]
2017-08-11 18:32:02 +00:00
# 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']
2017-08-11 18:32:02 +00:00
# Static files (CSS, JavaScript, Images)
2020-01-19 16:49:45 +00:00
# https://docs.djangoproject.com/en/3.0/howto/static-files/
# http://whitenoise.evans.io/en/stable/django.html
2017-08-11 18:32:02 +00:00
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
2017-08-11 18:32:02 +00:00
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)
2020-01-19 16:49:45 +00:00
# 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'
# Security
# https://docs.djangoproject.com/en/3.2/ref/settings/#secure-proxy-ssl-header
if os.environ.get('SECURE_PROXY_SSL_HEADER'):
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# https://docs.djangoproject.com/en/3.2/topics/http/sessions/#settings
SESSION_COOKIE_SECURE = True
# https://docs.djangoproject.com/en/3.2/ref/csrf/#settings
CSRF_COOKIE_SECURE = True
# https://docs.djangoproject.com/en/3.2/topics/auth/passwords/
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {
'min_length': 8,
}
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Django Rest Framework
2020-01-19 16:49:45 +00:00
# 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': [
2017-10-22 18:00:42 +00:00
'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'
2020-02-17 18:34:31 +00:00
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
2021-04-11 19:49:15 +00:00
# Set default auto field for models.
# See https://docs.djangoproject.com/en/3.2/releases/3.2/#customizing-type-of-auto-created-primary-keys
2021-04-11 19:49:15 +00:00
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
}