· 4 years ago · Mar 30, 2021, 07:08 PM
1"""
2Django settings for mapfre project.
3
4Generated by 'django-admin startproject' using Django 3.1.2.
5
6For more information on this file, see
7https://docs.djangoproject.com/en/3.1/topics/settings/
8
9For the full list of settings and their values, see
10https://docs.djangoproject.com/en/3.1/ref/settings/
11"""
12import os
13from decimal import getcontext, ROUND_HALF_UP
14from pathlib import Path
15
16# noinspection PyPackageRequirements
17import environ
18
19env = environ.Env(
20 # set casting, default value
21 DEBUG=(bool, False)
22)
23# reading .env file
24environ.Env.read_env()
25
26# Build paths inside the project like this: BASE_DIR / 'subdir'.
27BASE_DIR = Path(__file__).resolve().parent.parent
28
29# Quick-start development settings - unsuitable for production
30# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/
31
32# SECURITY WARNING: keep the secret key used in production secret!
33SECRET_KEY = env.str('SECRET_KEY')
34
35# SECURITY WARNING: don't run with debug turned on in production!
36DEBUG = env.bool('DEBUG')
37
38ALLOWED_HOSTS = env.list('ALLOWED_HOSTS')
39
40# Application definition
41
42INSTALLED_APPS = [
43 'django.contrib.admin',
44 'django.contrib.auth',
45 'django.contrib.contenttypes',
46 'django.contrib.sessions',
47 'django.contrib.messages',
48 'django.contrib.staticfiles',
49 'corsheaders',
50 'rest_framework',
51 'drf_yasg',
52 'django_extensions',
53]
54
55MIDDLEWARE = [
56 'django.middleware.security.SecurityMiddleware',
57 'django.contrib.sessions.middleware.SessionMiddleware',
58 'corsheaders.middleware.CorsMiddleware',
59 'django.middleware.common.CommonMiddleware',
60 'django.middleware.csrf.CsrfViewMiddleware',
61 'django.contrib.auth.middleware.AuthenticationMiddleware',
62 'django.contrib.messages.middleware.MessageMiddleware',
63 'django.middleware.clickjacking.XFrameOptionsMiddleware',
64]
65
66ROOT_URLCONF = 'mapfre.urls'
67
68TEMPLATES = [
69 {
70 'BACKEND': 'django.template.backends.django.DjangoTemplates',
71 'DIRS': [os.path.join(BASE_DIR, 'templates')],
72 'APP_DIRS': True,
73 'OPTIONS': {
74 'context_processors': [
75 'django.template.context_processors.debug',
76 'django.template.context_processors.request',
77 'django.contrib.auth.context_processors.auth',
78 'django.contrib.messages.context_processors.messages',
79 ],
80 },
81 },
82]
83
84WSGI_APPLICATION = 'mapfre.wsgi.application'
85
86# Database
87# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
88
89DATABASES = {
90 'default': {
91 'ENGINE': 'django.db.backends.postgresql_psycopg2',
92 'NAME': env.str('DB_NAME'),
93 'USER': env.str('DB_USER'),
94 'PASSWORD': env.str('DB_PASSWORD'),
95 'HOST': env.str('DB_HOST'),
96 'PORT': env.str('DB_PORT'),
97 }
98}
99
100# Password validation
101# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators
102
103AUTH_PASSWORD_VALIDATORS = [
104 {
105 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
106 },
107 {
108 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
109 },
110 {
111 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
112 },
113 {
114 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
115 },
116]
117
118# Internationalization
119# https://docs.djangoproject.com/en/3.1/topics/i18n/
120
121LANGUAGE_CODE = 'en-us'
122
123TIME_ZONE = 'UTC'
124
125USE_I18N = True
126
127USE_L10N = True
128
129USE_TZ = True
130
131# Static files (CSS, JavaScript, Images)
132# https://docs.djangoproject.com/en/3.1/howto/static-files/
133
134STATIC_URL = '/static/'
135STATIC_ROOT = os.path.join(BASE_DIR, 'static')
136
137MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
138MEDIA_URL = '/media/'
139
140AUTH_USER_MODEL = 'users.MyUser'
141
142LOGGING = {
143 'version': 1,
144 'disable_existing_loggers': False,
145 'handlers': {
146 'file': {
147 'level': 'DEBUG',
148 'class': 'logging.FileHandler',
149 'filename': 'debug.log',
150 },
151 },
152 'loggers': {
153 'django': {
154 'handlers': ['file'],
155 'level': 'DEBUG',
156 'propagate': True,
157 },
158 },
159}
160
161REST_FRAMEWORK = {
162 'DEFAULT_PERMISSION_CLASSES': (
163 'rest_framework.permissions.IsAuthenticated',
164 ),
165 'DEFAULT_AUTHENTICATION_CLASSES': (
166 'api.authentication.MyJWTAuthentication',
167 'rest_framework.authentication.SessionAuthentication',
168 'rest_framework.authentication.BasicAuthentication',
169
170 )
171}
172
173TEMPLATE_LOADERS = (
174 ('django.template.loaders.cached.Loader', (
175 'django.template.loaders.filesystem.Loader',
176 'django.template.loaders.app_directories.Loader',
177 )),
178)
179
180if not DEBUG:
181 CSRF_COOKIE_SECURE = True
182 SESSION_COOKIE_SECURE = True
183 SECURE_REFERRER_POLICY = "same-origin"
184 REST_FRAMEWORK['DEFAULT_RENDERER_CLASSES'] = (
185 'rest_framework.renderers.JSONRenderer',
186 )
187 SECURE_BROWSER_XSS_FILTER = True
188else:
189 CORS_ALLOW_ALL_ORIGINS = True
190
191AWS_S3_REGION_NAME = env.str('AWS_S3_REGION_NAME')
192AWS_STORAGE_BUCKET_NAME = env.str('AWS_STORAGE_BUCKET_NAME')
193AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
194
195AWS_S3_OBJECT_PARAMETERS = {
196 'CacheControl': 'max-age=86400',
197}
198
199AWS_PRIVATE_MEDIA_LOCATION = 'private'
200PRIVATE_FILE_STORAGE = 'api.storage_backends.PrivateMediaStorage'
201BACKEND_REGION = env.str("BACKEND_REGION")
202
203QUEUE_URL = env.str("BROKER_QUEUE_URL")
204QUEUE = env.str("BROKER_QUEUE")
205
206CELERY_BROKER_URL = env.str("BROKER_URL")
207CELERY_RESULT_BACKEND = None
208CELERY_ACCEPT_CONTENT = ['application/json']
209CELERY_RESULT_SERIALIZER = 'json'
210CELERY_TASK_SERIALIZER = 'json'
211CELERY_TIMEZONE = TIME_ZONE
212CELERY_TASK_DEFAULT_QUEUE = QUEUE
213
214CELERY_BROKER_TRANSPORT_OPTIONS = {
215 'region': BACKEND_REGION,
216 'polling_interval': 20,
217 'visibility_timeout': 15 * 60,
218 'predefined_queues': {
219 QUEUE: {
220 'url': QUEUE_URL,
221 }
222 }
223}
224
225getcontext().prec = 6
226getcontext().rounding = ROUND_HALF_UP
227
228
229
230