fabiocaccamo / django-treenode

:deciduous_tree: probably the best abstract model/admin for your tree based stuff.
MIT License
667 stars 32 forks source link

Accordion stops working when there are no actions in the admin panel or when the user lacks delete permissions #172

Open WhitePoodleMoth opened 4 hours ago

WhitePoodleMoth commented 4 hours ago

Python version
3.12.6

Django version
5.1.1

Package version
0.22.1

Current behavior (bug description)
When has_delete_permission is set to False in the Django admin for my model, the Accordion functionality of django-treenode stops working. The Accordion does not expand, and it remains with the default name without any options to expand.

from django.contrib import admin
from treenode.admin import TreeNodeModelAdmin
from treenode.forms import TreeNodeForm
from .models import Category

class CategoryAdmin(TreeNodeModelAdmin):
    # set the changelist display mode: 'accordion', 'breadcrumbs' or 'indentation' (default)
    # when changelist results are filtered by a querystring,
    # 'breadcrumbs' mode will be used (to preserve data display integrity)
    treenode_display_mode = TreeNodeModelAdmin.TREENODE_DISPLAY_MODE_ACCORDION
    # treenode_display_mode = TreeNodeModelAdmin.TREENODE_DISPLAY_MODE_BREADCRUMBS
    # treenode_display_mode = TreeNodeModelAdmin.TREENODE_DISPLAY_MODE_INDENTATION

    # use TreeNodeForm to automatically exclude invalid parent choices
    form = TreeNodeForm

    def has_delete_permission(self, request, obj=None):
        """
        Disallow deleting records.
        """
        return False

admin.site.register(Category, CategoryAdmin)

Expected behavior
I expect that even when the delete permission is disabled, the Accordion should function normally, allowing for expansion and proper display of tree nodes.

Upvote & Fund

Fund with Polar

fabiocaccamo commented 4 hours ago

@WhitePoodleMoth thank you for reporting this problem.

WhitePoodleMoth commented 4 hours ago

When attempting to block the delete option in the Django admin by overriding the delete_model method and removing the delete action, I've noticed that while the delete permission remains active, the Accordion functionality of django-treenode stops working.

Here's the code I'm using:

from django.core.exceptions import PermissionDenied
from django.contrib import admin
from treenode.admin import TreeNodeModelAdmin
from treenode.forms import TreeNodeForm
from .models import Category

class CategoryAdmin(TreeNodeModelAdmin):
    # set the changelist display mode: 'accordion', 'breadcrumbs' or 'indentation' (default)
    # when changelist results are filtered by a querystring,
    # 'breadcrumbs' mode will be used (to preserve data display integrity)
    treenode_display_mode = TreeNodeModelAdmin.TREENODE_DISPLAY_MODE_ACCORDION
    # treenode_display_mode = TreeNodeModelAdmin.TREENODE_DISPLAY_MODE_BREADCRUMBS
    # treenode_display_mode = TreeNodeModelAdmin.TREENODE_DISPLAY_MODE_INDENTATION

    # use TreeNodeForm to automatically exclude invalid parent choices
    form = TreeNodeForm

    # def has_delete_permission(self, request, obj=None):
    #     return False

    # def delete_model(self, request, obj):
    #     raise PermissionDenied("You do not have permission to delete this record.")

    def get_actions(self, request):
        actions = super().get_actions(request)
        if 'delete_selected' in actions:
            del actions['delete_selected']
        return actions

admin.site.register(Category, CategoryAdmin)

Despite removing the delete action from the admin interface, the Accordion does not function properly. It appears that the removal of the delete action might be causing this issue.

WhitePoodleMoth commented 4 hours ago

After blocking the delete option in the Django admin and adding a placeholder action, I found that the Accordion functionality in django-treenode is working correctly again.

It appears that the issue was not with the delete operation itself, but rather with having no actions available. By ensuring the delete permissions are restricted and adding a simple placeholder action, the Accordion feature is now functional.

Here’s the implementation:

from django.contrib import admin
from treenode.admin import TreeNodeModelAdmin
from treenode.forms import TreeNodeForm
from .models import Category

class CategoryAdmin(TreeNodeModelAdmin):
    # Set the changelist display mode: 'accordion', 'breadcrumbs' or 'indentation' (default)
    treenode_display_mode = TreeNodeModelAdmin.TREENODE_DISPLAY_MODE_ACCORDION

    # Use TreeNodeForm to automatically exclude invalid parent choices
    form = TreeNodeForm

    # Define the actions available in the admin interface
    actions = ['placeholder_action']  # Keeping only the placeholder action

    def has_delete_permission(self, request, obj=None):
        return False

    def placeholder_action(self, request, queryset):
        """
        A placeholder action to demonstrate action functionality.
        """
        pass

admin.site.register(Category, CategoryAdmin)