-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement settings initialization when django server starts
- Loading branch information
Showing
5 changed files
with
111 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]>"] | ||
|
@@ -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"] | ||
|