You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.0 KiB
Django/Jinja
82 lines
2.0 KiB
Django/Jinja
from __future__ import annotations
|
|
|
|
{% if database_dialect == "postgresql" -%}
|
|
import os
|
|
|
|
{% endif -%}
|
|
from pathlib import Path
|
|
|
|
from iti.config import (
|
|
DevConfig as BaseDevConfig,
|
|
TestConfig as BaseTestConfig,
|
|
ProdConfig as BaseProdConfig,
|
|
)
|
|
|
|
|
|
BASE_DIR = Path(__file__).resolve().parent
|
|
APP_NAME = "{{ project_name }}"
|
|
|
|
|
|
def runtime_path(name: str) -> str:
|
|
return str(BASE_DIR / "runtime" / name)
|
|
|
|
|
|
def apply_project_config(config) -> None:
|
|
config.app_name = APP_NAME
|
|
config.base_dir = BASE_DIR
|
|
config.file_storage["LOCAL"]["base_path"] = runtime_path("uploads")
|
|
config.log_dir = runtime_path("logs")
|
|
{% if database_dialect == "postgresql" %}
|
|
apply_database_config(config)
|
|
|
|
|
|
def apply_database_config(config) -> None:
|
|
if os.getenv("DATABASE_URL"):
|
|
return
|
|
database = os.getenv("POSTGRES_DB", default_database_name(config.app_env))
|
|
config.database_url = default_postgresql_url(database)
|
|
|
|
|
|
def default_postgresql_url(database: str) -> str:
|
|
return (
|
|
f"postgresql+psycopg://{os.getenv('POSTGRES_USER', 'postgres')}:"
|
|
f"{os.getenv('POSTGRES_PASSWORD', 'password')}@"
|
|
f"{os.getenv('POSTGRES_HOST', '127.0.0.1')}:"
|
|
f"{os.getenv('POSTGRES_PORT', '5432')}/{database}"
|
|
)
|
|
|
|
|
|
def default_database_name(app_env: str) -> str:
|
|
if app_env == "test":
|
|
return "{{ project_slug | lower | replace('-', '_') }}_test"
|
|
if app_env == "prod":
|
|
return "{{ project_slug | lower | replace('-', '_') }}"
|
|
return "{{ project_slug | lower | replace('-', '_') }}_dev"
|
|
{% endif %}
|
|
|
|
|
|
class DevConfig(BaseDevConfig):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
apply_project_config(self)
|
|
|
|
|
|
class TestConfig(BaseTestConfig):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
apply_project_config(self)
|
|
|
|
|
|
class ProdConfig(BaseProdConfig):
|
|
def __init__(self) -> None:
|
|
super().__init__()
|
|
apply_project_config(self)
|
|
|
|
|
|
config = {
|
|
"dev": DevConfig,
|
|
"test": TestConfig,
|
|
"prod": ProdConfig,
|
|
"default": DevConfig,
|
|
}
|