Skip to content

Commit c84492e

Browse files
committed
Misc updates
- Update PYTHON-5564 - Update project template - Add project specific settings via project_name.py - Update gitignore - Update settings - Remove `dm repo checkout`, combine with `branch` - Rename `dm project admin` -> manage - Always add remotes - Cap log at 10 - Rename `dm repo sync` -> pull - Add `dm repo push` command - Add missing contrib apps Update settings Update settings Update settings
1 parent c1c0cdf commit c84492e

File tree

18 files changed

+943
-332
lines changed

18 files changed

+943
-332
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
django_mongodb_cli.egg-info/
22
django_mongodb_cli/__pycache__/
33
/src/
4+
.idea
5+
server.log
6+
mongocryptd.pid

django_mongodb_cli/frontend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import importlib.resources as resources
66
import os
77

8-
frontend = typer.Typer(help="Manage Django apps.")
8+
frontend = typer.Typer(help="Manage Django frontends.")
99

1010

1111
@frontend.command("create")

django_mongodb_cli/project.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ def _create_pyproject_toml(project_path: Path, project_name: str):
8484
"pytest-django",
8585
"ruff",
8686
]
87+
encryption = [
88+
"pymongocrypt",
89+
]
8790
8891
[tool.pytest.ini_options]
8992
DJANGO_SETTINGS_MODULE = "{project_name}.settings.base"
@@ -114,7 +117,7 @@ def remove_project(name: str, directory: Path = Path(".")):
114117
typer.echo(f"❌ Project {name} does not exist.", err=True)
115118

116119

