Coder Social home page Coder Social logo

twidi / django-decorator-include Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jeffkistler/django-decorator-include

70.0 70.0 17.0 106 KB

Include Django URL patterns with decorators.

License: BSD 2-Clause "Simplified" License

Python 99.60% Shell 0.40%

django-decorator-include's People

Contributors

bradleyg avatar hirokiky avatar jdufresne avatar jeffkistler avatar justinabrahms avatar twidi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

django-decorator-include's Issues

Publish new release (with Django 2.0 support)

Hi @twidi

Thanks for maintaining django-decorator-include and merging the Django 2.0 support. What are you thoughts on publishing a new release so it can be integrated into projects?

Do you think #51 should be merged before or after? I'm happy with either. Thanks!

Please republish branch release/1.4

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.

support decorators that use parameters

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)

improperly configured error


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.

Upgrade to Django 4.2 breaks with TypeError: 'module' object is not iterable

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

not a registered namespace exception when using django-decorator-include

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?

Add in tests and check for Django 4.0, 4.1 and 4.2 support

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

Dead project?

@twidi This project definitely seems to have a lot of use and interest in the community (over 30k downloads/month! Downloads), 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?!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.