diff --git a/README.md b/README.md index ff53894..dcb783b 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,15 @@ Usage: dm [OPTIONS] COMMAND [ARGS]... Django MongoDB CLI + System executable: + + /Users/alexclark/Developer/django-mongodb-cli/.venv/bin/python + Options: --help Show this message and exit. Commands: - repo Run Django fork and third-party library tests. - startproject Run `startproject` with custom templates. + app Create Django apps configured to test django-mongodb-backend. + proj Create Django projects configured to test django-mongodb-backend. + repo Run tests configured to test django-mongodb-backend. ``` diff --git a/django_mongodb_cli/__init__.py b/django_mongodb_cli/__init__.py index c466a23..2b764c1 100644 --- a/django_mongodb_cli/__init__.py +++ b/django_mongodb_cli/__init__.py @@ -1,15 +1,9 @@ import click -import os import sys +from .app import app +from .proj import proj from .repo import repo -from .startproject import startproject - -if os.path.exists("manage.py"): - from .createsuperuser import createsuperuser - from .manage import manage - from .runserver import runserver - from .startapp import startapp def get_help_text(): @@ -24,11 +18,6 @@ def cli(): """Django MongoDB CLI""" +cli.add_command(app) +cli.add_command(proj) cli.add_command(repo) -cli.add_command(startproject) - -if os.path.exists("manage.py"): - cli.add_command(createsuperuser) - cli.add_command(manage) - cli.add_command(runserver) - cli.add_command(startapp) diff --git a/django_mongodb_cli/app.py b/django_mongodb_cli/app.py new file mode 100644 index 0000000..af47894 --- /dev/null +++ b/django_mongodb_cli/app.py @@ -0,0 +1,51 @@ +import click +import os +import subprocess + + +from .utils import get_management_command + + +class App: + def __init__(self): + self.config = {} + + def set_config(self, key, value): + self.config[key] = value + + def __repr__(self): + return f"" + + +pass_app = click.make_pass_decorator(App) + + +@click.group(invoke_without_command=True) +@click.pass_context +def app(context): + """ + Create Django apps configured to test django-mongodb-backend. + """ + context.obj = App() + + # Show help only if no subcommand is invoked + if context.invoked_subcommand is None: + click.echo(context.get_help()) + context.exit() + + +@app.command() +@click.argument("app_name", required=False, default="mongo_app") +def start(app_name): + """Run startapp command with the template from src/django-mongodb-app.""" + + click.echo("Running startapp.") + command = get_management_command("startapp") + subprocess.run( + command + + [ + app_name, + "--template", + os.path.join("src", "django-project-templates", "app_template"), + ], + ) diff --git a/django_mongodb_cli/createsuperuser.py b/django_mongodb_cli/createsuperuser.py deleted file mode 100644 index 47e8c3d..0000000 --- a/django_mongodb_cli/createsuperuser.py +++ /dev/null @@ -1,35 +0,0 @@ -import click -import os -import subprocess - - -from .utils import get_management_command - - -@click.command() -def createsuperuser(): - """Create a superuser with the username 'admin' and the email from git config.""" - try: - user_email = subprocess.check_output( - ["git", "config", "user.email"], text=True - ).strip() - except subprocess.CalledProcessError: - click.echo("Error: Unable to retrieve the user email from git config.") - return - - os.environ["DJANGO_SUPERUSER_PASSWORD"] = "admin" - - if os.environ.get("MONGODB_URI"): - click.echo(os.environ["MONGODB_URI"]) - click.echo(f"User email: {user_email}") - - command = get_management_command("createsuperuser") - - subprocess.run( - command - + [ - "--noinput", - "--username=admin", - f"--email={user_email}", - ] - ) diff --git a/django_mongodb_cli/manage.py b/django_mongodb_cli/manage.py deleted file mode 100644 index db94840..0000000 --- a/django_mongodb_cli/manage.py +++ /dev/null @@ -1,15 +0,0 @@ -import click -import subprocess - - -from .utils import get_management_command - - -@click.command(context_settings={"ignore_unknown_options": True}) -@click.argument("args", nargs=-1) -def manage(args): - """Run management commands.""" - - command = get_management_command() - - subprocess.run(command + [*args]) diff --git a/django_mongodb_cli/startproject.py b/django_mongodb_cli/proj.py similarity index 59% rename from django_mongodb_cli/startproject.py rename to django_mongodb_cli/proj.py index dfbda86..501fbe4 100644 --- a/django_mongodb_cli/startproject.py +++ b/django_mongodb_cli/proj.py @@ -4,18 +4,82 @@ import subprocess -@click.command() +from .utils import get_management_command + + +class Proj: + def __init__(self): + self.config = {} + + def set_config(self, key, value): + self.config[key] = value + + def __repr__(self): + return f"" + + +pass_proj = click.make_pass_decorator(Proj) + + +@click.group(invoke_without_command=True) +@click.pass_context +def proj(context): + """ + Create Django projects configured to test django-mongodb-backend. + """ + context.obj = Proj() + + # Show help only if no subcommand is invoked + if context.invoked_subcommand is None: + click.echo(context.get_help()) + context.exit() + + +@proj.command(context_settings={"ignore_unknown_options": True}) +@click.argument("args", nargs=-1) +def manage(args): + """Run management commands.""" + + command = get_management_command() + + subprocess.run(command + [*args]) + + +@proj.command() +def run(): + """Start the Django development server.""" + + if os.environ.get("MONGODB_URI"): + click.echo(os.environ["MONGODB_URI"]) + + command = get_management_command() + + # Start npm install + subprocess.run(["npm", "install"], cwd="frontend") + + # Start npm run watch + npm_process = subprocess.Popen(["npm", "run", "watch"], cwd="frontend") + + # Start django-admin runserver + django_process = subprocess.Popen(command + ["runserver"]) + + # Wait for both processes to complete + npm_process.wait() + django_process.wait() + + +@proj.command() @click.option("-d", "--delete", is_flag=True, help="Delete existing project files") @click.option("-dj", "--django", is_flag=True, help="Use django mongodb template") @click.option("-w", "--wagtail", is_flag=True, help="Use wagtail mongodb template") @click.argument("project_name", required=False, default="backend") -def startproject( +def start( delete, django, wagtail, project_name, ): - """Run `startproject` with custom templates.""" + """Run Django's `startproject` with custom templates.""" if os.path.exists("manage.py"): click.echo("manage.py already exists") if not delete: @@ -105,3 +169,32 @@ def startproject( home_template, ] ) + + +@proj.command() +def su(): + """Create a superuser with the username 'admin' and the email from git config.""" + try: + user_email = subprocess.check_output( + ["git", "config", "user.email"], text=True + ).strip() + except subprocess.CalledProcessError: + click.echo("Error: Unable to retrieve the user email from git config.") + return + + os.environ["DJANGO_SUPERUSER_PASSWORD"] = "admin" + + if os.environ.get("MONGODB_URI"): + click.echo(os.environ["MONGODB_URI"]) + click.echo(f"User email: {user_email}") + + command = get_management_command("createsuperuser") + + subprocess.run( + command + + [ + "--noinput", + "--username=admin", + f"--email={user_email}", + ] + ) diff --git a/django_mongodb_cli/runserver.py b/django_mongodb_cli/runserver.py deleted file mode 100644 index 3475bb4..0000000 --- a/django_mongodb_cli/runserver.py +++ /dev/null @@ -1,29 +0,0 @@ -import click -import os -import subprocess - - -from .utils import get_management_command - - -@click.command() -def runserver(): - """Start the Django development server.""" - - if os.environ.get("MONGODB_URI"): - click.echo(os.environ["MONGODB_URI"]) - - command = get_management_command() - - # Start npm install - subprocess.run(["npm", "install"], cwd="frontend") - - # Start npm run watch - npm_process = subprocess.Popen(["npm", "run", "watch"], cwd="frontend") - - # Start django-admin runserver - django_process = subprocess.Popen(command + ["runserver"]) - - # Wait for both processes to complete - npm_process.wait() - django_process.wait() diff --git a/django_mongodb_cli/startapp.py b/django_mongodb_cli/startapp.py deleted file mode 100644 index 26c7f45..0000000 --- a/django_mongodb_cli/startapp.py +++ /dev/null @@ -1,23 +0,0 @@ -import click -import os -import subprocess - - -from .utils import get_management_command - - -@click.command() -@click.argument("name", required=False) -def startapp(name): - """Run startapp command with the template from src/django-mongodb-app.""" - - click.echo("Running startapp.") - command = get_management_command("startapp") - subprocess.run( - command - + [ - name, - "--template", - os.path.join("src", "django-project-templates", "app_template"), - ], - ) diff --git a/justfile b/justfile index 8c935a2..84810a6 100644 --- a/justfile +++ b/justfile @@ -11,7 +11,7 @@ git-clone: dm repo clone django --install dm repo clone django-mongodb-app dm repo clone django-mongodb-backend --install - dm repo clone django-mongodb-extensions + dm repo clone django-mongodb-extensions --install dm repo clone django-mongodb-project dm repo clone django-mongodb-templates dm repo clone mongo-python-driver --install @@ -25,17 +25,17 @@ alias o := django-open [group('django')] django-runserver: - dm runserver + dm proj run alias s := django-runserver [group('django')] django-migrate: - dm manage migrate + dm proj migrate alias m := django-migrate [group('django')] django-createsuperuser: - dm createsuperuser + dm proj su alias su := django-createsuperuser # ---------------------------------------- mongodb ---------------------------------------- diff --git a/pyproject.toml b/pyproject.toml index 34ee293..1e1f511 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ dependencies = [ "sphinx-autobuild", # For django-mongodb-backend documentation "sphinx-copybutton", # For django-mongodb-backend documentation "toml", + "wagtail", # For django-mongodb-templates ] [tool.setuptools] diff --git a/test/settings/wagtail.py b/test/settings/wagtail.py index 372c427..3a64633 100644 --- a/test/settings/wagtail.py +++ b/test/settings/wagtail.py @@ -174,11 +174,11 @@ "0.0.0.0", ] -WAGTAILSEARCH_BACKENDS = { - "default": { - "BACKEND": "wagtail.search.backends.database.fallback", - } -} +WAGTAILSEARCH_BACKENDS = { # noqa + "default": { # noqa + "BACKEND": "wagtail.search.backends.database.fallback", # noqa + } # noqa +} # noqa EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"