Skip to content

Commit

Permalink
change version
Browse files Browse the repository at this point in the history
  • Loading branch information
adiletto64 committed Sep 10, 2023
1 parent 1e0b29a commit 99dab74
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ Checkout the [demo](./docs/gen_views_example.md)
python manage.py genviews <your-app-name> <model-name>
```
Arguments
- `smart-mode`, `-s` adds login required, sets user for CreateView and checks if client is owner of object in UpdateView
- `model-is-namespace`, `-mn` adds success_url with name model as [namespace](https://docs.djangoproject.com/en/4.2/topics/http/urls/#url-namespaces)
- `--smart-mode`, `-s` adds login required, sets user for CreateView and checks if client is owner of object in UpdateView
- `--model-is-namespace`, `-mn` adds success_url with name model as [namespace](https://docs.djangoproject.com/en/4.2/topics/http/urls/#url-namespaces)
- `--file`, `-f` specify view file (example: "views/posts_view.py" or "new_views.py") in your app

### Generate templates
Generates templates from `template_name`s from views from given app
Expand Down
20 changes: 19 additions & 1 deletion hogwarts/management/commands/genviews.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from typing import Optional

from rich.console import Console
from django.core.management.base import BaseCommand, CommandError
Expand Down Expand Up @@ -30,11 +31,17 @@ def add_arguments(self, parser):
"or try for yourself"
)

parser.add_argument(
"--file", "-f",
help="What views file it should read and write"
)

def handle(self, *args, **options):
app_name: str = options["app"]
model_name: str = options["model"]
smart_mode: bool = options["smart_mode"]
model_is_namespace: bool = options["model_is_namespace"]
views_file: Optional[str] = options["file"]

app_config = get_app_config(app_name)

Expand All @@ -46,7 +53,8 @@ def handle(self, *args, **options):
if model_is_namespace or model_name.lower() in app_name:
namespace_model = True

path = os.path.join(app_config.path, "views.py")
path = os.path.join(app_config.path, views_file or "views.py")
self.create_path_if_not_exists(path)

generator = ViewGenerator(model, smart_mode, namespace_model)

Expand All @@ -65,3 +73,13 @@ def handle(self, *args, **options):
self.stdout.write(
self.style.SUCCESS(f"Generated CRUD views in {path}")
)

def create_path_if_not_exists(self, path):
directory = os.path.dirname(path)

if not os.path.exists(directory):
os.makedirs(directory)
console.print("")

if not os.path.exists(path):
open(path, 'a').close()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-hogwarts"
version = "0.7.1"
version = "0.8.0"
description = "Django utilities for codegen and DX improvement"
authors = ["adiletto64 <adiletdj19@gmail.com>"]
packages = [{ include = "hogwarts" }]
Expand Down

0 comments on commit 99dab74

Please sign in to comment.