Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load a folder of views #24

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ class FooList(ListView):
model = Foo
```

### Parameters

* `paths`: is in a plural form because support also a list of paths
* `name` the classic route name
* `namespace`: {I don't know what to write here}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is required a review here

* `extra_modules`: can be a real path or the python module itselfs (require a list)
* `login_required`: so you can avoid the Django decorator
* `permission_required`: so you can avoid the Django decorator

### entra_modules example for real paths

```
import os
# We need the project directory
root = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
urlpatterns = [
path("", include_view_urls(extra_modules=[root + "/portal/views"])),
]
```

## Development

```console
Expand Down
20 changes: 18 additions & 2 deletions django_view_decorator/urls_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import importlib
import os
from collections.abc import Sequence
from importlib import import_module

from django.urls import include
from django.urls import URLPattern
Expand All @@ -19,6 +20,21 @@ def include_view_urls(
"""
if extra_modules:
for module in extra_modules:
import_module(f"{module}")
if module[0] == "/":
module_files = [
f[:-3]
for f in os.listdir(module)
if f.endswith(".py") and f != "__init__.py"
]

for module_name in module_files:
spec = importlib.util.spec_from_file_location(
module_name,
os.path.join(module, f"{module_name}.py"),
)
_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(_module)
else:
importlib.import_module(f"{module}")

return include("django_view_decorator.urls")
Loading