|
""" |
|
Django settings for api project. |
|
Generated by 'django-admin startproject' using Django 4.0.2. |
|
For more information on this file, see |
|
https://docs.djangoproject.com/en/4.0/topics/settings/ |
|
For the full list of settings and their values, see |
|
https://docs.djangoproject.com/en/4.0/ref/settings/ |
|
""" |
|
|
|
from pathlib import Path |
|
from datetime import timedelta |
|
import io |
|
import os |
|
from urllib.parse import urlparse |
|
|
|
import environ |
|
from google.cloud import secretmanager |
|
|
|
# Set the project base directory |
|
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
|
|
|
env = environ.Env() |
|
env_file = os.path.join(BASE_DIR, ".env") |
|
|
|
if os.path.isfile(env_file): |
|
# Use a local secret file, if provided |
|
env.read_env(env_file) |
|
# ... |
|
elif os.environ.get("GOOGLE_CLOUD_PROJECT", None): |
|
# Pull secrets from Secret Manager |
|
project_id = os.environ.get("GOOGLE_CLOUD_PROJECT") |
|
|
|
client = secretmanager.SecretManagerServiceClient() |
|
settings_name = os.environ.get("SETTINGS_NAME", "django_settings") |
|
name = f"projects/{project_id}/secrets/{settings_name}/versions/latest" |
|
payload = client.access_secret_version(name=name).payload.data.decode("UTF-8") |
|
|
|
env.read_env(io.StringIO(payload)) |
|
else: |
|
raise Exception("No local .env or GOOGLE_CLOUD_PROJECT detected. No secrets found.") |
|
|
|
|
|
# Build paths inside the project like this: BASE_DIR / 'subdir'. |
|
BASE_DIR = Path(__file__).resolve().parent.parent |
|
|
|
|
|
# Quick-start development settings - unsuitable for production |
|
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ |
|
# SECURITY WARNING: keep the secret key used in production secret! |
|
SECRET_KEY = env('SECRET_KEY') |
|
|
|
# SECURITY WARNING: don't run with debug turned on in production! |
|
DEBUG = True |
|
|
|
# SECURITY WARNING: It's recommended that you use this when |
|
# running in production. The URL will be known once you first deploy |
|
# to App Engine. This code takes the URL and converts it to both these settings formats. |
|
APPENGINE_URL = env("APPENGINE_URL", default=None) |
|
if APPENGINE_URL: |
|
# Ensure a scheme is present in the URL before it's processed. |
|
if not urlparse(APPENGINE_URL).scheme: |
|
APPENGINE_URL = f"https://{APPENGINE_URL}" |
|
|
|
CSRF_TRUSTED_ORIGINS = [APPENGINE_URL] |
|
CORS_ALLOWED_ORIGIN_REGEXES = [ |
|
r"^https://.*\.gitpod\.io$", |
|
] |
|
SECURE_SSL_REDIRECT = True |
|
else: |
|
CSRF_TRUSTED_ORIGINS = ['http://localhost', 'https://*.gitpod.io'] |
|
CORS_ALLOW_ALL_ORIGINS = True |
|
|
|
ALLOWED_HOSTS = ['*'] |
|
|
|
CORS_ALLOWED_ORIGIN_REGEXES = [ |
|
r"^https://.*\.gitpod\.io$", |
|
r"^https://.*\.web\.app$", |
|
] |
|
|
|
# Application definition |
|
INSTALLED_APPS = [ |
|
'django.contrib.admin', |
|
'django.contrib.auth', |
|
'django.contrib.contenttypes', |
|
'django.contrib.sessions', |
|
'django.contrib.messages', |
|
'django.contrib.staticfiles', |
|
'rest_framework', |
|
'rest_framework.authtoken', |
|
'corsheaders', |
|
] |
|
|
|
X_FRAME_OPTIONS = "SAMEORIGIN" |
|
SILENCED_SYSTEM_CHECKS = ["security.W019"] |
|
|
|
MIDDLEWARE = [ |
|
'corsheaders.middleware.CorsMiddleware', |
|
'django.middleware.security.SecurityMiddleware', |
|
'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', |
|
] |
|
|
|
ROOT_URLCONF = 'api.urls' |
|
|
|
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', |
|
], |
|
}, |
|
}, |
|
] |
|
|
|
WSGI_APPLICATION = 'api.wsgi.application' |
|
|
|
# Database |
|
# https://docs.djangoproject.com/en/4.1/ref/settings/#databases |
|
# Use django-environ to parse the connection string |
|
DATABASES = {"default": env.db()} |
|
|
|
# If the flag as been set, configure to use proxy |
|
if os.getenv("USE_CLOUD_SQL_AUTH_PROXY", None): |
|
DATABASES["default"]["HOST"] = "127.0.0.1" |
|
DATABASES["default"]["PORT"] = 5432 |
|
|
|
# Password validation |
|
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators |
|
|
|
AUTH_PASSWORD_VALIDATORS = [ |
|
{ |
|
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', |
|
}, |
|
{ |
|
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', |
|
}, |
|
{ |
|
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', |
|
}, |
|
{ |
|
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', |
|
}, |
|
] |
|
|
|
# Internationalization |
|
# https://docs.djangoproject.com/en/4.1/topics/i18n/ |
|
|
|
LANGUAGE_CODE = 'en-us' |
|
|
|
TIME_ZONE = 'UTC' |
|
|
|
USE_I18N = True |
|
|
|
USE_TZ = True |
|
|
|
|
|
REST_FRAMEWORK = { |
|
'DEFAULT_PERMISSION_CLASSES': ( |
|
'rest_framework.permissions.AllowAny', |
|
), |
|
'DEFAULT_AUTHENTICATION_CLASSES': ( |
|
'rest_framework_simplejwt.authentication.JWTAuthentication', |
|
), |
|
} |
|
|
|
SIMPLE_JWT = { |
|
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=60), |
|
'REFRESH_TOKEN_LIFETIME': timedelta(days=14), |
|
'ROTATE_REFRESH_TOKENS': True, |
|
'BLACKLIST_AFTER_ROTATION': False, |
|
'ALGORITHM': 'HS256', |
|
'SIGNING_KEY': SECRET_KEY, |
|
'VERIFYING_KEY': None, |
|
'USER_ID_FIELD': 'id', |
|
'USER_ID_CLAIM': 'user_id', |
|
'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',), |
|
'TOKEN_TYPE_CLAIM': 'token_type', |
|
} |
|
|
|
# Default primary key field type |
|
# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field |
|
|
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' |
|
|
|
# Static files (CSS, JavaScript, Images) |
|
# https://docs.djangoproject.com/en/4.0/howto/static-files/ |
|
STATIC_ROOT = "static" |
|
STATIC_URL = "/static/" |
|
STATICFILES_DIRS = [] |