Skip to content

Commit

Permalink
lots of
Browse files Browse the repository at this point in the history
  • Loading branch information
hnthh committed Nov 30, 2023
1 parent 2b8dc43 commit 8d26610
Show file tree
Hide file tree
Showing 27 changed files with 147 additions and 67 deletions.
7 changes: 5 additions & 2 deletions .gitignore
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
7 changes: 1 addition & 6 deletions hooks/post_gen_project.sh
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
2 changes: 1 addition & 1 deletion {{cookiecutter.name}}/Makefile
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:
Expand Down
6 changes: 6 additions & 0 deletions {{cookiecutter.name}}/src/.django-app-template/admin.py-tpl
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

This file was deleted.

This file was deleted.

12 changes: 0 additions & 12 deletions {{cookiecutter.name}}/src/.django-app-template/api/urls.py-tpl

This file was deleted.

Empty file.
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",
]
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 {{cookiecutter.name}}/src/.django-app-template/api/v1/urls.py-tpl
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)),
]
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",
]
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

This file was deleted.

This file was deleted.

2 changes: 1 addition & 1 deletion {{cookiecutter.name}}/src/.django-app-template/apps.py-tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ from core.base_config import AppConfig


class {{ camel_case_app_name }}Config(AppConfig):
name = "{{ app_name }}"
name = "apps.{{ app_name }}"
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,
},
),
]

This file was deleted.

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 }}",
]

This file was deleted.

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.
15 changes: 0 additions & 15 deletions {{cookiecutter.name}}/src/core/admin/README.md

This file was deleted.

1 change: 0 additions & 1 deletion {{cookiecutter.name}}/src/core/conf/environ.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
if envpath.exists():
env.read_env(envpath)


__all__ = [
"env",
]
71 changes: 62 additions & 9 deletions {{cookiecutter.name}}/src/core/management/commands/startapp.py
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)

0 comments on commit 8d26610

Please sign in to comment.