117-
def _django_admin_cmd(
120+
def _django_manage_command(
118121
name: str,
119122
directory: Path,
120123
*args: str,
@@ -132,12 +135,11 @@ def _django_admin_cmd(
132135

133136
parent_dir = project_path.parent.resolve()
134137
env = os.environ.copy()
135-
env["DJANGO_SETTINGS_MODULE"] = f"{name}.{
136-
repo._tool_cfg.get('project', {})
137-
.get('settings', {})
138-
.get('path', 'settings.base')
139-
}"
138+
env["DJANGO_SETTINGS_MODULE"] = (
139+
f"{name}.{repo._tool_cfg.get('project', {}).get('settings', {}).get('path', 'settings.base')}"
140+
)
140141
env["PYTHONPATH"] = str(name) + os.pathsep + env.get("PYTHONPATH", "")
142+
typer.echo(f"🔧 Using DJANGO_SETTINGS_MODULE={env['DJANGO_SETTINGS_MODULE']}")
141143
if extra_env:
142144
env.update(extra_env)
143145

@@ -206,7 +208,7 @@ def run_project(
206208
with MONGODB_URI set in the environment if provided.
207209
"""
208210
typer.echo(f"🚀 Running project '{name}' on http://{host}:{port}")
209-
_django_admin_cmd(
211+
_django_manage_command(
210212
name,
211213
directory / name,
212214
"runserver",
@@ -238,7 +240,9 @@ def migrate_project(
238240
cmd.append(migration_name)
239241

240242
typer.echo(f"📦 Applying migrations for project '{name}'")
241-
_django_admin_cmd(name, directory, *cmd, extra_env=_build_mongodb_env(mongodb_uri))
243+
_django_manage_command(
244+
name, directory, *cmd, extra_env=_build_mongodb_env(mongodb_uri)
245+
)
242246

243247

244248
@project.command("makemigrations")
@@ -259,11 +263,13 @@ def makemigrations_project(
259263
cmd.append(app_label)
260264

261265
typer.echo(f"🛠️ Making migrations for project '{name}'")
262-
_django_admin_cmd(name, directory, *cmd, extra_env=_build_mongodb_env(mongodb_uri))
266+
_django_manage_command(
267+
name, directory, *cmd, extra_env=_build_mongodb_env(mongodb_uri)
268+
)
263269

264270

265-
@project.command("admin")
266-
def admin_command(
271+
@project.command("manage")
272+
def manage_command(
267273
name: str,
268274
directory: Path = Path("."),
269275
command: str = typer.Argument(None),
@@ -276,10 +282,10 @@ def admin_command(
276282
Run any django-admin command for a project.
277283
278284
Examples:
279-
dm project admin mysite shell
280-
dm project admin mysite createsuperuser
281-
dm project admin mysite --mongodb-uri mongodb+srv://user:pwd@cluster
282-
dm project admin mysite
285+
dm project manage mysite shell
286+
dm project manage mysite createsuperuser
287+
dm project manage mysite --mongodb-uri mongodb+srv://user:pwd@cluster
288+
dm project manage mysite
283289
"""
284290
if args is None:
285291
args = []
@@ -293,10 +299,10 @@ def admin_command(
293299

294300
if command:
295301
typer.echo(f"⚙️ Running django-admin {command} {' '.join(args)} for '{name}'")
296-
_django_admin_cmd(name, directory, command, *args)
302+
_django_manage_command(name, directory, command, *args)
297303
else:
298304
typer.echo(f"ℹ️ Running django-admin with no arguments for '{name}'")
299-
_django_admin_cmd(name, directory)
305+
_django_manage_command(name, directory)
300306

301307

302308
@project.command("su")
@@ -331,7 +337,7 @@ def create_superuser(
331337
extra_env = _build_mongodb_env(mongodb_uri) or {}
332338
extra_env["DJANGO_SUPERUSER_PASSWORD"] = password
333339

334-
_django_admin_cmd(
340+
_django_manage_command(
335341
name,
336342
directory,
337343
"createsuperuser",

django_mongodb_cli/repo.py

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ def branch(
128128
repo = Repo()
129129
repo.ctx = ctx
130130
repo_list = repo.map
131+
132+
# Repo().checkout_branch(repo_name, branch_name)
133+
131134
if delete_branch and branch_name:
132135
repo.delete_branch(repo_name, branch_name)
133136
raise typer.Exit()
@@ -166,28 +169,6 @@ def cd(
166169
)
167170

168171

169-
@repo.command()
170-
def checkout(
171-
repo_name: str = typer.Argument(None),
172-
branch_name: str = typer.Argument(None, help="Branch name to checkout"),
173-
):
174-
"""
175-
Checkout a branch in a repository.
176-
"""
177-
178-
def checkout_branch(name):
179-
Repo().checkout_branch(repo_name, branch_name)
180-
181-
repo_command(
182-
False,
183-
repo_name,
184-
all_msg=None,
185-
missing_msg="Please specify a repository and branch name.",
186-
single_func=checkout_branch,
187-
all_func=checkout_branch,
188-
)
189-
190-
191172
@repo.command()
192173
def clone(
193174
repo_name: str = typer.Argument(None),
@@ -225,10 +206,9 @@ def commit(
225206
all_repos: bool = typer.Option(
226207
False, "--all-repos", "-a", help="Commit all repositories"
227208
),
228-
message: str = typer.Argument(None, help="Commit message"),
229209
):
230210
"""
231-
Commit changes in a repository with message provided or prompt for message.
211+
Commit changes in a repository
232212
"""
233213

234214
if all_repos:
@@ -237,7 +217,7 @@ def commit(
237217
)
238218

239219
def do_commit(name):
240-
Repo().commit_repo(name, message)
220+
Repo().commit_repo(name)
241221

242222
repo_command(
243223
all_repos,
@@ -503,16 +483,49 @@ def status(
503483

504484

505485
@repo.command()
506-
def sync(
486+
def pull(
487+
ctx: typer.Context,
488+
repo_name: str = typer.Argument(None),
489+
all_repos: bool = typer.Option(
490+
False, "--all-repos", "-a", help="Pull all repositories"
491+
),
492+
):
493+
"""
494+
Pull updates for the specified repository.
495+
If --all-repos is used, pull updates for all repositories.
496+
"""
497+
repo = Repo()
498+
repo.ctx = ctx
499+
if not repo.map:
500+
typer.echo(
501+
typer.style(
502+
f"No repositories found in {os.path.join(os.getcwd(), repo.pyproject_file)}.",
503+
fg=typer.colors.RED,
504+
)
505+
)
506+
raise typer.Exit()
507+
508+
repo_command(
509+
all_repos,
510+
repo_name,
511+
all_msg="Pulling all repositories...",
512+
missing_msg="Please specify a repository name or use -a,--all-repos to pull all repositories.",
513+
single_func=lambda repo_name: repo.pull(repo_name),
514+
all_func=lambda repo_name: repo.pull(repo_name),
515+
)
516+
517+
518+
@repo.command()
519+
def push(
507520
ctx: typer.Context,
508521
repo_name: str = typer.Argument(None),
509522
all_repos: bool = typer.Option(
510-
False, "--all-repos", "-a", help="Sync all repositories"
523+
False, "--all-repos", "-a", help="Push all repositories"
511524
),
512525
):
513526
"""
514-
Sync a repository with its remote counterpart.
515-
If --all-repos is used, sync all repositories.
527+
Push updates for the specified repository.
528+
If --all-repos is used, push updates for all repositories.
516529
"""
517530
repo = Repo()
518531
repo.ctx = ctx
@@ -528,10 +541,10 @@ def sync(
528541
repo_command(
529542
all_repos,
530543
repo_name,
531-
all_msg="Syncing all repositories...",
532-
missing_msg="Please specify a repository name or use -a,--all-repos to sync all repositories.",
533-
single_func=lambda repo_name: repo.sync_repo(repo_name),
534-
all_func=lambda repo_name: repo.sync_repo(repo_name),
544+
all_msg="Pushing all repositories...",
545+
missing_msg="Please specify a repository name or use -a,--all-repos to push all repositories.",
546+
single_func=lambda repo_name: repo.push(repo_name),
547+
all_func=lambda repo_name: repo.push(repo_name),
535548
)
536549

537550

django_mongodb_cli/templates/project_template/justfile

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,34 @@
33
default:
44
echo 'Hello, world!'
55

6-
[group('python')]
7-
install:
8-
python -m pip install --upgrade pip
9-
python -m pip install -e .
10-
alias i := install
6+
[group('django')]
7+
django-open:
8+
open http://localhost:8000
9+
alias o := django-open
1110

1211
[group('django')]
13-
serve:
12+
django-serve:
1413
python manage.py runserver
15-
alias s := serve
1614

1715
[group('django')]
18-
migrate:
16+
django-migrate:
1917
python manage.py migrate
20-
alias m := migrate
18+
alias m := django-migrate
19+
20+
[group('npm')]
21+
npm-install:
22+
cd frontend && npm install
23+
24+
[group('npm')]
25+
npm-serve:
26+
cd frontend && npm run watch &
27+
28+
[group('python')]
29+
pip-install:
30+
python -m pip install --upgrade pip
31+
python -m pip install -e .
32+
alias i := pip-install
33+
34+
serve: npm-install npm-serve django-serve
35+
36+
alias s := serve

django_mongodb_cli/templates/project_template/manage.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
def main():
99
"""Run administrative tasks."""
10-
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings.base")
10+
os.environ.setdefault(
11+
"DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings.{{ project_name }}"
12+
)
1113
try:
1214
from django.core.management import execute_from_command_line
1315
except ImportError as exc:

django_mongodb_cli/templates/project_template/project_name/apps.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from django.contrib.admin.apps import AdminConfig
22
from django.contrib.auth.apps import AuthConfig
33
from django.contrib.contenttypes.apps import ContentTypesConfig
4+
from django.contrib.flatpages.apps import FlatPagesConfig
5+
from django.contrib.redirects.apps import RedirectsConfig
6+
from django.contrib.sites.apps import SitesConfig
47

58

69
class MongoDBAdminConfig(AdminConfig):
@@ -13,3 +16,15 @@ class MongoDBAuthConfig(AuthConfig):
1316

1417
class MongoDBContentTypesConfig(ContentTypesConfig):
1518
default_auto_field = "django_mongodb_backend.fields.ObjectIdAutoField"
19+
20+
21+
class MongoDBFlatPagesConfig(FlatPagesConfig):
22+
default_auto_field = "django_mongodb_backend.fields.ObjectIdAutoField"
23+
24+
25+
class MongoDBRedirectsConfig(RedirectsConfig):
26+
default_auto_field = "django_mongodb_backend.fields.ObjectIdAutoField"
27+
28+
29+
class MongoDBSitesConfig(SitesConfig):
30+
default_auto_field = "django_mongodb_backend.fields.ObjectIdAutoField"
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class EncryptedRouter:
2+
def db_for_read(self, model, **hints):
3+
if model._meta.app_label == "django_mongodb_demo":
4+
return "encrypted"
5+
return None
6+
7+
db_for_write = db_for_read
8+
9+
def allow_migrate(self, db, app_label, model_name=None, **hints):
10+
if app_label == "django_mongodb_demo":
11+
print("allow_migrate for django_mongodb_demo:", db)
12+
return db == "encrypted"
13+
# Don't create other app's models in the encrypted database.
14+
if db == "encrypted":
15+
return False
16+
return None
17+
18+
def kms_provider(self, model, **hints):
19+
return "local"

0 commit comments

Comments
 (0)