Skip to content
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

[Feature] Check the available primary actions (edit, delete, view details) for each row on listing page. #874

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

maxim-f1
Copy link

Description

SQLAdmin has a convenient system for accessing certain actions from CRUD. I decided to extend this functionality. Now you can define basic actions for a certain record in the list, with the help of events:

  • check_can_view_details
  • check_can_edit
  • check_can_delete

Example of using such functionality:

class ExampleView(ModelView, model=Example):
    can_view_details = True
    can_edit = True
    can_delete = True

    column_list = (
        Example.id,
        Example.can_view_details,
        Example.can_edit,
        Example.can_delete
    )

    async def check_can_view_details(self, request: Request, model: Any) -> bool:
        """
        You can add a custom model attribute checker before view details.
        """
        if model.can_view_details is False:
            return False
        return self.can_view_details

    async def check_can_edit(self, request: Request, model: Any) -> bool:
        """
        You can add a custom model attribute checker before edit.
        """
        if model.can_edit is False:
            return False
        return self.can_edit

    async def check_can_delete(self, request: Request, model: Any) -> bool:
        """
        You can add a custom model attribute checker before delete.
        """
        if model.can_delete is False:
            return False
        return self.can_delete

The result is in the admin panel:

image

Important issues

  1. Seamless migration from older versions is provided, even customized Admin classes should not break.

@maxim-f1 maxim-f1 marked this pull request as ready for review January 22, 2025 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant