twidi / django-decorator-include Goto Github PK
View Code? Open in Web Editor NEWThis project forked from jeffkistler/django-decorator-include
Include Django URL patterns with decorators.
License: BSD 2-Clause "Simplified" License
This project forked from jeffkistler/django-decorator-include
Include Django URL patterns with decorators.
License: BSD 2-Clause "Simplified" License
The 1.4 release is the last release for Django < 2.0, this means that projects still using Django 1.11 can't receive bugfixes because DDI version 2.0 breaks compatibility.
If you want, I can maintain the 1.4 branch while you handle the Pypi releases, otherwise the project will fork again as it did when you forked @jeffkistler project to modernize it.
Can the maintainers verify that this package is indeed supported for django 3.2?
I'm using it and seems to work fine, but would be good to make it official?
I looked at updating travis.yml
and sending a PR but I'm not clear what happens with the excludes.
required permissions decorator takes in arguments. maybe pass them in as a tuple and use something like this?
for decorator in reversed(self.decorators):
if isinstance(decorator, tuple):
callback = decorator[0](decorator[1])(callback)
else:
callback = decorator(callback)
class DecoratedPatterns(object):
"""
A wrapper for an urlconf that applies a decorator to all its views.
"""
def __init__(self, urlconf_name, decorators):
try:
iter(decorators)
except TypeError:
decorators = [decorators]
self.decorators = decorators
if isinstance(urlconf_name, str):
self.urlconf_module = import_module(urlconf_name)
else:
self.urlconf_module = urlconf_name
def decorate_pattern(self, pattern):
if isinstance(pattern, RegexURLResolver):
regex = pattern.regex.pattern
urlconf_module = pattern.urlconf_name
default_kwargs = pattern.default_kwargs
namespace = pattern.namespace
app_name = pattern.app_name
urlconf = DecoratedPatterns(urlconf_module, self.decorators)
decorated = RegexURLResolver(
regex, urlconf, default_kwargs,
app_name, namespace
)
else:
callback = pattern.callback
for decorator in reversed(self.decorators):
callback = decorator(callback)
decorated = RegexURLPattern(
pattern.regex.pattern,
callback,
pattern.default_args,
pattern.name
)
return decorated
def _get_urlpatterns(self):
try:
patterns = self.urlconf_module.urlpatterns
except AttributeError:
patterns = self.urlconf_module
return [self.decorate_pattern(pattern) for pattern in patterns]
urlpatterns = property(_get_urlpatterns)
def __getattr__(self, name):
return getattr(self.urlconf_module, name)
def decorator_include(decorators, arg, namespace=None, app_name=None):
"""
Works like ``django.conf.urls.include`` but takes a view decorator
or an iterable of view decorators as the first argument and applies them,
in reverse order, to all views in the included urlconf.
"""
if isinstance(arg, tuple):
if namespace:
raise ImproperlyConfigured(
'Cannot override the namespace for a dynamic module that provides a namespace'
)
urlconf, app_name, namespace = arg
else:
urlconf = arg
decorated_urlconf = DecoratedPatterns(urlconf, decorators)
return (decorated_urlconf, app_name, namespace)
and in url
url(r'^client_config/',
alchemy_util.decorator_include(
vi_ajax_login_required,
'client_config.urls.urls')),
Gives me following error
Improperly Configured at /client_config/
The included urlconf does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
I installed the developer branch from @StevenMapes, but still it is not working on Django4.2 for me, while it was working fine before.
Example code:
path('', decorator_include(login_required, index_url)),
Error stacktrace:
File "/python3.9/site-packages/decorator_include.py", line 67, in urlpatterns
return [self.decorate_pattern(pattern) for pattern in patterns]
TypeError: 'module' object is not iterable
I use django 2.0.7
in my project with django-decorator-include
app (version 2.0).
My urls.py:
from django.conf import settings
from django.contrib import admin
from django.urls import path, re_path, include
from django.conf.urls.static import static
from django.conf.urls.i18n import i18n_patterns
from django.contrib.auth.decorators import login_required
from decorator_include import decorator_include
urlpatterns = i18n_patterns(
path('', include('web.urls', namespace='web')),
path('backoffice/', decorator_include([login_required,], 'backoffice.urls', namespace='backoffice')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
My backoffice.urls
:
from django.urls import path, re_path
from django.conf.urls.i18n import i18n_patterns
from django.utils.translation import gettext_lazy as _
from backoffice import views
app_name = 'backoffice'
urlpatterns = (
path('', views.DashboardView.as_view(), name='dashboard'),
)
backoffice
is added to INSTALLED_APPS.
In my views (in app web
for example) I am able to do reverse URL, for example in my login view I do return redirect('backoffice:dashboard')
and it works just perfectly - its redirecting to /backoffice/.
Problem is when I try to do reverse urls in my templates. In one of my templates, when I add code:
<li class="featured"><a href="{% url 'backoffice:dashboard' %}">{% trans 'Open dashboard' %}</a></li>
I get django error:
NoReverseMatch at /
'backoffice' is not a registered namespace
I suppose that problem is related to django-decorator-include, because if I change my include to standard django url include:
path('backoffice/', include('backoffice.urls', namespace='backoffice')),
it works just fine, and I am able to get reverse urls in templates again.
Has anyone observed similar behaviour?
This project officially only supports Django up to 3.1 which means the only current version of Django it supports it 2.2 LTS. I made a PR a few months back with support for 3.2 as I've been using this project with several projects for months and I've just updated that PR with updated tests for Django 4.0 and 4.1. From my some what limited actual functional testing in projects on both of those versions I've also found it to continue to work we well
A DeprecationWarning is now raised during tests due to pkg_resources being deprecated within setuptools, see Setuptools for more
@twidi This project definitely seems to have a lot of use and interest in the community (over 30k downloads/month! ), however it doesn't seem to have a lot of love ๐
Are you interested in transferring ownership? Or at least just merging #92 to confirm to the community that this package is still relevant?!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.