Skip to content

Move project* under dm proj, app* under dm app #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
```
19 changes: 4 additions & 15 deletions django_mongodb_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -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():
Expand All @@ -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)
51 changes: 51 additions & 0 deletions django_mongodb_cli/app.py
Original file line number Diff line number Diff line change
@@ -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"<App {self}>"


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"),
],
)
35 changes: 0 additions & 35 deletions django_mongodb_cli/createsuperuser.py

This file was deleted.

15 changes: 0 additions & 15 deletions django_mongodb_cli/manage.py

This file was deleted.

99 changes: 96 additions & 3 deletions django_mongodb_cli/startproject.py → django_mongodb_cli/proj.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<Proj {self}>"


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:
Expand Down Expand Up @@ -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}",
]
)
29 changes: 0 additions & 29 deletions django_mongodb_cli/runserver.py

This file was deleted.

23 changes: 0 additions & 23 deletions django_mongodb_cli/startapp.py

This file was deleted.

8 changes: 4 additions & 4 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ----------------------------------------
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
10 changes: 5 additions & 5 deletions test/settings/wagtail.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down