Skip to content

Commit

Permalink
Merge pull request #162 from vipulnarang95/feat/django-42-compatible
Browse files Browse the repository at this point in the history
Feat/django 42 compatible
  • Loading branch information
vipulnarang95 authored Mar 22, 2024
2 parents 7ec9e30 + 0e04d85 commit ecca22d
Show file tree
Hide file tree
Showing 18 changed files with 176 additions and 72 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [ 3.7, 3.8, 3.9 ]
python-version: [ 3.8, 3.9, '3.10' ]
requirements-file: [
dj22_cms40.txt,
dj42_cms40.txt,
dj32_cms40.txt,
]

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Changelog

Unreleased
==========
* Python 3.10 support added
* Python 3.7 support removed
* Django 4.2 support added
* Django 2.2 support removed

1.8.3 (2024-03-06)
==================
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
====================
djangocms-navigation
djangocms-navigation
====================

Installation
Expand Down
79 changes: 43 additions & 36 deletions djangocms_navigation/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string
from django.template.response import TemplateResponse
from django.urls import re_path, reverse, reverse_lazy
from django.urls import path, re_path, reverse, reverse_lazy
from django.utils.html import format_html, format_html_join
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
Expand All @@ -26,6 +26,7 @@
from djangocms_versioning.models import Version
from treebeard.admin import TreeAdmin

from .compat import TREEBEARD_4_5
from .conf import TREE_MAX_RESULT_PER_PAGE_COUNT
from .filters import LanguageFilter
from .forms import MenuContentForm, MenuItemForm
Expand Down Expand Up @@ -66,6 +67,7 @@ def url_for_result(self, result):
)


@admin.register(MenuContent)
class MenuContentAdmin(ExtendedVersionAdminMixin, admin.ModelAdmin):
form = MenuContentForm
menu_model = Menu
Expand Down Expand Up @@ -130,14 +132,15 @@ def get_list_display(self, request):

return menu_content_list_display

@admin.display(
description=_("Lock State")
)
def is_locked(self, obj):
version = self.get_version(obj)
if version.state == DRAFT and version_is_locked(version):
return render_to_string("djangocms_version_locking/admin/locked_icon.html")
return ""

is_locked.short_description = _("Lock State")

def _get_references_link(self, obj, request):
menu_content_type = ContentType.objects.get(
app_label=self.model._meta.app_label, model=Menu._meta.model_name,
Expand All @@ -153,6 +156,10 @@ def _get_references_link(self, obj, request):
{"url": url}
)

@admin.display(
description="Main Navigation",
boolean=True,
)
def get_main_navigation(self, obj):
"""
Return main_navigation field from Menu associated with MenuContent.
Expand All @@ -161,9 +168,6 @@ def get_main_navigation(self, obj):
"""
return obj.menu.main_navigation

get_main_navigation.short_description = "Main Navigation"
get_main_navigation.boolean = True

def _get_main_navigation_link(self, obj, request, disabled=False):
"""
Return an admin link to the confirmation page for setting main confirmations
Expand Down Expand Up @@ -237,6 +241,9 @@ def change_view(self, request, object_id, form_url="", extra_context=None):
)
)

@admin.display(
description=_("Menu Items")
)
def get_menuitem_link(self, obj):
object_menuitem_url = reverse(
"admin:{app}_{model}_list".format(
Expand All @@ -252,9 +259,9 @@ def get_menuitem_link(self, obj):
object_menuitem_url,
_("Items"),
)
get_menuitem_link.short_description = _("Menu Items")


@admin.register(MenuItem)
class MenuItemAdmin(TreeAdmin):
form = MenuItemForm
menu_content_model = MenuContent
Expand All @@ -277,56 +284,56 @@ class Media:
def get_urls(self):
info = self.model._meta.app_label, self.model._meta.model_name
return [
re_path(
r"^$",
path(
"",
self.admin_site.admin_view(self.changelist_view),
name="{}_{}_changelist".format(*info),
),
re_path(
r"^(?P<menu_content_id>\d+)/$",
path(
"<int:menu_content_id>/",
self.admin_site.admin_view(self.changelist_view),
name="{}_{}_list".format(*info),
),
re_path(
r"^(?P<menu_content_id>\d+)/preview/$",
path(
"<int:menu_content_id>/preview/",
self.admin_site.admin_view(self.preview_view),
name="{}_{}_preview".format(*info),
),
re_path(
r"^(?P<menu_content_id>\d+)/add/$",
path(
"<int:menu_content_id>/add/",
self.admin_site.admin_view(self.add_view),
name="{}_{}_add".format(*info),
),
re_path(
r"^(?P<menu_content_id>\d+)/(?P<object_id>\d+)/change/$",
path(
"<int:menu_content_id>/<int:object_id>/change/",
self.admin_site.admin_view(self.change_view),
name="{}_{}_change".format(*info),
),
re_path(
r"^(?P<menu_content_id>\d+)/(?P<object_id>\d+)/delete/$",
path(
"<int:menu_content_id>/<int:object_id>/delete/",
self.admin_site.admin_view(self.delete_view),
name="{}_{}_delete".format(*info),
),
re_path(
r"^(?P<menu_content_id>\d+)/move/$",
path(
"<int:menu_content_id>/move/",
self.admin_site.admin_view(self.move_node),
name="{}_{}_move_node".format(*info),
),
re_path(
r"^(?P<menu_content_id>\d+)/jsi18n/$",
path(
"<int:menu_content_id>/jsi18n/",
JavaScriptCatalog.as_view(packages=["treebeard"]),
),
re_path(
r"^select2/$",
path(
"select2/",
self.admin_site.admin_view(ContentObjectSelect2View.as_view(
menu_content_model=self.menu_content_model,
)),
name="{}_select2_content_object".format(
self.model._meta.app_label
)
),
re_path(
r"^(?P<menu_content_id>\d+)/messages/$",
path(
"<int:menu_content_id>/messages/",
self.admin_site.admin_view(MessageStorageView.as_view()),
name="{}_{}_message_storage".format(*info),
),
Expand Down Expand Up @@ -442,7 +449,7 @@ def change_view(self, request, object_id, menu_content_id=None, form_url="", ext
kwargs={"menu_content_id": menu_content_id, "object_id": object_id},
)
return super().change_view(
request, object_id, form_url="", extra_context=extra_context
request, str(object_id), form_url="", extra_context=extra_context
)

def add_view(self, request, menu_content_id=None, form_url="", extra_context=None):
Expand Down Expand Up @@ -492,7 +499,10 @@ def get_changelist_template(self, request):
"""
if is_preview_url(request=request):
return "admin/djangocms_navigation/menuitem/preview.html"
return "admin/djangocms_navigation/menuitem/change_list.html"
elif TREEBEARD_4_5:
return "admin/djangocms_navigation/menuitem/change_list.html"
else:
return "admin/djangocms_navigation/menuitem/tree_change_list.html"

def changelist_view(self, request, menu_content_id=None, extra_context=None):
self.change_list_template = self.get_changelist_template(request=request)
Expand Down Expand Up @@ -632,7 +642,7 @@ def delete_view(self, request, object_id, menu_content_id=None, form_url="", ext
to_be_deleted = [f"Menu item: {menu_item}"]
extra_context["deleted_objects"] = self._get_to_be_deleted(menu_item.get_children(), to_be_deleted)

return super().delete_view(request, object_id, extra_context)
return super().delete_view(request, str(object_id), extra_context)

def response_delete(self, request, obj_display, obj_id):
"""
Expand Down Expand Up @@ -784,21 +794,18 @@ def __new__(cls, *args, **kwargs):

return Form

@admin.display(
description=_("URL")
)
def get_object_url(self, obj):
if obj.content:
obj_url = obj.content.get_absolute_url()
return format_html("<a href='{0}'>{0}</a>", obj_url)

get_object_url.short_description = _("URL")

@property
def _versioning_enabled(self):
"""Helper property to check if versioning is enabled for navigation"""

return apps.get_app_config(
self.model._meta.app_label
).cms_config.djangocms_versioning_enabled


admin.site.register(MenuItem, MenuItemAdmin)
admin.site.register(MenuContent, MenuContentAdmin)
5 changes: 5 additions & 0 deletions djangocms_navigation/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from packaging.version import Version
from treebeard import __version__ as treebeard_version


TREEBEARD_4_5 = Version(treebeard_version) < Version('4.6')
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ Original code found in treebeard-admin.js

const EXPANDED_SESSION_KEY = 'expanded-';

// Add jQuery util for disabling selection
// Originally taken from jquery-ui (where it is deprecated)
// https://api.jqueryui.com/disableSelection/
if($.fn.disableSelection == undefined) {
$.fn.extend( {
disableSelection: ( function() {
var eventType = "onselectstart" in document.createElement( "div" ) ? "selectstart" : "mousedown";
return function() {
return this.on( eventType + ".ui-disableSelection", function( event ) {
event.preventDefault();
} );
};
} )(),

enableSelection: function() {
return this.off( ".ui-disableSelection" );
}
} );
}

// This is the basic Node class, which handles UI tree operations for each 'row'
var Node = function (elem) {
var $elem = $(elem);
Expand Down Expand Up @@ -146,7 +166,7 @@ Original code found in treebeard-admin.js
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
var cookie = $.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{# copy from djangocms-treebeard tree_change_list.html to override media files #}
{% extends "admin/change_list.html" %}
{% load static admin_list admin_tree admin_urls navigation_admin_tree i18n djangocms_versioning %}

{% block extrastyle %}
{{ block.super }}
<link rel="stylesheet" type="text/css" href="{% static 'treebeard/treebeard-admin.css' %}" />
<link rel="stylesheet" type="text/css" href="{% static 'djangocms_navigation/css/navigation_admin_overrides.css' %}"/>
{% endblock %}

{% block extrahead %}
{{ block.super }}
<script src="{% url 'admin:jsi18n' %}"></script>
<script data-csrftoken="{{ csrf_token }}" src="{% static 'djangocms_navigation/js/navigation-tree-admin.js' %}"></script>
{% endblock %}

{% block result_list %}
{% if action_form and actions_on_top and cl.full_result_count %}
{% admin_actions %}
{% endif %}
{% result_tree cl request %}
{% if action_form and actions_on_bottom and cl.full_result_count %}
{% admin_actions %}
{% endif %}
{% endblock %}

{% block object-tools-items %}
<li>
<a href="{% url opts|admin_urlname:'add' menu_content.pk %}" class="addlink">
{% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}
</a>
</li>
<li>
<a href="{% url opts|admin_urlname:'preview' menu_content.pk %}" class="cms-btn-group">
{% trans "Preview" %}
</a>
</li>
{% if versioning_enabled_for_nav %}
<li>
<a href="{{ menu_content|url_version_list|safe }}">
{% trans "Versions" %}
</a>
</li>
{% endif %}
{% endblock %}
5 changes: 3 additions & 2 deletions djangocms_navigation/templatetags/navigation_admin_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
result_hidden_fields,
)
from django.templatetags.static import static
from django.urls import reverse
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -104,7 +105,7 @@ def treebeard_js():
"""

js_file = static('djangocms_navigation/js/navigation-tree-admin.js')

jsi18n_url = reverse('admin:jsi18n')
jquery_ui = static('treebeard/jquery-ui-1.8.5.custom.min.js')

# Jquery UI is needed to call disableSelection() on drag and drop so
Expand All @@ -118,7 +119,7 @@ def treebeard_js():
'</script>'
'<script type="text/javascript" src="{}"></script>')
return format_html(
TEMPLATE, "jsi18n", mark_safe(js_file), mark_safe(jquery_ui))
TEMPLATE, jsi18n_url, mark_safe(js_file), mark_safe(jquery_ui))


admin_tree.treebeard_js = treebeard_js
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


INSTALL_REQUIREMENTS = [
"Django>=1.11,<3.3",
"django-treebeard>=4.3,<4.6.0",
"Django>=3.2,<5.0",
"django-treebeard>=4.3",
"django-cms",
]

Expand Down
5 changes: 0 additions & 5 deletions tests/requirements/dj22_cms40.txt

This file was deleted.

3 changes: 1 addition & 2 deletions tests/requirements/dj32_cms40.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
-r ./requirements_base.txt

Django>=3.2,<4.0
django-classy-tags
django-sekizai
django-treebeard<=4.5.1
4 changes: 4 additions & 0 deletions tests/requirements/dj42_cms40.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-r ./requirements_base.txt

Django>=4.2,<5.0
django-treebeard>=4.6.0
Loading

0 comments on commit ecca22d

Please sign in to comment.