Skip to content

Commit

Permalink
implement settings initialization when django server starts
Browse files Browse the repository at this point in the history
  • Loading branch information
Louxsdon committed Apr 18, 2023
1 parent 5de4f75 commit fb5b385
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 91 deletions.
89 changes: 1 addition & 88 deletions django_breeze/__init__.py
Original file line number Diff line number Diff line change
@@ -1,88 +1 @@
from django.conf import settings as django_settings
from inertia.settings import settings as inertia_settings
from pathlib import Path

BASE_DIR = getattr(django_settings, "BASE_DIR")

MIDDLEWARES = [
"inertia.middleware.InertiaMiddleware",
# "django-breeze.django_breeze.middleware.inertia_share",
]

INSTALLED_APPS = [
"inertia",
"django_vite",
]

# Django Breeze Default Settings
DJANGO_BREEZE: dict = {
"INERTIA": {
"LAYOUT": "index.html",
"SSR_URL": inertia_settings.INERTIA_SSR_URL,
"SSR_ENABLED": inertia_settings.INERTIA_SSR_ENABLED,
"JSON_ENCODER": inertia_settings.INERTIA_JSON_ENCODER,
},
"DJANGO_VITE": {
"DEV_MODE": getattr(django_settings, "DEBUG", True),
"SERVER_PROTOCOL": "http",
"DEV_SERVER_HOST": "localhost",
"DEV_SERVER_PORT": 5173,
"WS_CLIENT_URL": "@vite/client",
"ASSETS_PATH": Path(getattr(django_settings, "STATIC_PATH", "static")) / "dist",
"STATIC_URL_PREFIX": "",
"LEGACY_POLYFILLS_MOTIF": "legacy-polyfills",
},
"STATIC_ROOT": "static",
"CSRF_HEADER_NAME": "HTTP_X_XSRF_TOKEN",
"CSRF_COOKIE_NAME": "XSRF-TOKEN",
}


# Merge DJANGO_BREEZE and user-defined settings
user_settings = getattr(django_settings, "DJANGO_BREEZE", {})


def merge_dicts(*dicts):
"""
Recursively merges dictionaries.
"""
result = {}
for d in dicts:
for k, v in d.items():
if isinstance(v, dict):
result[k] = merge_dicts(result.get(k, {}), v)
else:
result[k] = v
return result


merged_settings = merge_dicts(DJANGO_BREEZE, user_settings)


def key_exist(key) -> bool:
return hasattr(django_settings, key) and getattr(django_settings, key) is not None


settings = {}

for key, value in merged_settings.items():
if isinstance(value, dict):
for sub_key, sub_value in value.items():
sub_key = f"{key}_{sub_key}"
if not key_exist(sub_key):
settings[sub_key] = sub_value
else:
if not key_exist(key):
settings[key] = value

for key, value in settings.items():
setattr(django_settings, key, value)

django_settings.TEMPLATES[0]["DIRS"].append(Path(BASE_DIR) / "src/")

for app in INSTALLED_APPS:
django_settings.INSTALLED_APPS.append(app)

for middleware in MIDDLEWARES:
django_settings.MIDDLEWARE.append(middleware)
# django_settings.STATICFILES_DIRS.append(settings.DJANGO_VITE_ASSETS_PATH)
default_app_config = "django_breeze.apps.DjangoBreezeConfig"
10 changes: 10 additions & 0 deletions django_breeze/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.apps import AppConfig
from django_breeze.settings import initialize


class DjangoBreezeConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "django_breeze"

def ready(self):
initialize()
7 changes: 6 additions & 1 deletion django_breeze/scripts/django-breeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
import sys
from django_breeze.core.management import execute_command

if __name__ == "__main__":

def run():
sys.argv[0] = re.sub(r"(-script\.pyw|\.exe)?$", "", sys.argv[0])
sys.exit(execute_command())


if __name__ == "__main__":
run()
92 changes: 92 additions & 0 deletions django_breeze/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from django.conf import settings as django_settings
from inertia.settings import settings as inertia_settings
from pathlib import Path
import django


def initialize() -> None:
"""Initialize all neccessary django, inertia and django-vite settings"""
BASE_DIR = getattr(django_settings, "BASE_DIR")

MIDDLEWARES = [
"inertia.middleware.InertiaMiddleware",
# "django-breeze.django_breeze.middleware.inertia_share",
]

INSTALLED_APPS = [
"inertia",
"django_vite",
]

# Django Breeze Default Settings
DJANGO_BREEZE: dict = {
"INERTIA": {
"LAYOUT": "index.html",
"SSR_URL": inertia_settings.INERTIA_SSR_URL,
"SSR_ENABLED": inertia_settings.INERTIA_SSR_ENABLED,
"JSON_ENCODER": inertia_settings.INERTIA_JSON_ENCODER,
},
"DJANGO_VITE": {
"DEV_MODE": getattr(django_settings, "DEBUG", True),
"SERVER_PROTOCOL": "http",
"DEV_SERVER_HOST": "localhost",
"DEV_SERVER_PORT": 5173,
"WS_CLIENT_URL": "@vite/client",
"ASSETS_PATH": Path(getattr(django_settings, "STATIC_PATH", "static"))
/ "dist",
"STATIC_URL_PREFIX": "",
"LEGACY_POLYFILLS_MOTIF": "legacy-polyfills",
},
"STATIC_ROOT": "static",
"CSRF_HEADER_NAME": "HTTP_X_XSRF_TOKEN",
"CSRF_COOKIE_NAME": "XSRF-TOKEN",
}

# Merge DJANGO_BREEZE and user-defined settings
user_settings = getattr(django_settings, "DJANGO_BREEZE", {})

def merge_dicts(*dicts):
"""
Recursively merges dictionaries.
"""
result = {}
for d in dicts:
for k, v in d.items():
if isinstance(v, dict):
result[k] = merge_dicts(result.get(k, {}), v)
else:
result[k] = v
return result

merged_settings = merge_dicts(DJANGO_BREEZE, user_settings)

def key_exist(key) -> bool:
return (
hasattr(django_settings, key) and getattr(django_settings, key) is not None
)

settings = {}

for key, value in merged_settings.items():
if isinstance(value, dict):
for sub_key, sub_value in value.items():
sub_key = f"{key}_{sub_key}"
if not key_exist(sub_key):
settings[sub_key] = sub_value
else:
if not key_exist(key):
settings[key] = value

for key, value in settings.items():
setattr(django_settings, key, value)

django_settings.TEMPLATES[0]["DIRS"].append(Path(BASE_DIR) / "src/")

for app in INSTALLED_APPS:
django_settings.INSTALLED_APPS.append(app)

for middleware in MIDDLEWARES:
django_settings.MIDDLEWARE.append(middleware)
# django_settings.STATICFILES_DIRS.append(settings.DJANGO_VITE_ASSETS_PATH)

django.setup()
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-breeze"
version = "0.1.2"
version = "0.1.3"
description = "Django Breeze provides a minimal and simple starting point for building a Django application with Inertia.js Styled with Tailwind CSS."
keywords = ["react", "django", "vue", "inertia", "vite"]
authors = ["louxsdon <[email protected]>"]
Expand All @@ -19,7 +19,7 @@ inertia-django = "^0.5.2"
mypy = "^1.2.0"

[tool.poetry.scripts]
django-breeze = 'django_breeze.scripts.django-breeze'
django-breeze = 'django_breeze.scripts.django-breeze:run'

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit fb5b385

Please sign in to comment.