feat: add support for detecting and restricting app creation in unified/single folder structures

This commit is contained in:
sankalp committed 2025-11-26 15:08:13 +05:45
1 parent d82884e831
commit f22c4613f5
3 files changed
+52 -23

No files matched your search

+36 -7
View File
@@ -36,6 +36,13 @@ class AppManager:
)
return False
if self._is_restricted_structure():
UIFormatter.print_error("The 'app' command is not supported for Unified or Single Folder structures.")
UIFormatter.print_info(
"Please add new apps manually by creating the necessary files and updating settings."
)
return False
if self._app_exists():
UIFormatter.print_error(f"Django app '{self.app_name}' already exists.")
return False
@@ -144,6 +151,28 @@ class AppManager:
api_dir = os.path.join(self.current_dir, "api")
return os.path.isdir(apps_dir) and os.path.isdir(api_dir)
def _is_restricted_structure(self) -> bool:
"""Check if the project structure is Unified or Single Folder."""
# Check for Unified Structure: apps/api exists
apps_api_dir = os.path.join(self.current_dir, "apps", "api")
if os.path.isdir(apps_api_dir):
return True
# Check for Single Folder Structure
# No apps/ directory, but project dir has models/ and api/
apps_dir = os.path.join(self.current_dir, "apps")
if os.path.isdir(apps_dir):
return False
project_dir, _ = find_project_dir(self.current_dir)
if project_dir:
models_dir = os.path.join(project_dir, "models")
api_dir = os.path.join(project_dir, "api")
if os.path.isdir(models_dir) and os.path.isdir(api_dir):
return True
return False
def _create_predefined_app(self, apps_dir: str) -> bool:
"""Create an app following the predefined nested structure with custom templates."""
app_dir = os.path.join(apps_dir, self.app_name)
@@ -158,18 +187,18 @@ class AppManager:
# Create apps.py
create_file_from_template(
os.path.join(app_dir, "apps.py"),
"base/apps.j2",
"components/apps.j2",
{"app_name": self.app_name},
f"Created apps/{self.app_name}/apps.py",
)
# Subfolders
subfolders = {
"models": [(f"{self.app_name}.py", "predefined/apps/generic/models.j2")],
"serializers": [(f"{self.app_name}_serializer.py", "predefined/apps/generic/serializers.j2")],
"services": [(f"{self.app_name}_service.py", "predefined/apps/generic/services.j2")],
"views": [(f"{self.app_name}_view.py", "predefined/apps/generic/views.j2")],
"tests": [(f"test_{self.app_name}_api.py", "predefined/apps/generic/tests.j2")],
"models": [(f"{self.app_name}.py", "presets/predefined/apps/generic/models.j2")],
"serializers": [(f"{self.app_name}_serializer.py", "presets/predefined/apps/generic/serializers.j2")],
"services": [(f"{self.app_name}_service.py", "presets/predefined/apps/generic/services.j2")],
"views": [(f"{self.app_name}_view.py", "presets/predefined/apps/generic/views.j2")],
"tests": [(f"test_{self.app_name}_api.py", "presets/predefined/apps/generic/tests.j2")],
}
for folder, files in subfolders.items():
folder_path = os.path.join(app_dir, folder)
@@ -183,7 +212,7 @@ class AppManager:
# urls.py at app root
create_file_from_template(
os.path.join(app_dir, "urls.py"),
"predefined/apps/generic/urls.j2",
"presets/predefined/apps/generic/urls.j2",
{"app_name": self.app_name, "app_module": app_module},
f"Created apps/{self.app_name}/urls.py",
)
+15 -15
View File
@@ -26,7 +26,7 @@ class DjangoHelper:
# Create manage.py
manage_py_path = os.path.join(directory, "manage.py")
create_file_from_template(manage_py_path, "shared/manage_py.j2", {}, "Created manage.py")
create_file_from_template(manage_py_path, "project/manage_py.j2", {}, "Created manage.py")
os.chmod(manage_py_path, 0o755)
# For unified structure, the project config is "core", not project_name
@@ -45,18 +45,18 @@ class DjangoHelper:
base_context = {"project_name": project_name, "app_names": []}
settings_files = [
("base.py", "project/settings/base.j2", base_context),
("development.py", "project/settings/development.j2", {}),
("production.py", "project/settings/production.j2", {}),
("base.py", "config/settings/base.j2", base_context),
("development.py", "config/settings/development.j2", {}),
("production.py", "config/settings/production.j2", {}),
]
create_files_from_templates(settings_dir, settings_files, f"{project_name}/settings/")
# Create project-level files
urls_context = {"project_name": project_name, "django_version": DjangoHelper.DJANGO_VERSION}
project_files = [
("urls.py", "project/urls.j2", urls_context),
("wsgi.py", "project/wsgi.j2", {}),
("asgi.py", "project/asgi.j2", {}),
("urls.py", "config/urls/base.j2", urls_context),
("wsgi.py", "config/wsgi.j2", {}),
("asgi.py", "config/asgi.j2", {}),
]
create_files_from_templates(project_config_dir, project_files, f"{project_name}/")
@@ -78,14 +78,14 @@ class DjangoHelper:
create_init_file(app_dir, f"Created {app_name}/__init__.py")
app_files = [
("apps.py", "base/apps.j2", context),
("models.py", "base/models.j2", context),
("views.py", "base/views.j2", context),
("admin.py", "base/admin.j2", context),
("urls.py", "base/urls.j2", context),
("serializers.py", "base/serializers.j2", context),
("routes.py", "base/routes.j2", context),
("tests.py", "base/tests.j2", context),
("apps.py", "components/apps.j2", context),
("models.py", "components/models.j2", context),
("views.py", "components/views.j2", context),
("admin.py", "components/admin.j2", context),
("urls.py", "components/urls.j2", context),
("serializers.py", "components/serializers.j2", context),
("routes.py", "components/routes.j2", context),
("tests.py", "components/tests.j2", context),
]
create_files_from_templates(app_dir, app_files, f"{app_name}/")
+1 -1
View File
@@ -44,7 +44,7 @@ def create_file_from_template(
# create __init__.py
def create_init_file(directory: str, success_message: str) -> None:
init_path = os.path.join(directory, "__init__.py")
create_file_from_template(init_path, "shared/init.j2", {}, success_message, should_format=False)
create_file_from_template(init_path, "project/init.j2", {}, success_message, should_format=False)
def create_directory_with_init(dir_path: str, message: str) -> None: