Skip to content

Commit

Permalink
Compatibility with django 3 and 4
Browse files Browse the repository at this point in the history
* removed support for django < 1.11
* fix compatibility and deprecation warnings on django > 3 and 4
* updated tox.ini
  • Loading branch information
izimobil committed Jan 12, 2021
1 parent 728dbeb commit ebd04e1
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 43 deletions.
5 changes: 4 additions & 1 deletion admin_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* a customizable menu bar,
* tools to make admin theming easier.
"""
import django

VERSION = '0.9.0'
default_app_config = 'admin_tools.apps.AdminToolsConfig'

if django.VERSION < (3, 2):
default_app_config = 'admin_tools.apps.AdminToolsConfig'
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
{% block module_content %}
<div class="group group-{{ module.display }}">
{% spaceless %}
{% ifequal module.display "tabs" %}
{% if module.display == "tabs" %}
<ul>
{% for sub_module in module.children %}
{% if not sub_module.is_empty %}<li class="group-tabs-link"><a href="#module_{{ sub_module.id }}">{{ sub_module.title|capfirst }}</a></li>{% endif %}
{% endfor %}
</ul>
{% endifequal %}
{% ifequal module.display "accordion" %}
{% endif %}
{% if module.display == "accordion" %}
{% for sub_module in module.children %}
{% if not sub_module.is_empty %}<span class="group-accordion-header"><a href="#">{{ sub_module.title|capfirst }}</a></span>{% endif %}
{% admin_tools_render_dashboard_module sub_module %}
Expand All @@ -19,7 +19,7 @@
{% for sub_module in module.children %}
{% admin_tools_render_dashboard_module sub_module %}
{% endfor %}
{% endifequal %}
{% endif %}
{% endspaceless %}
</div>
{% endblock %}
8 changes: 7 additions & 1 deletion admin_tools/dashboard/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from django.conf.urls import url
import django

if django.VERSION < (2, 0):
from django.conf.urls import url
else:
from django.urls import re_path as url

from admin_tools.dashboard import views

urlpatterns = [
Expand Down
5 changes: 3 additions & 2 deletions admin_tools/dashboard/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from .forms import DashboardPreferencesForm
from .models import DashboardPreferences
from admin_tools.utils import is_xhr


@staff_member_required
Expand All @@ -33,10 +34,10 @@ def set_preferences(request, dashboard_id):
)
if form.is_valid():
preferences = form.save()
if request.is_ajax():
if is_xhr(request):
return HttpResponse("true")
messages.success(request, "Preferences saved")
elif request.is_ajax():
elif is_xhr(request):
return HttpResponse("false")
else:
form = DashboardPreferencesForm(
Expand Down
2 changes: 1 addition & 1 deletion admin_tools/menu/templates/admin_tools/menu/item.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% load admin_tools_menu_tags %}
{% spaceless %}
{% if not item.is_empty %}
<li class="menu-item{% ifequal index 1 %} first{% endifequal %}{% if not item.enabled %} disabled{% endif %}{% if selected %} selected{% endif %}{% if item.css_classes %} {{ item.css_classes|join:' ' }}{% endif %}">
<li class="menu-item{% if index == 1 %} first{% endif %}{% if not item.enabled %} disabled{% endif %}{% if selected %} selected{% endif %}{% if item.css_classes %} {{ item.css_classes|join:' ' }}{% endif %}">
<a href="{% if item.url and item.enabled %}{{ item.url }}{% else %}#{% endif %}"{% if item.description %} title="{{ item.description }}"{% endif %}{% if item.accesskey %} accesskey="{{ item.accesskey }}"{% endif %}{% if item.children and item.enabled %} class="has-icon"{% endif %}>{{ item.title|capfirst }}</a>
{% if item.children and item.enabled %}
<ul>
Expand Down
7 changes: 6 additions & 1 deletion admin_tools/menu/urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from django.conf.urls import url
import django

if django.VERSION < (2, 0):
from django.conf.urls import url, include
else:
from django.urls import re_path as url, include
from admin_tools.menu import views

urlpatterns = [
Expand Down
7 changes: 4 additions & 3 deletions admin_tools/menu/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from .forms import BookmarkForm
from .models import Bookmark
from admin_tools.utils import is_xhr


@staff_member_required
Expand All @@ -24,7 +25,7 @@ def add_bookmark(request):
form = BookmarkForm(user=request.user, data=request.POST)
if form.is_valid():
bookmark = form.save()
if not request.is_ajax():
if not is_xhr(request):
messages.success(request, "Bookmark added")
if request.POST.get("next"):
return HttpResponseRedirect(request.POST.get("next"))
Expand Down Expand Up @@ -53,7 +54,7 @@ def edit_bookmark(request, id):
)
if form.is_valid():
form.save()
if not request.is_ajax():
if not is_xhr(request):
messages.success(request, "Bookmark updated")
if request.POST.get("next"):
return HttpResponseRedirect(request.POST.get("next"))
Expand All @@ -78,7 +79,7 @@ def remove_bookmark(request, id):
bookmark = get_object_or_404(Bookmark, id=id, user=request.user)
if request.method == "POST":
bookmark.delete()
if not request.is_ajax():
if not is_xhr(request):
messages.success(request, "Bookmark removed")
if request.POST.get("next"):
return HttpResponseRedirect(request.POST.get("next"))
Expand Down
7 changes: 6 additions & 1 deletion admin_tools/urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import django

if django.VERSION < (2, 0):
from django.conf.urls import url, include
else:
from django.urls import re_path as url, include
from django.conf import settings
from django.conf.urls import url, include

urlpatterns = []
if 'admin_tools.menu' in settings.INSTALLED_APPS:
Expand Down
7 changes: 7 additions & 0 deletions admin_tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
from fnmatch import fnmatch

import django
from django.conf import settings
from django.contrib import admin
try:
Expand All @@ -17,6 +18,12 @@
import warnings


def is_xhr(request):
if django.VERSION < (2, 2):
return request.is_ajax()
return request.headers.get("x-requested-with") == "XMLHttpRequest"


def uniquify(value, seen_values):
""" Adds value to seen_values set and ensures it is unique """
id = 1
Expand Down
22 changes: 1 addition & 21 deletions test_proj/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,8 @@
path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, path)

import django


def manage_16ormore():
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.management import execute_from_command_line

execute_from_command_line(sys.argv)


def manage_15orless():
from django.core.management import execute_manager
try:
import settings # Assumed to be in the same directory.
except ImportError:
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
sys.exit(1)

execute_manager(settings)

if __name__ == "__main__":
if django.VERSION > (1, 6):
manage_16ormore()
else:
manage_15orless()
3 changes: 3 additions & 0 deletions test_proj/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,6 @@

ADMIN_TOOLS_INDEX_DASHBOARD = 'test_proj.dashboard.CustomIndexDashboard'
ADMIN_TOOLS_MENU = 'test_proj.menu.CustomMenu'

# Added to avoid warnings with django >= 4
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
9 changes: 6 additions & 3 deletions test_proj/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
try:
import django

if django.VERSION < (2, 0):
from django.conf.urls import url, include
except ImportError: # django < 1.4
from django.conf.urls.defaults import url, include
else:
from django.urls import re_path as url, include

from django.conf import settings
from django.contrib import admin
from django.views.static import serve
Expand Down
11 changes: 6 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
[tox]
envlist =
py27-dj{111},
py35-dj{111,21,22},
py36-dj{111,21,22,30,dev}
py37-dj{111,21,22,30,dev}
py38-dj{111,21,22,30,dev}
py36-dj{21,22,30,31,dev}
py37-dj{21,22,30,31,dev}
py38-dj{21,22,30,31,dev}
py39-dj{21,22,30,31,dev}

[testenv]
basepython =
py27: python2.7
py35: python3.5
py36: python3.6
py37: python3.7
py38: python3.8
py39: python3.9
deps =
dj111: Django>=1.11,<2.0
dj21: Django>=2.1,<2.2
dj22: Django>=2.2,<3.0
dj30: Django>=3.0,<3.1
dj31: Django>=3.1,<3.2
djdev: https://github.com/django/django/archive/master.tar.gz
commands =
python -V
Expand Down

0 comments on commit ebd04e1

Please sign in to comment.