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 all 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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ 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`: the python module itselfs (require a list)
* `extra_packages`: can be a real path (require a list)
* `login_required`: so you can avoid the Django decorator
* `permission_required`: so you can avoid the Django decorator

### extra_packages 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
23 changes: 21 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 @@ -9,16 +10,34 @@
def include_view_urls(
*,
extra_modules: list[str] | None = None,
extra_packages: list[str] | None = None,
) -> tuple[Sequence[URLResolver | URLPattern], str | None, str | None]:
"""
Include the view urls from the registry discovered by django_view_decorator, and
optionally from the given modules.
:param extra_modules: A list of modules to import before including the view urls.
:param extra_packages: A list of packages to import before including the view urls.
:return: A tuple of (urlpatterns, app_name, namespace) (result from calling
include())
"""
if extra_modules:
for module in extra_modules:
import_module(f"{module}")
importlib.import_module(f"{module}")

if extra_packages:
for package in extra_packages:
if package[0] == "/":
package_files = [
f[:-3]
for f in os.listdir(package)
if f.endswith(".py") and f != "__init__.py"
]

for package_name in package_files:
spec = importlib.util.spec_from_file_location(
package_name,
os.path.join(package, f"{package_name}.py"),
)
spec.loader.exec_module(importlib.util.module_from_spec(spec))

return include("django_view_decorator.urls")