Skip to content

Commit 38ff5ec

Browse files
authored
Merge pull request #34 from aclark4life/main
Move project* under `dm proj`, app* under `dm app`
2 parents 70d9b70 + 14a56d3 commit 38ff5ec

File tree

11 files changed

+168
-131
lines changed

11 files changed

+168
-131
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,15 @@ Usage: dm [OPTIONS] COMMAND [ARGS]...
2626
2727
Django MongoDB CLI
2828
29+
System executable:
30+
31+
/Users/alexclark/Developer/django-mongodb-cli/.venv/bin/python
32+
2933
Options:
3034
--help Show this message and exit.
3135
3236
Commands:
33-
repo Run Django fork and third-party library tests.
34-
startproject Run `startproject` with custom templates.
37+
app Create Django apps configured to test django-mongodb-backend.
38+
proj Create Django projects configured to test django-mongodb-backend.
39+
repo Run tests configured to test django-mongodb-backend.
3540
```

django_mongodb_cli/__init__.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import click
2-
import os
32
import sys
43

4+
from .app import app
5+
from .proj import proj
56
from .repo import repo
6-
from .startproject import startproject
7-
8-
if os.path.exists("manage.py"):
9-
from .createsuperuser import createsuperuser
10-
from .manage import manage
11-
from .runserver import runserver
12-
from .startapp import startapp
137

148

159
def get_help_text():
@@ -24,11 +18,6 @@ def cli():
2418
"""Django MongoDB CLI"""
2519

2620

21+
cli.add_command(app)
22+
cli.add_command(proj)
2723
cli.add_command(repo)
28-
cli.add_command(startproject)
29-
30-
if os.path.exists("manage.py"):
31-
cli.add_command(createsuperuser)
32-
cli.add_command(manage)
33-
cli.add_command(runserver)
34-
cli.add_command(startapp)

django_mongodb_cli/app.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import click
2+
import os
3+
import subprocess
4+
5+
6+
from .utils import get_management_command
7+
8+
9+
class App:
10+
def __init__(self):
11+
self.config = {}
12+
13+
def set_config(self, key, value):
14+
self.config[key] = value
15+
16+
def __repr__(self):
17+
return f"<App {self}>"
18+
19+
20+
pass_app = click.make_pass_decorator(App)
21+
22+
23+
@click.group(invoke_without_command=True)
24+
@click.pass_context
25+
def app(context):
26+
"""
27+
Create Django apps configured to test django-mongodb-backend.
28+
"""
29+
context.obj = App()
30+
31+
# Show help only if no subcommand is invoked
32+
if context.invoked_subcommand is None:
33+
click.echo(context.get_help())
34+
context.exit()
35+
36+
37+
@app.command()
38+
@click.argument("app_name", required=False, default="mongo_app")
39+
def start(app_name):
40+
"""Run startapp command with the template from src/django-mongodb-app."""
41+
42+
click.echo("Running startapp.")
43+
command = get_management_command("startapp")
44+
subprocess.run(
45+
command
46+
+ [
47+
app_name,
48+
"--template",
49+
os.path.join("src", "django-project-templates", "app_template"),
50+
],
51+
)

django_mongodb_cli/createsuperuser.py

Lines changed: 0 additions & 35 deletions
This file was deleted.

django_mongodb_cli/manage.py

Lines changed: 0 additions & 15 deletions
This file was deleted.

django_mongodb_cli/startproject.py renamed to django_mongodb_cli/proj.py

Lines changed: 96 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,82 @@
44
import subprocess
55

66

7-
@click.command()
7+
from .utils import get_management_command
8+
9+
10+
class Proj:
11+
def __init__(self):
12+
self.config = {}
13+
14+
def set_config(self, key, value):
15+
self.config[key] = value
16+
17+
def __repr__(self):
18+
return f"<Proj {self}>"
19+
20+
21+
pass_proj = click.make_pass_decorator(Proj)
22+
23+
24+
@click.group(invoke_without_command=True)
25+
@click.pass_context
26+
def proj(context):
27+
"""
28+
Create Django projects configured to test django-mongodb-backend.
29+
"""
30+
context.obj = Proj()
31+
32+
# Show help only if no subcommand is invoked
33+
if context.invoked_subcommand is None:
34+
click.echo(context.get_help())
35+
context.exit()
36+
37+
38+
@proj.command(context_settings={"ignore_unknown_options": True})
39+
@click.argument("args", nargs=-1)
40+
def manage(args):
41+
"""Run management commands."""
42+
43+
command = get_management_command()
44+
45+
subprocess.run(command + [*args])
46+
47+
48+
@proj.command()
49+
def run():
50+
"""Start the Django development server."""
51+
52+
if os.environ.get("MONGODB_URI"):
53+
click.echo(os.environ["MONGODB_URI"])
54+
55+
command = get_management_command()
56+
57+
# Start npm install
58+
subprocess.run(["npm", "install"], cwd="frontend")
59+
60+
# Start npm run watch
61+
npm_process = subprocess.Popen(["npm", "run", "watch"], cwd="frontend")
62+
63+
# Start django-admin runserver
64+
django_process = subprocess.Popen(command + ["runserver"])
65+
66+
# Wait for both processes to complete
67+
npm_process.wait()
68+
django_process.wait()
69+
70+
71+
@proj.command()
872
@click.option("-d", "--delete", is_flag=True, help="Delete existing project files")
973
@click.option("-dj", "--django", is_flag=True, help="Use django mongodb template")
1074
@click.option("-w", "--wagtail", is_flag=True, help="Use wagtail mongodb template")
1175
@click.argument("project_name", required=False, default="backend")
12-
def startproject(
76+
def start(
1377
delete,
1478
django,
1579
wagtail,
1680
project_name,
1781
):
18-
"""Run `startproject` with custom templates."""
82+
"""Run Django's `startproject` with custom templates."""
1983
if os.path.exists("manage.py"):
2084
click.echo("manage.py already exists")
2185
if not delete:
@@ -105,3 +169,32 @@ def startproject(
105169
home_template,
106170
]
107171
)
172+
173+
174+
@proj.command()
175+
def su():
176+
"""Create a superuser with the username 'admin' and the email from git config."""
177+
try:
178+
user_email = subprocess.check_output(
179+
["git", "config", "user.email"], text=True
180+
).strip()
181+
except subprocess.CalledProcessError:
182+
click.echo("Error: Unable to retrieve the user email from git config.")
183+
return
184+
185+
os.environ["DJANGO_SUPERUSER_PASSWORD"] = "admin"
186+
187+
if os.environ.get("MONGODB_URI"):
188+
click.echo(os.environ["MONGODB_URI"])
189+
click.echo(f"User email: {user_email}")
190+
191+
command = get_management_command("createsuperuser")
192+
193+
subprocess.run(
194+
command
195+
+ [
196+
"--noinput",
197+
"--username=admin",
198+
f"--email={user_email}",
199+
]
200+
)

django_mongodb_cli/runserver.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

django_mongodb_cli/startapp.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

justfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ git-clone:
1111
dm repo clone django --install
1212
dm repo clone django-mongodb-app
1313
dm repo clone django-mongodb-backend --install
14-
dm repo clone django-mongodb-extensions
14+
dm repo clone django-mongodb-extensions --install
1515
dm repo clone django-mongodb-project
1616
dm repo clone django-mongodb-templates
1717
dm repo clone mongo-python-driver --install
@@ -25,17 +25,17 @@ alias o := django-open
2525

2626
[group('django')]
2727
django-runserver:
28-
dm runserver
28+
dm proj run
2929
alias s := django-runserver
3030

3131
[group('django')]
3232
django-migrate:
33-
dm manage migrate
33+
dm proj migrate
3434
alias m := django-migrate
3535

3636
[group('django')]
3737
django-createsuperuser:
38-
dm createsuperuser
38+
dm proj su
3939
alias su := django-createsuperuser
4040

4141
# ---------------------------------------- mongodb ----------------------------------------

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ dependencies = [
2828
"sphinx-autobuild", # For django-mongodb-backend documentation
2929
"sphinx-copybutton", # For django-mongodb-backend documentation
3030
"toml",
31+
"wagtail", # For django-mongodb-templates
3132
]
3233

3334
[tool.setuptools]

0 commit comments

Comments
 (0)