Skip to content

Django 4.0 compatibility #55

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

Open
wants to merge 4 commits into
base: master
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
2 changes: 0 additions & 2 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[run]
include =
django_activeurl/*
omit =

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ __pycache__
*~
.cache
htmlcov
.idea
2 changes: 0 additions & 2 deletions django_activeurl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from .__about__ import __version__ # noqa
4 changes: 1 addition & 3 deletions django_activeurl/conf.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from appconf import AppConf
from django.conf import settings # noqa


class ActiveUrlConf(AppConf):
'''default settings for django-activeurl'''
"""default settings for django-activeurl"""

# flipper for caching feature
CACHE = True
Expand Down
1 change: 0 additions & 1 deletion django_activeurl/ext/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# -*- coding: utf-8 -*-
'''jinja port'''
from __future__ import absolute_import, unicode_literals
2 changes: 0 additions & 2 deletions django_activeurl/ext/django_jinja.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
'''jinja extenions for django_jinja/django 1.8+'''
from __future__ import absolute_import, unicode_literals

from jinja2 import lexer, nodes
from jinja2.ext import Extension

Expand Down
3 changes: 1 addition & 2 deletions django_activeurl/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# -*- coding: utf-8 -*-
'''hello django test runner''' # pragma: no cover
from __future__ import absolute_import, unicode_literals # pragma: no cover
"""hello django test runner""" # pragma: no cover
1 change: 0 additions & 1 deletion django_activeurl/templatetags/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# -*- coding: utf-8 -*-
'''django template tags'''
from __future__ import absolute_import, unicode_literals
17 changes: 5 additions & 12 deletions django_activeurl/templatetags/activeurl.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
# -*- coding: utf-8 -*-
'''activeurl django template library'''
from __future__ import absolute_import, unicode_literals

"""activeurl django template library"""
from classytags.arguments import MultiKeywordArgument
from classytags.core import Options, Tag
from django import template

from ..utils import Configuration, render_content
from django_activeurl.utils import Configuration, render_content

# django template library
register = template.Library()


@register.tag(name='activeurl')
class ActiveUrl(Tag, Configuration):
'''django template tag via django-classy-tags'''
# tag name
name = 'activeurl'
"""django template tag via django-classy-tags"""

# template tag arguments
options = Options(
Expand All @@ -25,7 +22,7 @@ class ActiveUrl(Tag, Configuration):
)

def render_tag(self, context, kwargs, nodelist):
'''render content with "active" urls logic'''
"""render content with "active" urls logic"""
# load configuration from passed options
self.load_configuration(**kwargs)

Expand All @@ -51,7 +48,3 @@ def render_tag(self, context, kwargs, nodelist):
)

return content


# register new template tag
register.tag(ActiveUrl)
41 changes: 18 additions & 23 deletions django_activeurl/utils.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
# -*- coding: utf-8 -*-
'''template engine independent utils'''
from __future__ import absolute_import, unicode_literals

"""template engine independent utils"""
from hashlib import md5
from urllib.parse import quote

from django.core.cache import cache
from django.utils.http import urlquote
from django.utils.translation import get_language
from lxml.etree import ParserError
from lxml.html import fragment_fromstring, tostring

from .__about__ import __version__
from .conf import settings

try:
from django.utils.six.moves.urllib import parse as urlparse
except ImportError:
from urllib import parse as urlparse
from urllib import parse as urlparse


class ImproperlyConfigured(Exception):
'''django-like ImproperlyConfigured exception'''
"""django-like ImproperlyConfigured exception"""
pass


class Configuration(object):
'''abstract configuration'''
"""abstract configuration"""

def load_configuration(self, **kwargs):
'''load configuration, merge with default settings'''
"""load configuration, merge with default settings"""
# update passed arguments with default values
for key in settings.ACTIVE_URL_KWARGS:
kwargs.setdefault(key, settings.ACTIVE_URL_KWARGS[key])
Expand All @@ -44,7 +39,7 @@ def load_configuration(self, **kwargs):


def get_cache_key(content, **kwargs):
'''generate cache key'''
"""generate cache key"""
cache_key = ''
for key in sorted(kwargs.keys()):
cache_key = '{cache_key}.{key}:{value}'.format(
Expand Down Expand Up @@ -104,7 +99,7 @@ def yesno_to_bool(value, varname):


def check_active(url, element, **kwargs):
'''check "active" url, apply css_class'''
"""check "active" url, apply css_class"""
menu = yesno_to_bool(kwargs['menu'], 'menu')
ignore_params = yesno_to_bool(kwargs['ignore_params'], 'ignore_params')

Expand Down Expand Up @@ -155,9 +150,9 @@ def check_active(url, element, **kwargs):
kwargs['full_path'].startswith(href)
or
# maybe an urlquoted href was supplied
urlquote(kwargs['full_path']).startswith(href)
quote(kwargs['full_path']).startswith(href)
or
kwargs['full_path'].startswith(urlquote(href))
kwargs['full_path'].startswith(quote(href))
)
else:
logic = False
Expand All @@ -167,9 +162,9 @@ def check_active(url, element, **kwargs):
kwargs['full_path'] == href
or
# maybe an urlquoted href was supplied
urlquote(kwargs['full_path']) == href
quote(kwargs['full_path']) == href
or
kwargs['full_path'] == urlquote(href)
kwargs['full_path'] == quote(href)
)
# "active" url found
if logic:
Expand All @@ -190,7 +185,7 @@ def check_active(url, element, **kwargs):


def check_content(content, **kwargs):
'''check content for "active" urls'''
"""check content for "active" urls"""
# valid html root tag
try:
# render elements tree from content
Expand All @@ -202,9 +197,9 @@ def check_content(content, **kwargs):
if not kwargs['parent_tag']:
kwargs['parent_tag'] = 'self'
else:
raise ImproperlyConfigured('''
raise ImproperlyConfigured("""
parent_tag=True is not allowed
''')
""")
elif kwargs['parent_tag'] is None:
kwargs['parent_tag'] = 'self'
# if parent_tag is False\None\''\a\self
Expand Down Expand Up @@ -241,7 +236,7 @@ def check_content(content, **kwargs):
# not valid html root tag
except ParserError:
# raise an exception with configuration example
raise ImproperlyConfigured('''
raise ImproperlyConfigured("""
content of {% activeurl %} must have valid html root tag
for example
{% activeurl %}
Expand All @@ -255,12 +250,12 @@ def check_content(content, **kwargs):
</ul>
{% endactiveurl %}
in this case <ul> is valid content root tag
''')
""")
return content


def render_content(content, **kwargs):
'''check content for "active" urls, store results to django cache'''
"""check content for "active" urls, store results to django cache"""
# try to take pre rendered content from django cache, if caching is enabled
if settings.ACTIVE_URL_CACHE:
cache_key = get_cache_key(content, **kwargs)
Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[pytest]
addopts= -s --strict --keep-duplicates --cache-clear --verbose --maxfail=1 --no-cov-on-fail --cov=django_activeurl --cov-report=term --cov-report=html
addopts= -s --strict-markers --keep-duplicates --cache-clear --verbose --maxfail=1 --no-cov-on-fail --cov=django_activeurl --cov-report=term --cov-report=html
10 changes: 3 additions & 7 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import io

from setuptools import setup
Expand All @@ -19,12 +17,10 @@
Development Status :: 5 - Production/Stable
Natural Language :: English
Programming Language :: Python
Programming Language :: Python :: 2
Programming Language :: Python :: 2.7
Programming Language :: Python :: 3
Programming Language :: Python :: 3.4
Programming Language :: Python :: 3.5
Programming Language :: Python :: 3.6
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
Programming Language :: Python :: Implementation :: CPython
Programming Language :: Python :: Implementation :: PyPy
Operating System :: MacOS :: MacOS X
Expand Down
2 changes: 0 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

import os

import django
Expand Down
10 changes: 1 addition & 9 deletions tests/jinja_config.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from jinja2 import Environment

from django_activeurl.ext.django_jinja import ActiveUrl

try:
# django < 2
from django.core.urlresolvers import reverse
except ImportError:
# django > 2
from django.urls import reverse
from django.urls import reverse


def environment(**options):
Expand Down
23 changes: 10 additions & 13 deletions tests/test_activeurl.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from hashlib import md5

from django.core.cache import cache
from django.template import Context, Template
from django.test.client import Client, RequestFactory
from django.utils.translation import get_language
from django.template.engine import Engine
from lxml.html import fragment_fromstring, fromstring

from django_activeurl import __version__
from django_activeurl.conf import settings
from django_activeurl.utils import ImproperlyConfigured

try:
# django >= 1.7
from django.template.base import add_to_builtins
except ImportError:
# django >= 1.9
def add_to_builtins(module):
from django.template.engine import Engine
template_engine = Engine.get_default()
template_engine.builtins.append(module)
template_engine.template_builtins = \
template_engine.get_template_builtins(template_engine.builtins)

def add_to_builtins(module):
template_engine = Engine.get_default()
template_engine.builtins.append(module)
template_engine.template_builtins = template_engine.get_template_builtins(
template_engine.builtins
)


add_to_builtins('django_activeurl.templatetags.activeurl')


requests = RequestFactory()
client = Client()

Expand Down
22 changes: 10 additions & 12 deletions tests/urls.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from django.conf.urls import url
from django.http import HttpResponse
from django.shortcuts import render
from django.urls import path


def view(request):
return HttpResponse()


def djnago_template_view(request):
def django_template_view(request):
return render(request, 'django.html', {})


Expand All @@ -19,12 +17,12 @@ def jinja_template_view(request):


urlpatterns = [
url(r'^$', view),
url(r'^template/django/$', djnago_template_view),
url(r'^template/jinja/$', jinja_template_view),
url(r'^page/$', view),
url(r'^menu/$', view),
url(r'^menu/submenu/$', view),
url(r'^страница/$', view),
url(r'^другая_страница/$', view, name='non-ascii-reverse'),
path('', view),
path('template/django/', django_template_view),
path('template/jinja/', jinja_template_view),
path('page/', view),
path('menu/', view),
path('menu/submenu/', view),
path('страница/', view),
path('другая_страница/', view, name='non-ascii-reverse'),
]
2 changes: 0 additions & 2 deletions tests/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from django.http import HttpResponse
from django.shortcuts import render

Expand Down
Loading