Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDPF-92 Added redis, updated config files, added support for dbt-platform #4

Merged
merged 7 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ APP_ENV=local
DJANGO_SETTINGS_MODULE=config.settings.local
SECRET_KEY=this_is_an_example_use_a_proper_key_in_production
DATABASE_URL=postgres://postgres:postgres@postgres/postgres
REDIS_URL=redis://redis:6379
IDENTITY_REDIS_URL=redis://redis:6379
IDENTITY_REDIS=redis://redis:6379
ALLOWED_HOSTS=localhost

# Vite
VITE_DEV=True
Expand Down
130 changes: 73 additions & 57 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,47 @@
"""

from pathlib import Path
from typing import Any

import dj_database_url
import environ
import sentry_sdk
from dbt_copilot_python.database import database_url_from_env
from dbt_copilot_python.network import is_copilot, setup_allowed_hosts
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.redis import RedisIntegration


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent.parent


env = environ.Env()
BASE_DIR: Path = Path(__file__).resolve().parent.parent.parent
env: environ.Env = environ.Env()
environ.Env.read_env(BASE_DIR / ".env")

APP_ENV = env("APP_ENV")

SECRET_KEY = env("SECRET_KEY")
# App
#
APP_ENV: str = env.str("APP_ENV")
GIT_COMMIT: str = env.str("GIT_COMMIT", None)

DEBUG = False
# Django
# https://docs.djangoproject.com/en/5.1/topics/settings/
SECRET_KEY: str = env.str("SECRET_KEY")
DEBUG: bool = False
ALLOWED_HOSTS: list[str] = setup_allowed_hosts(env.list("ALLOWED_HOSTS", default=[]))
ROOT_URLCONF: str = "config.urls"
WSGI_APPLICATION: str = "config.wsgi.application"

ALLOWED_HOSTS: list[str] = []
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/
STATIC_URL: str = "static/"

# Vite
# https://vitejs.dev/guide/backend-integration.html
VITE_DEV: bool = env.bool("VITE_DEV")
VITE_DEV_SERVER_URL: str = env.str("VITE_DEV_SERVER_URL")
VITE_MANIFEST_PATH: Path = BASE_DIR / "frontend" / "dist" / "manifest.json"

# Application definition

INSTALLED_APPS = [
INSTALLED_APPS: list[str] = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
Expand All @@ -46,7 +62,7 @@
"core.apps.CoreConfig",
]

MIDDLEWARE = [
MIDDLEWARE: list[str] = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
Expand All @@ -56,9 +72,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "config.urls"

TEMPLATES = [
TEMPLATES: list[dict[str, Any]] = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
Expand All @@ -74,43 +88,68 @@
},
]

WSGI_APPLICATION = "config.wsgi.application"

# Sentry
# https://docs.sentry.io/platforms/python/integrations/django/
SENTRY_DSN: str = env.str("SENTRY_DSN")


def init_sentry():
sentry_environment = env("APP_ENV")

if SENTRY_DSN:
sentry_sdk.init(
dsn=env("SENTRY_DSN"),
environment=sentry_environment,
integrations=[
DjangoIntegration(),
],
dsn=SENTRY_DSN,
environment=APP_ENV,
release=GIT_COMMIT,
integrations=[DjangoIntegration(), RedisIntegration()],
enable_tracing=env.bool("SENTRY_ENABLE_TRACING", False),
traces_sample_rate=env.float("SENTRY_TRACES_SAMPLE_RATE", 0.0),
send_default_pii=True,
)


# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases

DATABASES = {
"default": env.db(),
if is_copilot():
DATABASES: dict[str, Any] = {
"default": dj_database_url.config(
default=database_url_from_env("DATABASE_CREDENTIALS")
)
}
else:
DATABASE_URL: str = env.str("DATABASE_URL")
DATABASES = {"default": env.db()}

# Redis
# https://docs.djangoproject.com/en/5.1/topics/cache/
IDENTITY_REDIS_URL: str = env.str("IDENTITY_REDIS_URL", None)
IDENTITY_REDIS: str = env.str("IDENTITY_REDIS", None)

if is_copilot():
IDENTITY_REDIS_URL = IDENTITY_REDIS_URL
else:
IDENTITY_REDIS_URL = IDENTITY_REDIS

CACHES: dict[str, Any] = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": IDENTITY_REDIS_URL,
"KEY_PREFIX": "wp_",
}
}

# Vite
# https://vitejs.dev/guide/backend-integration.html
# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/
LANGUAGE_CODE: str = "en-gb"
TIME_ZONE: str = "UTC"
USE_I18N: bool = True
USE_TZ: bool = True

VITE_DEV = env.bool("VITE_DEV")
VITE_DEV_SERVER_URL = env("VITE_DEV_SERVER_URL")
VITE_MANIFEST_PATH = BASE_DIR / "frontend" / "dist" / "manifest.json"
# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD: str = "django.db.models.BigAutoField"

# Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
AUTH_PASSWORD_VALIDATORS: list[dict[str, str]] = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
Expand All @@ -124,26 +163,3 @@ def init_sentry():
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]


# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/

LANGUAGE_CODE = "en-gb"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
4 changes: 0 additions & 4 deletions config/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@


DEBUG = True

ALLOWED_HOSTS = ["localhost"]

INSTALLED_APPS.append(
"django_extensions",
)

init_sentry()
19 changes: 19 additions & 0 deletions config/settings/prod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from .base import * # noqa


DEBUG = False

AWS_QUERYSTRING_AUTH: bool = False

SESSION_COOKIE_AGE: int = 60 * 60

SECURE_BROWSER_XSS_FILTER: bool = True
X_FRAME_OPTIONS: str = "DENY"
SECURE_CONTENT_TYPE_NOSNIFF: bool = True
SECURE_HSTS_SECONDS: int = 15768000
SECURE_HSTS_INCLUDE_SUBDOMAINS: bool = True
SECURE_HSTS_PRELOAD: bool = True
SECURE_SSL_REDIRECT: bool = True
CSRF_COOKIE_SECURE: bool = True
SESSION_COOKIE_SECURE: bool = True
SECURE_PROXY_SSL_HEADER: tuple[str, str] = ("HTTP_X_FORWARDED_PROTO", "https")
2 changes: 1 addition & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.prod")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand Down
Loading