refactor: restructure project for improved modularity and testability
This commit is contained in:
21 files changed
+171
-137
No files matched your search
+4
-4
@@ -6,10 +6,10 @@ Coordinates between different managers to create a complete Django project.
|
||||
import os
|
||||
from typing import Callable, List, Tuple
|
||||
|
||||
from djinit.scripts.console_ui import UIFormatter
|
||||
from djinit.scripts.files import FileManager
|
||||
from djinit.scripts.project import ProjectManager
|
||||
from djinit.scripts.secretkey_generator import generate_secret_command
|
||||
from djinit.services.files import FileManager
|
||||
from djinit.services.project import ProjectManager
|
||||
from djinit.ui.console import UIFormatter
|
||||
from djinit.utils.security import generate_secret_command
|
||||
|
||||
|
||||
class Cli:
|
||||
|
||||
File renamed without changes.
@@ -9,10 +9,10 @@ import sys
|
||||
from rich.panel import Panel
|
||||
from rich.text import Text
|
||||
|
||||
from djinit.scripts.app_generator import AppManager
|
||||
from djinit.scripts.console_ui import UIColors, UIFormatter, console
|
||||
from djinit.scripts.name_validator import validate_app_name
|
||||
from djinit.scripts.secretkey_generator import display_secret_keys, generate_multiple_keys
|
||||
from djinit.services.app import AppManager
|
||||
from djinit.ui.console import UIColors, UIFormatter, console
|
||||
from djinit.utils.security import display_secret_keys, generate_multiple_keys
|
||||
from djinit.utils.validators import validate_app_name
|
||||
|
||||
|
||||
def parse_arguments() -> argparse.Namespace:
|
||||
Whitespace-only changes.
@@ -0,0 +1,14 @@
|
||||
"""
|
||||
Core configuration and constants for djinit.
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
# Django version used for project generation
|
||||
DJANGO_VERSION = "5.2"
|
||||
|
||||
# Reserved names that cannot be used for projects or apps
|
||||
DJANGO_RESERVED = {"django", "test", "site-packages", "admin"}
|
||||
|
||||
# Python builtin module names
|
||||
PYTHON_BUILTINS = set(sys.builtin_module_names)
|
||||
@@ -0,0 +1,46 @@
|
||||
"""
|
||||
Core data structures and types for djinit.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Tuple
|
||||
|
||||
|
||||
@dataclass
|
||||
class ProjectMetadata:
|
||||
package_name: str
|
||||
use_github_actions: bool = False
|
||||
use_gitlab_ci: bool = False
|
||||
nested_apps: bool = False
|
||||
nested_dir: str | None = None
|
||||
use_database_url: bool = False
|
||||
predefined_structure: bool = False
|
||||
unified_structure: bool = False
|
||||
single_structure: bool = False
|
||||
project_module_name: str | None = None
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"package_name": self.package_name,
|
||||
"use_github_actions": self.use_github_actions,
|
||||
"use_gitlab_ci": self.use_gitlab_ci,
|
||||
"nested_apps": self.nested_apps,
|
||||
"nested_dir": self.nested_dir,
|
||||
"use_database_url": self.use_database_url,
|
||||
"predefined_structure": self.predefined_structure,
|
||||
"unified_structure": self.unified_structure,
|
||||
"single_structure": self.single_structure,
|
||||
"project_module_name": self.project_module_name,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class ProjectSetup:
|
||||
project_dir: str
|
||||
project_name: str
|
||||
primary_app: str
|
||||
app_names: list[str]
|
||||
metadata: ProjectMetadata
|
||||
|
||||
def to_tuple(self) -> Tuple[str, str, str, list, dict]:
|
||||
return (self.project_dir, self.project_name, self.primary_app, self.app_names, self.metadata.to_dict())
|
||||
+3
-3
@@ -8,9 +8,9 @@ import subprocess
|
||||
import sys
|
||||
|
||||
from djinit.cli import Cli
|
||||
from djinit.scripts.command_handler import handle_app_command, handle_secret_command, parse_arguments
|
||||
from djinit.scripts.console_ui import UIFormatter, console
|
||||
from djinit.scripts.input_handler import confirm_setup, get_user_input
|
||||
from djinit.commands.handler import handle_app_command, handle_secret_command, parse_arguments
|
||||
from djinit.ui.console import UIFormatter, console
|
||||
from djinit.ui.input import confirm_setup, get_user_input
|
||||
|
||||
|
||||
def clear_screen() -> None:
|
||||
|
||||
Whitespace-only changes.
@@ -6,9 +6,8 @@ Handles creation of Django apps and updating settings.
|
||||
import os
|
||||
from typing import Optional
|
||||
|
||||
from djinit.scripts.console_ui import UIFormatter
|
||||
from djinit.scripts.django_helper import DjangoHelper
|
||||
from djinit.utils import (
|
||||
from djinit.ui.console import UIFormatter
|
||||
from djinit.utils.common import (
|
||||
calculate_app_module_path,
|
||||
create_directory_with_init,
|
||||
create_file_from_template,
|
||||
@@ -20,6 +19,7 @@ from djinit.utils import (
|
||||
insert_apps_into_user_defined_apps,
|
||||
is_django_project,
|
||||
)
|
||||
from djinit.utils.django import DjangoHelper
|
||||
|
||||
|
||||
class AppManager:
|
||||
@@ -5,14 +5,13 @@ Handles creation and modification of project files.
|
||||
|
||||
import os
|
||||
|
||||
from djinit.scripts.template_engine import template_engine
|
||||
from djinit.utils import (
|
||||
from djinit.services.templates import template_engine
|
||||
from djinit.utils.common import (
|
||||
calculate_app_module_paths,
|
||||
change_cwd,
|
||||
create_directory_with_init,
|
||||
create_file_from_template,
|
||||
create_file_with_content,
|
||||
create_files_from_templates,
|
||||
get_package_name,
|
||||
)
|
||||
|
||||
@@ -200,19 +199,26 @@ class FileManager:
|
||||
return True
|
||||
|
||||
def create_unified_structure(self) -> bool:
|
||||
# 1. Create 'core' directory (Project Config)
|
||||
core_dir = os.path.join(self.project_root, "core")
|
||||
create_directory_with_init(core_dir, "Created core/__init__.py")
|
||||
|
||||
settings_dir = os.path.join(core_dir, "settings")
|
||||
create_directory_with_init(settings_dir, "Created core/settings/__init__.py")
|
||||
|
||||
from djinit.scripts.secretkey_generator import generate_secret_key
|
||||
from djinit.utils.security import generate_secret_key
|
||||
|
||||
secret_key = generate_secret_key()
|
||||
|
||||
# In this new structure, 'apps' is the main app.
|
||||
# We assume the user wants 'apps' to be the app label.
|
||||
# Or perhaps 'apps' is just a container and the components inside are what matters?
|
||||
# Given the structure: apps/models, apps/views etc.
|
||||
# It seems 'apps' is acting as the app.
|
||||
|
||||
base_context = {
|
||||
"project_name": "core", # Use "core" as the module name
|
||||
"app_names": ["apps.core", "apps.api"] + [f"apps.{app}" for app in self.app_names],
|
||||
"project_name": "core", # Project module is 'core'
|
||||
"app_names": ["apps"], # 'apps' is the main app
|
||||
"use_database_url": self.metadata.get("use_database_url", True),
|
||||
}
|
||||
dev_context = {"secret_key": secret_key}
|
||||
@@ -237,43 +243,38 @@ class FileManager:
|
||||
filepath = os.path.join(core_dir, filename)
|
||||
create_file_from_template(filepath, template, context, f"Created core/{filename}", should_format=True)
|
||||
|
||||
# 2. Create 'apps' directory (The Main App)
|
||||
apps_dir = os.path.join(self.project_root, "apps")
|
||||
create_directory_with_init(apps_dir, "Created apps/__init__.py")
|
||||
|
||||
apps_core_dir = os.path.join(apps_dir, "core")
|
||||
create_directory_with_init(apps_core_dir, "Created apps/core/__init__.py")
|
||||
# Create apps.py for the 'apps' app
|
||||
self._create_apps_py(apps_dir, "apps", "Created apps/apps.py", should_format=True)
|
||||
|
||||
self._create_apps_py(apps_core_dir, "", "Created apps/core/apps.py", should_format=True)
|
||||
# Create sub-components as directories with __init__.py
|
||||
components = ["admin", "models", "serializers", "tests", "urls", "views"]
|
||||
self._create_subdirectories_with_init(apps_dir, components, "apps/")
|
||||
|
||||
models_dir = os.path.join(apps_core_dir, "models")
|
||||
model_files = [
|
||||
("__init__.py", "components/models.j2"),
|
||||
("base.py", "presets/unified/apps/core/models/base.py.j2"),
|
||||
("mixins.py", "presets/unified/apps/core/models/mixins.py.j2"),
|
||||
]
|
||||
os.makedirs(models_dir, exist_ok=True)
|
||||
create_files_from_templates(
|
||||
models_dir, [(f, t, {}) for f, t in model_files], "apps/core/models/", should_format=True
|
||||
# Create 'api' directory with v1
|
||||
api_dir = os.path.join(apps_dir, "api")
|
||||
create_directory_with_init(api_dir, "Created apps/api/__init__.py")
|
||||
|
||||
create_file_from_template(
|
||||
os.path.join(api_dir, "urls.py"),
|
||||
"presets/predefined/api/urls.j2",
|
||||
{},
|
||||
"Created apps/api/urls.py",
|
||||
should_format=True,
|
||||
)
|
||||
|
||||
utils_dir = os.path.join(apps_core_dir, "utils")
|
||||
utils_files = [
|
||||
("__init__.py", "presets/unified/apps/core/utils/__init__.py.j2"),
|
||||
("responses.py", "presets/unified/apps/core/utils/responses.py.j2"),
|
||||
]
|
||||
os.makedirs(utils_dir, exist_ok=True)
|
||||
create_files_from_templates(
|
||||
utils_dir, [(f, t, {}) for f, t in utils_files], "apps/core/utils/", should_format=True
|
||||
)
|
||||
api_v1_dir = os.path.join(api_dir, "v1")
|
||||
create_directory_with_init(api_v1_dir, "Created apps/api/v1/__init__.py")
|
||||
|
||||
self._create_subdirectories_with_init(apps_core_dir, ["permissions", "middleware"], "apps/core/")
|
||||
|
||||
apps_api_dir = os.path.join(apps_dir, "api")
|
||||
create_directory_with_init(apps_api_dir, "Created apps/api/__init__.py")
|
||||
self._create_apps_py(apps_api_dir, "apps.api", "Created apps/api/apps.py", should_format=True)
|
||||
|
||||
self._create_subdirectories_with_init(
|
||||
apps_api_dir, ["models", "serializers", "views", "services", "tests", "urls", "admin"], "apps/api/"
|
||||
create_file_from_template(
|
||||
os.path.join(api_v1_dir, "urls.py"),
|
||||
"presets/predefined/api/v1/urls.j2",
|
||||
{},
|
||||
"Created apps/api/v1/urls.py",
|
||||
should_format=True,
|
||||
)
|
||||
|
||||
return True
|
||||
@@ -285,7 +286,7 @@ class FileManager:
|
||||
settings_dir = os.path.join(project_dir, "settings")
|
||||
create_directory_with_init(settings_dir, f"Created {self.module_name}/settings/__init__.py")
|
||||
|
||||
from djinit.scripts.secretkey_generator import generate_secret_key
|
||||
from djinit.utils.security import generate_secret_key
|
||||
|
||||
secret_key = generate_secret_key()
|
||||
|
||||
@@ -5,9 +5,8 @@ Handles creation of Django projects and apps.
|
||||
|
||||
import os
|
||||
|
||||
from djinit.scripts.console_ui import UIFormatter
|
||||
from djinit.scripts.django_helper import DjangoHelper
|
||||
from djinit.utils import (
|
||||
from djinit.ui.console import UIFormatter
|
||||
from djinit.utils.common import (
|
||||
calculate_app_module_paths,
|
||||
create_directory_with_init,
|
||||
extract_existing_apps,
|
||||
@@ -15,6 +14,7 @@ from djinit.utils import (
|
||||
insert_apps_into_user_defined_apps,
|
||||
read_base_settings,
|
||||
)
|
||||
from djinit.utils.django import DjangoHelper
|
||||
|
||||
|
||||
class ProjectManager:
|
||||
@@ -110,11 +110,26 @@ class ProjectManager:
|
||||
|
||||
apps_base_dir = self._get_apps_base_dir()
|
||||
|
||||
if (
|
||||
not self.metadata.get("predefined_structure")
|
||||
and not self.metadata.get("unified_structure")
|
||||
and not self.metadata.get("single_structure")
|
||||
):
|
||||
if self.metadata.get("unified_structure"):
|
||||
# Validate Unified Structure
|
||||
apps_dir = os.path.join(self.project_root, "apps")
|
||||
unified_files = [
|
||||
os.path.join(apps_dir, "__init__.py"),
|
||||
os.path.join(apps_dir, "apps.py"),
|
||||
os.path.join(apps_dir, "admin", "__init__.py"),
|
||||
os.path.join(apps_dir, "api", "__init__.py"),
|
||||
os.path.join(apps_dir, "api", "urls.py"),
|
||||
os.path.join(apps_dir, "api", "v1", "__init__.py"),
|
||||
os.path.join(apps_dir, "api", "v1", "urls.py"),
|
||||
os.path.join(apps_dir, "models", "__init__.py"),
|
||||
os.path.join(apps_dir, "serializers", "__init__.py"),
|
||||
os.path.join(apps_dir, "tests", "__init__.py"),
|
||||
os.path.join(apps_dir, "urls", "__init__.py"),
|
||||
os.path.join(apps_dir, "views", "__init__.py"),
|
||||
]
|
||||
required_files.extend(unified_files)
|
||||
|
||||
elif not self.metadata.get("predefined_structure") and not self.metadata.get("single_structure"):
|
||||
for app_name in self.app_names:
|
||||
app_files = [
|
||||
os.path.join(apps_base_dir, app_name, "__init__.py"),
|
||||
@@ -20,6 +20,7 @@ class TemplateEngine:
|
||||
template_dir: Path to templates directory. If None, uses default.
|
||||
"""
|
||||
if template_dir is None:
|
||||
# djinit/services/templates.py -> djinit/services -> djinit -> templates
|
||||
template_dir = Path(__file__).resolve().parent.parent / "templates"
|
||||
|
||||
if not template_dir.exists():
|
||||
@@ -114,36 +114,35 @@ Run `just` to see all available commands, including:
|
||||
└── README.md
|
||||
{% elif unified_structure %}
|
||||
{{ project_name }}/
|
||||
├── apps/
|
||||
│ ├── admin/
|
||||
│ │ └── __init__.py
|
||||
│ ├── api/
|
||||
│ │ ├── __init__.py
|
||||
│ │ ├── urls.py
|
||||
│ │ └── v1/
|
||||
│ │ ├── __init__.py
|
||||
│ │ └── urls.py
|
||||
│ ├── models/
|
||||
│ │ └── __init__.py
|
||||
│ ├── serializers/
|
||||
│ │ └── __init__.py
|
||||
│ ├── tests/
|
||||
│ │ └── __init__.py
|
||||
│ ├── urls/
|
||||
│ │ └── __init__.py
|
||||
│ └── views/
|
||||
│ └── __init__.py
|
||||
├── core/
|
||||
│ ├── asgi.py
|
||||
│ ├── __init__.py
|
||||
│ ├── settings/
|
||||
│ │ ├── base.py
|
||||
│ │ ├── development.py
|
||||
│ │ ├── __init__.py
|
||||
│ │ └── production.py
|
||||
│ ├── urls.py
|
||||
│ ├── wsgi.py
|
||||
│ └── asgi.py
|
||||
├── apps/
|
||||
│ ├── core/
|
||||
│ │ ├── apps.py
|
||||
│ │ ├── models/
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ ├── base.py
|
||||
│ │ │ └── mixins.py
|
||||
│ │ ├── utils/
|
||||
│ │ │ ├── __init__.py
|
||||
│ │ │ └── responses.py
|
||||
│ │ ├── permissions/
|
||||
│ │ ├── middleware/
|
||||
│ │ └── __init__.py
|
||||
│ └── api/
|
||||
│ ├── apps.py
|
||||
│ ├── models/
|
||||
│ ├── serializers/
|
||||
│ ├── views/
|
||||
│ ├── services/
|
||||
│ ├── tests/
|
||||
│ ├── urls/
|
||||
│ └── admin/
|
||||
│ └── wsgi.py
|
||||
├── manage.py
|
||||
├── requirements.txt
|
||||
├── pyproject.toml
|
||||
|
||||
Whitespace-only changes.
@@ -26,6 +26,7 @@ class UIColors:
|
||||
HIGHLIGHT = "bold cyan"
|
||||
MUTED = "dim white"
|
||||
ACCENT = "bold magenta"
|
||||
CODE = "bold white"
|
||||
|
||||
|
||||
class Icons:
|
||||
@@ -7,13 +7,13 @@ import os
|
||||
import sys
|
||||
import termios
|
||||
import tty
|
||||
from dataclasses import dataclass
|
||||
from enum import Enum
|
||||
from typing import Callable, Tuple
|
||||
|
||||
from djinit.scripts.console_ui import UIColors, UIFormatter, console
|
||||
from djinit.scripts.name_validator import validate_app_name, validate_project_name
|
||||
from djinit.utils import get_package_name
|
||||
from djinit.core.types import ProjectMetadata
|
||||
from djinit.ui.console import UIColors, UIFormatter, console
|
||||
from djinit.utils.common import get_package_name
|
||||
from djinit.utils.validators import validate_app_name, validate_project_name
|
||||
|
||||
|
||||
class CICDOption(Enum):
|
||||
@@ -27,47 +27,6 @@ class CICDOption(Enum):
|
||||
self.description = description
|
||||
|
||||
|
||||
@dataclass
|
||||
class ProjectMetadata:
|
||||
package_name: str
|
||||
use_github_actions: bool = False
|
||||
use_gitlab_ci: bool = False
|
||||
nested_apps: bool = False
|
||||
nested_dir: str | None = None
|
||||
use_database_url: bool = False
|
||||
predefined_structure: bool = False
|
||||
predefined_structure: bool = False
|
||||
unified_structure: bool = False
|
||||
single_structure: bool = False
|
||||
project_module_name: str | None = None
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"package_name": self.package_name,
|
||||
"use_github_actions": self.use_github_actions,
|
||||
"use_gitlab_ci": self.use_gitlab_ci,
|
||||
"nested_apps": self.nested_apps,
|
||||
"nested_dir": self.nested_dir,
|
||||
"use_database_url": self.use_database_url,
|
||||
"predefined_structure": self.predefined_structure,
|
||||
"unified_structure": self.unified_structure,
|
||||
"single_structure": self.single_structure,
|
||||
"project_module_name": self.project_module_name,
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class ProjectSetup:
|
||||
project_dir: str
|
||||
project_name: str
|
||||
primary_app: str
|
||||
app_names: list[str]
|
||||
metadata: ProjectMetadata
|
||||
|
||||
def to_tuple(self) -> Tuple[str, str, str, list, dict]:
|
||||
return (self.project_dir, self.project_name, self.primary_app, self.app_names, self.metadata.to_dict())
|
||||
|
||||
|
||||
class InputCollector:
|
||||
MAX_ATTEMPTS = 3
|
||||
|
||||
Whitespace-only changes.
@@ -8,8 +8,8 @@ import subprocess
|
||||
import sys
|
||||
from contextlib import contextmanager
|
||||
|
||||
from djinit.scripts.console_ui import UIFormatter
|
||||
from djinit.scripts.template_engine import template_engine
|
||||
from djinit.services.templates import template_engine
|
||||
from djinit.ui.console import UIFormatter
|
||||
|
||||
|
||||
def format_file(filename: str) -> None:
|
||||
@@ -6,8 +6,9 @@ This module provides functions to create Django projects and apps without requir
|
||||
|
||||
import os
|
||||
|
||||
from djinit.scripts.console_ui import UIFormatter
|
||||
from djinit.utils import (
|
||||
from djinit.core.config import DJANGO_VERSION
|
||||
from djinit.ui.console import UIFormatter
|
||||
from djinit.utils.common import (
|
||||
create_directory_with_init,
|
||||
create_file_from_template,
|
||||
create_files_from_templates,
|
||||
@@ -16,7 +17,7 @@ from djinit.utils import (
|
||||
|
||||
|
||||
class DjangoHelper:
|
||||
DJANGO_VERSION = "5.2"
|
||||
DJANGO_VERSION = DJANGO_VERSION
|
||||
|
||||
@staticmethod
|
||||
def startproject(project_name: str, directory: str, unified: bool = False) -> bool:
|
||||
@@ -12,7 +12,7 @@ from rich.panel import Panel
|
||||
from rich.table import Table
|
||||
from rich.text import Text
|
||||
|
||||
from djinit.scripts.console_ui import UIColors
|
||||
from djinit.ui.console import UIColors
|
||||
|
||||
console = Console()
|
||||
|
||||
@@ -5,12 +5,9 @@ Handles validation of project names, app names, and other user inputs.
|
||||
|
||||
import keyword
|
||||
import re
|
||||
import sys
|
||||
from typing import Any, Callable, Tuple
|
||||
|
||||
DJANGO_RESERVED = {"django", "test", "site-packages", "admin"}
|
||||
|
||||
PYTHON_BUILTINS = set(sys.builtin_module_names)
|
||||
from djinit.core.config import PYTHON_BUILTINS
|
||||
|
||||
NAME_PATTERN = re.compile(r"^[a-zA-Z][a-zA-Z0-9_]*$")
|
||||
|
||||
Reference in new issue
Block a user