Skip to content

Commit 0c864b9

Browse files
committed
Misc updates
- Add project templates - Add mongo migrations - Update qe.py - Update django test settings - Add -a with -s for `dm test` to show all test settings
1 parent 38ff5ec commit 0c864b9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+2272
-162
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ django_mongodb_cli.egg-info/
66
__pycache__
77
manage.py
88
mongo_app/
9-
mongo_migrations/
109
mongo_project/
1110
node_modules/
1211
server.log

django_mongodb_cli/app.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import click
22
import os
3+
import shutil
34
import subprocess
45

56

6-
from .utils import get_management_command
7+
from .utils import get_management_command, random_app_name
78

89

910
class App:
@@ -35,17 +36,37 @@ def app(context):
3536

3637

3738
@app.command()
38-
@click.argument("app_name", required=False, default="mongo_app")
39+
@click.argument("app_name", required=False)
3940
def start(app_name):
40-
"""Run startapp command with the template from src/django-mongodb-app."""
41+
"""Run startapp with a custom template and move the app into ./apps/."""
4142

42-
click.echo("Running startapp.")
43+
if not app_name:
44+
app_name = random_app_name()
45+
46+
if not app_name.isidentifier():
47+
raise click.UsageError(
48+
f"App name '{app_name}' is not a valid Python identifier."
49+
)
50+
51+
temp_path = app_name # Django will create the app here temporarily
52+
target_path = os.path.join("apps", app_name)
53+
54+
click.echo(f"Creating app '{app_name}' in ./apps")
55+
56+
# Make sure ./apps exists
57+
os.makedirs("apps", exist_ok=True)
58+
59+
# Run the Django startapp command
4360
command = get_management_command("startapp")
4461
subprocess.run(
4562
command
4663
+ [
47-
app_name,
64+
temp_path,
4865
"--template",
49-
os.path.join("src", "django-project-templates", "app_template"),
66+
os.path.join("templates", "app_template"),
5067
],
68+
check=True,
5169
)
70+
71+
# Move the generated app into ./apps/
72+
shutil.move(temp_path, target_path)

django_mongodb_cli/proj.py

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import subprocess
55

66

7-
from .utils import get_management_command
7+
from .utils import DELETE_DIRS_AND_FILES, get_management_command
88

99

1010
class Proj:
@@ -22,13 +22,27 @@ def __repr__(self):
2222

2323

2424
@click.group(invoke_without_command=True)
25+
@click.option("-d", "--delete", is_flag=True, help="Delete existing project files")
2526
@click.pass_context
26-
def proj(context):
27+
def proj(context, delete):
2728
"""
2829
Create Django projects configured to test django-mongodb-backend.
2930
"""
3031
context.obj = Proj()
3132

33+
if delete:
34+
for item, check_function in DELETE_DIRS_AND_FILES.items():
35+
if check_function(item):
36+
if os.path.isdir(item):
37+
shutil.rmtree(item)
38+
click.echo(f"Removed directory: {item}")
39+
elif os.path.isfile(item):
40+
os.remove(item)
41+
click.echo(f"Removed file: {item}")
42+
else:
43+
click.echo(f"Skipping: {item} does not exist")
44+
return
45+
3246
# Show help only if no subcommand is invoked
3347
if context.invoked_subcommand is None:
3448
click.echo(context.get_help())
@@ -69,54 +83,17 @@ def run():
6983

7084

