diff --git a/django_breeze/__init__.py b/django_breeze/__init__.py index 416ea7a..056f7fd 100644 --- a/django_breeze/__init__.py +++ b/django_breeze/__init__.py @@ -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" diff --git a/django_breeze/apps.py b/django_breeze/apps.py new file mode 100644 index 0000000..31ae219 --- /dev/null +++ b/django_breeze/apps.py @@ -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() diff --git a/django_breeze/scripts/django-breeze.py b/django_breeze/scripts/django-breeze.py index 42b3fd9..9711a92 100644 --- a/django_breeze/scripts/django-breeze.py +++ b/django_breeze/scripts/django-breeze.py @@ -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() diff --git a/django_breeze/settings.py b/django_breeze/settings.py new file mode 100644 index 0000000..ffd3560 --- /dev/null +++ b/django_breeze/settings.py @@ -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() diff --git a/pyproject.toml b/pyproject.toml index accfbc5..ce2134c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] @@ -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"]