-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
147 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
.git | ||
.idea | ||
.venv | ||
.vscode | ||
|
||
**/*~ | ||
**/.DS_Store | ||
**/db.sqlite | ||
|
||
{{cookiecutter.name}}/src/apps/testapp | ||
{{cookiecutter.name}}/tests/apps/testapp | ||
|
||
db.sqlite | ||
testproject | ||
venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,11 @@ | ||
#!/bin/bash -e | ||
|
||
rm -rf .mypy_cache \ | ||
.pytest_cache \ | ||
.venv \ | ||
db.sqlite | ||
|
||
cp src/core/.env.ci src/core/.env | ||
|
||
poetry install | ||
|
||
poetry run python src/manage.py collectstatic | ||
poetry run python src/manage.py migrate | ||
poetry run python src/manage.py startapp test_app | ||
poetry run python src/manage.py startapp some_app --entity_name some_entity | ||
|
||
make checks test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
manage = poetry run src/manage.py | ||
manage = poetry run python src/manage.py | ||
numprocesses = 4 | ||
|
||
checks: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
from django.contrib import admin | ||
|
||
from apps.{{ app_name }}.models import {{ camel_case_entity_name }} | ||
from core.admin import ModelAdmin | ||
|
||
|
||
@admin.register({{ camel_case_entity_name }}) | ||
class {{ camel_case_entity_name }}Admin(ModelAdmin): | ||
pass |
3 changes: 0 additions & 3 deletions
3
{{cookiecutter.name}}/src/.django-app-template/api/serializers/__init__.py
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
{{cookiecutter.name}}/src/.django-app-template/api/serializers/app_name.py-tpl
This file was deleted.
Oops, something went wrong.
12 changes: 0 additions & 12 deletions
12
{{cookiecutter.name}}/src/.django-app-template/api/urls.py-tpl
This file was deleted.
Oops, something went wrong.
Empty file.
5 changes: 5 additions & 0 deletions
5
{{cookiecutter.name}}/src/.django-app-template/api/v1/serializers/__init__.py-tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from apps.{{ app_name }}.api.v1.serializers.{{ entity_name }} import {{ camel_case_entity_name }}ReadSerializer | ||
|
||
__all__ = [ | ||
"{{ camel_case_entity_name }}ReadSerializer", | ||
] |
10 changes: 10 additions & 0 deletions
10
{{cookiecutter.name}}/src/.django-app-template/api/v1/serializers/entity_name.py-tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from rest_framework import serializers | ||
|
||
from apps.{{ app_name }}.models import {{ camel_case_entity_name }} | ||
|
||
|
||
class {{ camel_case_entity_name }}ReadSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
fields = ("somefield",) | ||
model = {{ camel_case_entity_name }} | ||
read_only_fields = fields |
11 changes: 11 additions & 0 deletions
11
{{cookiecutter.name}}/src/.django-app-template/api/v1/urls.py-tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.urls import include, path | ||
from rest_framework.routers import SimpleRouter | ||
|
||
from apps.{{ app_name }}.api.v1 import views | ||
|
||
router = SimpleRouter() | ||
router.register("{{ app_name }}", views.{{ camel_case_entity_name }}ViewSet) | ||
|
||
urlpatterns = [ | ||
path("", include(router.urls)), | ||
] |
5 changes: 5 additions & 0 deletions
5
{{cookiecutter.name}}/src/.django-app-template/api/v1/views/__init__.py-tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from apps.{{ app_name }}.api.v1.views.{{ entity_name }} import {{ camel_case_entity_name }}ViewSet | ||
|
||
__all__ = [ | ||
"{{ camel_case_entity_name }}ViewSet", | ||
] |
5 changes: 5 additions & 0 deletions
5
{{cookiecutter.name}}/src/.django-app-template/api/v1/views/entity_name.py-tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from core.api.viewsets import DefaultModelViewSet | ||
|
||
|
||
class {{ camel_case_entity_name }}ViewSet(DefaultModelViewSet): | ||
pass |
3 changes: 0 additions & 3 deletions
3
{{cookiecutter.name}}/src/.django-app-template/api/views/__init__.py
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
{{cookiecutter.name}}/src/.django-app-template/api/views/app_name.py-tpl
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
{{cookiecutter.name}}/src/.django-app-template/migrations/0001_entity_name.py-tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 4.2.7 on 2023-11-30 19:50 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
initial = True | ||
|
||
dependencies = [] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="{{ camel_case_entity_name }}", | ||
fields=[ | ||
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
("created", models.DateTimeField(auto_now_add=True, db_index=True)), | ||
("modified", models.DateTimeField(blank=True, db_index=True, null=True)), | ||
], | ||
options={ | ||
"abstract": False, | ||
}, | ||
), | ||
] |
3 changes: 0 additions & 3 deletions
3
{{cookiecutter.name}}/src/.django-app-template/models/__init__.py
This file was deleted.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
{{cookiecutter.name}}/src/.django-app-template/models/__init__.py-tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from apps.{{ app_name }}.models.{{ entity_name }} import {{ camel_case_entity_name }} | ||
|
||
__all__ = [ | ||
"{{ camel_case_entity_name }}", | ||
] |
5 changes: 0 additions & 5 deletions
5
{{cookiecutter.name}}/src/.django-app-template/models/app_name.py-tpl
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
{{cookiecutter.name}}/src/.django-app-template/models/entity_name.py-tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from django.db import models # noqa: F401 | ||
|
||
from core.models import TimestampedModel | ||
|
||
|
||
class {{ camel_case_entity_name }}(TimestampedModel): | ||
pass |
Empty file.
Empty file.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ | |
if envpath.exists(): | ||
env.read_env(envpath) | ||
|
||
|
||
__all__ = [ | ||
"env", | ||
] |
71 changes: 62 additions & 9 deletions
71
{{cookiecutter.name}}/src/core/management/commands/startapp.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,74 @@ | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING | ||
|
||
from django.conf import settings | ||
from django.core.management.commands.startapp import Command as BaseCommand | ||
|
||
if TYPE_CHECKING: | ||
from argparse import ArgumentParser | ||
from typing import Any | ||
|
||
|
||
class Command(BaseCommand): | ||
"""Set custom template for all newly generated apps""" | ||
def add_arguments(self, parser: "ArgumentParser") -> None: | ||
super().add_arguments(parser) | ||
|
||
parser.add_argument( | ||
"--entity_name", | ||
help="The name of some application entity given in `snake_case`; e. g., if the application name is `users`, then the entity is `user`, and so on.", | ||
) | ||
|
||
def handle(self, **options: "Any") -> None: # type: ignore[override] | ||
app_name = options["name"] | ||
|
||
directory = settings.BASE_DIR.parent / "apps" / app_name # type: ignore[misc] | ||
directory.mkdir() | ||
directory = str(directory) | ||
|
||
def handle(self, **options): | ||
if "template" not in options or options["template"] is None: | ||
options["template"] = str(Path(settings.BASE_DIR).parent / ".django-app-template") | ||
template = str(settings.BASE_DIR.parent / ".django-app-template") # type: ignore[misc] | ||
|
||
options.update( | ||
camel_case_entity_name=self.make_entity_name_camelized(options["entity_name"]), | ||
directory=directory, | ||
template=template, | ||
) | ||
|
||
super().handle(**options) | ||
|
||
testsdir = Path(settings.BASE_DIR).parent.parent / "tests" / "apps" / options["name"] | ||
testsdir.mkdir(parents=True, exist_ok=True) | ||
self.rename_entity_name_modules(app_name=app_name, entity_name=options["entity_name"]) | ||
self.add_app_to_installed_apps(app_name=app_name) | ||
self.move_created_tests_directory(app_name=app_name) | ||
|
||
def make_entity_name_camelized(self, entity_name: str) -> str: | ||
return entity_name.replace("_", " ").title().replace(" ", "") | ||
|
||
def rename_entity_name_modules(self, app_name: str, entity_name: str) -> None: | ||
app_dir = settings.BASE_DIR.parent / "apps" / app_name # type: ignore[misc] | ||
|
||
for path in app_dir.rglob("*.py"): | ||
if "entity_name" in path.name: | ||
new_name = path.name.replace("entity_name", entity_name) | ||
path.rename(path.with_name(new_name)) | ||
|
||
def add_app_to_installed_apps(self, app_name: str) -> None: | ||
installed_apps_path = settings.BASE_DIR / "conf" / "installed_apps.py" # type: ignore[misc] | ||
|
||
with installed_apps_path.open() as reader: | ||
lines = reader.readlines() | ||
|
||
for lineno, line in enumerate(lines): | ||
if line.strip().startswith("APPS ="): | ||
lines.insert(lineno + 1, f' "apps.{app_name}",\n') | ||
break | ||
|
||
with installed_apps_path.open("w") as writer: | ||
writer.writelines(lines) | ||
|
||
def move_created_tests_directory(self, app_name: str) -> None: | ||
root = settings.BASE_DIR.parent.parent # type: ignore[misc] | ||
|
||
src = root / "src" / "apps" / app_name / "tests" | ||
dst = root / "tests" / "apps" / app_name | ||
|
||
(testsdir / "__init__.py").touch() | ||
(testsdir / "factory.py").touch() | ||
(testsdir / "fixtures.py").touch() | ||
dst.mkdir() | ||
src.rename(dst) |