7185
@proj.command()
72-
@click.option("-d", "--delete", is_flag=True, help="Delete existing project files")
7386
@click.option("-dj", "--django", is_flag=True, help="Use django mongodb template")
7487
@click.option("-w", "--wagtail", is_flag=True, help="Use wagtail mongodb template")
7588
@click.argument("project_name", required=False, default="backend")
7689
def start(
77-
delete,
7890
django,
7991
wagtail,
8092
project_name,
8193
):
8294
"""Run Django's `startproject` with custom templates."""
8395
if os.path.exists("manage.py"):
8496
click.echo("manage.py already exists")
85-
if not delete:
86-
click.echo("Use -d to delete existing project files")
87-
return
88-
if delete:
89-
items = {
90-
".babelrc": os.path.isfile,
91-
".dockerignore": os.path.isfile,
92-
".browserslistrc": os.path.isfile,
93-
".eslintrc": os.path.isfile,
94-
".nvmrc": os.path.isfile,
95-
".stylelintrc.json": os.path.isfile,
96-
"Dockerfile": os.path.isfile,
97-
"apps": os.path.isdir,
98-
"home": os.path.isdir,
99-
"backend": os.path.isdir,
100-
"db.sqlite3": os.path.isfile,
101-
"frontend": os.path.isdir,
102-
"mongo_migrations": os.path.isdir,
103-
"manage.py": os.path.isfile,
104-
"package-lock.json": os.path.isfile,
105-
"package.json": os.path.isfile,
106-
"postcss.config.js": os.path.isfile,
107-
"requirements.txt": os.path.isfile,
108-
"search": os.path.isdir,
109-
}
110-
for item, check_function in items.items():
111-
if check_function(item):
112-
if os.path.isdir(item):
113-
shutil.rmtree(item)
114-
click.echo(f"Removed directory: {item}")
115-
elif os.path.isfile(item):
116-
os.remove(item)
117-
click.echo(f"Removed file: {item}")
118-
else:
119-
click.echo(f"Skipping: {item} does not exist")
12097
return
12198
template = None
12299
django_admin = "django-admin"
@@ -129,9 +106,7 @@ def start(
129106
elif django:
130107
template = os.path.join(os.path.join("src", "django-mongodb-project"))
131108
if not template:
132-
template = os.path.join(
133-
os.path.join("src", "django-mongodb-templates", "project_template")
134-
)
109+
template = os.path.join(os.path.join("templates", "project_template"))
135110
click.echo(f"Using template: {template}")
136111
subprocess.run(
137112
[
@@ -143,9 +118,7 @@ def start(
143118
template,
144119
]
145120
)
146-
frontend_template = os.path.join(
147-
"src", "django-mongodb-templates", "frontend_template"
148-
)
121+
frontend_template = os.path.join("templates", "frontend_template")
149122
click.echo(f"Using template: {frontend_template}")
150123
subprocess.run(
151124
[
@@ -158,7 +131,7 @@ def start(
158131
]
159132
)
160133
if not wagtail:
161-
home_template = os.path.join("src", "django-mongodb-templates", "home_template")
134+
home_template = os.path.join("templates", "home_template")
162135
click.echo(f"Using template: {home_template}")
163136
subprocess.run(
164137
[

django_mongodb_cli/repo.py

Lines changed: 78 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
get_management_command,
1515
get_repos,
1616
get_status,
17+
get_repo_name_map,
1718
install_package,
1819
)
1920

@@ -33,15 +34,6 @@ def __repr__(self):
3334
pass_repo = click.make_pass_decorator(Repo)
3435

3536

36-
def get_repo_name_map(repos, url_pattern):
37-
"""Return a dict mapping repo_name to repo_url from a list of repo URLs."""
38-
return {
39-
os.path.basename(url_pattern.search(url).group(0)): url
40-
for url in repos
41-
if url_pattern.search(url)
42-
}
43-
44-
4537
@click.group(invoke_without_command=True)
4638
@click.option(
4739
"-l",
@@ -52,7 +44,7 @@ def get_repo_name_map(repos, url_pattern):
5244
@click.pass_context
5345
def repo(context, list_repos):
5446
"""
55-
Run Django fork and third-party library tests.
47+
Run tests configured to test django-mongodb-backend.
5648
"""
5749
context.obj = Repo()
5850
repos, url_pattern, branch_pattern = get_repos("pyproject.toml")
@@ -207,15 +199,75 @@ def makemigrations(context, repo_name, args):
207199
click.echo(context.get_help())
208200

209201

202+
@repo.command()
203+
@click.argument("repo_names", nargs=-1)
204+
@click.option("-a", "--all-repos", is_flag=True, help="Status for all repositories")
205+
@click.option("-r", "--reset", is_flag=True, help="Reset")
206+
@click.option("-d", "--diff", is_flag=True, help="Show diff")
207+
@click.option("-b", "--branch", is_flag=True, help="Show branch")
208+
@click.option("-u", "--update", is_flag=True, help="Update repos")
209+
@click.option("-l", "--log", is_flag=True, help="Show log")
210+
@click.pass_context
211+
@pass_repo
212+
def status(repo, context, repo_names, all_repos, reset, diff, branch, update, log):
213+
"""Repository status."""
214+
repos, url_pattern, _ = get_repos("pyproject.toml")
215+
repo_name_map = get_repo_name_map(repos, url_pattern)
216+
217+
# Status for specified repo names
218+
if repo_names:
219+
not_found = []
220+
for repo_name in repo_names:
221+
repo_url = repo_name_map.get(repo_name)
222+
if repo_url:
223+
get_status(
224+
repo_url,
225+
url_pattern,
226+
repo,
227+
reset=reset,
228+
diff=diff,
229+
branch=branch,
230+
update=update,
231+
log=log,
232+
)
233+
else:
234+
not_found.append(repo_name)
235+
for name in not_found:
236+
click.echo(f"Repository '{name}' not found.")
237+
return
238+
239+
# Status for all repos
240+
if all_repos:
241+
click.echo(f"Status of {len(repos)} repositories...")
242+
for repo_name, repo_url in repo_name_map.items():
243+
get_status(
244+
repo_url,
245+
url_pattern,
246+
repo,
247+
reset=reset,
248+
diff=diff,
249+
branch=branch,
250+
update=update,
251+
log=log,
252+
)
253+
return
254+
255+
# Show help if nothing selected
256+
click.echo(context.get_help())
257+
258+
210259
@repo.command()
211260
@click.argument("repo_name", required=False)
212261
@click.argument("modules", nargs=-1)
213262
@click.option("-k", "--keyword", help="Filter tests by keyword")
214263
@click.option("-l", "--list-tests", is_flag=True, help="List tests")
215-
@click.option("-s", "--show", is_flag=True, help="Show settings")
264+
@click.option("-s", "--show-settings", is_flag=True, help="Show settings")
265+
@click.option("-a", "--all-repos", is_flag=True, help="All repos")
216266
@click.option("--keepdb", is_flag=True, help="Keep db")
217267
@click.pass_context
218-
def test(context, repo_name, modules, keyword, list_tests, show, keepdb):
268+
def test(
269+
context, repo_name, modules, keyword, list_tests, show_settings, keepdb, all_repos
270+
):
219271
"""Run tests for Django fork and third-party libraries."""
220272
repos, url_pattern, _ = get_repos("pyproject.toml")
221273
repo_name_map = get_repo_name_map(repos, url_pattern)
@@ -229,7 +281,7 @@ def test(context, repo_name, modules, keyword, list_tests, show, keepdb):
229281
)
230282
return
231283

232-
if show:
284+
if show_settings:
233285
click.echo(f"⚙️ Test settings for 📦 {repo_name}:")
234286
settings_dict = dict(sorted(test_settings_map[repo_name].items()))
235287
formatted = format_str(str(settings_dict), mode=Mode())
@@ -327,64 +379,23 @@ def test(context, repo_name, modules, keyword, list_tests, show, keepdb):
327379
subprocess.run(command, cwd=settings["test_dir"])
328380
return
329381

330-
# No repo_name, show help
331-
click.echo(context.get_help())
332-
333-
334-
@repo.command()
335-
@click.argument("repo_names", nargs=-1)
336-
@click.option("-a", "--all-repos", is_flag=True, help="Status for all repositories")
337-
@click.option("-r", "--reset", is_flag=True, help="Reset")
338-
@click.option("-d", "--diff", is_flag=True, help="Show diff")
339-
@click.option("-b", "--branch", is_flag=True, help="Show branch")
340-
@click.option("-u", "--update", is_flag=True, help="Update repos")
341-
@click.option("-l", "--log", is_flag=True, help="Show log")
342-
@click.pass_context
343-
@pass_repo
344-
def status(repo, context, repo_names, all_repos, reset, diff, branch, update, log):
345-
"""Repository status."""
346-
repos, url_pattern, _ = get_repos("pyproject.toml")
347-
repo_name_map = get_repo_name_map(repos, url_pattern)
348-
349-
# Status for specified repo names
350-
if repo_names:
351-
not_found = []
352-
for repo_name in repo_names:
353-
repo_url = repo_name_map.get(repo_name)
354-
if repo_url:
355-
get_status(
356-
repo_url,
357-
url_pattern,
358-
repo,
359-
reset=reset,
360-
diff=diff,
361-
branch=branch,
362-
update=update,
363-
log=log,
364-
)
382+
if all_repos and show_settings:
383+
repos, url_pattern, _ = get_repos("pyproject.toml")
384+
repo_name_map = get_repo_name_map(repos, url_pattern)
385+
for repo_name in repo_name_map:
386+
if repo_name in test_settings_map:
387+
click.echo(f"⚙️ Test settings for 📦 {repo_name}:")
388+
settings_dict = dict(sorted(test_settings_map[repo_name].items()))
389+
formatted = format_str(str(settings_dict), mode=Mode())
390+
rprint(formatted)
365391
else:
366-
not_found.append(repo_name)
367-
for name in not_found:
368-
click.echo(f"Repository '{name}' not found.")
392+
click.echo(f"Settings for '{repo_name}' not found.")
369393
return
370-
371-
# Status for all repos
372-
if all_repos:
373-
click.echo(f"Status of {len(repos)} repositories...")
374-
for repo_name, repo_url in repo_name_map.items():
375-
get_status(
376-
repo_url,
377-
url_pattern,
378-
repo,
379-
reset=reset,
380-
diff=diff,
381-
branch=branch,
382-
update=update,
383-
log=log,
384-
)
394+
else:
395+
click.echo("Can only use --all-repos with --show-settings")
385396
return
386397

387-
# Show help if nothing selected
398+
# No repo_name, show help
388399
click.echo(context.get_help())
389400

390401

0 commit comments

Comments
 (0)